aboutsummaryrefslogtreecommitdiff
path: root/Tests/NorgUITests/InlineTextTests.swift
blob: c2c653765a26695bd96e541204a1b8688dda17ae (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
import NorgKit
import SwiftUI
import Testing

@testable import NorgUI

@Suite struct InlineTextTests {
  @Test func plainSpanCarriesText() {
    let attributed = AttributedString(norg: [InlineSpan(text: "hello")])
    #expect(String(attributed.characters) == "hello")
  }

  @Test func multipleSpansConcatenate() {
    let attributed = AttributedString(norg: [
      InlineSpan(text: "foo"),
      InlineSpan(text: "bar"),
    ])
    #expect(String(attributed.characters) == "foobar")
  }

  @Test func verbatimUsesThemeColor() {
    let attributed = AttributedString(norg: [InlineSpan(text: "x", styles: .verbatim)])
    let run = attributed.runs.first
    #expect(run?.foregroundColor == NorgTheme.default.verbatimColor)
  }

  @Test func spoilerAppliesForegroundAndBackground() {
    let attributed = AttributedString(norg: [InlineSpan(text: "x", styles: .spoiler)])
    let run = attributed.runs.first
    #expect(run?.foregroundColor == NorgTheme.default.spoilerForeground)
    #expect(run?.backgroundColor == NorgTheme.default.spoilerBackground)
  }

  @Test func superscriptUsesThemeOffset() {
    let attributed = AttributedString(norg: [InlineSpan(text: "x", styles: .superscript)])
    #expect(attributed.runs.first?.baselineOffset == NorgTheme.default.superscriptOffset)
  }

  @Test func subscriptUsesThemeOffset() {
    let attributed = AttributedString(norg: [InlineSpan(text: "x", styles: .subscript)])
    #expect(attributed.runs.first?.baselineOffset == NorgTheme.default.subscriptOffset)
  }

  @Test func externalLinkBecomesTappable() {
    let attributed = AttributedString(norg: [
      InlineSpan(text: "site", link: InlineLink(kind: .link, target: "https://example.com"))
    ])
    let run = attributed.runs.first
    #expect(run?.link == URL(string: "https://example.com"))
    #expect(run?.foregroundColor == NorgTheme.default.linkColor)
  }

  @Test func internalLinkIsEncodedForRouting() {
    let attributed = AttributedString(norg: [
      InlineSpan(text: "note", link: InlineLink(kind: .link, target: "* Some heading"))
    ])
    let run = attributed.runs.first
    let decoded = run?.link.flatMap(NorgLinkURL.decode)
    #expect(decoded == InlineLink(kind: .link, target: "* Some heading"))
    #expect(run?.foregroundColor == NorgTheme.default.linkColor)
  }

  @Test func anchorIsEncodedForRouting() {
    let attributed = AttributedString(norg: [
      InlineSpan(text: "ref", link: InlineLink(kind: .anchor, target: "footnote"))
    ])
    let run = attributed.runs.first
    let decoded = run?.link.flatMap(NorgLinkURL.decode)
    #expect(decoded == InlineLink(kind: .anchor, target: "footnote"))
  }

  @Test func customThemeOverridesColors() {
    var theme = NorgTheme.default
    theme.verbatimColor = .orange
    let attributed = AttributedString(
      norg: [InlineSpan(text: "x", styles: .verbatim)], theme: theme)
    #expect(attributed.runs.first?.foregroundColor == .orange)
  }
}