forked from emacs-evil/evil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-tests.el
8325 lines (8034 loc) · 269 KB
/
evil-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; evil-tests.el --- unit tests for Evil -*- coding: utf-8 -*-
;; Author: Vegard Øye <vegard_oye at hotmail.com>
;; Maintainer: Vegard Øye <vegard_oye at hotmail.com>
;; Version: 1.2.13
;;
;; This file is NOT part of GNU Emacs.
;;; License:
;; This file is part of Evil.
;;
;; Evil is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Evil is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Evil. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file is for developers. It runs some tests on Evil.
;; To load it, run the Makefile target "make test" or add
;; the following lines to .emacs:
;;
;; (setq evil-tests-run nil) ; set to t to run tests immediately
;; (global-set-key [f12] 'evil-tests-run) ; hotkey
;; (require 'evil-tests)
;;
;; Loading this file enables profiling on Evil. The current numbers
;; can be displayed with `elp-results'. The Makefile target
;; "make profiler" shows profiling results in the terminal on the
;; basis of running all tests.
;;
;; To write a test, use `ert-deftest' and specify a :tags value of at
;; least '(evil). The test may inspect the output of functions given
;; certain input, or it may execute a key sequence in a temporary
;; buffer and investigate the results. For the latter approach, the
;; macro `evil-test-buffer' creates a temporary buffer in Normal
;; state. String descriptors initialize and match the contents of
;; the buffer:
;;
;; (ert-deftest evil-test ()
;; :tags '(evil)
;; (evil-test-buffer
;; "[T]his creates a test buffer." ; cursor on "T"
;; ("w") ; key sequence
;; "This [c]reates a test buffer."))) ; cursor moved to "c"
;;
;; The initial state, the cursor syntax, etc., can be changed
;; with keyword arguments. See the documentation string of
;; `evil-test-buffer' for more details.
;;
;; This file is NOT part of Evil itself.
(require 'elp)
(require 'ert)
(require 'evil)
(require 'evil-test-helpers)
;;; Code:
(defvar evil-tests-run nil
"*Run Evil tests.")
(defvar evil-tests-profiler nil
"*Profile Evil tests.")
(defun evil-tests-initialize (&optional tests profiler interactive)
(setq profiler (or profiler evil-tests-profiler))
(when (listp profiler)
(setq profiler (car profiler)))
(when profiler
(setq evil-tests-profiler t)
(setq profiler
(or (cdr (assq profiler
'((call . elp-sort-by-call-count)
(average . elp-sort-by-average-time)
(total . elp-sort-by-total-time))))))
(setq elp-sort-by-function (or profiler 'elp-sort-by-call-count))
(elp-instrument-package "evil"))
(if interactive
(if (y-or-n-p-with-timeout "Run tests? " 2 t)
(evil-tests-run tests interactive)
(message "You can run the tests at any time \
with `M-x evil-tests-run'"))
(evil-tests-run tests)))
(defun evil-tests-run (&optional tests interactive)
"Run Evil tests."
(interactive '(nil t))
(let ((elp-use-standard-output (not interactive)))
(setq tests
(or (null tests)
`(or ,@(mapcar #'(lambda (test)
(or (null test)
(and (memq test '(evil t)) t)
`(or (tag ,test)
,(format "^%s$" test))))
tests))))
(cond
(interactive
(ert-run-tests-interactively tests)
(when evil-tests-profiler
(elp-results)))
(evil-tests-profiler
(ert-run-tests-batch tests)
(elp-results))
(t
(ert-run-tests-batch-and-exit tests)))))
(defun evil-tests-profiler (&optional force)
"Profile Evil tests."
(when (or evil-tests-profiler force)
(setq evil-tests-profiler t)
(elp-instrument-package "evil")))
;;; States
(defun evil-test-local-mode-enabled ()
"Verify that `evil-local-mode' is enabled properly"
(ert-info ("Set the mode variable to t")
(should (eq evil-local-mode t)))
(ert-info ("Refresh `emulation-mode-map-alist'")
(should (memq 'evil-mode-map-alist emulation-mode-map-alists)))
(ert-info ("Create a buffer-local value for `evil-mode-map-alist'")
(should (assq 'evil-mode-map-alist (buffer-local-variables))))
(ert-info ("Initialize buffer-local keymaps")
(should (assq 'evil-normal-state-local-map (buffer-local-variables)))
(should (keymapp evil-normal-state-local-map))
(should (assq 'evil-emacs-state-local-map (buffer-local-variables)))
(should (keymapp evil-emacs-state-local-map)))
(ert-info ("Don't add buffer-local entries to the default value")
(should-not (rassq evil-normal-state-local-map
(default-value 'evil-mode-map-alist)))
(should-not (rassq evil-emacs-state-local-map
(default-value 'evil-mode-map-alist)))))
(defun evil-test-local-mode-disabled ()
"Verify that `evil-local-mode' is disabled properly"
(ert-info ("Set the mode variable to nil")
(should-not evil-local-mode))
(ert-info ("Disable all states")
(evil-test-no-states)))
(defun evil-test-no-states ()
"Verify that all states are disabled"
(ert-info ("Set `evil-state' to nil")
(should-not evil-state))
(ert-info ("Disable all state keymaps")
(dolist (state (mapcar #'car evil-state-properties) t)
(should-not (evil-state-property state :mode t))
(should-not (memq (evil-state-property state :keymap t)
(current-active-maps)))
(should-not (evil-state-property state :local t))
(should-not (memq (evil-state-property state :local-keymap t)
(current-active-maps)))
(dolist (map (evil-state-auxiliary-keymaps state))
(should-not (memq map (current-active-maps)))))))
(ert-deftest evil-test-toggle-local-mode ()
"Toggle `evil-local-mode'"
:tags '(evil state)
(with-temp-buffer
(ert-info ("Enable `evil-local-mode'")
(evil-local-mode 1)
(evil-test-local-mode-enabled))
(ert-info ("Disable `evil-local-mode'")
(evil-local-mode -1)
(evil-test-local-mode-disabled))))
(defun evil-test-change-state (state)
"Change state to STATE and check keymaps"
(let (mode keymap local-mode local-keymap tag)
(evil-change-state state)
(setq mode (evil-state-property state :mode)
keymap (evil-state-property state :keymap t)
local-mode (evil-state-property state :local)
local-keymap (evil-state-property state :local-keymap t)
tag (evil-state-property state :tag t))
(ert-info ("Update `evil-state'")
(should (eq evil-state state)))
(ert-info ("Ensure `evil-local-mode' is enabled")
(evil-test-local-mode-enabled))
(ert-info ("Enable state modes")
(should (symbol-value mode))
(should (symbol-value local-mode)))
(ert-info ("Push state keymaps to the top")
(evil-test-state-keymaps state))
(ert-info ("Refresh mode line tag")
(should (equal evil-mode-line-tag tag)))))
(defun evil-test-state-keymaps (state)
"Verify that STATE's keymaps are pushed to the top"
(let ((actual (evil-state-keymaps state))
(expected `((,(evil-state-property state :local)
. , (evil-state-property state :local-keymap t))
(,(evil-state-property state :mode)
. ,(evil-state-property state :keymap t)))))
;; additional keymaps inherited with :enable
(cond
((eq state 'operator)
(setq expected
`((evil-operator-shortcut-mode
. ,evil-operator-shortcut-map)
(evil-operator-state-local-minor-mode
. ,evil-operator-state-local-map)
(evil-operator-state-minor-mode
. ,evil-operator-state-map)
(evil-motion-state-local-minor-mode
. ,evil-motion-state-local-map)
(evil-motion-state-minor-mode
. ,evil-motion-state-map)
(evil-normal-state-local-minor-mode
. ,evil-normal-state-local-map)
(evil-normal-state-minor-mode
. ,evil-normal-state-map)))))
(let ((actual (butlast actual (- (length actual)
(length expected)))))
(should (equal actual expected))
(dolist (map actual)
(setq map (cdr-safe map))
(should (keymapp map))))))
(ert-deftest evil-test-exit-normal-state ()
"Enter Normal state and then disable all states"
:tags '(evil state)
(with-temp-buffer
(evil-test-change-state 'normal)
(evil-normal-state -1)
(evil-test-no-states)))
(ert-deftest evil-test-change-states ()
"Change between Normal state, Emacs state and Operator-Pending state"
:tags '(evil state)
(with-temp-buffer
(evil-test-change-state 'normal)
(evil-test-change-state 'emacs)
(evil-test-change-state 'normal)
(evil-test-change-state 'operator)
(evil-test-change-state 'normal)
(evil-test-change-state 'emacs)
(evil-test-change-state 'replace)
(evil-test-change-state 'normal)))
(ert-deftest evil-test-change-to-previous-state ()
"Change to some state and back."
:tags '(evil state)
(with-temp-buffer
(evil-test-change-state 'normal)
(evil-test-change-state 'visual)
(evil-test-change-state 'emacs)
(evil-change-to-previous-state)
(should (eq evil-state 'visual))
(evil-change-to-previous-state)
(should (eq evil-state 'normal))))
(ert-deftest evil-test-enter-normal-state-disabled ()
"Enter Normal state even if `evil-local-mode' is disabled"
:tags '(evil state)
(with-temp-buffer
(evil-local-mode -1)
(evil-test-local-mode-disabled)
(evil-test-change-state 'normal)))
(ert-deftest evil-test-execute-in-normal-state ()
"Test `evil-execute-in-normal-state'."
:tags '(evil)
(ert-info ("Execute normal state command in insert state")
(evil-test-buffer
"[a]bcdef\n"
("I")
(should (evil-insert-state-p))
("\C-ox")
(ert-info ("Should return to insert state")
(should (evil-insert-state-p)))
"[b]cdef\n"
("\C-oA")
(ert-info ("Should return to insert state after insert state command")
(should (evil-insert-state-p)))
("bcdef[]\n"))))
(defun evil-test-suppress-keymap (state)
"Verify that `self-insert-command' is suppressed in STATE"
(evil-test-buffer
";; This buffer is for notes."
(evil-test-change-state state)
;; TODO: this should be done better
(ert-info ("Disable the state's own keymaps so that the
suppression keymap comes first")
(setq evil-operator-state-minor-mode nil
evil-operator-state-local-minor-mode nil))
(should (eq (key-binding "Q") #'undefined))
(ert-info ("Don't insert text")
;; may or may not signal an error, depending on batch mode
(condition-case nil
(execute-kbd-macro "QQQ")
(error nil))
(should (string= (buffer-substring 1 4) ";; ")))))
(ert-deftest evil-test-emacs-state-suppress-keymap ()
"`self-insert-command' works in Emacs state"
:tags '(evil state)
(should-error (evil-test-suppress-keymap 'emacs)))
(ert-deftest evil-test-normal-state-suppress-keymap ()
"No `self-insert-command' in Normal state"
:tags '(evil state)
(evil-test-suppress-keymap 'normal))
(ert-deftest evil-test-operator-state-suppress-keymap ()
"Operator-Pending state should inherit suppression
of `self-insert-command' from Normal state"
:tags '(evil state)
(evil-test-suppress-keymap 'operator))
(ert-deftest evil-test-operator-state-shortcut-keymap ()
"Enable shortcut keymap in Operator-Pending state"
:tags '(evil state)
(evil-test-buffer
(ert-info ("Activate `evil-operator-shortcut-map' in \
Operator-Pending state")
(evil-test-change-state 'operator)
(should (rassq evil-operator-shortcut-map
(evil-state-keymaps 'operator)))
(should (keymapp evil-operator-shortcut-map))
(should evil-operator-shortcut-mode)
(should (memq evil-operator-shortcut-map
(current-active-maps))))
(ert-info ("Deactivate `evil-operator-shortcut-map' \
outside Operator-Pending state")
(evil-test-change-state 'emacs)
(should-not evil-operator-shortcut-mode)
(should-not (memq evil-operator-shortcut-map
(current-active-maps))))
(ert-info ("Reset `evil-operator-shortcut-map' \
when entering Operator-Pending state")
(define-key evil-operator-shortcut-map "f" 'foo)
(should (eq (lookup-key evil-operator-shortcut-map "f")
'foo))
(evil-test-change-state 'operator)
(should-not (eq (lookup-key evil-operator-shortcut-map "f")
'foo)))
(ert-info ("Reset `evil-operator-shortcut-map' \
when exiting Operator-Pending state")
(define-key evil-operator-shortcut-map "b" 'bar)
(should (eq (lookup-key evil-operator-shortcut-map "b")
'bar))
(evil-test-change-state 'emacs)
(should-not (eq (lookup-key evil-operator-shortcut-map "b")
'bar)))))
(ert-deftest evil-test-auxiliary-maps ()
"Test auxiliary keymaps"
:tags '(evil state)
(let ((map (make-sparse-keymap)) aux)
(ert-info ("Create a new auxiliary keymap")
(evil-define-key 'normal map "f" 'foo)
(setq aux (evil-get-auxiliary-keymap map 'normal))
(should (evil-auxiliary-keymap-p aux))
(should (eq (lookup-key aux "f") 'foo)))
(ert-info ("Add to auxiliary keymap")
(evil-define-key 'normal map "b" 'bar)
(should (eq (lookup-key aux "f") 'foo))
(should (eq (lookup-key aux "b") 'bar)))))
(ert-deftest evil-test-global-local-map-binding ()
"Test use of `evil-define-key' for binding in global maps."
:tags '(evil state)
(let ((evil-normal-state-map (copy-keymap evil-normal-state-map))
(evil-normal-state-local-map
(when (keymapp evil-normal-state-local-map)
(copy-keymap evil-normal-state-local-map)))
(global-map (copy-keymap global-map))
(orig-local-map
(when (keymapp (current-local-map))
(copy-keymap (current-local-map))))
(map (or (current-local-map) (make-sparse-keymap))))
(use-local-map map)
(ert-info ("Bind in a global state map")
(evil-define-key 'normal 'global "f" 'foo)
(should (eq (lookup-key evil-normal-state-map "f") 'foo)))
(ert-info ("Bind in a local state map")
(evil-define-key 'normal 'local "f" 'foo)
(should (eq (lookup-key evil-normal-state-local-map "f") 'foo)))
(ert-info ("Bind in the global map")
(evil-define-key nil 'global "b" 'bar)
(should (eq (lookup-key global-map "b") 'bar)))
(ert-info ("Bind in the local map")
(evil-define-key nil 'local "b" 'bar)
(should (eq (lookup-key (current-local-map) "b") 'bar)))
(use-local-map orig-local-map)))
;;; Type system
(ert-deftest evil-test-exclusive-type ()
"Expand and contract the `line' type"
:tags '(evil type)
(evil-test-buffer
";; This buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
(let* ((first-line 1)
(second-line (progn
(forward-line)
(point)))
(third-line (progn
(forward-line)
(point))))
(ert-info ("Return the beginning and end unchanged \
if they are the same")
(should (equal (evil-normalize 1 1 'exclusive)
(list 1 1 'exclusive))))
(ert-info ("expand to `inclusive' if the end position \
is at the beginning of a line")
(should (equal (evil-normalize (1+ first-line) second-line 'exclusive)
(list (1+ first-line) (1- second-line) 'inclusive
:expanded t))))
(ert-info ("expand to `line' if both the beginning and end \
are at the beginning of a line")
(should (equal (evil-normalize first-line second-line 'exclusive)
(list first-line second-line 'line
:expanded t))))
(ert-info ("Measure as the strict difference between the end \
and the beginning")
(should (string= (evil-describe 1 1 'exclusive)
"0 characters"))
(should (string= (evil-describe 1 2 'exclusive)
"1 character"))
(should (string= (evil-describe 5 2 'exclusive)
"3 characters"))))))
(ert-deftest evil-test-inclusive-type ()
"Expand and contract the `inclusive' type"
:tags '(evil type)
(evil-test-buffer
";; This buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
(ert-info ("Include the ending character")
(should (equal (evil-expand 1 1 'inclusive)
'(1 2 inclusive :expanded t))))
(ert-info ("Don't mind if positions are in wrong order")
(should (equal (evil-expand 5 2 'inclusive)
'(2 6 inclusive :expanded t))))
(ert-info ("Exclude the ending character when contracting")
(should (equal (evil-contract 1 2 'inclusive)
'(1 1 inclusive :expanded nil))))
(ert-info ("Don't mind positions' order when contracting")
(should (equal (evil-contract 6 2 'inclusive)
'(2 5 inclusive :expanded nil))))
(ert-info ("Measure as one more than the difference")
(should (string= (evil-describe 1 1 'inclusive)
"1 character"))
(should (string= (evil-describe 5 2 'inclusive)
"4 characters")))))
(ert-deftest evil-test-line-type ()
"Expand the `line' type"
:tags '(evil type)
(evil-test-buffer
";; This buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
(let* ((first-line 1)
(second-line (progn
(forward-line)
(point)))
(third-line (progn
(forward-line)
(point))))
(ert-info ("Expand to the whole first line")
(should (equal (evil-expand first-line first-line 'line)
(list first-line second-line 'line :expanded t)))
(should (string= (evil-describe first-line first-line 'line)
"1 line")))
(ert-info ("Expand to the two first lines")
(should (equal (evil-expand first-line second-line 'line)
(list first-line third-line 'line :expanded t)))
(should (string= (evil-describe first-line second-line 'line)
"2 lines"))))))
(ert-deftest evil-test-block-type ()
"Expand and contract the `block' type"
:tags '(evil type)
(evil-test-buffer
";; This buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
(let* ((first-line 1)
(second-line (progn
(forward-line)
(point)))
(third-line (progn
(forward-line)
(point))))
(ert-info ("Expand to a 1x1 block")
(should (equal (evil-expand 1 1 'block)
(list 1 2 'block :expanded t)))
(should (string= (evil-describe 1 1 'block)
"1 row and 1 column")))
(ert-info ("Expand to a 2x1 block")
(should (equal (evil-expand first-line second-line 'block)
(list first-line (1+ second-line) 'block :expanded t)))
(should (string= (evil-describe first-line second-line 'block)
"2 rows and 1 column")))
(ert-info ("Expand to a 3x2 block")
(should (equal (evil-expand first-line (1+ third-line) 'block)
(list first-line (1+ (1+ third-line))
'block :expanded t)))
(should (string= (evil-describe first-line (1+ third-line) 'block)
"3 rows and 2 columns")))
(ert-info ("Contract to a 0x0 rectangle")
(should (equal (evil-contract 1 2 'block)
(list 1 1 'block :expanded nil))))
(ert-info ("Contract to a 2x0 rectangle")
(should (equal (evil-contract first-line (1+ second-line) 'block)
(list first-line second-line 'block :expanded nil))))
(ert-info ("Contract to a 3x1 rectangle")
(should (equal (evil-contract first-line (1+ (1+ third-line)) 'block)
(list first-line (1+ third-line)
'block :expanded nil)))))))
(ert-deftest evil-test-type-transform ()
"Test `evil-transform'"
:tags '(evil type)
(evil-test-buffer
";; This buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
(ert-info ("Return positions unchanged when passed nil \
for TYPE or TRANSFORM")
(should (equal (evil-transform nil 1 2 'block)
'(1 2 block)))
(should (equal (evil-transform :expand 1 2 nil)
'(1 2)))
(should (equal (evil-transform nil 1 2 nil)
'(1 2))))
(ert-info ("Accept markers, but return positions")
(should (equal (evil-transform :expand
(move-marker (make-marker) 1) 1
'inclusive)
'(1 2 inclusive :expanded t)))
(should (equal (evil-transform nil (move-marker (make-marker) 1) 2
nil)
'(1 2))))))
(ert-deftest evil-test-type-modifiers ()
"Test type modifiers like \"dv}\""
:tags '(evil type)
(ert-info ("Change `inclusive' motions to `exclusive'")
(evil-test-buffer
"[A]bove some line"
("dve")
"[e] some line"))
(ert-info ("Change `exclusive' motions to `inclusive'")
(evil-test-buffer
"Above [s]ome line
Below some empty line"
("dv}")
"Above[ ]
Below some empty line"))
(ert-info ("Change type to `line'")
(evil-test-buffer
"Above [s]ome line
Below some empty line"
("dV}")
"[B]elow some empty line")))
;;; Insertion
(ert-deftest evil-test-insert ()
"Test `evil-insert'"
:tags '(evil insert)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save"
("ievil rulz " [escape])
";; evil rulz[ ]This buffer is for notes you don't want to save"))
(ert-deftest evil-test-append ()
"Test `evil-append'"
:tags '(evil insert)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save"
("aevil rulz " [escape])
";; Tevil rulz[ ]his buffer is for notes you don't want to save"))
(ert-deftest evil-test-open-above ()
"Test `evil-open-above'"
:tags '(evil insert)
(evil-test-buffer
";; This buffer is for notes you don't want to save,
\[;]; and for Lisp evaluation."
("Oabc\ndef" [escape])
";; This buffer is for notes you don't want to save,
abc
de[f]
;; and for Lisp evaluation.")
(ert-info ("Open empty line")
(evil-test-buffer
"(let (var)\n [t]est)\n"
(emacs-lisp-mode)
("O" [escape])
"(let (var)\n[\n] test)\n"))
(ert-info ("Open non-empty line")
(evil-test-buffer
"(let (var)\n [t]est)\n"
(emacs-lisp-mode)
("Odo-it" [escape])
"(let (var)\n do-i[t]\n test)\n")))
(ert-deftest evil-test-open-below ()
"Test `evil-open-below'"
:tags '(evil insert)
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save,
;; and for Lisp evaluation."
("oabc\ndef" [escape])
";; This buffer is for notes you don't want to save,
abc
de[f]
;; and for Lisp evaluation.")
(ert-info ("Open empty line")
(evil-test-buffer
"[(]let (var)\n test)\n"
(emacs-lisp-mode)
("o" [escape])
"(let (var)\n[\n] test)\n"))
(ert-info ("Open non-empty line")
(evil-test-buffer
"[(]let (var)\n test)\n"
(emacs-lisp-mode)
("odo-it" [escape])
"(let (var)\n do-i[t]\n test)\n"))
(let ((evil-auto-indent t))
(ert-info ("With count")
(evil-test-buffer
"[(]and a\n c)\n"
(emacs-lisp-mode)
("3ob" [escape])
"(and a\n b\n b\n [b]\n c)\n"))))
(ert-deftest evil-test-open-below-folded ()
"Test `evil-open-below' on folded lines"
:tags '(evil insert)
(evil-test-buffer
"[l]ine1\n\n(let ()\n var)\n\nlast line\n"
(emacs-lisp-mode)
(hs-minor-mode 1)
("zm2joABC" [escape])
"line1\n\n(let ()\n var)\nAB[C]\n\nlast line\n"))
(ert-deftest evil-test-insert-line ()
"Test `evil-insert-line'"
:tags '(evil insert)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save"
("Ievil rulz " [escape])
"evil rulz[ ];; This buffer is for notes you don't want to save"))
(ert-deftest evil-test-append-line ()
"Test `evil-append-line'"
:tags '(evil insert)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save"
("Aevil rulz " [escape])
";; This buffer is for notes you don't want to saveevil rulz[ ]"))
(ert-deftest evil-test-insert-digraph ()
"Test `evil-insert-digraph'"
:tags '(evil insert)
(ert-info ("Predefined digraph")
(evil-test-buffer
("i\C-kae")
"æ[]"))
(ert-info ("Custom digraph")
(let ((evil-digraphs-table-user '(((?a ?o) . ?å))))
(evil-test-buffer
("i\C-kao")
"å[]"))))
;;; Repeat system
(ert-deftest evil-test-normalize-repeat-info ()
"Test `evil-normalize-repeat-info'"
:tags '(evil repeat)
(ert-info ("Single array")
(should (equal (evil-normalize-repeat-info
'("abc"))
'([?a ?b ?c])))
(should (equal (evil-normalize-repeat-info
'("\M-f"))
(list (kbd "M-f")))))
(ert-info ("Single symbol")
(should (equal (evil-normalize-repeat-info
'(SYM))
'(SYM))))
(ert-info ("Arrays only")
(should (equal (evil-normalize-repeat-info
'("abc" [XX YY] "def"))
'([?a ?b ?c XX YY ?d ?e ?f]))))
(ert-info ("Several symbols")
(should (equal (evil-normalize-repeat-info
'(BEG MID END))
'(BEG MID END))))
(ert-info ("Arrays with symbol at the beginning")
(should (equal (evil-normalize-repeat-info
'(BEG "abc" [XX YY] "def"))
'(BEG [?a ?b ?c XX YY ?d ?e ?f]))))
(ert-info ("Arrays with symbol at the end")
(should (equal (evil-normalize-repeat-info
'("abc" [XX YY] "def" END))
'([?a ?b ?c XX YY ?d ?e ?f] END))))
(ert-info ("Arrays with symbol in the middle")
(should (equal (evil-normalize-repeat-info
'("abc" [XX YY] MID "def" ))
'([?a ?b ?c XX YY] MID [?d ?e ?f]))))
(ert-info ("Concatenate arrays with several symbols")
(should (equal (evil-normalize-repeat-info
'(BEG "abc" [XX YY] MID "def" END))
'(BEG [?a ?b ?c XX YY] MID [?d ?e ?f] END)))))
(defun evil-test-repeat-info (keys &optional recorded)
"Execute a sequence of keys and verify that `evil-repeat-ring'
records them correctly. KEYS is the sequence of keys to execute.
RECORDED is the expected sequence of recorded events.
If nil, KEYS is used."
(execute-kbd-macro keys)
(should (equal (evil-normalize-repeat-info (ring-ref evil-repeat-ring 0))
(list (vconcat (or recorded keys))))))
(ert-deftest evil-test-normal-repeat-info-simple-command ()
"Save key-sequence after simple editing command in Normal state"
:tags '(evil repeat)
(evil-test-buffer
"[T]his is a test buffer"
(ert-info ("Call simple command without count")
(evil-test-repeat-info "x"))
(ert-info ("Call simple command with count 3")
(evil-test-repeat-info "3x"))))
(ert-deftest evil-test-normal-repeat-info-char-command ()
"Save key-sequence after editing command with character in Normal state"
:tags '(evil repeat)
(evil-test-buffer
"[T]his is a test buffer"
(ert-info ("Call command with character argument without count")
(evil-test-repeat-info "r5"))
(ert-info ("Call command with character argument with count 12")
(evil-test-repeat-info "12rX"))))
(ert-deftest evil-test-insert-repeat-info ()
"Save key-sequence after Insert state"
:tags '(evil repeat)
(evil-test-buffer
(ert-info ("Insert text without count")
(evil-test-repeat-info (vconcat "iABC" [escape])))
(ert-info ("Insert text with count 42")
(evil-test-repeat-info (vconcat "42iABC" [escape])))))
(ert-deftest evil-test-repeat ()
"Repeat several editing commands"
:tags '(evil repeat)
(ert-info ("Repeat replace")
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("rX")
"[X]; This buffer is for notes you don't want to save"
([right right] ".")
"X;[X]This buffer is for notes you don't want to save"))
(ert-info ("Repeat replace with count")
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("2rX")
"X[X] This buffer is for notes you don't want to save"
([right right] ".")
"XX X[X]is buffer is for notes you don't want to save"))
(ert-info ("Repeat replace without count with a new count")
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("rX")
"[X]; This buffer is for notes you don't want to save"
([right right] "13.")
"X;XXXXXXXXXXXX[X]is for notes you don't want to save"))
(ert-info ("Repeat replace with count replacing original count")
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("10rX")
"XXXXXXXXX[X]ffer is for notes you don't want to save"
([right right] "20.")
"XXXXXXXXXXfXXXXXXXXXXXXXXXXXXX[X] don't want to save"))
(ert-info ("Repeat movement in Insert state")
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save"
("i(\M-f)" [escape])
";; (This[)] buffer is for notes you don't want to save"
("w.")
";; (This) (buffer[)] is for notes you don't want to save")))
(ert-deftest evil-test-repeat-register ()
"Test repeating a register command."
:tags '(evil repeat)
(evil-test-buffer
"[l]ine 1\nline 2\nline 3\nline 4\n"
("\"addyy\"aP")
"[l]ine 1\nline 2\nline 3\nline 4\n"
(".")
"[l]ine 1\nline 1\nline 2\nline 3\nline 4\n"))
(ert-deftest evil-test-repeat-numeric-register ()
"Test repeating a command with a numeric register."
:tags '(evil repeat)
(evil-test-buffer
"[l]ine 1\nline 2\nline 3\nline 4\nline 5\n"
("dd...")
"[l]ine 5\n"
("\"1P")
"[l]ine 4\nline 5\n"
(".")
"[l]ine 3\nline 4\nline 5\n"
(".")
"[l]ine 2\nline 3\nline 4\nline 5\n"
(".")
"[l]ine 1\nline 2\nline 3\nline 4\nline 5\n"))
(ert-deftest evil-test-cmd-replace-char ()
"Calling `evil-replace-char' should replace characters"
:tags '(evil repeat)
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("r5")
"[5]; This buffer is for notes you don't want to save"
("3rX")
"XX[X]This buffer is for notes you don't want to save")
(ert-info ("Replace digraph")
(evil-test-buffer
"[;]; This buffer is for notes you don't want to save"
("re'")
"[é]; This buffer is for notes you don't want to save"
("3rc*")
"ξξ[ξ]This buffer is for notes you don't want to save"))
(ert-info ("Replacing \\n should insert only one newline")
(evil-test-buffer
"(setq var xxx [y]yy zzz)\n"
(emacs-lisp-mode)
(setq indent-tabs-mode nil)
("2r\n")
"(setq var xxx \n [y] zzz)\n")))
(ert-deftest evil-test-insert-with-count ()
"Test `evil-insert' with repeat count"
:tags '(evil repeat)
(evil-test-buffer
";; [T]his buffer is for notes"
("2ievil rulz " [escape])
";; evil rulz evil rulz[ ]This buffer is for notes"))
(ert-deftest evil-test-repeat-insert ()
"Test repeating of `evil-insert'"
:tags '(evil repeat)
(ert-info ("Repeat insert")
(evil-test-buffer
"[;]; This buffer is for notes"
("iABC" [escape])
"AB[C];; This buffer is for notes"
("..")
"ABABAB[C]CC;; This buffer is for notes"))
(ert-info ("Repeat insert with count")
(evil-test-buffer
"[;]; This buffer is for notes"
("2iABC" [escape])
"ABCAB[C];; This buffer is for notes"
("..")
"ABCABABCABABCAB[C]CC;; This buffer is for notes"))
(ert-info ("Repeat insert with repeat count")
(evil-test-buffer
"[;]; This buffer is for notes"
("iABC" [escape])
"AB[C];; This buffer is for notes"
("11.")
"ABABCABCABCABCABCABCABCABCABCABCAB[C]C;; This buffer is for notes"))
(ert-info ("Repeat insert with count with repeat with count")
(evil-test-buffer
"[;]; This buffer is for notes"
("10iABC" [escape])
"ABCABCABCABCABCABCABCABCABCAB[C];; This buffer is for notes"
("11.")
"ABCABCABCABCABCABCABCABCABCABABCABCABCABCABCABCABCABCABCABCAB[C]C;; \
This buffer is for notes")))
(ert-deftest evil-test-repeat-error ()
"Test whether repeat returns to normal state in case of an error."
(evil-test-buffer
"[l]ine 1\nline 2\nline 3\nline 4"
("ixxx" [down] [down] [left] [left] [left] "yyy" [escape])
"xxxline 1\nline 2\nyy[y]line 3\nline 4"
(should-error (execute-kbd-macro "j^."))
(should (evil-normal-state-p))
("^")
"xxxline 1\nline 2\nyyyline 3\n[x]xxline 4"))
(ert-deftest evil-test-repeat-quoted-insert ()
"Test whether `quoted-insert' can be repeated."
(ert-info ("Insert C-v")
(evil-test-buffer
"lin[e] 1\nline 2\nline 3\n"
("i\C-v\C-v" [escape])
"lin[]e 1\nline 2\nline 3\n"))
(ert-info ("Insert ESC")
(evil-test-buffer
"lin[e] 1\nline 2\nline 3\n"
("i\C-v" [escape escape])
"lin[]e 1\nline 2\nline 3\n"))
(ert-info ("Block insert C-v")
(evil-test-buffer
"lin[e] 1\nline 2\nline 3\n"
("gg\C-vGI\C-v\C-v" [escape])
"[]line 1\nline 2\nline 3\n")))
(ert-deftest evil-test-insert-vcount ()
"Test `evil-insert' with vertical repeating"
:tags '(evil repeat)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.
;; Below the empty line."
(define-key evil-normal-state-local-map "i"
#'(lambda (count)
(interactive "p")
(evil-insert count 5)))
("2iABC" [escape])
"\
;; ABCAB[C]This buffer is for notes you don't want to save.
;; ABCABCIf you want to create a file, visit that file with C-x C-f,
;; ABCABCthen enter the text in that file's own buffer.
ABCABC
;; ABCABCBelow the empty line."))
(ert-deftest evil-test-append-with-count ()
"Test `evil-append' with repeat count"
:tags '(evil repeat)
(evil-test-buffer
";; [T]his buffer is for notes"
("2aevil rulz " [escape])
";; Tevil rulz evil rulz[ ]his buffer is for notes"))
(ert-deftest evil-test-repeat-append ()
"Test repeating of `evil-append'"
:tags '(evil repeat)
(ert-info ("Repeat insert")
(evil-test-buffer
"[;]; This buffer is for notes"
("aABC" [escape])
";AB[C]; This buffer is for notes"
("..")
";ABCABCAB[C]; This buffer is for notes"))
(ert-info ("Repeat insert with count")
(evil-test-buffer
"[;]; This buffer is for notes"
("2aABC" [escape])
";ABCAB[C]; This buffer is for notes"
("..")
";ABCABCABCABCABCAB[C]; This buffer is for notes"))
(ert-info ("Repeat insert with repeat count")
(evil-test-buffer
"[;]; This buffer is for notes"
("aABC" [escape])
";AB[C]; This buffer is for notes"
("11.")
";ABCABCABCABCABCABCABCABCABCABCABCAB[C]; This buffer is for notes"))
(ert-info ("Repeat insert with count with repeat with count")
(evil-test-buffer
"[;]; This buffer is for notes"
("10aABC" [escape])
";ABCABCABCABCABCABCABCABCABCAB[C]; This buffer is for notes"
("11.")
";ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCAB[C]; \
This buffer is for notes")))
(ert-deftest evil-test-append-vcount ()
"Test `evil-append' with vertical repeating"
:tags '(evil repeat)
(evil-test-buffer
";; [T]his buffer is for notes you don't want to save.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.