forked from airblade/vim-gitgutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gitgutter.vim
More file actions
1128 lines (871 loc) · 28.9 KB
/
Copy pathtest_gitgutter.vim
File metadata and controls
1128 lines (871 loc) · 28.9 KB
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
let s:current_dir = expand('%:p:h')
let s:test_repo = s:current_dir.'/test-repo'
let s:bufnr = bufnr('')
"
" Helpers
"
" Ignores unexpected keys in actual.
function s:assert_list_of_dicts(expected, actual)
if empty(a:expected)
call assert_equal([], a:actual)
return
endif
let expected_keys = keys(a:expected[0])
for dict in a:actual
for k in keys(dict)
if index(expected_keys, k) == -1
call remove(dict, k)
endif
endfor
endfor
call assert_equal(a:expected, a:actual)
endfunction
" Ignores unexpected keys.
"
" expected - list of signs
function s:assert_signs(expected, filename)
let actual = sign_getplaced(a:filename, {'group': 'gitgutter'})[0].signs
call s:assert_list_of_dicts(a:expected, actual)
endfunction
function s:git_diff(...)
return split(system('git diff -U0 '.(a:0 ? a:1 : 'fixture.txt')), '\n')
endfunction
function s:git_diff_staged(...)
return split(system('git diff -U0 --staged '.(a:0 ? a:1 : 'fixture.txt')), '\n')
endfunction
function s:trigger_gitgutter()
doautocmd CursorHold
endfunction
"
" SetUp / TearDown
"
function SetUp()
call system("git init ".s:test_repo.
\ " && cd ".s:test_repo.
\ " && cp ../fixture.txt .".
\ " && cp ../fixture_dos.txt .".
\ " && git add . && git commit -m 'initial'".
\ " && git config diff.mnemonicPrefix false")
execute ':cd' s:test_repo
edit! fixture.txt
call gitgutter#sign#reset()
" FIXME why won't vim autoload the file?
execute 'source' '../../autoload/gitgutter/diff_highlight.vim'
execute 'source' '../../autoload/gitgutter/fold.vim'
endfunction
function TearDown()
" delete all buffers except this one
" TODO: move to runner.vim, accounting for multiple test files
if s:bufnr > 1
silent! execute '1,'.s:bufnr-1.'bdelete!'
endif
silent! execute s:bufnr+1.',$bdelete!'
execute ':cd' s:current_dir
call system("rm -rf ".s:test_repo)
endfunction
"
" The tests
"
function Test_add_lines()
normal ggo*
call s:trigger_gitgutter()
let expected = [{'lnum': 2, 'name': 'GitGutterLineAdded', 'group': 'gitgutter', 'priority': 10}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_add_lines_fish()
let _shell = &shell
set shell=/usr/local/bin/fish
normal ggo*
call s:trigger_gitgutter()
let expected = [{'lnum': 2, 'name': 'GitGutterLineAdded'}]
call s:assert_signs(expected, 'fixture.txt')
let &shell = _shell
endfunction
function Test_modify_lines()
normal ggi*
call s:trigger_gitgutter()
let expected = [{'lnum': 1, 'name': 'GitGutterLineModified'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_remove_lines()
execute '5d'
call s:trigger_gitgutter()
let expected = [{'lnum': 4, 'name': 'GitGutterLineRemoved'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_remove_first_lines()
execute '1d'
call s:trigger_gitgutter()
let expected = [{'lnum': 1, 'name': 'GitGutterLineRemovedFirstLine'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_priority()
let g:gitgutter_sign_priority = 5
execute '1d'
call s:trigger_gitgutter()
call s:assert_signs([{'priority': 5}], 'fixture.txt')
let g:gitgutter_sign_priority = 10
endfunction
function Test_overlapping_hunks()
execute '3d'
execute '1d'
call s:trigger_gitgutter()
let expected = [{'lnum': 1, 'name': 'GitGutterLineRemovedAboveAndBelow'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_edit_file_with_same_name_as_a_branch()
normal 5Gi*
call system('git checkout -b fixture.txt')
call s:trigger_gitgutter()
let expected = [{'lnum': 5, 'name': 'GitGutterLineModified'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_file_added_to_git()
let tmpfile = 'fileAddedToGit.tmp'
call system('touch '.tmpfile.' && git add '.tmpfile)
execute 'edit '.tmpfile
normal ihello
call s:trigger_gitgutter()
let expected = [{'lnum': 1, 'name': 'GitGutterLineAdded'}]
call s:assert_signs(expected, 'fileAddedToGit.tmp')
endfunction
function Test_filename_with_equals()
call system('touch =fixture=.txt && git add =fixture=.txt')
edit =fixture=.txt
normal ggo*
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 1, 'name': 'GitGutterLineAdded'},
\ {'lnum': 2, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, '=fixture=.txt')
endfunction
function Test_filename_with_square_brackets()
call system('touch fix[tu]re.txt && git add fix[tu]re.txt')
edit fix[tu]re.txt
normal ggo*
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 1, 'name': 'GitGutterLineAdded'},
\ {'lnum': 2, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, 'fix[tu]re.txt')
endfunction
function Test_filename_leading_dash()
call system('touch -- -fixture.txt && git add -- -fixture.txt')
edit -fixture.txt
normal ggo*
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 1, 'name': 'GitGutterLineAdded'},
\ {'lnum': 2, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, '-fixture.txt')
endfunction
function Test_filename_umlaut()
call system('touch -- fixtüre.txt && git add -- fixtüre.txt')
edit fixtüre.txt
normal ggo*
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 1, 'name': 'GitGutterLineAdded'},
\ {'lnum': 2, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, 'fixtüre.txt')
endfunction
" FIXME: this test fails when it is the first (or only) test to be run
function Test_follow_symlink()
let tmp = 'symlink'
call system('ln -nfs fixture.txt '.tmp)
execute 'edit '.tmp
6d
call s:trigger_gitgutter()
let expected = [{'lnum': 5, 'name': 'GitGutterLineRemoved'}]
call s:assert_signs(expected, 'symlink')
endfunction
function Test_keep_alt()
enew
execute "normal! \<C-^>"
call assert_equal('fixture.txt', bufname(''))
call assert_equal('', bufname('#'))
normal ggx
call s:trigger_gitgutter()
call assert_equal('', bufname('#'))
endfunction
function Test_keep_modified()
normal 5Go*
call assert_equal(1, getbufvar('', '&modified'))
call s:trigger_gitgutter()
call assert_equal(1, getbufvar('', '&modified'))
endfunction
function Test_keep_op_marks()
normal 5Go*
call assert_equal([0,6,1,0], getpos("'["))
call assert_equal([0,6,2,0], getpos("']"))
call s:trigger_gitgutter()
call assert_equal([0,6,1,0], getpos("'["))
call assert_equal([0,6,2,0], getpos("']"))
endfunction
function Test_no_modifications()
call s:assert_signs([], 'fixture.txt')
endfunction
function Test_orphaned_signs()
execute "normal 5GoX\<CR>Y"
call s:trigger_gitgutter()
6d
call s:trigger_gitgutter()
let expected = [{'lnum': 6, 'name': 'GitGutterLineAdded'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_untracked_file_outside_repo()
let tmp = tempname()
call system('touch '.tmp)
execute 'edit '.tmp
call s:assert_signs([], tmp)
endfunction
function Test_untracked_file_within_repo()
let tmp = 'untrackedFileWithinRepo.tmp'
call system('touch '.tmp)
execute 'edit '.tmp
normal ggo*
call s:trigger_gitgutter()
call s:assert_signs([], tmp)
call assert_equal(-2, b:gitgutter.path)
call system('rm '.tmp)
endfunction
function Test_untracked_file_square_brackets_within_repo()
let tmp = '[un]trackedFileWithinRepo.tmp'
call system('touch '.tmp)
execute 'edit '.tmp
normal ggo*
call s:trigger_gitgutter()
call s:assert_signs([], tmp)
call system('rm '.tmp)
endfunction
function Test_hunk_outside_noop()
5
GitGutterStageHunk
call s:assert_signs([], 'fixture.txt')
call assert_equal([], s:git_diff())
call assert_equal([], s:git_diff_staged())
GitGutterUndoHunk
call s:assert_signs([], 'fixture.txt')
call assert_equal([], s:git_diff())
call assert_equal([], s:git_diff_staged())
endfunction
function Test_preview()
normal 5Gi*
GitGutterPreviewHunk
wincmd P
call assert_equal(2, line('$'))
call assert_equal('-e', getline(1))
call assert_equal('+*e', getline(2))
wincmd p
endfunction
function Test_preview_dos()
edit! fixture_dos.txt
normal 5Gi*
GitGutterPreviewHunk
wincmd P
call assert_equal(2, line('$'))
call assert_equal('-e', getline(1))
call assert_equal('+*e', getline(2))
wincmd p
endfunction
function Test_hunk_stage()
let _shell = &shell
set shell=foo
normal 5Gi*
GitGutterStageHunk
call assert_equal('foo', &shell)
let &shell = _shell
call s:assert_signs([], 'fixture.txt')
" Buffer is unsaved
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index ae8e546..f5c6aff 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5 +5 @@ d',
\ '-*e',
\ '+e'
\ ]
call assert_equal(expected, s:git_diff())
" Index has been updated
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..ae8e546 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5 +5 @@ d',
\ '-e',
\ '+*e'
\ ]
call assert_equal(expected, s:git_diff_staged())
" Save the buffer
write
call assert_equal([], s:git_diff())
endfunction
function Test_hunk_stage_nearby_hunk()
execute "normal! 2Gox\<CR>y\<CR>z"
normal 2jdd
normal k
GitGutterStageHunk
let expected = [
\ {'lnum': 3, 'name': 'GitGutterLineAdded'},
\ {'lnum': 4, 'name': 'GitGutterLineAdded'},
\ {'lnum': 5, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, 'fixture.txt')
" Buffer is unsaved
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 53b13df..f5c6aff 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -3,0 +4 @@ c',
\ '+d',
\ ]
call assert_equal(expected, s:git_diff())
" Index has been updated
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..53b13df 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -4 +3,0 @@ c',
\ '-d',
\ ]
call assert_equal(expected, s:git_diff_staged())
" Save the buffer
write
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 53b13df..8fdfda7 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -2,0 +3,3 @@ b',
\ '+x',
\ '+y',
\ '+z',
\ ]
call assert_equal(expected, s:git_diff())
endfunction
function Test_hunk_stage_partial_visual_added()
call append(5, ['A','B','C','D'])
execute "normal 7GVj:GitGutterStageHunk\<CR>"
let expected = [
\ {'lnum': 6, 'name': 'GitGutterLineAdded'},
\ {'lnum': 9, 'name': 'GitGutterLineAdded'},
\ ]
call s:assert_signs(expected, 'fixture.txt')
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 8a7026e..f5c6aff 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -6,2 +5,0 @@ e',
\ '-B',
\ '-C',
\ ]
call assert_equal(expected, s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..8a7026e 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6,2 @@ e',
\ '+B',
\ '+C',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_stage_partial_cmd_added()
call append(5, ['A','B','C','D'])
6
7,8GitGutterStageHunk
let expected = [
\ {'lnum': 6, 'name': 'GitGutterLineAdded'},
\ {'lnum': 9, 'name': 'GitGutterLineAdded'},
\ ]
call s:assert_signs(expected, 'fixture.txt')
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 8a7026e..f5c6aff 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -6,2 +5,0 @@ e',
\ '-B',
\ '-C',
\ ]
call assert_equal(expected, s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..8a7026e 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6,2 @@ e',
\ '+B',
\ '+C',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_stage_partial_preview_added()
call append(5, ['A','B','C','D'])
6
GitGutterPreviewHunk
wincmd P
" remove C and A so we stage B and D
3delete
1delete
GitGutterStageHunk
write
let expected = [
\ {'lnum': 6, 'name': 'GitGutterLineAdded'},
\ {'lnum': 8, 'name': 'GitGutterLineAdded'},
\ ]
call s:assert_signs(expected, 'fixture.txt')
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 975852f..3dd23a3 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6 @@ e',
\ '+A',
\ '@@ -6,0 +8 @@ B',
\ '+C',
\ ]
call assert_equal(expected, s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..975852f 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6,2 @@ e',
\ '+B',
\ '+D',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_stage_preview_write()
call append(5, ['A','B','C','D'])
6
GitGutterPreviewHunk
wincmd P
" preview window
call feedkeys(":w\<CR>", 'tx')
" original window
write
call s:assert_signs([], 'fixture.txt')
call assert_equal([], s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..3dd23a3 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6,4 @@ e',
\ '+A',
\ '+B',
\ '+C',
\ '+D',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_stage_partial_preview_added_removed()
4,5delete
call append(3, ['A','B','C','D'])
4
GitGutterPreviewHunk
wincmd P
" -d
" -e
" +A
" +B
" +C
" +D
" remove D and d so they do not get staged
6delete
1delete
GitGutterStageHunk
write
let expected = [
\ {'lnum': 3, 'name': 'GitGutterLineRemoved'},
\ {'lnum': 7, 'name': 'GitGutterLineAdded'},
\ ]
call s:assert_signs(expected, 'fixture.txt')
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index 9a19589..e63fb0a 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -4 +3,0 @@ c',
\ '-d',
\ '@@ -7,0 +7 @@ C',
\ '+D',
\ ]
call assert_equal(expected, s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..9a19589 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5 +5,3 @@ d',
\ '-e',
\ '+A',
\ '+B',
\ '+C',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_undo()
let _shell = &shell
set shell=foo
normal 5Gi*
GitGutterUndoHunk
call assert_equal('foo', &shell)
let &shell = _shell
call s:assert_signs([], 'fixture.txt')
call assert_equal([], s:git_diff())
call assert_equal([], s:git_diff_staged())
call assert_equal('e', getline(5))
endfunction
function Test_hunk_undo_dos()
edit! fixture_dos.txt
normal 5Gi*
GitGutterUndoHunk
call s:assert_signs([], 'fixture_dos.txt')
call assert_equal([], s:git_diff('fixture_dos.txt'))
call assert_equal([], s:git_diff_staged('fixture_dos.txt'))
call assert_equal('e', getline(5))
endfunction
function Test_undo_nearby_hunk()
execute "normal! 2Gox\<CR>y\<CR>z"
normal 2jdd
normal k
call s:trigger_gitgutter()
GitGutterUndoHunk
call s:trigger_gitgutter()
let expected = [
\ {'lnum': 3, 'name': 'GitGutterLineAdded'},
\ {'lnum': 4, 'name': 'GitGutterLineAdded'},
\ {'lnum': 5, 'name': 'GitGutterLineAdded'}
\ ]
call s:assert_signs(expected, 'fixture.txt')
call assert_equal([], s:git_diff())
call assert_equal([], s:git_diff_staged())
" Save the buffer
write
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -2,0 +3,3 @@ b',
\ '+x',
\ '+y',
\ '+z',
\ ]
call assert_equal(expected, s:git_diff())
endfunction
function Test_overlapping_hunk_op()
func Answer(char)
call feedkeys(a:char."\<CR>")
endfunc
" Undo upper
execute '3d'
execute '1d'
call s:trigger_gitgutter()
normal gg
call timer_start(100, {-> Answer('u')} )
GitGutterUndoHunk
call s:trigger_gitgutter()
let expected = [{'lnum': 2, 'name': 'GitGutterLineRemoved'}]
call s:assert_signs(expected, 'fixture.txt')
" Undo lower
execute '1d'
call s:trigger_gitgutter()
normal gg
call timer_start(100, {-> Answer('l')} )
GitGutterUndoHunk
call s:trigger_gitgutter()
let expected = [{'lnum': 1, 'name': 'GitGutterLineRemovedFirstLine'}]
call s:assert_signs(expected, 'fixture.txt')
endfunction
function Test_write_option()
set nowrite
normal ggo*
call s:trigger_gitgutter()
let expected = [{'lnum': 2, 'name': 'GitGutterLineAdded'}]
call s:assert_signs(expected, 'fixture.txt')
set write
endfunction
function Test_inner_text_object()
execute "normal! 2Gox\<CR>y\<CR>z\<CR>\<CR>"
call s:trigger_gitgutter()
normal dic
call s:trigger_gitgutter()
call s:assert_signs([], 'fixture.txt')
call assert_equal(readfile('fixture.txt'), getline(1,'$'))
" Excludes trailing lines
normal 9Gi*
normal 10Gi*
call s:trigger_gitgutter()
execute "normal vic\<Esc>"
call assert_equal([9, 10], [line("'<"), line("'>")])
endfunction
function Test_around_text_object()
execute "normal! 2Gox\<CR>y\<CR>z\<CR>\<CR>"
call s:trigger_gitgutter()
normal dac
call s:trigger_gitgutter()
call s:assert_signs([], 'fixture.txt')
call assert_equal(readfile('fixture.txt'), getline(1,'$'))
" Includes trailing lines
normal 9Gi*
normal 10Gi*
call s:trigger_gitgutter()
execute "normal vac\<Esc>"
call assert_equal([9, 11], [line("'<"), line("'>")])
endfunction
function Test_user_autocmd()
autocmd User GitGutter let s:autocmd_user = g:gitgutter_hook_context.bufnr
" Verify not fired when nothing changed.
let s:autocmd_user = 0
call s:trigger_gitgutter()
call assert_equal(0, s:autocmd_user)
" Verify fired when there was a change.
normal ggo*
let bufnr = bufnr('')
call s:trigger_gitgutter()
call assert_equal(bufnr, s:autocmd_user)
endfunction
function Test_fix_file_references()
" No special characters
let hunk_diff = join([
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
let filepath = 'blah.txt'
let expected = join([
\ 'diff --git a/blah.txt b/blah.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- a/blah.txt',
\ '+++ b/blah.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
" diff.mnemonicPrefix; spaces in filename
let hunk_diff = join([
\ 'diff --git i/x/cat dog w/x/cat dog',
\ 'index f5c6aff..3fbde56 100644',
\ '--- i/x/cat dog',
\ '+++ w/x/cat dog',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
let filepath = 'blah.txt'
let expected = join([
\ 'diff --git i/blah.txt w/blah.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- i/blah.txt',
\ '+++ w/blah.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
" Backslashes in filename; quotation marks
let hunk_diff = join([
\ 'diff --git "a/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\11.1.vim" "b/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\12.1.vim"',
\ 'index f42aeb0..4930403 100644',
\ '--- "a/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\11.1.vim"',
\ '+++ "b/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\12.1.vim"',
\ '@@ -172,0 +173 @@ stuff',
\ '+x'
\ ], "\n")."\n"
let filepath = 'init.vim'
let expected = join([
\ 'diff --git "a/init.vim" "b/init.vim"',
\ 'index f42aeb0..4930403 100644',
\ '--- "a/init.vim"',
\ '+++ "b/init.vim"',
\ '@@ -172,0 +173 @@ stuff',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
endfunction
function Test_encoding()
call system('cp ../cp932.txt . && git add cp932.txt')
edit ++enc=cp932 cp932.txt
call s:trigger_gitgutter()
call s:assert_signs([], 'cp932.txt')
endfunction
function Test_empty_file()
" 0-byte file
call system('touch empty.txt && git add empty.txt')
edit empty.txt
call s:trigger_gitgutter()
call s:assert_signs([], 'empty.txt')
" File consisting only of a newline
call system('echo "" > newline.txt && git add newline.txt')
edit newline.txt
call s:trigger_gitgutter()
call s:assert_signs([], 'newline.txt')
" 1 line file without newline
" Vim will force a newline unless we tell it not to.
call system('echo -n a > oneline.txt && git add oneline.txt')
set noeol nofixeol
edit! oneline.txt
call s:trigger_gitgutter()
call s:assert_signs([], 'oneline.txt')
set eol fixeol
endfunction
function Test_quickfix()
call setline(5, ['A', 'B'])
call setline(9, ['C', 'D'])
write
let bufnr1 = bufnr('')
edit fixture_dos.txt
call setline(2, ['A', 'B'])
write
let bufnr2 = bufnr('')
GitGutterQuickFix
let expected = [
\ {'lnum': 5, 'bufnr': bufnr1, 'text': '-e'},
\ {'lnum': 9, 'bufnr': bufnr1, 'text': '-i'},
\ {'lnum': 2, 'bufnr': bufnr2, 'text': "-b\r"}
\ ]
call s:assert_list_of_dicts(expected, getqflist())
GitGutterQuickFixCurrentFile
let expected = [
\ {'lnum': 2, 'bufnr': bufnr(''), 'text': "-b\r"},
\ ]
call s:assert_list_of_dicts(expected, getqflist())
endfunction
function Test_common_prefix()
" zero length
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('', 'foo'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('foo', ''))
" nothing in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+pqrst'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('abcde', 'pqrst'))
" something in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcpq'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abcpq'))
call assert_equal(0, gitgutter#diff_highlight#common_prefix('abc', 'apq'))
" everything in common
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abcde'))
call assert_equal(4, gitgutter#diff_highlight#common_prefix('abcde', 'abcde'))
" different lengths
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abx'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abcde', 'abx'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abx', '+abcde'))
call assert_equal(1, gitgutter#diff_highlight#common_prefix('abx', 'abcde'))
call assert_equal(-1, gitgutter#diff_highlight#common_prefix('-abcde', '+abc'))
call assert_equal(2, gitgutter#diff_highlight#common_prefix('abcde', 'abc'))
endfunction