krz/orgstar

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

Tests/OrgPresentationTests/VisibilityPropertyTests.swift

main
orgstar/Tests/OrgPresentationTests/VisibilityPropertyTests.swift history · blame · raw

30 lines · 1592 bytes

2 symbols in this file
 1import Foundation
 2import Testing
 3import OrgCore
 4@testable import OrgPresentation
 5
 6struct VisibilityPropertyTests {
 7    static let text = "#+STARTUP: overview\n* A\n:PROPERTIES:\n:VISIBILITY: children\n:END:\nbody a\n** A1\na1 body\n*** A11\n** A2\n* B\n:PROPERTIES:\n:VISIBILITY: content\n:END:\nbody b\n** B1\nb1 body\n*** B11\nb11\n* C\n:PROPERTIES:\n:VISIBILITY: all\n:END:\nbody c\n** C1\nc1\n* D :ARCHIVE:\n** D1\n* E\n:PROPERTIES:\n:VISIBILITY: folded\n:END:\n** E1\n"
 8
 9    /// Line visibility after opening the file, as Emacs 31.1 / Org 9.8.7 shows it, except that
10    /// Emacs leaves B's `:PROPERTIES:` line (line 12) showing under `content`.
11    static let emacs = "111001100110000101011001111010000"
12
13    @Test func foldsByVisibilityProperties() {
14        let tree = OrgParser.parse(Self.text)
15        var state = Presentation.applyVisibilityProperties(Presentation.startupFoldState(tree)!, in: tree)
16        let wrappers = Presentation.startupWrappers(tree)
17        state.wrapperFolds = Set(wrappers.map(\.start))
18        let hidden = Presentation.hiddenRanges(tree, folds: state.folds, bodyFolds: state.bodyFolds, wrapperRanges: wrappers.map(\.hidden))
19        let ns = Self.text as NSString
20        var visibility = ""
21        var offset = 0
22        while offset < ns.length {
23            visibility += hidden.contains { $0.contains(offset) } ? "0" : "1"
24            offset = NSMaxRange(ns.lineRange(for: NSRange(location: offset, length: 0)))
25        }
26        var expected = Array(Self.emacs)
27        expected[11] = "0"
28        #expect(visibility == String(expected))
29    }
30}