Skip to content

Commit 0006447

Browse files
committed
Testes search engine
1 parent 01421ba commit 0006447

File tree

5 files changed

+157
-88
lines changed

5 files changed

+157
-88
lines changed

scaladoc-js/main/src/searchbar/SearchbarComponent.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SearchbarComponent(engine: PageSearchEngine, inkuireEngine: InkuireJSSearc
9494
span(kind)
9595
)
9696
def handleNewFluffQuery(query: NameAndKindQuery) =
97-
val searchTask: Future[List[MatchResult]] = engine.query(query)
97+
val searchTask: Future[List[MatchResult]] = Future(engine.query(query))
9898
searchTask.map { result =>
9999
val resultWithDocBonus = result
100100
.map(entry =>

scaladoc-js/main/src/searchbar/engine/PageSearchEngine.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import scala.annotation.tailrec
1919
*/
2020
class PageSearchEngine(pages: List[PageEntry]):
2121

22-
def query(query: NameAndKindQuery): Future[List[MatchResult]] = Future {
22+
def query(query: NameAndKindQuery): List[MatchResult] = {
2323
matchPages(query)
2424
.filter {
2525
case MatchResult(score, _, _) => score >= 0

scaladoc-js/main/test/dotty/tools/scaladoc/MatchersTest.scala

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package dotty.tools.scaladoc
2+
3+
import org.junit.{Assert, Test}
4+
import org.junit.Assert.*
5+
6+
import scala.concurrent.Await
7+
import scala.concurrent.duration.*
8+
9+
class PageSearchEngineTest {
10+
11+
def page(kind: String, name: String) = PageEntry(
12+
s"$kind $name",
13+
"",
14+
"",
15+
"",
16+
false,
17+
s"$name",
18+
kind,
19+
StringUtils.createCamelCaseTokens(name)
20+
)
21+
22+
case class ExpectedMatch(kind: String, name: String, indices: Set[Int])
23+
def assertMatches(query: NameAndKindQuery, pages: List[PageEntry], matches: List[String]): Unit =
24+
val expectedMatches = matches.map { mat =>
25+
val splitResult = mat.split(" ")
26+
val kind = splitResult(0)
27+
val name = splitResult.tail.mkString(" ")
28+
val (realName, indices, _) = name.foldLeft[(String, Set[Int], Boolean)]("", Set.empty, false) {
29+
case ((name, matchIndices, inParam), c) =>
30+
val index = name.length
31+
if c == '(' then
32+
if inParam then
33+
throw new IllegalArgumentException("Nested params not allowed")
34+
else
35+
(name, matchIndices, true)
36+
else if c == ')' then
37+
(name, matchIndices, false)
38+
else if inParam then
39+
(name + c, matchIndices + index, true)
40+
else
41+
(name + c, matchIndices, false)
42+
}
43+
ExpectedMatch(kind, realName, indices)
44+
}
45+
val engine = new PageSearchEngine(pages)
46+
val resultingMatches = engine.query(query)
47+
.map(mat => ExpectedMatch(mat.pageEntry.kind, mat.pageEntry.shortName, mat.indices))
48+
49+
val matchesNames = resultingMatches.map(s => (s.name, s.kind))
50+
val expectedNames = expectedMatches.map(s => (s.name, s.kind))
51+
val missingNames = expectedNames.diff(matchesNames)
52+
val extraNames = matchesNames.diff(expectedNames)
53+
val itemsNotMatchingNames = (resultingMatches.diff(expectedMatches) ++ expectedMatches.diff(resultingMatches))
54+
.filter(m => !(missingNames ++ extraNames).contains((m.name, m.kind))).map(s => (s.name, s.kind))
55+
val itemsNotMatching = itemsNotMatchingNames.map {
56+
case pair @ (itemName, itemKind) =>
57+
val expectedItem: ExpectedMatch = expectedMatches.find(s => (s.name, s.kind) == pair).get
58+
val matchedItem: ExpectedMatch = resultingMatches.find(s => (s.name, s.kind) == pair).get
59+
s"${itemKind} ${itemName}: ${expectedItem.indices.toList.sorted.mkString("[", ", ", "]")} vs ${matchedItem.indices.toList.sorted.mkString("[", ", ", "]")}"
60+
}.mkString("\n")
61+
62+
assertTrue(
63+
s"\nFound: ${matchesNames.mkString("[", ", ", "]")} \n" +
64+
s"Expected: ${expectedNames.mkString("[", ", ", "]")} \n" +
65+
s"Extra elements: ${extraNames.mkString(", ")} \n" +
66+
s"Missing elements: ${missingNames.mkString(", ")}\n" +
67+
s"Not matching items: \n${itemsNotMatching}\n",
68+
resultingMatches == expectedMatches
69+
)
70+
71+
72+
private val correctFilterPages = List(
73+
page("class", "ListBuffer"),
74+
page("object", "ListBuffer"),
75+
page("class", "ListBuff"),
76+
page("class", "LisBfufer"),
77+
page("class", "ListBufferTwo"),
78+
page("class", "ListerBuffer")
79+
)
80+
@Test
81+
def correctFilter(): Unit = {
82+
assertMatches(
83+
NameAndKindQuery(Some("ListBuffer"), Some("class")),
84+
correctFilterPages,
85+
List(
86+
"class (ListBuffer)",
87+
"class (List)er(Buffer)",
88+
"class (ListBuffer)Two",
89+
)
90+
)
91+
}
92+
93+
private val abbrevFilterPages = List(
94+
page("class", "NullPointerException"),
95+
page("class", "NullBointerException"),
96+
page("class", "NullBpointerException"),
97+
page("class", "nullpointerexception"),
98+
)
99+
@Test
100+
def abbrevFilter(): Unit = {
101+
assertMatches(
102+
NameAndKindQuery(Some("NPE"), Some("class")),
103+
abbrevFilterPages,
104+
List(
105+
"class (N)ull(P)ointer(E)xception",
106+
"class (N)ullB(p)oint(e)rException",
107+
"class (n)ull(p)oint(e)rexception",
108+
)
109+
)
110+
}
111+
112+
private val correctOrderPages = List(
113+
page("class", "ListBuffer"),
114+
page("object", "ListBuffer"),
115+
page("static", "Using List Buffers"),
116+
page("class", "ListUnbucle"),
117+
page("object", "Malibu")
118+
)
119+
@Test
120+
def correctOrder(): Unit = {
121+
assertMatches(
122+
NameAndKindQuery(Some("LiBu"), None),
123+
correctOrderPages,
124+
List(
125+
"class (Li)st(Bu)ffer",
126+
"object (Li)st(Bu)ffer",
127+
"static Using (Li)st (Bu)ffers",
128+
"class (Li)stUn(bu)cle",
129+
"object Ma(libu)"
130+
)
131+
)
132+
}
133+
134+
private val correctSelectionPages = List(
135+
page("class", "FoobarBar"),
136+
page("class", "FooBbar"),
137+
page("class", "FobaroBar")
138+
)
139+
140+
@Test
141+
def correctSelection(): Unit = {
142+
assertMatches(
143+
NameAndKindQuery(Some("FooBar"), None),
144+
correctSelectionPages,
145+
List(
146+
"class (Foo)bar(Bar)",
147+
"class (FooB)b(ar)",
148+
"class (Fo)bar(oBar)"
149+
)
150+
)
151+
}
152+
}

scaladoc-js/main/test/dotty/tools/scaladoc/QueryParserTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotty.tools.scaladoc
33
import org.junit.{Test, Assert}
44
import org.junit.Assert._
55

6-
class QueryParserTest
6+
class QueryParserTest:
77
val queryParser = QueryParser()
88
val kinds = Seq(
99
"class",
@@ -28,8 +28,8 @@ class QueryParserTest
2828

2929
@Test
3030
def queryParserTests() = {
31-
kinds.foreach(k => testCase(s"$k ", NameAndKindQuery(List(ByKind(k), ByName("")))))
32-
testCase("trait", NameAndKindQuery(None, Some("trait")))
31+
kinds.foreach(k => testCase(s"$k ", NameAndKindQuery(Some(""), Some(k))))
32+
testCase("trait", NameAndKindQuery(Some("trait"), None))
3333
testCase("trait A", NameAndKindQuery(Some("A"), Some("trait")))
3434
testCase("`trait A`", NameAndKindQuery(Some("trait A"), None))
3535
}

0 commit comments

Comments
 (0)