Tests/OrgCoreTests/ExportScopeTests.swift
cmc Export: per-file settings, and subtree and body-only for the export commands
2986af1074 4 days ago122 lines · 5570 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct ExportScopeTests {
6 static let file = """
7 #+TITLE: Whole file
8 #+AUTHOR: Ann
9 #+OPTIONS: toc:nil num:nil
10 * Intro
11 Some intro.
12 * TODO [#A] Project :work:
13 SCHEDULED: <2026-10-10 Sat>
14 :PROPERTIES:
15 :EXPORT_FILE_NAME: project-notes
16 :EXPORT_AUTHOR: Bob
17 :END:
18 Body of project.
19 ** Step one
20 Do it.
21 *** Deep
22 deep text
23 * Other
24 """
25
26 static var project: Int { (file as NSString).range(of: "Body of").location }
27
28 /// As `org-export-as` with SUBTREEP in Emacs 31: the heading's title is the title, its
29 /// EXPORT_AUTHOR the author, in place of the file's; planning and drawer left out; children
30 /// one level up.
31 @Test func subtreeIsADocumentOfItsOwn() throws {
32 let text = try #require(ExportScope.subtree(Self.file, at: Self.project))
33 #expect(text == """
34 #+OPTIONS: toc:nil num:nil
35 #+TITLE: Project
36 #+AUTHOR: Bob
37 Body of project.
38 * Step one
39 Do it.
40 ** Deep
41 deep text
42
43 """)
44 let meta = HTMLExport.metadata(text)
45 #expect(meta.title == "Project" && meta.author == "Bob")
46 let body = HTMLExport.page(text, fallbackTitle: "t", bodyOnly: true)
47 #expect(!body.contains("<html") && !body.contains("class=\"title\"") && !body.contains("Intro"))
48 #expect(body.contains("<h2") && body.contains("Step one") && body.contains("<h3") && !body.contains("table-of-contents"))
49 #expect(ExportScope.subtree(Self.file, at: 0) == nil)
50 }
51
52 @Test func exportTitleOverridesTheHeading() throws {
53 let file = "* Proj\n:PROPERTIES:\n:EXPORT_TITLE: Custom\n:EXPORT_OPTIONS: toc:nil\n:END:\nText.\n"
54 let text = try #require(ExportScope.subtree(file, at: 0))
55 #expect(HTMLExport.metadata(text).title == "Custom")
56 #expect(text.hasSuffix("#+OPTIONS: toc:nil\nText.\n"))
57 }
58
59 /// `org-export-output-file-name`: relative to the file's folder, the extension replaced.
60 @Test func fileNames() {
61 let source = URL(fileURLWithPath: "/notes/t.org")
62 #expect(ExportScope.fileName(Self.file, subtreeAt: Self.project) == "project-notes")
63 #expect(ExportScope.fileName(Self.file, subtreeAt: 0) == nil)
64 #expect(ExportScope.fileName("#+EXPORT_FILE_NAME: out/x\n* A\n") == "out/x")
65 #expect(ExportScope.output(for: source, name: "out/x", extension: "html").path == "/notes/out/x.html")
66 #expect(ExportScope.output(for: source, name: "out/x.txt", extension: "html").path == "/notes/out/x.html")
67 #expect(ExportScope.output(for: source, name: "/tmp/abs", extension: "md").path == "/tmp/abs.md")
68 #expect(ExportScope.output(for: source, name: nil, extension: "md").path == "/notes/t.md")
69 }
70
71 /// The words `C-s` and `C-b` keep in ox-html and ox-md, for the whole file and a subtree.
72 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
73 func subtreeAndBodyOnlyKeepWhatOrgKeeps() throws {
74 let text = """
75 #+TITLE: wordtitle
76 #+AUTHOR: wordauthor
77 #+OPTIONS: toc:nil num:nil
78 wordintro
79 * wordone
80 SCHEDULED: <2026-10-10 Sat>
81 :PROPERTIES:
82 :EXPORT_AUTHOR: wordbob
83 :wordkey: wordvalue
84 :END:
85 wordfirst
86 ** wordsub
87 worddeep
88 * wordtwo
89 wordsecond
90 """ + "\n"
91 let offset = (text as NSString).range(of: "wordfirst").location
92 let point = EmacsOracle.point(offset, in: text)
93 let emacs = try EmacsOracle.evaluate(text, """
94 (progn
95 (require 'ox-html) (require 'ox-md)
96 (goto-char \(point))
97 (list (org-export-as 'html t nil nil) (org-export-as 'html nil nil t) (org-export-as 'html t nil t)
98 (org-export-as 'md t nil nil) (org-export-as 'md nil nil t) (org-export-as 'md t nil t)))
99 """)
100 let subtree = try #require(ExportScope.subtree(text, at: offset))
101 let ours = [
102 HTMLExport.page(subtree, fallbackTitle: "t"), HTMLExport.page(text, fallbackTitle: "t", bodyOnly: true),
103 HTMLExport.page(subtree, fallbackTitle: "t", bodyOnly: true),
104 MarkdownExport.document(subtree), MarkdownExport.document(text, bodyOnly: true), MarkdownExport.document(subtree, bodyOnly: true),
105 ]
106 let names = ["html subtree", "html body", "html subtree body", "md subtree", "md body", "md subtree body"]
107 let words = Set(text.matches(of: /\bword[a-z]+\b/).map { String($0.0) })
108 for (index, name) in names.enumerated() {
109 // ox-md writes no title; this export makes it the top heading.
110 for word in words where !(name == "md subtree" && word == "wordone") {
111 #expect(ours[index].contains(word) == emacs[index].contains(word), "\(name) \(word)\nours:\n\(ours[index])\nemacs:\n\(emacs[index])")
112 }
113 }
114 #expect(emacs[0].contains("wordone") && !emacs[2].contains("wordone") && !emacs[1].contains("wordtitle") && emacs[1].contains("wordsecond"))
115 }
116
117 @Test func markdownBodyOnlyHasNoTitle() {
118 let text = "#+TITLE: T\n* A\nx\n"
119 #expect(MarkdownExport.document(text).hasPrefix("# T"))
120 #expect(MarkdownExport.document(text, bodyOnly: true).hasPrefix("## A"))
121 }
122}