Tests/OrgEditorAppKitTests/MultiViewTests.swift

d6defe0dc0a9a2097c50011a25c810644b5ffaef
orgstar/Tests/OrgEditorAppKitTests/MultiViewTests.swift

92 lines · 3718 bytes

 1import AppKit
 2import OrgCore
 3import OrgDocument
 4import OrgPresentation
 5import Testing
 6@testable import OrgEditorAppKit
 7
 8/// One editor, several text views on its storage: text, undo, folds and narrowing are the
 9/// editor's; the caret and the focus are each view's.
10@MainActor
11struct MultiViewTests {
12    @MainActor struct Two {
13        let editor: OrgEditor
14        let second: NSTextView
15        let windows: [NSWindow]
16
17        init(_ text: String) {
18            _ = NSApplication.shared
19            let editor = OrgEditor(document: DocumentState(bytes: Array(text.utf8)))
20            let second = editor.addView()
21            let windows = [editor.textView, second].map { view in
22                let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 800), styleMask: [.titled], backing: .buffered, defer: false)
23                window.contentView = editor.makeScrollView(for: view)
24                return window
25            }
26            windows[0].makeFirstResponder(editor.textView)
27            editor.finishStyling()
28            for view in editor.textViews { view.textLayoutManager!.ensureLayout(for: view.textLayoutManager!.documentRange) }
29            self.editor = editor
30            self.second = second
31            self.windows = windows
32        }
33
34        func lines(_ view: NSTextView) -> Int {
35            let layout = view.textLayoutManager!
36            layout.ensureLayout(for: layout.documentRange)
37            var count = 0
38            layout.enumerateTextLayoutFragments(from: layout.documentRange.location, options: [.ensuresLayout]) { _ in count += 1; return true }
39            return count
40        }
41    }
42
43    @Test func typingInOneViewShowsInTheOtherAndInTheDocument() {
44        let two = Two("* a\nbody\n")
45        two.editor.textView.insertText("X", replacementRange: NSRange(location: 0, length: 0))
46        #expect(two.second.string == "X* a\nbody\n")
47        #expect(two.editor.document.text == "X* a\nbody\n")
48    }
49
50    @Test func undoIsShared() {
51        let two = Two("* a\nbody\n")
52        two.editor.textView.insertText("X", replacementRange: NSRange(location: 0, length: 0))
53        two.second.undoManager?.undo()
54        #expect(two.second.string == "* a\nbody\n")
55        #expect(two.editor.document.text == "* a\nbody\n")
56    }
57
58    @Test func foldsAreTheEditors() {
59        let two = Two("* a\nbody\n* b\n")
60        let before = two.lines(two.second)
61        two.editor.setCaret(0)
62        _ = two.editor.cycleAtCaret()
63        #expect(two.lines(two.second) < before)
64        #expect(two.lines(two.editor.textView) == two.lines(two.second))
65    }
66
67    @Test func commandsActOnTheFocusedView() {
68        let two = Two("* a\nbody\n* b\nmore\n")
69        two.windows[1].makeFirstResponder(two.second)
70        #expect(two.editor.textView === two.second)
71        two.second.setSelectedRange(NSRange(location: 9, length: 0))
72        _ = two.editor.cycleAtCaret()
73        #expect(two.editor.foldState.folds == [9])
74    }
75
76    @Test func aDiskChangeMapsEveryViewsCaret() {
77        let two = Two("* a\nbody\n")
78        two.second.setSelectedRange(NSRange(location: 5, length: 0))
79        _ = two.editor.diskChanged(to: Array("XY* a\nbody\n".utf8))
80        #expect(two.second.selectedRange() == NSRange(location: 7, length: 0))
81        #expect(two.second.string == "XY* a\nbody\n")
82    }
83
84    @Test func removingAViewDetachesItsLayoutManager() {
85        let two = Two("* a\n")
86        let storage = two.editor.textView.textLayoutManager!.textContentManager!
87        #expect(storage.textLayoutManagers.count == 2)
88        two.editor.removeView(two.second)
89        #expect(storage.textLayoutManagers.count == 1)
90        #expect(two.editor.textViews.count == 1)
91    }
92}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form