aboutsummaryrefslogtreecommitdiff
path: root/Tests/NorgKitTests/Models/NorgBlockTests.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Tests/NorgKitTests/Models/NorgBlockTests.swift')
-rw-r--r--Tests/NorgKitTests/Models/NorgBlockTests.swift127
1 files changed, 127 insertions, 0 deletions
diff --git a/Tests/NorgKitTests/Models/NorgBlockTests.swift b/Tests/NorgKitTests/Models/NorgBlockTests.swift
new file mode 100644
index 0000000..d9d18c2
--- /dev/null
+++ b/Tests/NorgKitTests/Models/NorgBlockTests.swift
@@ -0,0 +1,127 @@
+import Foundation
+import Testing
+
+@testable import NorgKit
+
+struct NorgBlockTests {
+
+ private let span = [InlineSpan(text: "x")]
+ private let title = [InlineSpan(text: "t")]
+ private let body: [NorgBlock] = [.paragraph(content: [InlineSpan(text: "b")], line: 1)]
+
+ private var allBlocks: [NorgBlock] {
+ [
+ .heading(level: 1, status: .done, content: span, line: 0),
+ .paragraph(content: span, line: 1),
+ .unorderedListItem(level: 1, status: .undone, content: span, line: 2),
+ .orderedListItem(level: 1, status: .pending, content: span, line: 3),
+ .quote(level: 1, status: .urgent, content: span, line: 4),
+ .codeBlock(language: "swift", code: "c", line: 5),
+ .definition(title: title, status: .needsInput, body: body, line: 6),
+ .footnote(title: title, status: .onHold, body: body, line: 7),
+ .tableCell(title: title, status: .recurring, body: body, line: 8),
+ .rangedTag(name: ["img"], parameters: ["a"], content: "c", line: 9),
+ .horizontalRule(line: 10),
+ .weakDelimiter(line: 11),
+ .strongDelimiter(line: 12),
+ ]
+ }
+
+ @Test func lineReportsSourceLineForEveryCase() {
+ for (expected, block) in allBlocks.enumerated() {
+ #expect(block.line == expected)
+ }
+ }
+
+ @Test func statusReturnsValueForStatusBearingBlocks() {
+ #expect(NorgBlock.heading(level: 1, status: .done, content: span, line: 0).status == .done)
+ #expect(
+ NorgBlock.unorderedListItem(level: 1, status: .undone, content: span, line: 0).status
+ == .undone)
+ #expect(
+ NorgBlock.orderedListItem(level: 1, status: .pending, content: span, line: 0).status
+ == .pending)
+ #expect(NorgBlock.quote(level: 1, status: .urgent, content: span, line: 0).status == .urgent)
+ #expect(
+ NorgBlock.definition(title: title, status: .needsInput, body: body, line: 0).status
+ == .needsInput)
+ #expect(
+ NorgBlock.footnote(title: title, status: .onHold, body: body, line: 0).status == .onHold)
+ #expect(
+ NorgBlock.tableCell(title: title, status: .recurring, body: body, line: 0).status
+ == .recurring)
+ }
+
+ @Test func statusIsNilForBlocksThatCannotCarryOne() {
+ #expect(NorgBlock.paragraph(content: span, line: 0).status == nil)
+ #expect(NorgBlock.codeBlock(language: nil, code: "c", line: 0).status == nil)
+ #expect(NorgBlock.rangedTag(name: ["x"], parameters: [], content: "c", line: 0).status == nil)
+ #expect(NorgBlock.horizontalRule(line: 0).status == nil)
+ #expect(NorgBlock.weakDelimiter(line: 0).status == nil)
+ #expect(NorgBlock.strongDelimiter(line: 0).status == nil)
+ }
+
+ @Test func contentReturnsInlineSpansOrTitle() {
+ #expect(NorgBlock.heading(level: 1, status: nil, content: span, line: 0).content == span)
+ #expect(NorgBlock.paragraph(content: span, line: 0).content == span)
+ #expect(
+ NorgBlock.unorderedListItem(level: 1, status: nil, content: span, line: 0).content == span)
+ #expect(
+ NorgBlock.orderedListItem(level: 1, status: nil, content: span, line: 0).content == span)
+ #expect(NorgBlock.quote(level: 1, status: nil, content: span, line: 0).content == span)
+ #expect(NorgBlock.definition(title: title, status: nil, body: body, line: 0).content == title)
+ #expect(NorgBlock.footnote(title: title, status: nil, body: body, line: 0).content == title)
+ #expect(NorgBlock.tableCell(title: title, status: nil, body: body, line: 0).content == title)
+ }
+
+ @Test func contentIsNilForContentlessBlocks() {
+ #expect(NorgBlock.codeBlock(language: nil, code: "c", line: 0).content == nil)
+ #expect(NorgBlock.rangedTag(name: ["x"], parameters: [], content: "c", line: 0).content == nil)
+ #expect(NorgBlock.horizontalRule(line: 0).content == nil)
+ #expect(NorgBlock.weakDelimiter(line: 0).content == nil)
+ #expect(NorgBlock.strongDelimiter(line: 0).content == nil)
+ }
+
+ @Test func bodyReturnsNestedBlocksForRangeableBlocks() {
+ #expect(NorgBlock.definition(title: title, status: nil, body: body, line: 0).body == body)
+ #expect(NorgBlock.footnote(title: title, status: nil, body: body, line: 0).body == body)
+ #expect(NorgBlock.tableCell(title: title, status: nil, body: body, line: 0).body == body)
+ }
+
+ @Test func bodyIsNilForNonRangeableBlocks() {
+ #expect(NorgBlock.heading(level: 1, status: nil, content: span, line: 0).body == nil)
+ #expect(NorgBlock.paragraph(content: span, line: 0).body == nil)
+ #expect(NorgBlock.codeBlock(language: nil, code: "c", line: 0).body == nil)
+ #expect(NorgBlock.horizontalRule(line: 0).body == nil)
+ }
+
+ @Test func settingStatusReplacesStatusOnSupportingBlocks() {
+ let cases: [NorgBlock] = [
+ .heading(level: 2, status: nil, content: span, line: 0),
+ .unorderedListItem(level: 1, status: nil, content: span, line: 0),
+ .orderedListItem(level: 1, status: nil, content: span, line: 0),
+ .quote(level: 1, status: nil, content: span, line: 0),
+ .definition(title: title, status: nil, body: body, line: 0),
+ .footnote(title: title, status: nil, body: body, line: 0),
+ .tableCell(title: title, status: nil, body: body, line: 0),
+ ]
+ for block in cases {
+ #expect(block.status == nil)
+ let set = block.settingStatus(.done)
+ #expect(set.status == .done)
+ #expect(set.line == block.line)
+ #expect(set.content == block.content)
+ #expect(set.body == block.body)
+ #expect(set.settingStatus(nil).status == nil)
+ }
+ }
+
+ @Test func settingStatusLeavesUnsupportedBlocksUnchanged() {
+ let paragraph = NorgBlock.paragraph(content: span, line: 0)
+ #expect(paragraph.settingStatus(.done) == paragraph)
+ let rule = NorgBlock.horizontalRule(line: 0)
+ #expect(rule.settingStatus(.done) == rule)
+ let code = NorgBlock.codeBlock(language: "swift", code: "c", line: 0)
+ #expect(code.settingStatus(.urgent) == code)
+ }
+}