|
| 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 | +} |
0 commit comments