import Foundation import Testing @testable import OrgCore struct ExportOptionsTests { static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil @Test func readsOptions() { let o = ExportOptions.parse("#+OPTIONS: toc:2 num:nil tags:not-in-toc\n#+OPTIONS: todo:nil ^:{} pri:t H:4\n#+EXCLUDE_TAGS: private\n#+HTML_HEAD: \n") #expect(o.toc == 2 && o.num == nil && o.tags && !o.tagsInTOC && !o.todo && o.scripts == .braces && o.priority && o.headlineLevels == 4) #expect(o.excludeTags == ["private"] && o.htmlHead == [""]) #expect(ExportOptions.parse("* x\n").toc == 3) #expect(ExportOptions.parse("#+OPTIONS: H:2 toc:t\n").toc == 2) } /// ox-latex's keywords: the last class and class options, header lines in order, the first /// compiler. @Test func readsLaTeXKeywords() { let o = ExportOptions.parse(""" #+LATEX_CLASS: article #+LATEX_CLASS: report #+LATEX_CLASS_OPTIONS: [a4paper] #+LATEX_HEADER: \\usepackage{a} #+LATEX_HEADER_EXTRA: \\usepackage{c} #+LATEX_HEADER: \\usepackage{b} #+LATEX_COMPILER: xelatex #+LATEX_COMPILER: lualatex #+OPTIONS: toc:nil toc:2 \\n:t """) #expect(o.latexClass == "report" && o.latexClassOptions == "[a4paper]") #expect(o.latexHeader == ["\\usepackage{a}", "\\usepackage{b}"] && o.latexHeaderExtra == ["\\usepackage{c}"]) #expect(o.latexCompiler == "xelatex") #expect(o.items["toc"] == "2" && o.items["\\n"] == "t") #expect(ExportOptions.parse("x\n").latexClass == nil) } @Test func pagesFollowOptions() { let org = """ #+TITLE: Doc #+OPTIONS: ^:{} pri:t #+HTML_HEAD: * TODO [#A] One :work: x_1 y_{2} \\(a^2\\) $b$ and $5 ** Two * Secret :noexport: hidden * COMMENT Draft gone * Old :ARCHIVE: archived body """ let page = HTMLExport.page(org, fallbackTitle: "f") #expect(page.contains("
a
") && selected.contains("b
") && selected.contains("c
")) } @Test(.enabled(if: oracle)) func expandsPropertyMacroLikeOrg() throws { let text = """ #+PROPERTY: color filecolor Top: [{{{property(color)}}}] * One :PROPERTIES: :color: red :Size: big :CUSTOM_ID: one :END: In one: [{{{property(color)}}}] [{{{property(SIZE)}}}] [{{{property(missing)}}}] [{{{property(ITEM)}}}] ** Child Child: [{{{property(color)}}}] * Two Two: [{{{property(color,*One)}}}] [{{{property(color,#one)}}}] [{{{property(Size, One)}}}] """ + "\n" let emacs = try EmacsOracle.evaluate(text, """ (progn (require 'ox) (org-macro-initialize-templates) (org-macro-replace-all org-macro-templates) (list (buffer-substring-no-properties (point-min) (point-max)))) """) let ours = ExportPreprocess.macros(text, path: nil) #expect(ours.contains("[red] [big] [] [One]") && ours == emacs.first, "ours:\n\(ours)\nemacs:\n\(emacs.first ?? "")") } @Test(.enabled(if: oracle)) func expandsIncludesAndMacrosLikeOrg() throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-include-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } try "* Part\nbody\n** Sub\nsub body\n* Other\n:PROPERTIES:\n:CUSTOM_ID: oth\n:END:\nother\n".write(to: folder.appendingPathComponent("part.org"), atomically: true, encoding: .utf8) try "line 1\nline 2\n* not a heading\nline 4\nline 5\n".write(to: folder.appendingPathComponent("code.sh"), atomically: true, encoding: .utf8) let text = """ #+TITLE: The Doc #+AUTHOR: Me #+MACRO: greet Hello, $1 and $2! Intro {{{title}}} by {{{author}}}. #+INCLUDE: "part.org" #+INCLUDE: "part.org::*Sub" :minlevel 1 #+INCLUDE: "part.org::#oth" #+INCLUDE: "code.sh" src sh :lines "2-4" #+INCLUDE: "code.sh" example {{{greet(you\\, me,them)}}} {{{n}}} {{{n}}} {{{n(x)}}} {{{keyword(AUTHOR)}}} {{{input-file}}} """ + "\n" let path = folder.appendingPathComponent("main.org").path try text.write(toFile: path, atomically: true, encoding: .utf8) let emacs = try EmacsOracle.evaluate(text, """ (with-current-buffer (find-file-noselect \(lispString(path))) (require 'ox) (org-export-expand-include-keyword) (org-macro-initialize-templates) (org-macro-replace-all org-macro-templates) (list (buffer-substring-no-properties (point-min) (point-max)))) """) let ours = ExportPreprocess.macros(ExportPreprocess.includes(text, directory: folder), path: path) #expect(ours == emacs.first, "ours:\n\(ours)\nemacs:\n\(emacs.first ?? "")") } @Test func unreadableIncludesAreListed() throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-includes-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } try "#+INCLUDE: \"gone.org\"\ntext\n".write(to: folder.appendingPathComponent("part.org"), atomically: true, encoding: .utf8) let text = """ #+INCLUDE: "part.org::*Sub" :minlevel 1 #+INCLUDE: "missing.org" src sh #+INCLUDE: "missing.org" """ #expect(ExportPreprocess.unreadableIncludes(text, directory: folder) == [ folder.appendingPathComponent("gone.org").path, folder.appendingPathComponent("missing.org").path, ]) #expect(ExportPreprocess.unreadableIncludes("no includes", directory: folder).isEmpty) } }