aboutsummaryrefslogtreecommitdiff
path: root/Tests/NorgKitTests/Models/NorgBlockTests.swift
blob: d9d18c2e46d476bce415da09132f80dc59719464 (plain)
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
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)
  }
}