Tests/OrgAppTests/ExportSettingsTests.swift

97 lines · 5329 bytes

8 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgApp
 4import OrgCore
 5
 6struct ExportSettingsTests {
 7    static func defaults() -> UserDefaults { TestDefaults("orgstar-export") }
 8
 9    static let file = "#+TITLE: Whole\n#+OPTIONS: toc:nil\nIntro.\n* One\nFirst.\n** Sub\nSub body.\n* Two\nSecond.\n"
10    static var caret: Int { (file as NSString).range(of: "First.").location }
11
12    /// What the Mac's export writes for a format, scope and body-only choice.
13    static func export(_ format: String, subtree: Bool, bodyOnly: Bool) throws -> String {
14        let text = subtree ? try #require(ExportScope.subtree(file, at: caret)) : file
15        switch format {
16        case "markdown": return MarkdownExport.document(text, bodyOnly: bodyOnly)
17        case "latex": return try LaTeXExport.document(text, bodyOnly: bodyOnly, author: "A", file: true)
18        case "text": return try ASCIIExport.document(text, bodyOnly: bodyOnly, author: "A", file: true)
19        default: return HTMLExport.page(text, fallbackTitle: "t", bodyOnly: bodyOnly)
20        }
21    }
22
23    @Test func settingsAreKeptPerFile() {
24        let defaults = Self.defaults()
25        #expect(ExportSettings.saved(for: "/notes/a.org", defaults: defaults) == ExportSettings())
26        var a = ExportSettings()
27        (a.format, a.subtree, a.bodyOnly, a.openAfter) = ("markdown", true, true, true)
28        a.save(for: "/notes/a.org", defaults: defaults)
29        var b = ExportSettings()
30        b.format = "latex"
31        b.save(for: "/notes/b.org", defaults: defaults)
32        #expect(ExportSettings.saved(for: "/notes/a.org", defaults: defaults) == a)
33        #expect(ExportSettings.saved(for: "/notes/b.org", defaults: defaults) == b)
34        #expect(ExportSettings.saved(for: "/notes/c.org", defaults: defaults) == ExportSettings())
35    }
36
37    /// A file without settings starts with the format and open-after choice last exported
38    /// with, and exports the whole file as a whole document.
39    @Test func aFileWithoutSettingsGetsTheLastUsedFormat() {
40        let defaults = Self.defaults()
41        var a = ExportSettings()
42        (a.format, a.subtree, a.bodyOnly, a.openAfter) = ("markdown", true, true, true)
43        a.save(for: "/notes/a.org", lastUsed: true, defaults: defaults)
44        var fresh = ExportSettings()
45        (fresh.format, fresh.openAfter) = ("markdown", true)
46        #expect(ExportSettings.saved(for: "/notes/c.org", defaults: defaults) == fresh)
47        var b = ExportSettings()
48        b.format = "odt"
49        b.save(for: "/notes/b.org", defaults: defaults)
50        #expect(ExportSettings.saved(for: "/notes/c.org", defaults: defaults) == fresh)
51    }
52
53    /// A single-format command exports what the dialog does with the same scope and body-only
54    /// choice.
55    @Test(arguments: ["html", "html-open", "markdown", "latex", "text"])
56    func commandsFollowTheFilesSettings(_ format: String) throws {
57        let defaults = Self.defaults()
58        let whole = try Self.export(format, subtree: false, bodyOnly: false)
59        let fresh = try #require(ExportSettings.saved(for: "/notes/a.org", defaults: defaults).commandOptions(format))
60        #expect(try Self.export(format, subtree: fresh.subtree, bodyOnly: fresh.bodyOnly) == whole)
61        for (subtree, bodyOnly) in [(true, false), (false, true), (true, true)] {
62            var settings = ExportSettings()
63            (settings.subtree, settings.bodyOnly) = (subtree, bodyOnly)
64            settings.save(for: "/notes/a.org", defaults: defaults)
65            let options = try #require(ExportSettings.saved(for: "/notes/a.org", defaults: defaults).commandOptions(format))
66            let dialog = try Self.export(format, subtree: subtree, bodyOnly: bodyOnly)
67            #expect(try Self.export(format, subtree: options.subtree, bodyOnly: options.bodyOnly) == dialog)
68            #expect(dialog != whole)
69            #expect(dialog.contains("Second.") == !subtree)
70        }
71    }
72
73    @Test func theMessageSaysWhatWasExported() {
74        #expect(ExportSettings.exported("a.html", subtree: false, bodyOnly: false) == "Exported to a.html")
75        #expect(ExportSettings.exported("a.html", subtree: true, bodyOnly: false) == "Exported the subtree to a.html")
76        #expect(ExportSettings.exported("a.html", subtree: false, bodyOnly: true) == "Exported the body to a.html")
77        #expect(ExportSettings.exported("a.html", subtree: true, bodyOnly: true) == "Exported the subtree's body to a.html")
78    }
79
80    /// Body only is refused for the formats the dialog disables it for; the agenda files'
81    /// calendars follow neither choice.
82    @Test func bodyOnlyIsRefusedForWholeDocumentFormats() throws {
83        var settings = ExportSettings()
84        (settings.subtree, settings.bodyOnly) = (true, true)
85        for format in ["pdf", "print-pdf", "odt", "docx", "rtf", "plain-text", "icalendar"] {
86            #expect(!ExportSettings.allowsBodyOnly(format))
87            #expect(settings.commandOptions(format) == nil)
88        }
89        for format in ["icalendar-agenda", "icalendar-combined"] {
90            let options = try #require(settings.commandOptions(format))
91            #expect(options == (false, false))
92        }
93        settings.bodyOnly = false
94        let options = try #require(settings.commandOptions("print-pdf"))
95        #expect(options == (true, false))
96    }
97}