Files
sst/Tests/SstDataTests/DictionaryFileTests.swift

72 lines
2.9 KiB
Swift

import Testing
@testable import SstData
struct DictionaryFileTests {
let sample = "# Opmerking\nAnthropic\n\ncloud code => Claude Code\n"
@Test func parsesTermsPairsCommentsAndBlanks() {
let file = DictionaryFile(parsing: sample)
#expect(file.lines.count == 4)
#expect(file.entries == [term("Anthropic"), pair("cloud code", "Claude Code")])
#expect(file.lines[0] == .comment("# Opmerking"))
#expect(file.lines[2] == .blank)
}
@Test func roundTripKeepsCommentsBlankLinesAndOrder() {
#expect(DictionaryFile(parsing: sample).serialized() == sample)
}
@Test func normalisesSpacingAroundTheArrow() {
#expect(DictionaryFile(parsing: "a=>b\n").serialized() == "a => b\n")
#expect(DictionaryFile(parsing: " cloud code => Claude Code \n").entries == [pair("cloud code", "Claude Code")])
}
@Test func unreadableLinesAreKeptAsInvalid() {
let file = DictionaryFile(parsing: "oops =>\n=> nothing\nok\n")
#expect(file.lines[0] == .invalid("oops =>"))
#expect(file.lines[1] == .invalid("=> nothing"))
#expect(file.serialized() == "oops =>\n=> nothing\nok\n")
}
@Test func handlesWindowsLineEndingsAndMissingFinalNewline() {
let file = DictionaryFile(parsing: "a\r\nb => c")
#expect(file.entries == [term("a"), pair("b", "c")])
#expect(file.serialized() == "a\nb => c\n")
}
@Test func emptyTextIsAValidEmptyFile() {
#expect(DictionaryFile(parsing: "").entries.isEmpty)
#expect(DictionaryFile(parsing: "").serialized() == "")
}
@Test func appendUpdateAndRemove() {
var file = DictionaryFile(parsing: "# c\n")
let id = file.append(.term("Vercel"))
#expect(file.entries == [term("Vercel")])
file.update(id: id, kind: .correction(heard: "versel", wrote: "Vercel"))
#expect(file.entries == [pair("versel", "Vercel")])
file.remove(id: id)
#expect(file.entries.isEmpty)
#expect(file.serialized() == "# c\n")
}
@Test func preservingIDsKeepsIdsOfUnchangedLines() {
let old = DictionaryFile(parsing: "a\nb => c\nd\n")
let new = DictionaryFile(parsing: "z\na\nb => c\nd2\n").preservingIDs(from: old)
let oldIDs = old.entries.map(\.id), newIDs = new.entries.map(\.id)
#expect(newIDs[1] == oldIDs[0])
#expect(newIDs[2] == oldIDs[1])
#expect(!oldIDs.contains(newIDs[0]))
#expect(!oldIDs.contains(newIDs[3]))
#expect(new.serialized() == "z\na\nb => c\nd2\n")
}
@Test func preservingIDsMapsDuplicatesOneToOneInOrder() {
let old = DictionaryFile(parsing: "a\na\n")
let new = DictionaryFile(parsing: "a\na\na\n").preservingIDs(from: old)
let oldIDs = old.entries.map(\.id), newIDs = new.entries.map(\.id)
#expect(Array(newIDs[0...1]) == oldIDs)
#expect(!oldIDs.contains(newIDs[2]))
}
}