|
95 | 95 | (with-syntax-table table
|
96 | 96 | (split-string str "\\b" t)))))
|
97 | 97 |
|
| 98 | +(defun lispy-simulate-key (key) |
| 99 | + "Simulate key press KEY. |
| 100 | +This is used rather than `execute-kbd-macro' because apparently |
| 101 | +that function somehow fails to run within `ert-deftest'." |
| 102 | + (should (numberp key)) |
| 103 | + (let ((cmd (keymap-lookup nil (key-description (vector key))))) |
| 104 | + (setq last-command-event key) |
| 105 | + (call-interactively cmd) |
| 106 | + (setq last-command cmd))) |
| 107 | + |
| 108 | +(defun lispy-simulate-keys (keys) |
| 109 | + "Simulate a sequence of KEYS. |
| 110 | +See `lispy-simulate-key'." |
| 111 | + (seq-do #'lispy-simulate-key keys)) |
| 112 | + |
| 113 | +(cl-defun lispy-simulate-expect |
| 114 | + (keys &key buffer point (mode #'lisp-mode)) |
| 115 | + "Simulate key sequence KEYS and check the result. |
| 116 | +If KEYS is a sequence of sequence, simulate each element of KEYS |
| 117 | +instead. |
| 118 | + |
| 119 | +MODE is the major mode in effect. |
| 120 | + |
| 121 | +BUFFER, if non-nil, is the buffer string to match after the keys |
| 122 | +are simulated. |
| 123 | + |
| 124 | +POINT, if non-nil, is the point to match after the keys are |
| 125 | +pressed." |
| 126 | + (declare (indent 1)) |
| 127 | + (if (seqp (seq-first keys)) |
| 128 | + (seq-do (lambda (keys) |
| 129 | + (lispy-simulate-expect keys |
| 130 | + :buffer buffer |
| 131 | + :point point |
| 132 | + :mode mode)) |
| 133 | + keys) |
| 134 | + (with-temp-buffer |
| 135 | + (funcall mode) |
| 136 | + (lispy-mode) |
| 137 | + (lispy-simulate-keys keys) |
| 138 | + (when buffer |
| 139 | + (should (thread-last (buffer-substring-no-properties |
| 140 | + (point-min) (point-max)) |
| 141 | + (string= buffer)))) |
| 142 | + (let ((point (cl-case point |
| 143 | + (max (point-max)) |
| 144 | + (min (point-min)) |
| 145 | + (t point)))) |
| 146 | + (when point |
| 147 | + (should (= (point) point))))))) |
| 148 | + |
| 149 | +(ert-deftest lispy-read-unsafe-chars () |
| 150 | + "See #648." |
| 151 | + ;; Expect: (de|) |
| 152 | + ;; Recipe: ( d e |
| 153 | + (lispy-simulate-expect '(?\( ?d ?e) |
| 154 | + :buffer "(de)" |
| 155 | + :point 4) |
| 156 | + ;; Expect: (.)| |
| 157 | + ;; Recipes: |
| 158 | + ;; 1: ( . ) i |
| 159 | + ;; 2. ( . SPC C-b C-t ) i |
| 160 | + ;; 3. ( . SPC ) i |
| 161 | + ;; 4. ( . SPC C-b C-t SPC ) i |
| 162 | + (lispy-simulate-expect |
| 163 | + '((?\( ?. ?\) ?i) ; format "(.)" |
| 164 | + (?\( ?. ? ?\C-b ?\C-t ?\) ?i) ; format "( .)" |
| 165 | + (?\( ?. ? ?\) ?i) ; format "(. )" |
| 166 | + (?\( ?. ? ?\C-b ?\C-t ? ?\) ?i)) ; format "( . )" |
| 167 | + :buffer "(.)" |
| 168 | + :point 'max) |
| 169 | + ;; Expect: (f .)| |
| 170 | + ;; Recipes: |
| 171 | + ;; 1. ( f SPC . ) i |
| 172 | + ;; 2. ( f SPC . SPC ) i |
| 173 | + (lispy-simulate-expect |
| 174 | + '((?\( ?f ? ?. ?\) ?i) ; format "(f .)" |
| 175 | + (?\( ?f ? ?. ? ?\) ?i)) ; format "(f . )" |
| 176 | + :buffer "(f .)" |
| 177 | + :point 'max) |
| 178 | + ;; Expect: (. f)| |
| 179 | + ;; Recipe: ( . SPC f ) i |
| 180 | + (lispy-simulate-expect '(?\( ?. ? ?f ?\) ?i) |
| 181 | + :buffer "(. f)" |
| 182 | + :point 'max)) |
| 183 | + |
98 | 184 | (ert-deftest lispy-decode-keysequence ()
|
99 | 185 | (should (equal (lispy-decode-keysequence "23ab50c")
|
100 | 186 | '(23 "a" "b" 50 "c")))
|
@@ -2388,7 +2474,7 @@ Insert KEY if there's no command."
|
2388 | 2474 | (should (string= (lispy-with "|;;* Intro" "a")
|
2389 | 2475 | ";;* Intro\n;;* |")))
|
2390 | 2476 |
|
2391 |
| -(ert-deftest lispy-outline-add () |
| 2477 | +(ert-deftest lispy-outline-add-2 () ; FIXME: duplicate name |
2392 | 2478 | (should (string= (lispy-with "(quote ~foo|)" "~")
|
2393 | 2479 | "(quote ~~foo|)"))
|
2394 | 2480 | (should (string= (lispy-with "(quote ~~foo|)" "~")
|
@@ -2594,7 +2680,7 @@ Insert KEY if there's no command."
|
2594 | 2680 | (execute-kbd-macro (kbd "aa")))
|
2595 | 2681 | "(progn (setq type 'norwegian-blue)\n (~setq| plumage-type 'lovely))"))))
|
2596 | 2682 |
|
2597 |
| -(ert-deftest lispy-ace-subword () |
| 2683 | +(ert-deftest lispy-ace-subword-2 () ; FIXME: duplicate name |
2598 | 2684 | (should (string= (lispy-with "|(progn (setq type 'norwegian-blue)\n (setq plumage-type 'lovely))"
|
2599 | 2685 | (execute-kbd-macro (kbd "-g")))
|
2600 | 2686 | "(progn (setq type 'norwegian-blue)\n (setq |plumage~-type 'lovely))"))
|
|
0 commit comments