Tests/OrgCoreTests/ParserGapsTests.swift
cmc Parser: citations, export snippets, inline calls, diary sexps, table.el, item tags
0cc0e6b91b 1 month ago122 lines · 5633 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// Citations, export snippets, inline Babel calls, diary sexps, table.el tables, and item
6/// counters and tags, where org-element finds them.
7struct ParserGapsTests {
8 static let text = #"""
9 As [cite:@doe2020] and [cite/t/b:see @a p. 2; @b;and @c-d:e] say, also [cite:pre; @x; post].
10 Not a cite: [cite:no key], [cite/:@x], [cite:@k [nested] here], [cite:@open
11 and on @@html:<b>bold</b>@@ or @@latex:\emph{x}@@, not @@x@@ or @@:y@@ or @@html:open.
12 Calls: call_square(4) call_f[:results raw](x=1, y=(2))[:exports both] xcall_no(1) call_f[x] call_(1).
13 %%(diary-float t 4 2) Thanksgiving
14 %%(indented) is text
15
16 +----+-----+
17 | a | b |
18 +----+-----+
19 | c | d |
20 +----+-----+
21
22 +--+--+
23 Single rule is a paragraph.
24
25 +--+
26 |x |
27 not closed by a rule
28
29 | org | table |
30 +---+---+
31 | other |
32 +-+
33 +-+
34
35 - term :: definition with *bold*
36 - a :: b :: c
37 - [X] boxed :: yes
38 - [@3] counted
39 - [@start:7] [X] both
40 - no tag::here
41 - tag ::
42 1. one :: two
43 2. [@5] five
44 - plain
45 - *bold* term :: x
46 """# + "\n"
47
48 static let kinds: [(lisp: String, ours: SyntaxKind)] = [
49 ("citation", .citation), ("citation-reference", .citationReference), ("export-snippet", .exportSnippet),
50 ("inline-babel-call", .inlineBabelCall), ("diary-sexp", .diarySexp), ("paragraph", .paragraph), ("item", .item),
51 ]
52
53 @Test func objectsAndElementsMatchOrgElement() throws {
54 let tree = OrgParser.parse(Self.text)
55 var forms = Self.kinds.map { kind in
56 "(mapconcat (lambda (o) (format \"%d\" (1- (org-element-begin o)))) (org-element-map (org-element-parse-buffer) '\(kind.lisp) #'identity) \",\")"
57 }
58 forms.append("(mapconcat (lambda (o) (format \"%d:%s\" (1- (org-element-begin o)) (org-element-property :type o))) (org-element-map (org-element-parse-buffer) 'table #'identity) \",\")")
59 forms.append("""
60 (mapconcat (lambda (o) (format "%s|%s|%s" (let ((tag (org-element-interpret-data (org-element-property :tag o)))) (if (string-empty-p tag) "-" tag))
61 (or (org-element-property :counter o) "-") (or (org-element-property :checkbox o) "-")))
62 (org-element-map (org-element-parse-buffer) 'item #'identity) ",")
63 """)
64 let emacs = try EmacsOracle.evaluate(Self.text, "(with-current-buffer (find-file-noselect \"oracle.org\") (org-mode) (list \(forms.joined(separator: " "))))")
65 let nodes = tree.root.descendants()
66 for (i, kind) in Self.kinds.enumerated() {
67 let ours = nodes.filter { $0.kind == kind.ours }.map { String($0.range.lowerBound) }
68 #expect(ours == emacs[i].split(separator: ",").map(String.init), "\(kind.lisp)")
69 }
70 let tables = nodes.filter { $0.kind == .table || $0.kind == .tableEl }.map { "\($0.range.lowerBound):\($0.kind == .table ? "org" : "table.el")" }
71 #expect(tables == emacs[Self.kinds.count].split(separator: ",").map(String.init), "tables")
72 let items = nodes.filter { $0.kind == .item }.map { item -> String in
73 let tag = item.firstChild(.itemTag)?.text ?? "-"
74 let counter = item.tokens.first { $0.kind == .counter }.map { $0.text.replacingOccurrences(of: "[@start:", with: "").replacingOccurrences(of: "[@", with: "").replacingOccurrences(of: "]", with: "") } ?? "-"
75 let box = item.tokens.first { $0.kind == .checkbox }.map { ["[X]": "on", "[x]": "on", "[ ]": "off", "[-]": "trans"][$0.text]! } ?? "-"
76 return "\(tag)|\(counter)|\(box)"
77 }
78 #expect(items == emacs[Self.kinds.count + 1].split(separator: ",").map(String.init), "items")
79 }
80
81 @Test func citationParts() throws {
82 let tree = OrgParser.parse("[cite/t:see @a p. 2; @b; post]\n")
83 let cite = try #require(tree.root.descendants().first { $0.kind == .citation })
84 #expect(cite.text == "[cite/t:see @a p. 2; @b; post]")
85 let references = cite.children.filter { $0.kind == .citationReference }
86 #expect(references.map(\.text) == ["see @a p. 2;", " @b;"])
87 #expect(references.compactMap { $0.tokens.first { $0.kind == .citationKey }?.text } == ["@a", "@b"])
88 }
89}
90
91/// The new syntax through ox-html, compared as the conformance files are.
92struct ParserGapsExportTests {
93 static let text = """
94 Raw @@html:<b>bold</b>@@ and @@latex:\\emph{x}@@ here.
95
96 %%(diary-float t 4 2) Thanksgiving
97
98 - term :: definition here
99 - untagged
100 - [X] boxed :: yes
101
102
103 1. one
104 2. two
105
106
107 - [@3] unordered counter
108 """ + "\n"
109
110 @Test func htmlMatchesOxHTML() throws {
111 let emacs = try EmacsOracle.evaluate(Self.text, """
112 (list (progn (require 'ox-html)
113 (with-current-buffer (find-file-noselect "oracle.org")
114 (org-export-as 'html nil nil t '(:with-toc nil :section-numbers nil)))))
115 """)
116 let ours = Skeleton.reduce(HTMLExport.body(Self.text))
117 #expect(ours == Skeleton.reduce(emacs[0]), "ours:\n\(ours)\nemacs:\n\(Skeleton.reduce(emacs[0]))")
118 // Org 9.8.7's ox-html fails on an ordered item's counter (`format` given a number);
119 // the value is what it means to write.
120 #expect(HTMLExport.body("1. one\n2. [@5] five\n").contains("<li value=\"5\">five</li>"))
121 }
122}