Files

83 lines
3.0 KiB
Swift

import Foundation
import Testing
@testable import SstData
@MainActor
struct NoteStoreTests {
private func temp() -> URL {
FileManager.default.temporaryDirectory.appending(path: "note-\(UUID().uuidString)/note.txt")
}
@Test func missingFileLoadsEmpty() {
let store = NoteStore(fileURL: temp())
store.load()
#expect(store.text == "")
#expect(!store.isReadOnly)
}
@Test func flushWritesAndAnotherStoreReadsIt() {
let url = temp()
let store = NoteStore(fileURL: url)
store.text = "eerste regel\nté"
store.flush()
let other = NoteStore(fileURL: url)
other.load()
#expect(other.text == "eerste regel\nté")
}
@Test func editsAreSavedAfterTheDebounce() async throws {
let url = temp()
let store = NoteStore(fileURL: url)
store.text = "bewaard"
#expect(!FileManager.default.fileExists(atPath: url.path))
try await Task.sleep(for: .milliseconds(900))
#expect(try String(contentsOf: url, encoding: .utf8) == "bewaard")
}
@Test func loadingDoesNotTriggerASave() async throws {
let url = temp()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
try "oud".write(to: url, atomically: true, encoding: .utf8)
let store = NoteStore(fileURL: url)
store.load()
#expect(store.text == "oud")
try FileManager.default.removeItem(at: url)
try await Task.sleep(for: .milliseconds(900))
#expect(!FileManager.default.fileExists(atPath: url.path))
}
@Test func unreadableFileIsNeverOverwritten() throws {
let url = temp()
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
try Data([0xFF, 0xFE, 0xFD]).write(to: url)
let store = NoteStore(fileURL: url)
store.load()
#expect(store.isReadOnly)
store.text = "nieuw"
store.flush()
#expect(try Data(contentsOf: url) == Data([0xFF, 0xFE, 0xFD]))
}
}
struct DictationSpacingTests {
@Test func noSpaceAtTheStartOrAfterWhitespace() {
#expect(DictationSpacing.prepared("hallo", characterBefore: nil) == "hallo")
#expect(DictationSpacing.prepared("hallo", characterBefore: " ") == "hallo")
#expect(DictationSpacing.prepared("hallo", characterBefore: "\n") == "hallo")
}
@Test func spaceAfterAWordOrSentenceEnd() {
#expect(DictationSpacing.prepared("hallo", characterBefore: "k") == " hallo")
#expect(DictationSpacing.prepared("Nog een.", characterBefore: ".") == " Nog een.")
}
@Test func noSpaceBeforeClosingPunctuationOrLeadingWhitespace() {
#expect(DictationSpacing.prepared(", dus", characterBefore: "k") == ", dus")
#expect(DictationSpacing.prepared(" dus", characterBefore: "k") == " dus")
}
@Test func emptyTextStaysEmpty() {
#expect(DictationSpacing.prepared("", characterBefore: "k") == "")
}
}