1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import Testing
@testable import NorgKeyboardToolbar
struct EditorSnippetTests {
@Test func idMatchesRawValueAndIsUnique() {
for snippet in EditorSnippet.allCases {
#expect(snippet.id == snippet.rawValue)
}
#expect(Set(EditorSnippet.allCases.map(\.id)).count == EditorSnippet.allCases.count)
}
@Test func insertsIntoEmptyDocumentWithoutLeadingNewline() {
let result = EditorSnippet.heading.apply(to: "", cursor: 0)
#expect(result.text == "* ")
#expect(result.cursor == 2)
}
@Test func insertsOnNewLineBeneathCurrentLine() {
let result = EditorSnippet.task.apply(to: "first line", cursor: 3)
#expect(result.text == "first line\n- ( ) ")
// Cursor sits at the end of the inserted task marker, ready to type.
#expect(result.cursor == result.text.count)
}
@Test func insertsBetweenLinesNotSplittingThem() {
let result = EditorSnippet.task.apply(to: "a\nb", cursor: 0)
#expect(result.text == "a\n- ( ) \nb")
}
@Test func delimiterHasNoTrailingSpace() {
let weak = EditorSnippet.weakReverse.apply(to: "x", cursor: 1)
#expect(weak.text == "x\n---")
let strong = EditorSnippet.strongReverse.apply(to: "x", cursor: 1)
#expect(strong.text == "x\n===")
}
@Test func codeBlockPlacesCursorIndentedBetweenMarkers() {
let result = EditorSnippet.code.apply(to: "note", cursor: 4)
#expect(result.text == "note\n@code\n\t\n@end")
// Cursor lands right after the tab on the middle line.
let index = result.text.index(result.text.startIndex, offsetBy: result.cursor)
#expect(result.text[result.text.startIndex..<index].hasSuffix("@code\n\t"))
#expect(String(result.text[index...]) == "\n@end")
}
@Test func clampsCursorBeyondBounds() {
let result = EditorSnippet.heading.apply(to: "abc", cursor: 999)
#expect(result.text == "abc\n* ")
}
// MARK: - Heading promotion (the `*` button's special behaviour)
@Test func headingPromotesEmptyMarker() {
// "* |" -> "** |"
let result = EditorSnippet.heading.apply(to: "* ", cursor: 2)
#expect(result.text == "** ")
#expect(result.cursor == 3)
}
@Test func headingPromotesWithTrailingContent() {
// "* |Hello" -> "** |Hello"
let result = EditorSnippet.heading.apply(to: "* Hello", cursor: 2)
#expect(result.text == "** Hello")
#expect(result.cursor == 3)
}
@Test func headingPromotesBareStars() {
// "***|" -> "****|"
let result = EditorSnippet.heading.apply(to: "***", cursor: 3)
#expect(result.text == "****")
#expect(result.cursor == 4)
}
@Test func headingInsertsNewLineWhenCursorAfterContent() {
// "* Hello|" -> new heading line
let result = EditorSnippet.heading.apply(to: "* Hello", cursor: 7)
#expect(result.text == "* Hello\n* ")
}
@Test func headingInsertsNewLineWhenMarkerNotAtLineStart() {
// "Hello *|" -> new heading line, not promotion
let result = EditorSnippet.heading.apply(to: "Hello *", cursor: 7)
#expect(result.text == "Hello *\n* ")
}
@Test func headingPromotesIndentedMarker() {
let result = EditorSnippet.heading.apply(to: " ** ", cursor: 5)
#expect(result.text == " *** ")
}
@Test func headingPromotionKeepsCursorWhenInsertingAfterIt() {
// "*|*" -> "**|*" : the new star lands after the cursor, so the offset
// is unchanged.
let result = EditorSnippet.heading.apply(to: "**", cursor: 1)
#expect(result.text == "***")
#expect(result.cursor == 1)
}
// MARK: - Task button heading awareness
@Test func taskAddsMarkerToBareHeading() {
// "* |" -> "* ( ) |"
let result = EditorSnippet.task.apply(to: "* ", cursor: 2)
#expect(result.text == "* ( ) ")
#expect(result.cursor == 6)
}
@Test func taskAddsMarkerToHeadingPreservingLevelAndContent() {
// "** |Hello" -> "** ( ) |Hello"
let result = EditorSnippet.task.apply(to: "** Hello", cursor: 3)
#expect(result.text == "** ( ) Hello")
#expect(result.cursor == 7)
}
@Test func taskInsertsNewLineWhenHeadingHasContent() {
// "** Hello|" -> "** Hello\n- ( ) "
let result = EditorSnippet.task.apply(to: "** Hello", cursor: 8)
#expect(result.text == "** Hello\n- ( ) ")
#expect(result.cursor == result.text.count)
}
@Test func taskAddsMarkerToBareOrderedListItem() {
// "~ |" -> "~ ( ) |"
let result = EditorSnippet.task.apply(to: "~ ", cursor: 2)
#expect(result.text == "~ ( ) ")
#expect(result.cursor == 6)
}
@Test func taskAddsMarkerToOrderedListPreservingLevelAndContent() {
// "~~ |Hello" -> "~~ ( ) |Hello"
let result = EditorSnippet.task.apply(to: "~~ Hello", cursor: 3)
#expect(result.text == "~~ ( ) Hello")
#expect(result.cursor == 7)
}
@Test func taskInsertsNewLineWhenOrderedListHasContent() {
// "~ Hello|" -> "~ Hello\n- ( ) "
let result = EditorSnippet.task.apply(to: "~ Hello", cursor: 7)
#expect(result.text == "~ Hello\n- ( ) ")
#expect(result.cursor == result.text.count)
}
@Test func headingIgnoresOrderedListMarker() {
// The `*` button only promotes `*` headings, not `~` lists.
let result = EditorSnippet.heading.apply(to: "~ ", cursor: 2)
#expect(result.text == "~ \n* ")
}
// MARK: - Task button supports every detached modifier
@Test func taskAddsMarkerToBareUnorderedListItem() {
// "- |" -> "- ( ) |"
let result = EditorSnippet.task.apply(to: "- ", cursor: 2)
#expect(result.text == "- ( ) ")
#expect(result.cursor == 6)
}
@Test func taskAddsMarkerToEveryDetachedModifier() {
// Quotes, definitions, footnotes and the like all carry tasks per the
// Norg spec, matching NorgKit's task scanner.
for marker in [">", "$", "^", ":"] {
let result = EditorSnippet.task.apply(to: "\(marker) ", cursor: 2)
#expect(result.text == "\(marker) ( ) ")
#expect(result.cursor == 6)
}
}
@Test func taskAddsMarkerToNestedDetachedModifierPreservingContent() {
// ">> |Quote" -> ">> ( ) |Quote"
let result = EditorSnippet.task.apply(to: ">> Quote", cursor: 3)
#expect(result.text == ">> ( ) Quote")
#expect(result.cursor == 7)
}
@Test func taskIgnoresWeakDelimiterMadeOfDashes() {
// "---|" is a delimiter, not a list item — start a new task line instead.
let result = EditorSnippet.task.apply(to: "---", cursor: 3)
#expect(result.text == "---\n- ( ) ")
}
@Test func taskInsertsNewLineWhenMarkerHasNoSeparatingSpace() {
// A bare marker with no trailing space isn't a detached modifier yet.
let result = EditorSnippet.task.apply(to: ">", cursor: 1)
#expect(result.text == ">\n- ( ) ")
}
}
|