Tests/OrgAppTests/EditorPaneTests.swift
87 lines · 3148 bytes
7 symbols in this file
1import Foundation
2import OrgCore
3import OrgDocument
4import Testing
5@testable import OrgApp
6
7@MainActor
8struct EditorPaneTests {
9 func session(_ notes: Folder) -> DocumentSession {
10 DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
11 }
12
13 @Test func aFileOpenInTwoPanesIsOneBuffer() throws {
14 let notes = try Folder()
15 let a = try notes.write("a.org", "* A\n").path
16 let session = session(notes)
17 let first = session.focused
18 let second = session.makePane()
19 session.open(a)
20 session.open(a, in: second)
21 #expect(session.openPaths == [a])
22 #expect(first.path == a && second.path == a)
23 #expect(session.panes.count == 2)
24 }
25
26 @Test func killingABufferEmptiesEveryPaneShowingIt() throws {
27 let notes = try Folder()
28 let a = try notes.write("a.org", "* A\n").path
29 let b = try notes.write("b.org", "* B\n").path
30 let session = session(notes)
31 let first = session.focused
32 let second = session.makePane()
33 session.open(a)
34 session.open(b)
35 session.open(b, in: second)
36 session.kill(b)
37 #expect(first.path == a)
38 #expect(second.path == a)
39 #expect(first.shown == [a] && second.shown == [a])
40 session.kill(a)
41 #expect(first.path == nil && second.path == nil && session.path == nil)
42 }
43
44 @Test func revealGoesToOnePane() throws {
45 let notes = try Folder()
46 let a = try notes.write("a.org", "* A\n* B\n").path
47 let session = session(notes)
48 let first = session.focused
49 let second = session.makePane()
50 session.open(a)
51 session.open(a, in: second)
52 session.reveal(4, in: second)
53 #expect(second.revealRequest?.offset == 4)
54 #expect(first.revealRequest == nil)
55 session.focused = second
56 #expect(session.revealRequest?.offset == 4)
57 }
58
59 @Test func dirtyPathsCountABufferOnce() throws {
60 let notes = try Folder()
61 let a = try notes.write("a.org", "* A\n").path
62 let session = session(notes)
63 session.saveMode = .explicit
64 let second = session.makePane()
65 session.open(a)
66 session.open(a, in: second)
67 let result = EditResult(baseRevision: session.document!.revision, edits: [TextEdit(range: 0..<0, replacement: "x")])
68 _ = session.perform(PrecomputedEdit(result), path: a, at: 0, expect: nil)
69 #expect(session.dirtyPaths == [a])
70 #expect(session.focused.isDirty && second.isDirty)
71 }
72
73 @Test func closingAPaneKeepsTheBufferAndTheFocus() throws {
74 let notes = try Folder()
75 let a = try notes.write("a.org", "* A\n").path
76 let session = session(notes)
77 let first = session.focused
78 let second = session.makePane()
79 session.open(a, in: second)
80 session.focused = second
81 session.closePane(second)
82 #expect(session.panes.count == 1 && session.focused === first)
83 #expect(session.isOpen(a))
84 session.closePane(first)
85 #expect(session.panes.count == 1)
86 }
87}