Tests/OrgCoreTests/ExportScopeTests.swift

d85099bc95bf1766aee990d94671dacda0c0686e
orgstar/Tests/OrgCoreTests/ExportScopeTests.swift history · blame · raw

77 lines · 3198 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; planning and drawer left out; children one level up.
30    @Test func subtreeIsADocumentOfItsOwn() throws {
31        let text = try #require(ExportScope.subtree(Self.file, at: Self.project))
32        #expect(text == """
33            #+TITLE: Whole file
34            #+AUTHOR: Ann
35            #+OPTIONS: toc:nil num:nil
36            #+TITLE: Project
37            #+AUTHOR: Bob
38            Body of project.
39            * Step one
40            Do it.
41            ** Deep
42            deep text
43
44            """)
45        let meta = HTMLExport.metadata(text)
46        #expect(meta.title == "Project" && meta.author == "Bob")
47        let body = HTMLExport.page(text, fallbackTitle: "t", bodyOnly: true)
48        #expect(!body.contains("<html") && !body.contains("class=\"title\"") && !body.contains("Intro"))
49        #expect(body.contains("<h2") && body.contains("Step one") && body.contains("<h3") && !body.contains("table-of-contents"))
50        #expect(ExportScope.subtree(Self.file, at: 0) == nil)
51    }
52
53    @Test func exportTitleOverridesTheHeading() throws {
54        let file = "* Proj\n:PROPERTIES:\n:EXPORT_TITLE: Custom\n:EXPORT_OPTIONS: toc:nil\n:END:\nText.\n"
55        let text = try #require(ExportScope.subtree(file, at: 0))
56        #expect(HTMLExport.metadata(text).title == "Custom")
57        #expect(text.hasSuffix("#+OPTIONS: toc:nil\nText.\n"))
58    }
59
60    /// `org-export-output-file-name`: relative to the file's folder, the extension replaced.
61    @Test func fileNames() {
62        let source = URL(fileURLWithPath: "/notes/t.org")
63        #expect(ExportScope.fileName(Self.file, subtreeAt: Self.project) == "project-notes")
64        #expect(ExportScope.fileName(Self.file, subtreeAt: 0) == nil)
65        #expect(ExportScope.fileName("#+EXPORT_FILE_NAME: out/x\n* A\n") == "out/x")
66        #expect(ExportScope.output(for: source, name: "out/x", extension: "html").path == "/notes/out/x.html")
67        #expect(ExportScope.output(for: source, name: "out/x.txt", extension: "html").path == "/notes/out/x.html")
68        #expect(ExportScope.output(for: source, name: "/tmp/abs", extension: "md").path == "/tmp/abs.md")
69        #expect(ExportScope.output(for: source, name: nil, extension: "md").path == "/notes/t.md")
70    }
71
72    @Test func markdownBodyOnlyHasNoTitle() {
73        let text = "#+TITLE: T\n* A\nx\n"
74        #expect(MarkdownExport.document(text).hasPrefix("# T"))
75        #expect(MarkdownExport.document(text, bodyOnly: true).hasPrefix("## A"))
76    }
77}