Tests/OrgHighlightTests/OrgHighlighterTests.swift

main
org-live/Tests/OrgHighlightTests/OrgHighlighterTests.swift history · blame · raw

88 lines · 3878 bytes

10 symbols in this file
 1import Testing
 2@testable import OrgHighlight
 3
 4/// The inner HTML of each line, without the `<div class="ln">` wrapper.
 5private func lines(_ source: String) -> [String] {
 6    OrgHighlighter.lines(source).map { line in
 7        #expect(line.hasPrefix("<div class=\"ln\">") && line.hasSuffix("\n</div>"))
 8        return String(line.dropFirst("<div class=\"ln\">".count).dropLast("\n</div>".count))
 9    }
10}
11
12@Test func oneDivPerSourceLine() {
13    #expect(lines("").count == 1)
14    #expect(lines("a\n").count == 2)
15    #expect(lines("a\n\nb").count == 3)
16}
17
18@Test func escapesHTML() {
19    #expect(lines("<script>&") == ["&lt;script&gt;&amp;"])
20}
21
22@Test func headings() {
23    #expect(lines("* Title") == [#"<span class="h1">* Title</span>"#])
24    #expect(lines("** TODO [#A] Fix it :work:urgent:") == [
25        #"<span class="h2">** <span class="todo">TODO</span> <span class="pri">[#A]</span> Fix it <span class="tag">:work:urgent:</span></span>"#
26    ])
27    #expect(lines("**** DONE x") == [#"<span class="h1">**** <span class="done">DONE</span> x</span>"#])
28    #expect(lines("*bold* text") == [#"<span class="b">*bold*</span> text"#])
29    #expect(lines("* TODOs") == [#"<span class="h1">* TODOs</span>"#])
30    #expect(lines("* a:b:") == [#"<span class="h1">* a:b:</span>"#])
31}
32
33@Test func blocks() {
34    let src = "#+BEGIN_SRC swift\nlet x = *y*\n#+end_src\n*b*"
35    #expect(lines(src) == [
36        #"<span class="meta">#+BEGIN_SRC swift</span>"#,
37        #"<span class="blk">let x = *y*</span>"#,
38        #"<span class="meta">#+end_src</span>"#,
39        #"<span class="b">*b*</span>"#,
40    ])
41    #expect(lines("#+begin_quote\n* not a heading /i/\n#+end_quote") == [
42        #"<span class="meta">#+begin_quote</span>"#,
43        #"* not a heading <span class="i">/i/</span>"#,
44        #"<span class="meta">#+end_quote</span>"#,
45    ])
46}
47
48@Test func lineKinds() {
49    #expect(lines("#+TITLE: Notes") == [#"<span class="kw">#+TITLE:</span> Notes"#])
50    #expect(lines("# a comment") == [#"<span class="cmt"># a comment</span>"#])
51    #expect(lines("#hashtag") == ["#hashtag"])
52    #expect(lines("| a | b |") == [#"<span class="tbl">| a | b |</span>"#])
53    #expect(lines("  :PROPERTIES:") == [#"<span class="drw">  :PROPERTIES:</span>"#])
54    #expect(lines(":ID: 42") == [#"<span class="drw">:ID:</span> 42"#])
55    #expect(lines(": fixed") == [#"<span class="fix">: fixed</span>"#])
56    #expect(lines("-----") == [#"<span class="hr">-----</span>"#])
57}
58
59@Test func lists() {
60    #expect(lines("- [X] done") == [#"<span class="bul">- </span><span class="cb">[X]</span> done"#])
61    #expect(lines("  1. one") == [#"<span class="bul">  1. </span>one"#])
62    #expect(lines("  * star item") == [#"<span class="bul">  * </span>star item"#])
63    #expect(lines("-5 degrees") == ["-5 degrees"])
64}
65
66@Test func inlineMarkup() {
67    #expect(lines("a /it/ _u_ +s+ =v= ~c~.") == [
68        #"a <span class="i">/it/</span> <span class="u">_u_</span> <span class="s">+s+</span> <span class="v">=v=</span> <span class="v">~c~</span>."#
69    ])
70    #expect(lines("a/b/c and 2*3*4") == ["a/b/c and 2*3*4"])
71    #expect(lines("* not closed") == [#"<span class="h1">* not closed</span>"#])
72    #expect(lines("x * y * z") == ["x * y * z"])
73}
74
75@Test func linksAndTimestamps() {
76    #expect(lines("see [[https://a.b][A <b>]] now") == [
77        #"see <span class="lnk">[[https://a.b][A &lt;b&gt;]]</span> now"#
78    ])
79    #expect(lines("at https://a.b/c/d/ ok") == [#"at <span class="lnk">https://a.b/c/d/</span> ok"#])
80    #expect(lines("<2026-11-01 Sun> [2026-11-01]") == [
81        #"<span class="ts">&lt;2026-11-01 Sun&gt;</span> <span class="ts">[2026-11-01]</span>"#
82    ])
83    #expect(lines("note[fn:1]") == [#"note<span class="fn">[fn:1]</span>"#])
84}
85
86@Test func nonASCII() {
87    #expect(lines("* Café /naïve/ 日本") == [#"<span class="h1">* Café <span class="i">/naïve/</span> 日本</span>"#])
88}