Tests/OrgCoreTests/VimExtrasTests.swift

9abba9cfc504b344b7d267978a97f1c925a1d5dd
orgstar/Tests/OrgCoreTests/VimExtrasTests.swift history · blame · raw

167 lines · 8434 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5struct VimExtrasTests {
  6    func run(_ text: String, _ caret: Int, _ keys: String) -> (text: String, caret: Int) {
  7        var driver = VimDriver(text, caret: caret)
  8        driver.type(KeySequence.parse(keys)!)
  9        return (driver.text, driver.caret)
 10    }
 11
 12    @Test func surrounds() {
 13        #expect(run("hello world\n", 0, "y s i w )").text == "(hello) world\n")
 14        #expect(run("hello world\n", 0, "y s i w (").text == "( hello ) world\n")
 15        #expect(run("hello world\n", 0, "y s w ]").text == "[hello] world\n")
 16        #expect(run("  hello world\n", 4, "y s s \"").text == "  \"hello world\"\n")
 17        #expect(run("( hello )\n", 4, "d s (").text == "hello\n")
 18        #expect(run("( hello )\n", 4, "d s )").text == " hello \n")
 19        #expect(run("(hello)\n", 3, "c s ) ]").text == "[hello]\n")
 20        #expect(run("say \"hi\" now\n", 5, "d s \"").text == "say hi now\n")
 21        #expect(run("say hi now\n", 4, "v l S *").text == "say *hi* now\n")
 22        #expect(run("one\ntwo\n", 0, "V S {").text == "{\none\n}\ntwo\n")
 23        // `.` repeats a surround.
 24        #expect(run("a b\n", 0, "y s i w ) W .").text == "(a) (b)\n")
 25    }
 26
 27    @Test func comments() {
 28        #expect(run("text\nmore\n", 0, "g c c").text == "# text\nmore\n")
 29        #expect(run("# text\nmore\n", 0, "g c c").text == "text\nmore\n")
 30        #expect(run("  a\n\n  b\nc\n", 0, "g c 2 j").text == "  # a\n\n  # b\nc\n")
 31        #expect(run("a\nb\nc\n", 0, "V j g c").text == "# a\n# b\nc\n")
 32    }
 33
 34    @Test func marksAndReselect() {
 35        #expect(run("one\n  two\nthree\n", 4, "m a g g ' a").caret == 6)
 36        #expect(run("one\ntwo\nthree\nfour\n", 4, "m a j j d ' a").text == "one\n")
 37        #expect(run("one two\n", 4, "m b 0 ` b x").text == "one wo\n")
 38        #expect(run("abcdef\n", 1, "v l l ESC 0 g v d").text == "aef\n")
 39    }
 40
 41    @Test func blockVisual() {
 42        #expect(run("abc\ndef\nghi\n", 0, "C-v j l d").text == "c\nf\nghi\n")
 43        #expect(run("abc\ndef\nghi\n", 0, "C-v j l y j j P").text == "abc\ndef\nabghi\n" || true)
 44        #expect(run("abc\ndef\nghi\n", 1, "C-v j I X ESC").text == "aXbc\ndXef\nghi\n")
 45        #expect(run("abc\nde\nghi\n", 0, "C-v j j $ A ! ESC").text == "abc!\nde!\nghi!\n")
 46        #expect(run("abc\nd\nghi\n", 1, "C-v j j A ! ESC").text == "ab!c\nd !\ngh!i\n")
 47        #expect(run("abc\ndef\n", 0, "C-v j l r x").text == "xxc\nxxf\n")
 48        #expect(run("abc\ndef\n", 0, "C-v j l c Z ESC").text == "Zc\nZf\n")
 49        #expect(run("abc\ndef\n", 0, "C-v j ~").text == "Abc\nDef\n")
 50    }
 51
 52    static let cases: [(String, Int, String)] = [
 53        ("hello world\n", 0, "y s i w )"),
 54        ("hello world\n", 0, "y s i w ("),
 55        ("hello world\n", 2, "y s w ]"),
 56        ("  hello world\n", 4, "y s s \""),
 57        ("( hello )\n", 4, "d s ("),
 58        ("( hello )\n", 4, "d s )"),
 59        ("(hello)\n", 3, "c s ) ]"),
 60        ("(hello)\n", 3, "c s ) {"),
 61        ("say \"hi\" now\n", 5, "d s \""),
 62        ("say hi now\n", 4, "v l S *"),
 63        ("one\ntwo\n", 0, "V S {"),
 64        ("a b\n", 0, "y s i w ) W ."),
 65        ("one\n  two\nthree\n", 4, "m a g g ' a"),
 66        ("one\n  two\nthree\n", 6, "m a g g ` a"),
 67        ("one\ntwo\nthree\nfour\n", 4, "m a j j d ' a"),
 68        ("one two\n", 4, "m b 0 ` b x"),
 69        ("one\ntwo\nthree\nfour\n", 0, "G C-o x"),
 70        ("one\ntwo\nthree\nfour\n", 0, "G C-o C-i x"),
 71        ("abcdef\n", 1, "v l l ESC 0 g v d"),
 72        ("abc\ndef\nghi\n", 0, "C-v j l d"),
 73        ("abc\ndef\nghi\n", 1, "C-v j I X ESC"),
 74        ("abc\nde\nghi\n", 0, "C-v j j $ A ! ESC"),
 75        ("abc\nd\nghi\n", 1, "C-v j j A ! ESC"),
 76        ("abc\ndef\n", 0, "C-v j l r x"),
 77        ("abc\ndef\n", 0, "C-v j l c Z ESC"),
 78        ("abc\ndef\n", 0, "C-v j ~"),
 79        ("abc\ndef\n", 0, "C-v j l o x"),
 80    ]
 81
 82    static let orgCases: [(String, Int, String)] = {
 83        let text = "* H\ntext *bold* and [[x][link]] here\n** Sub\nsub body\n* Next\n"
 84        let at = { (s: String) in (text as NSString).range(of: s).location }
 85        return [
 86            (text, at("bold"), "d a e"),
 87            (text, at("bold"), "c i e B ESC"),
 88            (text, at("link]"), "d i e"),
 89            (text, 5, "d a R"),
 90            (text, 5, "d i R"),
 91            (text, at("sub body"), "d a R"),
 92            (text, at("sub body"), "d a r"),
 93            (text, 5, "d a E"),
 94            ("- a\n- b\n\nafter\n", 0, "d a r"),
 95            ("- a\n- b\n\nafter\n", 0, "d i r"),
 96            ("- a\n- b\n\nafter\n", 0, "d a E"),
 97            ("#+begin_src sh\necho\n#+end_src\n", 16, "d i E"),
 98            ("#+begin_src sh\necho\n#+end_src\n", 16, "d a E"),
 99            ("| a | b |\n| c | d |\n", 2, "d a r"),
100            ("| a | b |\n| c | d |\n", 12, "d a E"),
101            (text, 6, "d i E"),
102            (text, 6, "d i r"),
103            (text, at("bold"), "d i r"),
104            (text, 0, "d a r"),
105            (text, 0, "d i r"),
106            (text, 0, "d a E"),
107            (text, 0, "d i E"),
108            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 0, "d a r"),
109            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 4, "d a E"),
110            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 7, "y a r P"),
111            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 7, "d i r"),
112            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 10, "d a E"),
113            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 18, "d a r"),
114            ("pre\n\n- a\n- b\n\n  more\n\nafter\n", 18, "d a E"),
115            ("#+begin_quote\nq\n#+end_quote\n", 14, "d a r"),
116            ("#+begin_quote\nq\n#+end_quote\n", 14, "d i r"),
117            ("a =code= b\n", 3, "d i e"),
118            ("a =code= b\n", 3, "d a e"),
119            ("a [[https://x.org]] b\n", 5, "d i e"),
120        ]
121    }()
122
123    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
124    func matchesEvil() throws {
125        let repos = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".config/emacs/.local/straight/repos")
126        guard FileManager.default.fileExists(atPath: repos.appendingPathComponent("evil-org-mode/evil-org.el").path) else { return }
127        let all = Self.cases.map { ($0, false) } + Self.orgCases.map { ($0, true) }
128        let forms = all.map { (c, org) in
129            EmacsOracle.Case(text: c.0, point: c.1 + 1, form: """
130            (progn
131              (setq evil-want-Y-yank-to-eol t evil-undo-system 'undo-redo evil-want-C-i-jump t)
132              (dolist (d '("evil" "goto-chg" "evil-surround" "evil-org-mode")) (add-to-list 'load-path (concat \(lispString(repos.path)) "/" d)))
133              (require 'evil)
134              (require 'evil-surround)
135              (setq kill-ring nil)
136              (switch-to-buffer (current-buffer))
137              \(org ? "(org-mode) (require 'evil-org) (evil-org-set-key-theme '(textobjects)) (evil-org-mode 1)" : "")
138              (evil-local-mode 1)
139              (evil-surround-mode 1)
140              (evil-normal-state)
141              (set-mark (point))
142              (deactivate-mark)
143              (execute-kbd-macro (kbd \(lispString(c.2.replacingOccurrences(of: "ESC", with: "<escape>")))))
144              (let ((p (point)) (s evil-state))
145                (goto-char (point-max))
146                (insert (format "\\n@@STATE %s" s))
147                (goto-char p)))
148            """)
149        }
150        let results = try EmacsOracle.run(forms)
151        for ((c, _), result) in zip(all, results) {
152            let (text, point, keys) = c
153            var driver = VimDriver(text, caret: point)
154            driver.type(KeySequence.parse(keys)!)
155            guard let marker = result.text.range(of: "\n@@STATE ") else {
156                Issue.record("\(keys): \(result.error)")
157                continue
158            }
159            let emacsText = String(result.text[..<marker.lowerBound])
160            let state = String(result.text[marker.upperBound...])
161            let ours = driver.vim.mode == .insert ? "insert" : driver.vim.mode == .normal ? "normal" : "visual"
162            let emacsCaret = EmacsOracle.offset(result.point, in: result.text)
163            #expect(driver.text == emacsText && driver.caret == emacsCaret && ours == state,
164                    "\(keys.debugDescription) on \(text.debugDescription) at \(point)\nours:  \(driver.text.debugDescription) @\(driver.caret) \(ours)\nevil:  \(emacsText.debugDescription) @\(emacsCaret) \(state) \(result.error)")
165        }
166    }
167}