Tests/OrgCoreTests/VimTests.swift
229 lines · 10380 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// Plays keys through `Vim` the way the editor does: printable keys in insert state type,
6/// actions apply to the text.
7struct VimDriver {
8 var vim = Vim()
9 var text: String
10 var caret: Int
11 var clipboard: String?
12 var commands: [String] = []
13
14 init(_ text: String, caret: Int) {
15 self.text = text
16 self.caret = caret
17 }
18
19 mutating func type(_ keys: [KeyChord]) {
20 for key in keys {
21 if vim.mode == .insert, vim.passesThrough(key) {
22 if key == KeyChord("DEL") {
23 if caret > 0 {
24 let ns = text as NSString
25 text = ns.replacingCharacters(in: NSRange(location: caret - 1, length: 1), with: "")
26 caret -= 1
27 vim.deletedBackward()
28 }
29 } else {
30 let s = key.key == "SPC" ? " " : key.key == "RET" ? "\n" : key.key == "TAB" ? "\t" : key.key
31 guard key.modifiers.isEmpty, s.count == 1 else { continue }
32 let ns = text as NSString
33 text = ns.replacingCharacters(in: NSRange(location: caret, length: 0), with: s)
34 caret += s.utf16.count
35 vim.inserted(s)
36 }
37 continue
38 }
39 let actions = vim.feed(key, Vim.Context(text: text, caret: caret, clipboard: clipboard))
40 for action in actions {
41 switch action {
42 case .edit(let edits, let to):
43 for edit in edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { text = edit.apply(to: text) }
44 caret = to
45 case .move(let to): caret = to
46 case .setClipboard(let s): clipboard = s
47 case .command(let id): commands.append(id)
48 default: break
49 }
50 }
51 }
52 }
53}
54
55struct VimTests {
56 static func keys(_ s: String) -> [KeyChord] { KeySequence.parse(s)! }
57
58 static let cases: [(String, Int, String)] = [
59 ("hello world foo\nsecond line here\n", 0, "d w"),
60 ("hello world foo\nsecond line here\n", 0, "j c i w X ESC"),
61 ("hello world foo\nsecond line here\n", 6, "d i w"),
62 ("hello world foo\nsecond line here\n", 6, "d a w"),
63 ("hello world foo\nsecond line here\n", 0, "2 d w"),
64 ("hello world foo\nsecond line here\n", 0, "d 2 w"),
65 ("hello world foo\nsecond line here\n", 12, "d w"),
66 ("hello world foo\nsecond line here\n", 4, "x"),
67 ("hello world foo\nsecond line here\n", 4, "3 x"),
68 ("hello world foo\nsecond line here\n", 4, "X"),
69 ("hello world foo\nsecond line here\n", 4, "D"),
70 ("hello world foo\nsecond line here\n", 4, "C n e w ESC"),
71 ("hello world foo\nsecond line here\n", 4, "d d"),
72 ("one\ntwo\nthree\nfour\n", 4, "2 d d"),
73 ("one\ntwo\nthree\nfour\n", 14, "d d"),
74 ("one\ntwo\nthree\nfour", 14, "d d"),
75 ("one\ntwo\nthree\n", 0, "y y p"),
76 ("one\ntwo\nthree\n", 0, "y y P"),
77 ("one\ntwo\nthree\n", 4, "y w $ p"),
78 ("one\ntwo\nthree\n", 0, "d d p"),
79 ("one two three\n", 0, "d w w P"),
80 ("one two three\n", 4, "Y $ p"),
81 ("one\ntwo\nthree\n", 0, "J"),
82 ("one\n two\nthree\n", 0, "3 J"),
83 ("one two\n", 0, "r X"),
84 ("one two\n", 0, "~ ~"),
85 ("Hello World\n", 0, "g U i w"),
86 ("Hello World\n", 0, "g u u"),
87 ("one two\n", 0, "o n e w ESC"),
88 ("one two\n", 4, "O n e w ESC"),
89 (" one two\n", 6, "I x ESC"),
90 ("one two\n", 2, "A ! ESC"),
91 ("one two\n", 2, "a X ESC"),
92 ("one two three four\n", 0, "d w ."),
93 ("one two three four\n", 0, "c w X ESC w ."),
94 ("one\ntwo\nthree\nfour\n", 0, "d d j ."),
95 ("a(b, c) d\n", 3, "d i ("),
96 ("a(b, c) d\n", 3, "d a ("),
97 ("say \"hi there\" now\n", 7, "c i \" X ESC"),
98 ("say \"hi there\" now\n", 7, "d a \""),
99 ("x = [1, 2, 3]\n", 6, "d i ["),
100 ("f {\n body\n}\n", 6, "d i {"),
101 ("one two three\n", 0, "v e d"),
102 ("one two three\n", 4, "v i w y $ p"),
103 ("one\ntwo\nthree\n", 4, "V d"),
104 ("one\ntwo\nthree\n", 0, "V j d"),
105 ("one\ntwo\nthree\n", 0, "V j y G p"),
106 ("one two three\n", 0, "f t x"),
107 ("one two three\n", 0, "t t x"),
108 ("one two three\n", 12, "F o x"),
109 ("one two three\n", 0, "f e ; x"),
110 ("one two three\n", 0, "d f t"),
111 ("one two three\n", 0, "d t t"),
112 ("one two three\n", 0, "$ x"),
113 ("one two three\n", 8, "0 x"),
114 (" one two\n", 8, "^ x"),
115 ("one\ntwo\nthree\n", 0, "G x"),
116 ("one\ntwo\nthree\n", 10, "g g x"),
117 ("one\ntwo\nthree\n", 0, "2 G x"),
118 ("one two three\n", 0, "e x"),
119 ("one two three\n", 0, "w w b x"),
120 ("one.two three\n", 0, "w x"),
121 ("one.two three\n", 0, "W x"),
122 ("one.two three\n", 12, "B x"),
123 ("one two three\n", 0, "d e"),
124 ("one two three\n", 8, "d b"),
125 ("para one\nstill\n\npara two\n", 0, "} x"),
126 ("para one\nstill\n\npara two\n", 0, "d }"),
127 ("para one\nstill\n\npara two\n", 0, "d a p"),
128 ("para one\nstill\n\npara two\n", 0, "y i p G p"),
129 ("(a (b) c)\n", 0, "% x"),
130 ("one two\nthree four\n", 0, "j l l l k x"),
131 ("long line here\nab\nlonger line\n", 10, "j j x"),
132 ("one two three\n", 0, "s t h x"),
133 ("one two three\n", 12, "S t w x"),
134 ("one two three\n", 0, "\" a y w w \" a P"),
135 ("one two three\n", 0, "\" _ d w"),
136 (" one\n", 2, "o x ESC"),
137 ("- item\n", 3, "o x ESC"),
138 ("- [ ] item\n", 3, "o x ESC"),
139 ("1. item\n", 3, "o x ESC"),
140 ("text\n- item\n", 8, "O x ESC"),
141 ("- item\n more\n", 9, "o x ESC"),
142 ("* Heading\n", 3, "o x ESC"),
143 ("one two three\n", 0, "v e ~"),
144 ("one two three\n", 0, "v e U"),
145 ("one\ntwo\nthree\n", 0, "V j >"),
146 ("one\ntwo\nthree\n", 0, "v j J"),
147 ("one two three\n", 0, "y i w w v e p"),
148 ("a (b c) d\n", 0, "c i ( X ESC"),
149 ("one two\n", 4, "d a w"),
150 ("one two\n", 4, "d i w"),
151 ("one two three four\n", 0, "d 2 a w"),
152 ("one two three\n", 4, "c $ X ESC"),
153 ("one two three\n", 8, "d 0"),
154 (" one two\n", 8, "d ^"),
155 ("one\ntwo\nthree\n", 4, "d G"),
156 ("one\ntwo\nthree\n", 4, "d g g"),
157 ("one two three\n", 4, "y $ P"),
158 ("ab\n", 0, "x p"),
159 ("one\ntwo\n", 0, "d d p"),
160 ("abc\n", 1, "5 x"),
161 ("abcdef\n", 1, "3 r x"),
162 ("one two three four\n", 0, "x . ."),
163 ("one two three four five\n", 0, "d w 2 ."),
164 ("one two\n", 3, "c w X ESC"),
165 ("aXbXcXd\n", 0, "2 f X x"),
166 ("one two three\n", 0, "w v $ d"),
167 ("one\ntwo\nthree\n", 0, "j V d p"),
168 ("x\n\n\ny\n", 0, "d }"),
169 ("one two\n", 0, "e a X ESC b ."),
170 ("one\n\nthree\n", 0, "j o x ESC"),
171 ("one two\n", 0, "g U U"),
172 ("a b\n", 0, "w i x ESC h ."),
173 ("one two three\n", 4, "c c X ESC"),
174 (" one two\n", 4, "c c X ESC"),
175 ("one two\n", 0, "> >"),
176 (" one two\n", 0, "< <"),
177 ("one two\n", 0, "i a b DEL c ESC"),
178 ("one two\n", 0, "i x C-w y ESC"),
179 ("one two\n", 3, "a X ESC . ."),
180 ("one\ntwo\n", 0, "A ; ESC j ."),
181 ("one two three\n", 0, "3 l v l d"),
182 ("one two three\n", 8, "v b d"),
183 ("one two three\n", 0, "v 2 e c X ESC"),
184 ]
185
186 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
187 func matchesEvil() throws {
188 let evil = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".config/emacs/.local/straight/repos")
189 guard FileManager.default.fileExists(atPath: evil.appendingPathComponent("evil/evil.el").path) else { return }
190 var forms: [EmacsOracle.Case] = []
191 for (text, point, keys) in Self.cases {
192 forms.append(EmacsOracle.Case(text: text, point: point + 1, form: """
193 (progn
194 (setq evil-want-Y-yank-to-eol t evil-undo-system 'undo-redo evil-want-C-w-delete t evil-want-C-u-delete t evil-shift-width 8)
195 (add-to-list 'load-path "\(evil.path)/evil")
196 (add-to-list 'load-path "\(evil.path)/goto-chg")
197 (add-to-list 'load-path "\(evil.path)/evil-snipe")
198 (require 'evil)
199 (require 'evil-snipe)
200 (setq evil-snipe-smart-case t evil-snipe-scope 'line)
201 (setq kill-ring nil)
202 (switch-to-buffer (current-buffer))
203 (evil-local-mode 1)
204 (evil-snipe-local-mode 1)
205 (evil-normal-state)
206 (execute-kbd-macro (kbd "\(keys.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"").replacingOccurrences(of: "ESC", with: "<escape>").replacingOccurrences(of: "DEL", with: "DEL"))"))
207 (let ((p (point)) (s evil-state))
208 (goto-char (point-max))
209 (insert (format "\\n@@STATE %s" s))
210 (goto-char p)))
211 """))
212 }
213 let results = try EmacsOracle.run(forms)
214 for ((text, point, keys), result) in zip(Self.cases, results) {
215 var driver = VimDriver(text, caret: point)
216 driver.type(Self.keys(keys))
217 guard let marker = result.text.range(of: "\n@@STATE ") else {
218 Issue.record("\(keys): \(result.error)")
219 continue
220 }
221 let emacsText = String(result.text[..<marker.lowerBound])
222 let state = String(result.text[marker.upperBound...])
223 let ours = driver.vim.mode == .insert ? "insert" : driver.vim.mode == .normal ? "normal" : "visual"
224 let emacsCaret = EmacsOracle.offset(result.point, in: result.text)
225 #expect(driver.text == emacsText && driver.caret == emacsCaret && ours == state,
226 "\(keys.debugDescription) on \(text.debugDescription) at \(point)\nours: \(driver.text.debugDescription) @\(driver.caret) \(ours)\nevil: \(emacsText.debugDescription) @\(emacsCaret) \(state) \(result.error)")
227 }
228 }
229}