Tests/OrgCoreTests/ExportScopeTests.swift
76 lines · 3172 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 @Test func markdownBodyOnlyHasNoTitle() {
72 let text = "#+TITLE: T\n* A\nx\n"
73 #expect(MarkdownExport.document(text).hasPrefix("# T"))
74 #expect(MarkdownExport.document(text, bodyOnly: true).hasPrefix("## A"))
75 }
76}