aboutsummaryrefslogtreecommitdiff
path: root/Tests/NorgKitTests/Helpers
diff options
context:
space:
mode:
authorRuben Beltran del Rio <jj@r.bdr.sh>2026-06-16 12:08:21 +0200
committerRuben Beltran del Rio <jj@r.bdr.sh>2026-06-16 12:22:47 +0200
commit501fdce29e4d2c46c4708ad7f44056878e0db3fa (patch)
treedb923b77037db9f5b37600a6e34c3bb2e8a971be /Tests/NorgKitTests/Helpers
Initial extraction from Norganize1.0.0
Diffstat (limited to 'Tests/NorgKitTests/Helpers')
-rw-r--r--Tests/NorgKitTests/Helpers/ASCIIByteSetTests.swift42
-rw-r--r--Tests/NorgKitTests/Helpers/ASCIIHelperTests.swift22
-rw-r--r--Tests/NorgKitTests/Helpers/TextHelperTests.swift71
3 files changed, 135 insertions, 0 deletions
diff --git a/Tests/NorgKitTests/Helpers/ASCIIByteSetTests.swift b/Tests/NorgKitTests/Helpers/ASCIIByteSetTests.swift
new file mode 100644
index 0000000..eb29ecb
--- /dev/null
+++ b/Tests/NorgKitTests/Helpers/ASCIIByteSetTests.swift
@@ -0,0 +1,42 @@
+import Testing
+
+@testable import NorgKit
+
+struct ASCIIByteSetTests {
+
+ @Test func containsLowBytes() {
+ // `*` (0x2A) and `-` (0x2D) live in the low 64-bit word.
+ let set = ASCIIByteSet("*-")
+ #expect(set.contains(UInt8(ascii: "*")))
+ #expect(set.contains(UInt8(ascii: "-")))
+ #expect(!set.contains(UInt8(ascii: "/")))
+ }
+
+ @Test func containsHighBytes() {
+ // `~` (0x7E) and `_` (0x5F) live in the high 64-bit word.
+ let set = ASCIIByteSet("~_")
+ #expect(set.contains(UInt8(ascii: "~")))
+ #expect(set.contains(UInt8(ascii: "_")))
+ #expect(!set.contains(UInt8(ascii: "x")))
+ }
+
+ @Test func spansBothWords() {
+ let set = ASCIIByteSet("*~")
+ #expect(set.contains(UInt8(ascii: "*")))
+ #expect(set.contains(UInt8(ascii: "~")))
+ }
+
+ @Test func rejectsNonAsciiAndOutOfSetBytes() {
+ let set = ASCIIByteSet("*")
+ #expect(!set.contains(0)) // NUL, low word, unset
+ #expect(!set.contains(200)) // beyond ASCII, never stored
+ #expect(!set.contains(127)) // high word, unset
+ }
+
+ @Test func emptySetContainsNothing() {
+ let set = ASCIIByteSet("")
+ #expect(!set.contains(UInt8(ascii: "*")))
+ #expect(!set.contains(0))
+ #expect(!set.contains(127))
+ }
+}
diff --git a/Tests/NorgKitTests/Helpers/ASCIIHelperTests.swift b/Tests/NorgKitTests/Helpers/ASCIIHelperTests.swift
new file mode 100644
index 0000000..8a807fe
--- /dev/null
+++ b/Tests/NorgKitTests/Helpers/ASCIIHelperTests.swift
@@ -0,0 +1,22 @@
+import Testing
+
+@testable import NorgKit
+
+struct ASCIIHelperTests {
+ @Test func detectsSpaces() {
+ #expect(ASCIIHelper.isWhitespace(0x20) == true)
+ #expect(ASCIIHelper.isWhitespace(0x09) == true)
+ #expect(ASCIIHelper.isWhitespace(0x0A) == true)
+ #expect(ASCIIHelper.isWhitespace(0x0B) == true)
+ #expect(ASCIIHelper.isWhitespace(0x0C) == true)
+ #expect(ASCIIHelper.isWhitespace(0x0D) == true)
+ }
+
+ @Test func doesNotDetectNonSpaces() {
+ #expect(ASCIIHelper.isWhitespace(0x19) == false)
+ #expect(ASCIIHelper.isWhitespace(0x21) == false)
+ #expect(ASCIIHelper.isWhitespace(0x0E) == false)
+ #expect(ASCIIHelper.isWhitespace(0x08) == false)
+ #expect(ASCIIHelper.isWhitespace(0x67) == false)
+ }
+}
diff --git a/Tests/NorgKitTests/Helpers/TextHelperTests.swift b/Tests/NorgKitTests/Helpers/TextHelperTests.swift
new file mode 100644
index 0000000..9e236ff
--- /dev/null
+++ b/Tests/NorgKitTests/Helpers/TextHelperTests.swift
@@ -0,0 +1,71 @@
+import Testing
+
+@testable import NorgKit
+
+struct TextHelperTests {
+
+ @Test func lineSlicesSplitsOnNewlines() {
+ #expect(TextHelper.lineSlices("a\nb\nc").map(String.init) == ["a", "b", "c"])
+ }
+
+ @Test func lineSlicesKeepsTrailingEmptyLine() {
+ #expect(TextHelper.lineSlices("a\n").map(String.init) == ["a", ""])
+ }
+
+ @Test func lineSlicesHandlesEmptyString() {
+ #expect(TextHelper.lineSlices("").map(String.init) == [""])
+ }
+
+ @Test func enumerateLinesProvidesIndices() {
+ var seen: [(String, Int)] = []
+ TextHelper.enumerateLines(in: "one\ntwo") { line, index in
+ seen.append((String(line), index))
+ }
+ #expect(seen.map(\.0) == ["one", "two"])
+ #expect(seen.map(\.1) == [0, 1])
+ }
+
+ @Test func enumerateLinesStripsCarriageReturns() {
+ var lines: [String] = []
+ TextHelper.enumerateLines(in: "a\r\nb\r\n") { line, _ in lines.append(String(line)) }
+ #expect(lines == ["a", "b", ""])
+ }
+
+ @Test func lineReturnsRequestedLine() {
+ #expect(TextHelper.line(in: "a\nb\nc", at: 1).map(String.init) == "b")
+ }
+
+ @Test func lineReturnsLastLineWithoutTrailingNewline() {
+ #expect(TextHelper.line(in: "a\nb", at: 1).map(String.init) == "b")
+ }
+
+ @Test func lineStripsCarriageReturn() {
+ #expect(TextHelper.line(in: "a\r\nb", at: 0).map(String.init) == "a")
+ }
+
+ @Test func lineReturnsNilForOutOfRange() {
+ #expect(TextHelper.line(in: "a\nb", at: 5) == nil)
+ }
+
+ @Test func lineReturnsNilForNegativeIndex() {
+ #expect(TextHelper.line(in: "a", at: -1) == nil)
+ }
+
+ @Test func whitespaceTrimmedSubstringTrimsBothEnds() {
+ let trimmed = TextHelper.whitespaceTrimmed(" \t hi \t "[...])
+ #expect(trimmed == "hi")
+ }
+
+ @Test func whitespaceTrimmedSubstringOfAllWhitespaceIsEmpty() {
+ #expect(TextHelper.whitespaceTrimmed(" \t "[...]).isEmpty)
+ }
+
+ @Test func whitespaceTrimmedStringTrimsWhenNeeded() {
+ #expect(TextHelper.whitespaceTrimmed(" hi ") == "hi")
+ }
+
+ @Test func whitespaceTrimmedStringReturnsSameValueWhenAlreadyTrimmed() {
+ let input = "already-clean"
+ #expect(TextHelper.whitespaceTrimmed(input) == input)
+ }
+}