Tests/OrgCoreTests/ASCIIExportTests.swift
392 lines · 25675 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// ox-ascii under `emacs -Q`, for whole documents: each case is exported in its own file.
6enum ASCIIOracle {
7 static let author = "A. Writer"
8
9 struct Case {
10 var text: String
11 var charset = ASCIIExport.Charset.ascii
12 var bodyOnly = false
13 /// Emacs point to export the subtree at.
14 var subtree: Int?
15 }
16
17 /// Each case's `org-export-as 'ascii`, or `ERROR: message`.
18 static func export(_ cases: [Case]) throws -> [String] {
19 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-ascii-\(UUID().uuidString)")
20 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
21 defer { try? FileManager.default.removeItem(at: folder) }
22 var input: [[String]] = []
23 for (index, c) in cases.enumerated() {
24 let url = folder.appendingPathComponent("doc\(index).org")
25 try c.text.write(to: url, atomically: true, encoding: .utf8)
26 input.append([url.path, c.bodyOnly ? "1" : "0", c.subtree.map(String.init) ?? "0", c.charset.rawValue])
27 }
28 let inputURL = folder.appendingPathComponent("input.json")
29 let output = folder.appendingPathComponent("output.json")
30 try JSONEncoder().encode(input).write(to: inputURL)
31 let script = folder.appendingPathComponent("export.el")
32 try #"""
33 ;;; -*- lexical-binding: t -*-
34 (require 'org)
35 (require 'ox-ascii)
36 (require 'json)
37 (setq user-full-name "A. Writer")
38 (let ((results nil))
39 (seq-doseq (c (json-read-file (getenv "ORACLE_INPUT")))
40 (with-current-buffer (find-file-noselect (aref c 0))
41 (let ((point (string-to-number (aref c 2)))
42 (org-ascii-charset (intern (aref c 3))))
43 (push (condition-case e
44 (progn (when (> point 0) (goto-char point))
45 (org-export-as 'ascii (> point 0) nil (equal (aref c 1) "1")))
46 (error (concat "ERROR: " (error-message-string e))))
47 results))
48 (kill-buffer)))
49 (let ((coding-system-for-write 'utf-8-unix))
50 (with-temp-file (getenv "ORACLE_OUTPUT")
51 (insert (json-encode (vconcat (nreverse results)))))))
52 """#.write(to: script, atomically: true, encoding: .utf8)
53 let log = try EmacsOracle.run(["-Q", "--batch", "-l", script.path], environment: ["ORACLE_INPUT": inputURL.path, "ORACLE_OUTPUT": output.path, "TZ": "UTC"])
54 guard let data = try? Data(contentsOf: output) else { throw EmacsOracle.EvaluateError(log: log) }
55 return try JSONDecoder().decode([String].self, from: data)
56 }
57
58 /// Ours for the same case, or `ERROR: reason`.
59 static func native(_ c: Case) -> String {
60 do {
61 var text = c.text
62 if let point = c.subtree {
63 guard let narrowed = ExportScope.subtree(text, at: EmacsOracle.offset(point, in: text)) else { return "ERROR: not in a subtree" }
64 text = narrowed
65 }
66 return try ASCIIExport.document(text, charset: c.charset, bodyOnly: c.bodyOnly, author: author)
67 } catch {
68 return "ERROR: \(error.localizedDescription)"
69 }
70 }
71}
72
73struct ASCIIExportTests {
74 static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
75 static let folder = URL(fileURLWithPath: #filePath).deletingLastPathComponent().appendingPathComponent("Conformance")
76 static let corpus = ["minimal", "core", "headings", "listblocks", "lists", "table", "blocks", "timestamps", "footnote", "images", "tblfm"]
77
78 /// Snippets for what the corpus doesn't show.
79 static let snippets: [String] = [
80 "No title here.\n",
81 "#+TITLE: A /styled/ title\n#+SUBTITLE: and more\n#+AUTHOR: Someone & Co\n#+DATE: <2024-03-01 Fri>\n\nText.\n",
82 "#+TITLE: A rather long title that goes on and on until it has to be filled over lines\n#+AUTHOR: Me\nx\n",
83 "#+OPTIONS: toc:nil num:nil author:nil date:nil title:nil\n#+TITLE: T\n* One\n** Two\n*** Three\n**** Four\n***** Five\n",
84 "#+OPTIONS: toc:2 num:1 H:2\n* One\n** Two\n*** Three\nbody\n*** Four\n* Back\n",
85 "#+OPTIONS: H:5\n* One\n** Two\n*** Three\n**** Four\n***** Five\ntext\n",
86 "#+OPTIONS: author:nil\n#+DATE: today\nx\n",
87 "#+DATE: today\nx\n",
88 "#+AUTHOR:\nx\n",
89 "Special: \\- -- --- ... a\\\\b\nand *bold*[x] then.\n",
90 "Math \\(a+b\\) and $x^2$ and \\alpha \\to \\deg{}C \\copy \\nbsp{}here \\pi.\n",
91 "Scripts x^2 y_{i} a_b.\n",
92 "#+OPTIONS: ^:{}\nScripts x^2 y_{i} a_b.\n",
93 "#+OPTIONS: ^:nil tex:nil\nScripts x^2 y_{i} and \\(a\\) gone.\n",
94 "Markup *b* /i/ _u_ +s+ =v= ~c~ and *nested /italic/ bold*.\n",
95 "Snippets @@ascii:raw@@ and @@html:<br>@@ gone. Also @@html:x@@ @@html:y@@ end.\n@@html:z@@ start\n",
96 "Line one\\\\\nline two \\\\\nafter\n",
97 "Links: [[https://orgmode.org][Org & co]] <https://a.b/c_d> https://x.y/z [[mailto:me@x.org]] [[file:doc.pdf][the doc]] [[./rel/file.txt]] [[file:/abs/path.org]]\n",
98 "* Target section\nSee [[*Other]].\n* Other\nSee [[*Target section]] and [[Target section][named]] and [[#cid]].\n* Third\n:PROPERTIES:\n:CUSTOM_ID: cid\n:END:\nx\n",
99 "#+OPTIONS: num:nil\n* Head\nSee [[*Head]] and [[*Head][desc]].\n",
100 "Footnotes[fn:1] twice[fn:1] and[fn:2][fn:3] and inline[fn::anon /x/] and[fn:named:inline def].\n\n[fn:1] First.\n\n[fn:2] Second\n with two lines.\n\n[fn:3] Third.\n",
101 "1. one\n2. [@5] five\n 1. nested\n 2. [@3] three\n3. after\n\n- [X] done\n- [-] half\n- [ ] todo\n\n- term :: def\n- [X] checked term :: def2\n",
102 "+ plus bullet\n+ second\n\n* not a heading\n",
103 "#+CAPTION: Scores\n#+NAME: tab:scores\n| Name | Score |\n|------+-------|\n| a | 1.5 |\n| b | |\n| c | 1e3 |\n",
104 "| *b* | =v= | [[https://x.y][l]] |\n|-----+-----+-----|\n| 1 | -2 | 3% |\n| x | | 4 |\n|-----+-----+-----|\n",
105 "|---+---|\n| a | b |\n|---+---|\n| c | d |\n|---+---|\n",
106 "| <l> | <r5> | <c> |\n| a | b | c |\n| long | x | yy |\n",
107 "#+NAME: code\n#+CAPTION: A listing\n#+begin_src python -n\nprint(1)\nprint(2)\n#+end_src\n\n#+begin_src emacs-lisp +n 10\n(a)\n#+end_src\n\n#+begin_example -r\nline (ref:x)\n#+end_example\n\n#+begin_src sh\n indented\n more\n#+end_src\n",
108 "#+begin_src c -n -k\nint x; (ref:decl)\nreturn x;\n#+end_src\n\nSee line [[(decl)]].\n",
109 "#+begin_quote\nQ /one/.\n\nQ two.\n#+end_quote\n\n#+begin_center\nC\n#+end_center\n#+begin_verse\n Roses are red,\n violets blue\n\nnext stanza\n#+end_verse\n",
110 "#+begin_warning\nW *b*.\n#+end_warning\n\n#+begin_export ascii\n raw\n#+end_export\n#+begin_export html\n<p>no</p>\n#+end_export\n#+begin_comment\nhidden\n#+end_comment\nafter\n",
111 "#+begin_JUSTIFYRIGHT\nRight.\n#+end_JUSTIFYRIGHT\n\n#+begin_center\nA table:\n| a | b |\n#+end_center\n",
112 "#+ASCII: raw line\n#+TOC: headlines 1\n: fixed\n: width\n\n-----\n\n#+ATTR_ASCII: :width 10\n-----\n* One\n** Two\n",
113 "\\begin{equation}\nx = 1\n\\end{equation}\n",
114 "Para one.\n# a comment\nPara two.\n\n# comment\n\nPara three.\n",
115 "* TODO [#B] Task :work:urgent_x:\nSCHEDULED: <2024-01-01 Mon>\n:PROPERTIES:\n:X: 1\n:END:\n:LOGBOOK:\n- note\n:END:\n:NOTES:\nDrawer *text*.\n:END:\nbody\n* Archived :ARCHIVE:\nhidden\n** child\n* Kept\n* Drop :noexport:\nx\n* COMMENT Gone\ny\n",
116 "#+OPTIONS: pri:t tags:nil todo:nil\n* TODO [#A] Title [1/2] [50%]\n- [X] a\n- [ ] b\n",
117 "#+OPTIONS: tags:not-in-toc\n* One :a:\n** Two :b:\n",
118 "#+SELECT_TAGS: pick\nPreamble text.\n* A\n* B :pick:\n** B1\n* C\n",
119 "Dates <2024-01-15 Mon 9:05> [2024-02-29 Thu 10:00-11:30] <2024-01-15 Mon +1w -2d> <2024-01-15 Mon>--<2024-01-17 Wed> end.\n",
120 "#+MACRO: greet Hello, $1!\n{{{greet(World)}}} and {{{title}}}.\n",
121 "#+begin_src emacs-lisp :exports code\n(+ 1 2)\n#+end_src\n\n#+RESULTS:\n: 3\n\nafter\n",
122 " Indented paragraph\n second line\n deeper\n\n- item first line\n continued\n more\n",
123 "* Low levels\n** two\n*** three\n**** four a\n**** four b\nbody\n***** five\n* next\n",
124 "#+EXCLUDE_TAGS: secret\n* A\n** B :secret:\n** C\n",
125 "#+OPTIONS: -:nil\nNo special... strings -- \\- here.\n",
126 "- a\n\n- b\n para two\n\n para three\n- c\n - d\n\n - e\nafter list\n",
127 "#+LANGUAGE: de\n* Teil\nText[fn:1] and [[https://x.y][link]].\n\n[fn:1] Note.\n",
128 "#+LANGUAGE: fr\n#+TOC: headlines\n* Un\n",
129 "A long paragraph that has to be filled because it runs on for well over the seventy-two columns of a line. Sentences end. Here.\n",
130 "* Heading with [[https://a.b][a link]] in it\nAnd [[https://c.d][another]] and [[https://a.b][a link]] again.\n** Sub\n[[https://e.f][third]]\n",
131 "#+TITLE: Title with [[https://a.b][link]]\nx\n",
132 "* A\n#+FOO: bar\n\n* B\n",
133 "#+TITLE: T\n\n* H\nx\n",
134 "| a |\n| 1 |\n",
135 "#+begin_example\n#+end_example\n\n#+begin_src sh\n#+end_src\n\nafter\n",
136 ]
137
138 static let extra: [String] = [
139 "#+OPTIONS: H:2 toc:3\n* A :x:\n** B\n*** C\n**** D\n***** E\n****** F\n* G\n",
140 "#+OPTIONS: num:2\n* A\n** B\n*** C\n**** D\n",
141 "* A\nText.[fn:1]\n** B\nMore.[fn:2]\n\n[fn:1] A note under A that is long enough to be filled at a width shorter than seventy-two.\n\n[fn:2] Short.\n\n\nNot part of the note.\n",
142 "Refs[fn:1].\n\n[fn:1] A note in the preamble that is long enough to be filled at the full width of seventy-two.\n",
143 "Inline[fn:: an inline note that is long enough to be filled at the full width of seventy-two columns wide].\n",
144 "| a[fn:1] | b |\n\n- item[fn:2]\n\n[fn:1] One.\n\n[fn:2] Two.\n",
145 "* Heading\nSee [[https://a.b][A]] here.\n** Child\nx\n",
146 "* Heading [[https://a.b][A]]\n** Child\nx\n",
147 "- item [[https://a.b][A]]\n - nested [[https://c.d][C]]\n\n| [[https://e.f][E]] |\n",
148 "Same [[https://a.b][A]] and [[https://a.b][A]] and [[https://a.b][B]] and [[https://a.b]].\n",
149 "#+begin_src sh -n -r\necho a (ref:one)\necho b\n#+end_src\nLine [[(one)]] and [[(one)][here]].\n",
150 "#+begin_center\n- one\n- two\n\n#+begin_quote\nq\n#+end_quote\n#+end_center\n",
151 "#+begin_quote\n- a list in a quote that runs on long enough to need filling inside the narrower width\n-----\n#+end_quote\n",
152 "- term :: definition\n - nested :: inner definition that is long enough to be filled at the narrower width\n",
153 "1. a\n2. b\n3. c\n4. d\n5. e\n6. f\n7. g\n8. h\n9. i\n10. j that is long enough to be filled at a width shorter by the wider bullet here\n",
154 "1. [X] done and long enough to wrap because the checkbox narrows the width of the item text\n2) other\n",
155 "| <c> |\n| a |\n| long cell |\n\n| <6> | <l3> |\n| a | b |\n",
156 "| 1 | x |\n| | y |\n| 3 | z |\n",
157 "#+begin_verse\n /Roses/ are *red*,\n violets \\alpha blue\n#+end_verse\n",
158 "Entities \\eacute \\copy \\laquo \\nbsp \\mdash \\hellip \\Aring \\sup2 \\frac12 \\times.\n",
159 "* TODO [#A] A task with a title long enough to push the tags past the edge :tag:other:\n",
160 "* A\n:PROPERTIES:\n:CUSTOM_ID: a\n:END:\n* B\nSee [[#a][the A]] and [[#a]].\n",
161 "#+OPTIONS: num:nil toc:nil\n* A\n* B\nSee [[*A]] and [[*A][x]].\n",
162 "#+TITLE: T\n#+SUBTITLE: The subtitle is longer than the title\n#+AUTHOR: An author with a long name\n#+DATE: 2024\nx\n",
163 "#+TITLE: Ünïcödé — title\n#+AUTHOR: Ëm\nx\n",
164 "#+begin_example\n a\n b\n#+end_example\n\n#+begin_example -i\n a\n#+end_example\n",
165 ": a\n: b\n: c\n",
166 "Text\n\n#+begin_src emacs-lisp :exports none\n(x)\n#+end_src\n\nafter\n",
167 "* A\n** B :noexport:\n*** C\n** D\n",
168 "* COMMENT A\n* B\n",
169 "x\n\n* Footnotes\n[fn:1] in section\n* After\ny[fn:1]\n",
170 "A sentence ends here. Another one follows. And a third e.g. this one continues on for a while to wrap the line.\n",
171 "Word\nbroken\n across\n lines\n",
172 "*bold that is long enough* /italic that goes on/ _underline_ +strike+ =verbatim text= ~code text~ and more words to wrap the line around.\n",
173 "#+begin_center\nA centered paragraph that is long enough to be filled over more than one line of the text width.\n#+end_center\n",
174 "#+begin_JUSTIFYRIGHT\n| a | b |\n#+end_JUSTIFYRIGHT\n",
175 "#+begin_JUSTIFYLEFT\nleft\n#+end_JUSTIFYLEFT\n",
176 "#+ATTR_ASCII: :width 20\n-----\n",
177 "* A\n#+TOC: headlines 2\n** B\n** C\n",
178 "Quoted mail:\n> first line of the quote that goes on long enough to need a second line\n> second line\n",
179 "-- a dash opens this paragraph which goes on long enough to need a second line of text.\n",
180 "| a | b | c |\n| 1 |\n| 2 | 3 |\n",
181 "- [X] term :: with a checkbox\n- plain :: def\n",
182 "#+begin_quote\n#+begin_quote\nInner quote that goes on long enough to need filling at the narrower width.\n#+end_quote\n#+end_quote\n",
183 "- - nested first\n - second\n- after\n",
184 "- line one\\\\\n line two\n\n#+begin_quote\nq one\\\\\nq two\n#+end_quote\n",
185 "#+OPTIONS: H:1\n* A\n** B\n*** C\n**** D\n***** E\ntext under E that is long enough to be filled at the narrower width of it.\n",
186 "#+OPTIONS: num:nil\n* /Styled/ title\n* Other\nSee [[*/Styled/ title][it]] and [[*/Styled/ title]].\n",
187 "* A\n:PROPERTIES:\n:CUSTOM_ID: a\n:END:\n* B\nNote.[fn:1]\n\n[fn:1] See [[#a][A]] and [[https://x.y][X]].\n",
188 "#+OPTIONS: <:nil\n<2024-01-15 Mon>\n\nText <2024-01-15 Mon> kept.\n",
189 "#+OPTIONS: stat:t\n* Tasks [1/2]\n- [X] a\n- [ ] b\n",
190 "Averyveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongword and more.\n",
191 "\n",
192 "#+TITLE: Only a title\n",
193 "#+begin_center\n#+ASCII: raw\n\\begin{x}\ny\n\\end{x}\n#+end_center\n",
194 "#+begin_justifyright\nlower\n#+end_justifyright\n",
195 "* A\n- item\n [fn:1]\n\n#+begin_quote\nq[fn:2]\n#+end_quote\n\n[fn:1] One.\n\n[fn:2] Two.\n",
196 "#+OPTIONS: H:2\n* A\n** B\n*** C\n#+TOC: headlines\n",
197 "#+OPTIONS: toc:1\n* A\n** B\n* C [[https://a.b][link]]\n",
198 "- a\n # comment\n b\n- c\n\n # comment\n\n d\n- # only\n e\n",
199 "* Tasks [1/3] [33%]\n** DONE One\nCLOSED: [2024-01-01 Mon 10:00]\n** TODO Two\n** Three\n",
200 "Some *bold\ntext* across /lines of\nitalic/ text.\n",
201 "#+begin_src emacs-lisp\n(message \"hi\")\n#+end_src\n\n#+begin_src python :exports code\nprint(1)\n#+end_src\n",
202 "Para.\n\n\n\nAfter three blank lines.\n\n- item\n\n\n- item after two\n",
203 "* A\n\n\nBody after blanks.\n\n\n* B\n",
204 "1. one\n\n para\n\n #+begin_example\n ex\n #+end_example\n2. two\n",
205 "- a\n | x | y |\n |---+---|\n | 1 | 2 |\n- b\n",
206 "#+TITLE: T\n#+AUTHOR: A\n#+DATE: <2024-01-02 Tue>\n#+OPTIONS: toc:nil\nx\n",
207 "#+AUTHOR: Just an author\n#+DATE: 2024-01-02\nx\n",
208 "#+DATE: 2024-01-02\nx\n",
209 "#+begin_src makefile\nall: x\n\tcc -o x x.c\n#+end_src\n",
210 "#+begin_src makefile -n -r\nall: x\n \t\tcc\tx (ref:a)\n#+end_src\n\n#+begin_example\nall: x\n\tcc\n#+end_example\n",
211 "#+LANGUAGE: ja\n#+OPTIONS: toc:nil\n* A\nx[fn:1]\n\n[fn:1] n\n",
212 "#+LANGUAGE: zh-CN\n* A\nx\n",
213 "Types [[DOI:10.1/z][d]] [[Attachment:x.png]] [[ID:x]] [[INFO:emacs][i]] b_doi:10.1/x.\n",
214 "#+LANGUAGE: ja\n* A\nSee [[*B][b]].\n* B\ny\n",
215 "#+LANGUAGE: zh-CN\n* A\nSee [[*B][b]].\n* B\ny\n",
216 "#+LANGUAGE: ja\n* A\nSee [[*B][\(String(repeating: "b", count: 72))]].\n* B\ny\n",
217 "Text doi:- here, doi:a. and b.\n",
218 "x HTTPS://x.y/z and mailto:a@b.c and File:a.txt. FILE:foo-- \"q\" FILE:a(b) Mailto:a@b, y\n",
219 "a\\\\\\\\\nb\n\nc\\\\\\\\\\\\ \nd\n\ne\\\\\nf\n",
220 "#+OPTIONS: e:t\n\\alpha\\beta \\ast{} \\S \\para \\deg \\plusmn \\micro \\middot \\cedil \\ordf \\iexcl \\pound \\yen \\euro \\trade\n",
221 ]
222
223 static var cases: [(String, ASCIIOracle.Case)] {
224 var cases: [(String, ASCIIOracle.Case)] = []
225 for charset in ASCIIExport.Charset.allCases {
226 for name in corpus {
227 let text = (try? String(contentsOf: folder.appendingPathComponent("\(name).org"), encoding: .utf8)) ?? ""
228 cases.append(("\(name) \(charset.rawValue)", ASCIIOracle.Case(text: text, charset: charset)))
229 }
230 for (index, snippet) in (snippets + extra).enumerated() {
231 cases.append(("snippet \(index) \(charset.rawValue)", ASCIIOracle.Case(text: snippet, charset: charset)))
232 }
233 }
234 return cases
235 }
236
237 @Test(.enabled(if: oracle))
238 func matchesEmacs() throws {
239 let all = Self.cases
240 try compare(all.map(\.1), names: all.map(\.0))
241 }
242
243 @Test(.enabled(if: oracle))
244 func bodyOnlyAndSubtreesMatchEmacs() throws {
245 let doc = "#+TITLE: Doc\n#+OPTIONS: toc:nil\nIntro.\n* One\nFirst.\n** Sub\n:PROPERTIES:\n:EXPORT_TITLE: Exported title\n:END:\nSub body *b*.\n*** Deeper\nx\n* Two\nSecond.\n"
246 let sub = (doc as NSString).range(of: "** Sub").location
247 let one = (doc as NSString).range(of: "First.").location
248 var cases: [ASCIIOracle.Case] = []
249 var names: [String] = []
250 for charset in ASCIIExport.Charset.allCases {
251 cases += [
252 ASCIIOracle.Case(text: doc, charset: charset, bodyOnly: true),
253 ASCIIOracle.Case(text: try String(contentsOf: Self.folder.appendingPathComponent("blocks.org"), encoding: .utf8), charset: charset, bodyOnly: true),
254 ASCIIOracle.Case(text: doc, charset: charset, subtree: EmacsOracle.point(sub, in: doc)),
255 ASCIIOracle.Case(text: doc, charset: charset, subtree: EmacsOracle.point(one, in: doc)),
256 ASCIIOracle.Case(text: doc, charset: charset, bodyOnly: true, subtree: EmacsOracle.point(sub, in: doc)),
257 ]
258 names += ["body", "blocks body", "subtree", "subtree from body", "subtree body"].map { "\($0) \(charset.rawValue)" }
259 }
260 try compare(cases, names: names)
261 }
262
263 /// A subtree's `EXPORT_*` properties replace the file's keywords.
264 @Test(.enabled(if: oracle))
265 func subtreePropertiesMatchEmacs() throws {
266 let doc = """
267 #+TITLE: Doc
268 #+SUBTITLE: File sub
269 #+AUTHOR: File author
270 #+LANGUAGE: fr
271 #+EXCLUDE_TAGS: drop
272 * Top
273 :PROPERTIES:
274 :EXPORT_SUBTITLE: Sub
275 :EXPORT_AUTHOR: Someone
276 :EXPORT_DATE: 2024-05-06
277 :EXPORT_LANGUAGE: de
278 :EXPORT_EXCLUDE_TAGS: gone
279 :EXPORT_OPTIONS: toc:nil
280 :END:
281 Body[fn:1].
282 ** Child
283 x
284 ** Dropped :gone:
285 z
286 ** Kept :drop:
287 w
288
289 [fn:1] Note.
290
291 """
292 let top = (doc as NSString).range(of: "Body").location
293 let cases = ASCIIExport.Charset.allCases.map { ASCIIOracle.Case(text: doc, charset: $0, subtree: EmacsOracle.point(top, in: doc)) }
294 try compare(cases, names: ASCIIExport.Charset.allCases.map { "subtree properties \($0.rawValue)" })
295 }
296
297 /// Links to what the export leaves out stop both exports.
298 @Test(.enabled(if: oracle))
299 func abortsWhereEmacsAborts() throws {
300 let docs = [
301 "* A :noexport:\n* B\nSee [[*A]].\n",
302 "* A :noexport:\n:PROPERTIES:\n:CUSTOM_ID: a\n:END:\n* B\nSee [[#a]].\n",
303 "* COMMENT A\n* B\nSee [[*A]].\n",
304 "See [[nowhere]].\n",
305 "See [[id:nowhere]].\n",
306 "#+OPTIONS: num:nil\n* /Styled/ title\nSee [[*Styled title]].\n",
307 ]
308 let emacs = try ASCIIOracle.export(docs.map { ASCIIOracle.Case(text: $0) })
309 for (doc, theirs) in zip(docs, emacs) {
310 let ours = ASCIIOracle.native(ASCIIOracle.Case(text: doc))
311 #expect(theirs.hasPrefix("ERROR: "), "\(doc)\n--- emacs\n\(theirs)")
312 #expect(ours.hasPrefix("ERROR: Unable to resolve link"), "\(doc)\n--- ours\n\(ours)")
313 }
314 }
315
316 /// Slovenian's "See section %d" stops Emacs's `format` when given a section.
317 @Test(.enabled(if: oracle))
318 func abortsOnSlovenianSectionReference() throws {
319 let cases = ASCIIExport.Charset.allCases.map { ASCIIOracle.Case(text: "#+LANGUAGE: sl\n* A\nSee [[*B][b]].\n* B\ny\n", charset: $0) }
320 for (c, theirs) in zip(cases, try ASCIIOracle.export(cases)) {
321 #expect(theirs == "ERROR: Format specifier doesn’t match argument type" || theirs == "ERROR: Format specifier doesn't match argument type", "\(theirs)")
322 #expect(ASCIIOracle.native(c) == "ERROR: Format specifier doesn't match argument type")
323 }
324 }
325
326 /// What Emacs exports differently or refuses is refused, naming it.
327 @Test func refusesWhatItCannotMatch() {
328 func reason(_ org: String) -> String? {
329 do {
330 _ = try ASCIIExport.document(org)
331 return nil
332 } catch let error as ASCIIExport.Unsupported {
333 return error.reason
334 } catch {
335 return "\(error)"
336 }
337 }
338 #expect(reason("Ref[fn:9].\n") == "Definition not found for footnote 9")
339 #expect(reason("#+OPTIONS: \\n:t\nx\n") == "#+OPTIONS: \\n:t")
340 #expect(reason("#+OPTIONS: tex:verbatim\nx\n") == "#+OPTIONS: tex:verbatim")
341 #expect(reason("#+begin_src sh :exports results\necho\n#+end_src\n") == ":exports results")
342 #expect(reason("[cite:@key]\n") == "Citations")
343 #expect(reason("src_sh{echo 1}\n") == "Inline source blocks")
344 #expect(reason("A <<here>> target.\nSee [[here]].\n") == "Links to targets and named elements")
345 #expect(reason("#+NAME: t\n| a |\nSee [[t]].\n") == "Links to targets and named elements")
346 #expect(reason("| / | < | > |\n| | a | b |\n") == "Tables with special columns or column groups")
347 #expect(reason("Text[fn:1].\n\n[fn:1] One.\n\nTwo.\n") == "Footnote definitions of several paragraphs")
348 #expect(reason("漢字\n") == "Wide or combining characters")
349 #expect(reason("a\tb\n") == "Tabs outside code")
350 #expect(reason("#+begin_src make\n\tcc\n#+end_src\n") == "Tabs in indented code")
351 #expect(reason("#+TOC: tables\n") == "#+TOC: tables")
352 #expect(reason("-\n- x\n") == "Empty list items")
353 #expect(reason("* A\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n") == "UNNUMBERED and ALT_TITLE properties")
354 #expect(reason("* A [fn:1]\n\n[fn:1] x\n") == "Footnotes in headings")
355 #expect(reason("#+begin_quote\n\nq\n#+end_quote\n") == "Blocks opening with a blank line")
356 #expect(reason("Plain doi:10.1/z here.\n") == "Plain links of type doi")
357 #expect(reason("Plain info:emacs#Top\n") == "Plain links of type info")
358 #expect(reason("Run shell:ls and elisp:foo.\n") == "Plain links of type shell")
359 #expect(reason("<irc:/irc.x/chan>\n") == "Plain links of type irc")
360 #expect(reason("| docview:a.pdf |\n") == "Plain links of type docview")
361 #expect(reason("[[doi:10.1/z][d]]\n") == "Links of type doi")
362 #expect(reason("[[attachment:x.png]]\n") == "Links of type attachment")
363 #expect(reason("<info:emacs#Top>\n") == "Links of type info")
364 #expect(reason("Case DOI:10.1/z.\n") == "Plain links of type DOI")
365 #expect(reason("#+LANGUAGE: ja\n#+CAPTION: c\n| a |\n") == "Wide or combining characters")
366 #expect(reason("#+LANGUAGE: ja\n* A\nSee [[*B][\(String(repeating: "b", count: 56))]].\n* B\n") == "Wide or combining characters")
367 #expect(reason("#+LANGUAGE: ja\nx[fn:1]\n* B\n\n[fn:1] See [[*B][b]].\n") == "Wide or combining characters")
368 #expect(reason("Text doi:- here.\n") == nil)
369 #expect(reason("Plain FILE:foo.txt and HTTPS://x.y here.\n") == nil)
370 #expect(reason("Plain FILE:foo.txt and SHELL:ls here.\n") == "Plain links of type SHELL")
371 #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B][b]].\n* B\n") == "Format specifier doesn't match argument type")
372 #expect(reason("#+LANGUAGE: sl\n* A\nSee [[*B]].\n* B\n") == nil)
373 }
374
375 private func compare(_ cases: [ASCIIOracle.Case], names: [String]) throws {
376 let emacs = try ASCIIOracle.export(cases)
377 for (index, c) in cases.enumerated() {
378 let ours = ASCIIOracle.native(c)
379 if ours.hasPrefix("ERROR: ") {
380 Issue.record("\(names[index]) unsupported: \(ours)\n--- org\n\(c.text)")
381 continue
382 }
383 if let dump = ProcessInfo.processInfo.environment["ASCII_DUMP"], ours != emacs[index] {
384 let name = names[index].replacingOccurrences(of: " ", with: "_")
385 try? ours.write(toFile: dump + "/" + name + ".ours", atomically: true, encoding: .utf8)
386 try? emacs[index].write(toFile: dump + "/" + name + ".emacs", atomically: true, encoding: .utf8)
387 try? c.text.write(toFile: dump + "/" + name + ".org", atomically: true, encoding: .utf8)
388 }
389 #expect(ours == emacs[index], "\(names[index])\n--- org\n\(c.text)\n--- ours\n\(ours)\n--- emacs\n\(emacs[index])")
390 }
391 }
392}