Tests/OrgCoreTests/ExportScopeTests.swift

design-themes-widgets
orgstar/Tests/OrgCoreTests/ExportScopeTests.swift history · blame · raw

124 lines · 5782 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.fileName(OrgParser.parse(Self.file), subtreeAt: Self.project) == "project-notes")
 66        #expect(ExportScope.fileName(OrgParser.parse("#+EXPORT_FILE_NAME: out/x\n* A\n")) == "out/x")
 67        #expect(ExportScope.output(for: source, name: "out/x", extension: "html").path == "/notes/out/x.html")
 68        #expect(ExportScope.output(for: source, name: "out/x.txt", extension: "html").path == "/notes/out/x.html")
 69        #expect(ExportScope.output(for: source, name: "/tmp/abs", extension: "md").path == "/tmp/abs.md")
 70        #expect(ExportScope.output(for: source, name: nil, extension: "md").path == "/notes/t.md")
 71    }
 72
 73    /// The words `C-s` and `C-b` keep in ox-html and ox-md, for the whole file and a subtree.
 74    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 75    func subtreeAndBodyOnlyKeepWhatOrgKeeps() throws {
 76        let text = """
 77            #+TITLE: wordtitle
 78            #+AUTHOR: wordauthor
 79            #+OPTIONS: toc:nil num:nil
 80            wordintro
 81            * wordone
 82            SCHEDULED: <2026-10-10 Sat>
 83            :PROPERTIES:
 84            :EXPORT_AUTHOR: wordbob
 85            :wordkey: wordvalue
 86            :END:
 87            wordfirst
 88            ** wordsub
 89            worddeep
 90            * wordtwo
 91            wordsecond
 92            """ + "\n"
 93        let offset = (text as NSString).range(of: "wordfirst").location
 94        let point = EmacsOracle.point(offset, in: text)
 95        let emacs = try EmacsOracle.evaluate(text, """
 96            (progn
 97              (require 'ox-html) (require 'ox-md)
 98              (goto-char \(point))
 99              (list (org-export-as 'html t nil nil) (org-export-as 'html nil nil t) (org-export-as 'html t nil t)
100                    (org-export-as 'md t nil nil) (org-export-as 'md nil nil t) (org-export-as 'md t nil t)))
101            """)
102        let subtree = try #require(ExportScope.subtree(text, at: offset))
103        let ours = [
104            HTMLExport.page(subtree, fallbackTitle: "t"), HTMLExport.page(text, fallbackTitle: "t", bodyOnly: true),
105            HTMLExport.page(subtree, fallbackTitle: "t", bodyOnly: true),
106            MarkdownExport.document(subtree), MarkdownExport.document(text, bodyOnly: true), MarkdownExport.document(subtree, bodyOnly: true),
107        ]
108        let names = ["html subtree", "html body", "html subtree body", "md subtree", "md body", "md subtree body"]
109        let words = Set(text.matches(of: /\bword[a-z]+\b/).map { String($0.0) })
110        for (index, name) in names.enumerated() {
111            // ox-md writes no title; this export makes it the top heading.
112            for word in words where !(name == "md subtree" && word == "wordone") {
113                #expect(ours[index].contains(word) == emacs[index].contains(word), "\(name) \(word)\nours:\n\(ours[index])\nemacs:\n\(emacs[index])")
114            }
115        }
116        #expect(emacs[0].contains("wordone") && !emacs[2].contains("wordone") && !emacs[1].contains("wordtitle") && emacs[1].contains("wordsecond"))
117    }
118
119    @Test func markdownBodyOnlyHasNoTitle() {
120        let text = "#+TITLE: T\n* A\nx\n"
121        #expect(MarkdownExport.document(text).hasPrefix("# T"))
122        #expect(MarkdownExport.document(text, bodyOnly: true).hasPrefix("## A"))
123    }
124}