Tests/OrgCoreTests/LinesTests.swift
64 lines · 2265 bytes
1import Testing
2@testable import OrgCore
3
4struct LinesTests {
5 @Test func splitKeepsEveryEnding() {
6 let lines = splitRawLines("a\r\nb\n\nc")
7 #expect(lines.map { String($0.content) } == ["a", "b", "", "c"])
8 #expect(lines.map { String($0.ending) } == ["\r\n", "\n", "\n", ""])
9 #expect(splitRawLines("").isEmpty)
10 #expect(splitRawLines("x\n").count == 1)
11 }
12
13 @Test func splitIsLossless() {
14 let text = "\r\n\n a\r b\r\n😀\n"
15 #expect(splitRawLines(text).map { String($0.content) + String($0.ending) }.joined() == text)
16 }
17
18 @Test(arguments: [
19 ("", LineClass.blank),
20 (" \t", .blank),
21 ("* a", .heading(level: 1)),
22 ("*** ", .heading(level: 3)),
23 ("*", .heading(level: 1)),
24 ("*bold* text", .plain),
25 (" * a", .listItem),
26 ("#+BEGIN_SRC sh :results output", .blockBegin(name: "src")),
27 ("#+end_src", .blockEnd(name: "src")),
28 ("#+BEGIN: clocktable :scope file", .dynamicBegin),
29 ("#+END:", .dynamicEnd),
30 ("#+TITLE: x", .keyword(key: "TITLE")),
31 ("#+tblfm: $2=$1", .keyword(key: "TBLFM")),
32 ("# comment", .comment),
33 ("#", .comment),
34 ("#hashtag", .plain),
35 (": fixed", .fixedWidth),
36 (":", .fixedWidth),
37 (":PROPERTIES:", .drawerBegin(name: "PROPERTIES")),
38 (" :LOGBOOK:", .drawerBegin(name: "LOGBOOK")),
39 (":END:", .drawerEnd),
40 ("| a | b |", .tableRow),
41 ("-----", .horizontalRule),
42 ("----", .plain),
43 ("[fn:1] note", .footnoteDefinition),
44 ("CLOCK: [2026-10-04 Sun 10:00]", .clock),
45 ("SCHEDULED: <2026-10-04 Sun>", .planning),
46 ("- item", .listItem),
47 ("+ item", .listItem),
48 ("1. item", .listItem),
49 ("2) item", .listItem),
50 ("-", .listItem),
51 ("-x", .plain),
52 ("1.5 apples", .plain),
53 ("plain text", .plain),
54 ])
55 func classify(line: String, expected: LineClass) {
56 #expect(classifyLine(line[...]).cls == expected)
57 }
58
59 @Test func indentCountsTabsToEight() {
60 #expect(classifyLine("\t- a").indent == 8)
61 #expect(classifyLine(" \t- a").indent == 8)
62 #expect(classifyLine(" - a").indent == 3)
63 }
64}