restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct FuzzyTests {
|
||||
@Test func matchesSubsequencesCaseInsensitively() {
|
||||
#expect(Fuzzy.match("uc", in: "UserController.php") == [0, 4])
|
||||
#expect(Fuzzy.match("xyz", in: "UserController.php") == nil)
|
||||
}
|
||||
|
||||
@Test func ranksBasenameMatchesFirst() {
|
||||
let paths = ["src/user/list.php", "app/UserList.php", ".env"]
|
||||
let ranked = Fuzzy.rankFileNames(query: "userl", paths: paths, showHidden: false)
|
||||
#expect(ranked.map(\.path) == ["app/UserList.php", "src/user/list.php"])
|
||||
#expect(ranked[0].rank == 0 && ranked[1].rank == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct FuzzyPortedTests {
|
||||
@Test("an empty query matches nothing in the ranked list")
|
||||
func emptyQuery() {
|
||||
#expect(Fuzzy.rankFileNames(query: " ", paths: ["a.ts"], showHidden: true).isEmpty)
|
||||
#expect(Fuzzy.match("", in: "abc") == [])
|
||||
}
|
||||
|
||||
@Test("hidden paths are left out unless asked for")
|
||||
func hiddenPaths() {
|
||||
#expect(Fuzzy.rankFileNames(query: "env", paths: [".env"], showHidden: false).isEmpty)
|
||||
#expect(Fuzzy.rankFileNames(query: "env", paths: [".env"], showHidden: true).map(\.path) == [".env"])
|
||||
}
|
||||
|
||||
@Test("in-file hits are case-insensitive with UTF-16 offsets")
|
||||
func inFileHits() {
|
||||
let hits = InFileSearch.hits(query: "Bal", in: "😀 balance\nnone\nBALANCE")
|
||||
#expect(hits == [ContentHit(line: 1, text: "😀 balance", matchOffset: 3), ContentHit(line: 3, text: "BALANCE", matchOffset: 0)])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct RipgrepJSONTests {
|
||||
private func matchLine(path: String = "/p/a.ts", text: String = "const balance = 2\n", line: Int = 2, start: Int = 6) -> String {
|
||||
"""
|
||||
{"type":"match","data":{"path":{"text":"\(path)"},"lines":{"text":"\(text.replacingOccurrences(of: "\n", with: "\\n"))"},"line_number":\(line),"absolute_offset":12,"submatches":[{"match":{"text":"balance"},"start":\(start),"end":13}]}}
|
||||
"""
|
||||
}
|
||||
|
||||
@Test("a match event yields path, line, text without its newline, and the byte offset")
|
||||
func parsesMatch() {
|
||||
#expect(RipgrepJSON.parseLine(matchLine()) == RipgrepMatch(path: "/p/a.ts", line: 2, text: "const balance = 2", byteOffset: 6))
|
||||
}
|
||||
|
||||
@Test("every other event type is skipped", arguments: [
|
||||
#"{"type":"begin","data":{"path":{"text":"/p/a.ts"}}}"#,
|
||||
#"{"type":"end","data":{"path":{"text":"/p/a.ts"}}}"#,
|
||||
#"{"type":"summary","data":{"elapsed_total":{"secs":0}}}"#,
|
||||
"not json",
|
||||
"",
|
||||
])
|
||||
func skipsOtherEvents(line: String) {
|
||||
#expect(RipgrepJSON.parseLine(line) == nil)
|
||||
}
|
||||
|
||||
@Test("base64 bytes for a non-UTF-8 path or line skip the event")
|
||||
func skipsBytes() {
|
||||
#expect(RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"bytes":"L3AvYQ=="},"lines":{"text":"x\n"},"line_number":1,"submatches":[]}}"#) == nil)
|
||||
#expect(RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"text":"/p/a"},"lines":{"bytes":"eA=="},"line_number":1,"submatches":[]}}"#) == nil)
|
||||
}
|
||||
|
||||
@Test("a match without submatches or a line number defaults to zero")
|
||||
func defaults() {
|
||||
let parsed = RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"text":"/p/a"},"lines":{"text":"x\r\n"}}}"#)
|
||||
#expect(parsed == RipgrepMatch(path: "/p/a", line: 0, text: "x", byteOffset: 0))
|
||||
}
|
||||
|
||||
@Test("paths are made relative to the root")
|
||||
func relativePath() {
|
||||
#expect(RipgrepJSON.relativePath("/p/src/a.ts", root: "/p") == "src/a.ts")
|
||||
#expect(RipgrepJSON.relativePath("/p/src/a.ts", root: "/p/") == "src/a.ts")
|
||||
#expect(RipgrepJSON.relativePath("/other/a.ts", root: "/p") == "/other/a.ts")
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct ContentGrouperTests {
|
||||
@Test("matches group per file in first-seen order")
|
||||
func groups() {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
grouper.add(RipgrepMatch(path: "/p/b.ts", line: 1, text: "x", byteOffset: 0))
|
||||
grouper.add(RipgrepMatch(path: "/p/a.ts", line: 3, text: "y", byteOffset: 0))
|
||||
grouper.add(RipgrepMatch(path: "/p/b.ts", line: 7, text: "z", byteOffset: 0))
|
||||
#expect(grouper.groups.map(\.path) == ["b.ts", "a.ts"])
|
||||
#expect(grouper.groups[0].hits.map(\.line) == [1, 7])
|
||||
}
|
||||
|
||||
@Test("the byte offset becomes a UTF-16 offset", arguments: [
|
||||
("const balance", 6, 6),
|
||||
("é balance", 3, 2),
|
||||
("😀 balance", 5, 3),
|
||||
])
|
||||
func utf16Offset(text: String, byteOffset: Int, expected: Int) {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: byteOffset))
|
||||
#expect(grouper.groups[0].hits[0].matchOffset == expected)
|
||||
}
|
||||
|
||||
@Test("long lines are capped and the offset clamped to what is left")
|
||||
func capsLongLines() {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
let text = String(repeating: "a", count: 1500)
|
||||
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: 1400))
|
||||
let hit = grouper.groups[0].hits[0]
|
||||
#expect(hit.text.utf16.count == SearchLimits.maxLineLength)
|
||||
#expect(hit.matchOffset == SearchLimits.maxLineLength)
|
||||
}
|
||||
|
||||
@Test("a cap never splits a surrogate pair")
|
||||
func capKeepsSurrogates() {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
let text = String(repeating: "a", count: SearchLimits.maxLineLength - 1) + "😀 tail"
|
||||
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: 0))
|
||||
#expect(grouper.groups[0].hits[0].text == String(repeating: "a", count: SearchLimits.maxLineLength - 1))
|
||||
}
|
||||
|
||||
@Test("new files beyond the file limit are dropped, known files still collect hits")
|
||||
func fileLimit() {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
for k in 0..<(SearchLimits.maxFiles + 5) {
|
||||
grouper.add(RipgrepMatch(path: "/p/f\(k)", line: 1, text: "x", byteOffset: 0))
|
||||
}
|
||||
grouper.add(RipgrepMatch(path: "/p/f0", line: 2, text: "x", byteOffset: 0))
|
||||
#expect(grouper.groups.count == SearchLimits.maxFiles)
|
||||
#expect(grouper.groups[0].hits.count == 2)
|
||||
}
|
||||
|
||||
@Test("hits per file stop at the per-file limit")
|
||||
func hitLimit() {
|
||||
var grouper = ContentGrouper(root: "/p")
|
||||
for k in 0..<(SearchLimits.maxHitsPerFile + 3) {
|
||||
grouper.add(RipgrepMatch(path: "/p/a", line: k, text: "x", byteOffset: 0))
|
||||
}
|
||||
#expect(grouper.groups[0].hits.count == SearchLimits.maxHitsPerFile)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user