Tests/OrgCoreTests/VimEvilTests.swift
197 lines · 11080 bytes
12 symbols in this file
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// Evil's `gi`, `g;`, `g,`, `gJ`, `&`, the numbered and read-only registers, sentences, tags
6/// and scrolling by lines and columns.
7struct VimEvilTests {
8 static let sentences = "One two. Three four! Five\nsix seven? Eight.\n\n\nNine ten. Eleven\n"
9 static let lines = "One two.\nThree four. Five\n\nSix. Seven\n"
10 static let tags = "<a><b>x y</b> z</a>\n<p class=\"k\">\nline\n</p>\n<br/> tail\n"
11 static let joined = "a b\n c d\ne\n\nf\n"
12 static let words = "alpha beta\ngamma delta\nepsilon zeta\neta theta\n"
13
14 static let cases: [(String, Int, String)] = {
15 var cases: [(String, Int, String)] = []
16 for point in [0, 4, 9, 12, 22, 30, 38, 45, 46, 47, 58] {
17 cases.append((sentences, point, ")"))
18 if point > 0 { cases.append((sentences, point, "(")) }
19 }
20 cases += [(sentences, 0, "2 )"), (sentences, 0, "5 )"), (sentences, 58, "3 ("), (sentences, 4, "v ) d"), (sentences, 30, "y ( P")]
21 for point in [0, 8, 9, 20, 21, 22, 45, 47, 58] {
22 cases.append((sentences, point, "d i s"))
23 cases.append((sentences, point, "d a s"))
24 }
25 for point in [0, 9, 21, 26, 27, 32] {
26 cases.append((lines, point, "d i s"))
27 cases.append((lines, point, "d a s"))
28 }
29 cases += [
30 (sentences, 0, "d 2 a s"), (sentences, 0, "d 2 i s"), (sentences, 0, "d 3 i s"), (sentences, 12, "c a s X ESC"),
31 (sentences, 12, "v i s d"), (sentences, 12, "y i s P"),
32 (lines, 0, "d )"), (lines, 12, "d ("), (lines, 12, "d )"), (lines, 21, "d )"), (lines, 27, "d ("), (lines, 32, "d )"),
33 ]
34 for point in [0, 3, 6, 8, 10, 14, 18, 20, 34, 37, 46] {
35 cases.append((tags, point, "d i t"))
36 cases.append((tags, point, "d a t"))
37 }
38 cases += [(tags, 7, "d 2 i t"), (tags, 7, "d 2 a t"), (tags, 7, "c i t X ESC"), (tags, 7, "v a t d"), (tags, 7, "y i t P")]
39 cases += ["g J", "3 g J", "V j g J", "j g J", "j j j g J", "g J .", "v j j g J"].map { (joined, 1, $0) }
40 cases += [
41 "g i X ESC", "A X ESC g g g i Y ESC", "w i X Y ESC j j g i Z ESC", "A X ESC g g O n e w ESC j j g i Y ESC",
42 "A X ESC g g g i Y ESC j .",
43 "x j x j x g ;", "x j x j x g ; g ;", "x j x j x g ; g ; g ,", "x j x j x g ; g ; g ; g ;", "A a b c ESC g g g ;",
44 "x w x w x j g ; g ;", "A 1 2 3 4 5 6 7 8 9 0 ESC j 0 x g g g ; g ;", "j d d j x g g g ; g ;",
45 "y w j d w j d d g g \" 1 P", "y w j d w j d d g g \" 2 P", "y w j d w j d d g g \" 3 P", "y w j d w j d d g g \" 0 P",
46 "y w j d w j d d g g \" - P", "d w j d d g g \" - P", "x j x j d w g g \" 1 P", "x j x j d w g g \" 3 p",
47 "\" a y w j d w g g \" 1 P", "\" a y w j d w g g \" 0 P", "v e d j \" - P", "y y j \" 1 p",
48 "A f o o ESC g g \" . P", "c w n e w ESC j 0 \" . P",
49 ].map { (words, 0, $0) }
50 return cases
51 }()
52
53 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
54 func matchesEvil() throws {
55 let repos = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".config/emacs/.local/straight/repos")
56 guard FileManager.default.fileExists(atPath: repos.appendingPathComponent("goto-chg/goto-chg.el").path) else { return }
57 let forms = Self.cases.map { c in
58 EmacsOracle.Case(text: c.0, point: c.1 + 1, form: """
59 (progn
60 (setq evil-want-Y-yank-to-eol t evil-undo-system 'undo-redo sentence-end-double-space nil)
61 (dolist (d '("evil" "goto-chg")) (add-to-list 'load-path (concat \(lispString(repos.path)) "/" d)))
62 (require 'evil)
63 (require 'goto-chg)
64 (setq kill-ring nil register-alist nil evil-last-small-deletion nil evil-last-insertion nil)
65 (buffer-enable-undo)
66 (setq buffer-undo-list nil)
67 (switch-to-buffer (current-buffer))
68 (evil-local-mode 1)
69 (evil-normal-state)
70 (ignore-errors (execute-kbd-macro (kbd \(lispString(c.2.replacingOccurrences(of: "ESC", with: "<escape>"))))))
71 (let ((p (point)) (s evil-state))
72 (goto-char (point-max))
73 (insert (format "\\n@@STATE %s" s))
74 (goto-char p)))
75 """)
76 }
77 let results = try EmacsOracle.run(forms)
78 for (c, result) in zip(Self.cases, results) {
79 let (text, point, keys) = c
80 var driver = VimDriver(text, caret: point)
81 driver.type(KeySequence.parse(keys)!)
82 guard let marker = result.text.range(of: "\n@@STATE ") else {
83 Issue.record("\(keys): \(result.error)")
84 continue
85 }
86 let emacsText = String(result.text[..<marker.lowerBound])
87 let state = String(result.text[marker.upperBound...])
88 let ours = driver.vim.mode == .insert ? "insert" : driver.vim.mode == .normal ? "normal" : "visual"
89 let emacsCaret = EmacsOracle.offset(result.point, in: result.text)
90 #expect(driver.text == emacsText && driver.caret == emacsCaret && ours == state,
91 "\(keys.debugDescription) on \(text.debugDescription) at \(point)\nours: \(driver.text.debugDescription) @\(driver.caret) \(ours)\nevil: \(emacsText.debugDescription) @\(emacsCaret) \(state) \(result.error)")
92 }
93 }
94
95 private func run(_ text: String, at caret: Int = 0, _ keys: String) -> VimDriver {
96 var driver = VimDriver(text, caret: caret)
97 driver.type(KeySequence.parse(keys)!)
98 return driver
99 }
100
101 @Test func resumesInsertAtTheLastInsertion() {
102 #expect(run(Self.words, "A X ESC g g g i Y ESC").text.hasPrefix("alpha betaXY\n"))
103 // The mark moves with later edits.
104 #expect(run(Self.words, "A X ESC 0 x j j g i Y ESC").text.hasPrefix("lpha betaXY\n"))
105 #expect(run(Self.words, "g i X ESC").text.hasPrefix("Xalpha"))
106 }
107
108 @Test func goesThroughTheChangeList() {
109 #expect(run(Self.words, "x j x j x g g g ;").caret == 21)
110 #expect(run(Self.words, "x j x j x g g g ; g ;").caret == 10)
111 #expect(run(Self.words, "x j x j x g g g ; g ; g ,").caret == 21)
112 // A change within eight characters of a later one is passed over.
113 #expect(run(Self.words, "x w x G g ;").caret == 5)
114 #expect(run(Self.words, "x w x G g ; g ;").caret == 5)
115 #expect(run(Self.words, "3 j g ;").caret == 36)
116 // Another command starts over from the latest change.
117 #expect(run(Self.words, "x j x j x g ; g ; j g ;").caret == 21)
118 }
119
120 @Test func joinsWithoutChangingBlanks() {
121 #expect(run(Self.joined, at: 1, "g J").text == "a b c d\ne\n\nf\n")
122 #expect(run(Self.joined, at: 1, "3 g J").text == "a b c de\n\nf\n")
123 #expect(run(Self.joined, at: 1, "V j j g J").text == "a b c de\n\nf\n")
124 #expect(run(Self.joined, at: 1, "g J").caret == 3)
125 }
126
127 @Test func repeatsTheLastSubstitution() {
128 #expect(run(Self.words, "&").actions == [.ex("&")])
129 #expect(run(Self.words, "g &").actions == [.ex("%&&")])
130 }
131
132 @Test func numberedRegistersHoldEarlierKills() {
133 let keys = "y w j d w j d d g g "
134 #expect(run(Self.words, keys + "\" 1 P").text.hasPrefix("epsilon zeta\nalpha beta\n"))
135 #expect(run(Self.words, keys + "\" 2 P").text.hasPrefix("gamma alpha beta\n"))
136 #expect(run(Self.words, keys + "\" 3 P").text.hasPrefix("alpha alpha beta\n"))
137 #expect(run(Self.words, keys + "\" 0 P").text.hasPrefix("alpha alpha beta\n"))
138 #expect(run(Self.words, keys + "\" - P").text.hasPrefix("gamma alpha beta\n"))
139 var vim = Vim()
140 let context = Vim.Context(text: "abcdefghijkl\n", caret: 0)
141 for _ in 0..<11 { _ = vim.feed(KeyChord("x"), context) }
142 #expect(vim.registers["9"]?.text == "a" && vim.registers["1"]?.text == "a" && vim.registers.keys.filter(\.isNumber).count == 9)
143 }
144
145 @Test func readOnlyRegisters() {
146 #expect(run(Self.words, "A f o o ESC 0 \" . P").text.hasPrefix("fooalpha betafoo\n"))
147 var driver = VimDriver(Self.words, caret: 0)
148 driver.fileName = "/notes/todo.org"
149 driver.lastEx = "s/a/b"
150 driver.type(KeySequence.parse("\" % P")!)
151 #expect(driver.text.hasPrefix("/notes/todo.orgalpha"))
152 driver.type(KeySequence.parse("j 0 \" : P")!)
153 #expect(driver.text.contains("\ns/a/bgamma"))
154 _ = driver.vim.search("zeta", forward: true, Vim.Context(text: driver.text, caret: 0))
155 driver.type(KeySequence.parse("2 j 0 \" / P")!)
156 #expect(driver.text.contains("\nzetaeta theta"))
157 // Without a file, a command or a search there is nothing to put.
158 #expect(run(Self.words, "\" % P \" : P \" / P \" . P").text == Self.words)
159 }
160
161 @Test func sentenceMotionsAndObjects() {
162 #expect(run(Self.sentences, ")").caret == 9)
163 #expect(run(Self.sentences, at: 38, ")").caret == 45)
164 #expect(run(Self.sentences, at: 47, "(").caret == 46)
165 #expect(run(Self.sentences, at: 12, "d i s").text.hasPrefix("One two. Five\n"))
166 #expect(run(Self.sentences, at: 12, "d a s").text.hasPrefix("One two. Five\n"))
167 #expect(run(Self.lines, at: 21, "d a s").text.hasPrefix("One two.\nThree four.\n\n"))
168 #expect(run(Self.lines, "d a s").text.hasPrefix("\nThree four."))
169 #expect(run(Self.sentences, "d 2 a s").text.hasPrefix("Five\n"))
170 // In Org, a heading and a list item begin sentences of their own.
171 var driver = VimDriver("* Head\nBody one. Two\n- item\n", caret: 0)
172 driver.org = true
173 driver.type(KeySequence.parse(")")!)
174 #expect(driver.caret == 7)
175 driver.type(KeySequence.parse(") )")!)
176 #expect(driver.caret == 21)
177 }
178
179 @Test func tagObjects() {
180 #expect(run(Self.tags, at: 7, "d i t").text.hasPrefix("<a><b></b> z</a>\n"))
181 #expect(run(Self.tags, at: 7, "d a t").text.hasPrefix("<a> z</a>\n"))
182 #expect(run(Self.tags, at: 7, "d 2 i t").text.hasPrefix("<a></a>\n"))
183 #expect(run(Self.tags, at: 35, "d i t").text.contains("<p class=\"k\"></p>"))
184 #expect(run(Self.tags, at: 46, "d a t").text == Self.tags)
185 // A pair that fills its lines goes with them.
186 #expect(run(Self.tags, at: 35, "d a t").text == "<a><b>x y</b> z</a>\n<br/> tail\n")
187 }
188
189 @Test func scrollsByLinesAndColumns() {
190 #expect(run(Self.words, "C-e").actions == [.scrollLines(1)])
191 #expect(run(Self.words, "3 C-y").actions == [.scrollLines(-3)])
192 #expect(run(Self.words, "z l").actions == [.scrollColumns(1, halfWidths: false)])
193 #expect(run(Self.words, "4 z h").actions == [.scrollColumns(-4, halfWidths: false)])
194 #expect(run(Self.words, "z L").actions == [.scrollColumns(1, halfWidths: true)])
195 #expect(run(Self.words, "z H").actions == [.scrollColumns(-1, halfWidths: true)])
196 }
197}