Tests/OrgEditorAppKitTests/ViewToggleTests.swift
60 lines · 2845 bytes
1import AppKit
2import OrgCore
3import Testing
4@testable import OrgEditorAppKit
5
6@MainActor
7struct ViewToggleTests {
8 /// A plain click at the middle of the character at `offset`.
9 func click(_ h: Harness, _ offset: Int) {
10 let screen = h.textView.firstRect(forCharacterRange: NSRange(location: offset, length: 1), actualRange: nil)
11 let inWindow = h.window.convertPoint(fromScreen: NSPoint(x: screen.midX, y: screen.midY))
12 let event = NSEvent.mouseEvent(with: .leftMouseDown, location: inWindow, modifierFlags: [], timestamp: 0,
13 windowNumber: h.window.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: 1)!
14 h.textView.mouseDown(with: event)
15 }
16
17 @Test func clickingStarsCyclesAndCheckboxesToggle() {
18 let h = Harness("* Top\nbody\n- [ ] task\n* Next\n")
19 click(h, 0)
20 #expect(h.visibleLines == ["* Top\n", "* Next\n"])
21 click(h, 0)
22 #expect(h.visibleLines.count == 4)
23 click(h, h.offset(of: "[ ]") + 1)
24 #expect(h.editor.document.text == "* Top\nbody\n- [X] task\n* Next\n")
25 // Elsewhere a click is the text view's.
26 #expect(!h.editor.click(at: h.offset(of: "Top") + 1))
27 #expect(!h.editor.click(at: h.offset(of: "task") + 1))
28 }
29
30 @Test func truncatingAndSpellChecking() {
31 let h = Harness("* Hedding\n~codez~ wurds\n")
32 #expect(!h.editor.truncatesLines)
33 h.editor.run("editor.toggle-truncate-lines")
34 #expect(h.editor.truncatesLines && h.textView.enclosingScrollView?.hasHorizontalScroller == true)
35 h.editor.run("editor.toggle-truncate-lines")
36 #expect(!h.editor.truncatesLines && h.textView.textContainer?.widthTracksTextView == true)
37 h.editor.run("editor.toggle-spell-check")
38 #expect(h.textView.isContinuousSpellCheckingEnabled)
39 let code = NSRange(location: h.offset(of: "codez"), length: 5)
40 let prose = NSRange(location: h.offset(of: "wurds"), length: 5)
41 #expect(h.editor.textView(h.textView, shouldSetSpellingState: 1, range: code) == 0)
42 #expect(h.editor.textView(h.textView, shouldSetSpellingState: 1, range: prose) == 1)
43 }
44
45 @Test func gjMovesByScreenLines() {
46 let long = Array(repeating: "word", count: 60).joined(separator: " ")
47 let h = Harness(long + "\nnext\n")
48 // Doom binds `g j` to the next heading; without that binding it's the engine's.
49 h.editor.keymap = KeymapPreset.emacs.keymap
50 h.editor.modal = true
51 h.caret(at: 0)
52 h.caret(at: 0)
53 ModalTests().type(h, "g j")
54 #expect(h.caret > 0 && h.caret < long.utf16.count, "\(h.caret)")
55 ModalTests().type(h, "g k")
56 #expect(h.caret == 0)
57 ModalTests().type(h, "j")
58 #expect(h.caret == long.utf16.count + 1)
59 }
60}