krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Tests/OrgCoreTests/NowebTests.swift

c1295824f55c7bba681fca8338bf08167e110105
orgstar/Tests/OrgCoreTests/NowebTests.swift history · blame · raw

65 lines · 2000 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct NowebTests {
 6    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 7
 8    static let text = """
 9        * Code
10        #+NAME: greet
11        #+begin_src sh
12        echo hello
13        echo world
14        #+end_src
15
16        #+begin_src sh :noweb-ref parts
17        part one
18        #+end_src
19
20        #+begin_src sh :noweb-ref parts :noweb-sep "\\n# --\\n"
21          part two
22        #+end_src
23
24        #+NAME: nested
25        #+begin_src sh :noweb yes
26        start
27        <<greet>>
28        #+end_src
29
30        #+begin_src sh :noweb yes
31        # <<greet>>
32        x <<nested>> y
33        <<parts>>
34        <<missing>> after
35        <<notes>>
36        #+end_src
37
38        #+begin_src sh :noweb yes :noweb-prefix no
39          # <<greet>>
40        #+end_src
41        * Notes
42        :PROPERTIES:
43        :ID: notes
44        :END:
45        Some text
46        here.
47        * COMMENT hidden
48        #+begin_src sh :noweb-ref parts
49        hidden part
50        #+end_src
51        """ + "\n"
52
53    @Test(.enabled(if: oracle))
54    func expandsLikeOrg() throws {
55        let tree = OrgParser.parse(Self.text)
56        let model = DocumentModel(tree: tree)
57        let ns = Self.text as NSString
58        let blocks = model.srcBlocks.filter { $0.body.contains("<<") }
59        let forms = blocks.map { "(progn (goto-char \(EmacsOracle.point($0.range.lowerBound, in: Self.text) + 2)) (org-babel-expand-noweb-references))" }
60        let emacs = try EmacsOracle.evaluate(Self.text, "(with-current-buffer (find-file-noselect \"oracle.org\") (list \(forms.joined(separator: " "))))")
61        let ours = try blocks.map { try Babel.expandNoweb($0, params: try Babel.Params(block: $0, model: model, text: ns), model: model, text: ns) }
62        #expect(ours == emacs)
63        #expect(ours.count == 3 && ours[1].contains("# echo world") && ours[1].contains("x echo world y\npart one\npart two") && ours[1].contains("Some text"))
64    }
65}