Repository navigation
Expand file tree
/
Copy pathtest_map.lua
More file actions
1427 lines (1130 loc) · 41.7 KB
/
Copy pathtest_map.lua
File metadata and controls
1427 lines (1130 loc) · 41.7 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
local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()
local expect, eq = helpers.expect, helpers.expect.equality
local new_set = MiniTest.new_set
-- Helpers with child processes
--stylua: ignore start
local load_module = function(config) child.mini_load('map', config) end
local unload_module = function() child.mini_unload('map') end
local set_cursor = function(...) return child.set_cursor(...) end
local get_cursor = function(...) return child.get_cursor(...) end
local set_lines = function(...) return child.set_lines(...) end
local type_keys = function(...) return child.type_keys(...) end
--stylua: ignore end
-- Main function wrappers
local map_open = function(opts) child.lua('MiniMap.open(...)', { opts }) end
local map_refresh = function(opts, parts) child.lua('MiniMap.refresh(...)', { opts, parts }) end
local map_close = function() child.lua('MiniMap.close()') end
-- Helpers related to 'mini.map'
local get_resolution_test_file = function(id) return 'tests/dir-map/resolution_' .. id end
local get_map_win_id =
function() return child.lua_get('MiniMap.current.win_data[vim.api.nvim_get_current_tabpage()]') end
local get_map_win_side = function()
local win_config = child.api.nvim_win_get_config(get_map_win_id())
if win_config.anchor == 'NE' and win_config.col == child.o.columns then return 'right' end
if win_config.anchor == 'NW' and win_config.col == 0 then return 'left' end
return 'something is wrong'
end
local get_current = function() return child.lua_get('MiniMap.current') end
local get_map_width = function() return child.api.nvim_win_get_width(get_map_win_id()) end
local disable_map_updates = function()
child.cmd([[
augroup MiniMap
au!
augroup END
]])
end
local mock_diagnostic = function() child.cmd('source tests/dir-map/mock-diagnostic.lua') end
local mock_gitsigns = function() child.cmd('set rtp+=tests/dir-map') end
local source_test_integration = function() child.cmd('source tests/dir-map/src-test-integration.lua') end
local mock_test_integration = function()
source_test_integration()
child.lua([[
local integrations = MiniMap.config.integrations or {}
table.insert(integrations, _G.test_integration)
MiniMap.config.integrations = integrations
]])
end
local source_test_encode_symbols =
function() child.lua([[_G.test_encode_symbols = { '1', '2', '3', '4', resolution = { row = 1, col = 2 } }]]) end
-- Various utilities
local tbl_repeat = function(x, n)
local res = {}
for _ = 1, n do
table.insert(res, x)
end
return res
end
local eq_keys = function(tbl, ref_keys)
local test_keys = vim.tbl_keys(tbl)
local ref_keys_copy = vim.deepcopy(ref_keys)
table.sort(test_keys)
table.sort(ref_keys_copy)
eq(test_keys, ref_keys_copy)
end
local get_n_shown_windows = function() return #child.api.nvim_tabpage_list_wins(0) end
-- Output test set
T = new_set({
hooks = {
pre_case = function()
child.setup()
load_module()
end,
post_once = child.stop,
},
})
-- Data =======================================================================
-- All possible encodings of '3x2' resolution
local example_lines = {
' a aaa a aaa',
' a a a a ',
' ',
' a aaa a aaa',
' a a a aaaaaaaaa',
' ',
' a aaa a aaa',
' a a a a ',
'a a a a a a a a ',
' a aaa a aaa',
' a a a aaaaaaaaa',
'a a a a a a a a ',
' a aaa a aaa',
' a a a a ',
' a a a a a a a a',
' a aaa a aaa',
' a a a aaaaaaaaa',
' a a a a a a a a',
' a aaa a aaa',
' a a a a ',
'aaaaaaaaaaaaaaaa',
' a aaa a aaa',
' a a a aaaaaaaaa',
'aaaaaaaaaaaaaaaa',
}
local extended_example_lines = vim.deepcopy(example_lines)
vim.list_extend(extended_example_lines, example_lines)
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
-- Global variable
eq(child.lua_get('type(_G.MiniMap)'), 'table')
-- Autocommand group
eq(child.fn.exists('#MiniMap'), 1)
-- Highlight groups
local has_highlight = function(group, value) expect.match(child.cmd_capture('hi ' .. group), value) end
has_highlight('MiniMapNormal', 'links to NormalFloat')
has_highlight('MiniMapSymbolCount', 'links to Special')
has_highlight('MiniMapSymbolLine', 'links to Title')
has_highlight('MiniMapSymbolView', 'links to Delimiter')
end
T['setup()']['creates `config` field'] = function()
eq(child.lua_get('type(_G.MiniMap.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniMap.config.' .. field), value) end
-- Check default values
expect_config('integrations', vim.NIL)
expect_config('symbols.encode', vim.NIL)
expect_config('symbols.scroll_line', '█')
expect_config('symbols.scroll_view', '┃')
expect_config('window.focusable', false)
expect_config('window.side', 'right')
expect_config('window.show_integration_count', true)
expect_config('window.width', 10)
expect_config('window.winblend', 25)
end
T['setup()']['respects `config` argument'] = function()
unload_module()
load_module({ window = { width = 1 } })
eq(child.lua_get('MiniMap.config.window.width'), 1)
end
T['setup()']['validates `config` argument'] = function()
unload_module()
local expect_config_error = function(config, name, target_type)
expect.error(load_module, vim.pesc(name) .. '.*' .. vim.pesc(target_type), config)
end
local expect_all_encode_symbols_check = function()
local expect_bad_config = function(err_pattern)
expect.error(function() child.lua([[MiniMap.setup(_G.bad_config)]]) end, err_pattern)
end
child.lua('_G.bad_config = { symbols = { encode = { resolution = { col = 2, row = 2 } } } }')
for i = 1, 4 do
expect_bad_config('symbols%.encode%[' .. i .. '%].*string')
child.lua(string.format('_G.bad_config.symbols.encode[%d] = "%d"', i, i))
end
end
expect_config_error('a', 'config', 'table')
expect_config_error({ integrations = 'a' }, 'integrations', 'array')
expect_config_error({ integrations = { 'a' } }, 'integrations', 'callable')
expect_config_error({ symbols = 'a' }, 'symbols', 'table')
expect_config_error({ symbols = { encode = 'a' } }, 'symbols.encode', 'table')
expect_config_error({ symbols = { encode = { resolution = 'a' } } }, 'symbols.encode.resolution', 'table')
expect_config_error(
{ symbols = { encode = { resolution = { col = 'a' } } } },
'symbols.encode.resolution.col',
'number'
)
expect_config_error(
{ symbols = { encode = { resolution = { col = 2, row = 'a' } } } },
'symbols.encode.resolution.row',
'number'
)
expect_all_encode_symbols_check()
expect_config_error({ symbols = { scroll_line = 1 } }, 'symbols.scroll_line', 'string')
expect_config_error({ symbols = { scroll_view = 1 } }, 'symbols.scroll_view', 'string')
expect_config_error({ window = 'a' }, 'window', 'table')
expect_config_error({ window = { focusable = 1 } }, 'window.focusable', 'boolean')
expect_config_error({ window = { side = 1 } }, 'window.side', 'one of')
expect_config_error({ window = { side = 'a' } }, 'window.side', 'one of')
expect_config_error({ window = { show_integration_count = 1 } }, 'window.show_integration_count', 'boolean')
expect_config_error({ window = { width = 'a' } }, 'window.width', 'number')
expect_config_error({ window = { winblend = 'a' } }, 'window.winblend', 'number')
end
local encode_strings = function(strings, opts)
local cmd = string.format('MiniMap.encode_strings(%s, %s)', vim.inspect(strings), vim.inspect(opts))
return child.lua_get(cmd)
end
T['encode_strings()'] = new_set()
T['encode_strings()']['works'] = function() eq(encode_strings({ 'aa', 'aa', 'aa' }), { '█' }) end
T['encode_strings()']['validates `strings` argument'] = function()
expect.error(encode_strings, 'array', 'a')
expect.error(encode_strings, 'strings', { 1, 'a' })
end
T['encode_strings()']['respects `strings` argument'] = function() eq(encode_strings({ 'aa' }), { '🬂' }) end
T['encode_strings()']['respects `opts.n_rows`'] = function()
local strings = tbl_repeat('aa', 3 * 3)
eq(encode_strings(strings), { '█', '█', '█' })
eq(encode_strings(strings, { n_rows = 1 }), { '█' })
-- Very big values are trimmed to minimum necessary needed
eq(encode_strings(strings, { n_rows = 1000 }), { '█', '█', '█' })
-- Rescaling should be done via "output is non-empty if at least one cell is
-- non-empty; empty if all empty"
eq(encode_strings({ 'a', ' ', ' ', ' ', ' ', 'a', 'a', 'a', ' ', ' ', ' ' }, { n_rows = 2 }), { '🬐', '🬀' })
end
T['encode_strings()']['respects `opts.n_cols`'] = function()
local strings = tbl_repeat('aaaaaa', 3)
eq(encode_strings(strings), { '███' })
eq(encode_strings(strings, { n_cols = 1 }), { '█' })
-- Very big values are trimmed to minimum necessary needed
eq(encode_strings(strings, { n_cols = 1000 }), { '███' })
-- Rescaling should be done via "output is non-empty if at least one cell is
-- non-empty; empty if all empty"
eq(encode_strings({ 'a a aa' }, { n_cols = 2 }), { '🬂🬁' })
end
T['encode_strings()']['respects `opts.symbols`'] = function()
source_test_encode_symbols()
eq(
child.lua_get([[MiniMap.encode_strings({ ' aa', 'a a' }, { symbols = _G.test_encode_symbols })]]),
{ '14', '23' }
)
end
T['encode_strings()']['works with empty strings'] =
function() eq(encode_strings({ 'aaaa', '', 'aaaa', '' }), { '🬰🬰', ' ' }) end
T['encode_strings()']['correctly computes default dimensions'] =
function() eq(encode_strings({ 'a', 'aa', 'aaa', 'aaaa', 'aaaaa', '' }), { '🬺🬏 ', '🬎🬎🬃' }) end
T['encode_strings()']['does not trim whitespace'] = function()
eq(encode_strings({ ' ' }), { ' ' })
eq(encode_strings({ 'aa ', 'aa ', 'aa ' }), { '█ ' })
end
T['encode_strings()']['works with multibyte strings'] = function()
eq(encode_strings({ 'ыыыыыы', 'ыыыы', 'ыы', 'aaaaaa', 'aaaa', 'aa' }), { '█🬎🬂', '█🬎🬂' })
end
T['encode_strings()']['correctly rescales in edge cases'] = function()
-- There were cases with more straightforward rescaling when certain middle
-- output row was not affected by any input row, leaving it empty. This was
-- because rescaling coefficient was more than 1.
local strings = tbl_repeat('aa', 37)
local ref_output = tbl_repeat('█', 12)
table.insert(ref_output, '🬂')
eq(encode_strings(strings), ref_output)
end
T['encode_strings()']['can work with input dimensions being not multiple of resolution'] = function()
eq(encode_strings({ 'a' }), { '🬀' })
eq(encode_strings({ 'aaa' }), { '🬂🬀' })
eq(encode_strings({ 'a', 'a' }), { '🬄' })
eq(encode_strings({ 'a', 'a', 'a', 'a' }), { '▌', '🬀' })
end
T['encode_strings()']['expands tabs'] = function()
eq(encode_strings({ '\taa' }), { ' 🬂' })
child.o.tabstop = 4
eq(encode_strings({ '\taa' }), { ' 🬂' })
end
T['open()'] = new_set({ hooks = { pre_case = function() child.set_size(30, 30) end } })
T['open()']['works'] = function()
set_lines(example_lines)
set_cursor(15, 0)
mock_test_integration()
map_open()
child.expect_screenshot()
end
T['open()']['sets important map buffer options'] = function()
local init_buf = child.api.nvim_get_current_buf()
map_open()
local all_bufs = child.api.nvim_list_bufs()
eq(#all_bufs, 2)
local map_buf_id = all_bufs[1] == init_buf and all_bufs[2] or all_bufs[1]
local validate_option = function(name, value) eq(child.api.nvim_buf_get_option(map_buf_id, name), value) end
validate_option('filetype', 'minimap')
validate_option('buftype', 'nofile')
validate_option('buflisted', false)
validate_option('swapfile', false)
end
T['open()']['sets important window options'] = function()
local init_win = child.api.nvim_get_current_win()
map_open()
local all_wins = child.api.nvim_tabpage_list_wins(0)
eq(#all_wins, 2)
local map_win_id = all_wins[1] == init_win and all_wins[2] or all_wins[1]
local validate_option = function(name, value) eq(child.api.nvim_win_get_option(map_win_id, name), value) end
validate_option('foldcolumn', '0')
validate_option('signcolumn', 'auto')
validate_option('wrap', false)
end
T['open()']['correctly computes window config'] = function()
child.set_size(30, 20)
map_open()
local win_id = get_map_win_id()
eq(child.api.nvim_win_get_config(win_id), {
anchor = 'NE',
col = 20,
external = false,
focusable = false,
height = 28,
relative = 'editor',
row = 0,
width = 10,
zindex = 10,
})
eq(child.api.nvim_win_get_option(win_id, 'winblend'), child.lua_get('MiniMap.config.window.winblend'))
end
T['open()']['respects `opts.integrations` argument'] = function()
set_lines(example_lines)
source_test_integration()
child.lua('MiniMap.open({ integrations = { _G.test_integration } })')
child.expect_screenshot()
-- Updates current data accordingly
eq(child.lua_get('MiniMap.current.opts.integrations[1] == _G.test_integration'), true)
end
T['open()']['respects `opts.symbols` argument'] = function()
set_lines(example_lines)
source_test_encode_symbols()
child.lua([[MiniMap.open({
symbols = { encode = _G.test_encode_symbols, scroll_line = '>', scroll_view = '+' },
})]])
child.expect_screenshot()
-- Can have empty strings as scrollbar characters, virtually disabling it
map_close()
map_open({ symbols = { scroll_line = '', scroll_view = '' } })
child.expect_screenshot()
end
T['open()']['allows more than single character in scroll symbols'] = function()
set_lines(example_lines)
source_test_encode_symbols()
map_open({ symbols = { scroll_line = '>|<', scroll_view = '||' } })
child.expect_screenshot()
end
T['open()']['respects `opts.window` argument'] = function()
set_lines(example_lines)
local opts =
{ window = { focusable = true, side = 'left', show_integration_count = false, width = 15, winblend = 50 } }
map_open(opts)
child.expect_screenshot()
eq(child.api.nvim_win_get_option(get_map_win_id(), 'winblend'), 50)
-- Map window should be focusable
child.cmd('wincmd w')
eq(child.api.nvim_get_current_win(), get_map_win_id())
-- Updates current data accordingly
eq(child.lua_get('MiniMap.current.opts.window'), opts.window)
end
T['open()']['respects `MiniMap.config`'] = function()
child.lua('MiniMap.config.window.width = 20')
map_open()
eq(child.api.nvim_win_get_width(get_map_win_id()), 20)
eq(child.lua_get('MiniMap.current.opts.window.width'), 20)
end
T['open()']['respects `MiniMap.current`'] = function()
child.lua([[MiniMap.current.opts = { window = { side = 'left' } }]])
map_open()
eq(get_map_win_side(), 'left')
end
T['open()']['correctly updates `MiniMap.current`'] = function()
map_open()
local current = get_current()
eq_keys(current, { 'buf_data', 'opts', 'win_data' })
eq_keys(current.buf_data, { 'map', 'source' })
eq(current.buf_data.source, child.api.nvim_get_current_buf())
eq(current.opts, child.lua_get('MiniMap.config'))
eq_keys(current.win_data, { child.api.nvim_get_current_tabpage() })
end
T['open()']['respects important options when computing window height'] = function()
local validate = function(options, row, height)
local default_opts = { showtabline = 1, laststatus = 2, cmdheight = 1 }
options = vim.tbl_deep_extend('force', default_opts, options)
for name, value in pairs(options) do
child.o[name] = value
end
map_open()
local config = child.api.nvim_win_get_config(get_map_win_id())
eq(config.row, row)
eq(config.height, height)
map_close()
for name, value in pairs(default_opts) do
child.o[name] = value
end
end
validate({ showtabline = 0, laststatus = 0, cmdheight = 1 }, 0, 29)
-- Tabline. Should make space for it if it is actually shown
validate({ showtabline = 2, laststatus = 0 }, 1, 28)
validate({ showtabline = 1, laststatus = 0 }, 0, 29)
child.cmd('tabedit')
validate({ showtabline = 2, laststatus = 0 }, 1, 28)
child.cmd('tabclose')
-- Statusline
validate({ showtabline = 0, laststatus = 1 }, 0, 28)
validate({ showtabline = 0, laststatus = 2 }, 0, 28)
if child.fn.has('nvim-0.8') == 1 then validate({ showtabline = 0, laststatus = 3 }, 0, 28) end
-- Command line
validate({ showtabline = 0, laststatus = 0, cmdheight = 4 }, 0, 26)
if child.fn.has('nvim-0.8') == 1 then validate({ showtabline = 0, laststatus = 0, cmdheight = 0 }, 0, 30) end
end
T['open()']['can be used with already opened window'] = function()
map_open()
local current = get_current()
expect.no_error(map_open)
eq(current, get_current())
end
T['open()']['can be used in multiple tabpages'] = function()
local init_tabpage = child.api.nvim_get_current_tabpage()
map_open()
child.cmd('tabedit')
local second_tabpage = child.api.nvim_get_current_tabpage()
-- Shouldn't be open in new tabpage
eq(get_map_win_id(), vim.NIL)
-- Should open window and register it as second opened
map_open()
eq(#get_current().win_data, 2)
eq(get_map_win_id() ~= nil, true)
-- Should be independently closed
child.api.nvim_set_current_tabpage(init_tabpage)
map_close()
eq(child.lua_get('vim.tbl_count(MiniMap.current.win_data)'), 1)
eq(get_map_win_id(), vim.NIL)
child.api.nvim_set_current_tabpage(second_tabpage)
eq(get_map_win_id() ~= nil, true)
end
T['open()']['can open pure scrollbar'] = function()
set_lines(example_lines)
set_cursor(15, 0)
child.cmd('normal! zz')
map_open({ window = { width = 1 } })
child.expect_screenshot()
end
T['open()']['works after previous window was closed manually'] = function()
map_open()
eq(#child.api.nvim_tabpage_list_wins(0), 2)
child.api.nvim_win_close(get_map_win_id(), true)
eq(#child.api.nvim_tabpage_list_wins(0), 1)
expect.no_error(map_open)
eq(#child.api.nvim_tabpage_list_wins(0), 2)
end
T['open()']['shows appropriate integration counts'] = function()
child.lua([[_G.integration_many_matches = function()
local res = {}
for i = 1, 11 do
for j = 1, i do
table.insert(res, { line = i, hl_group = 'Operator' })
end
end
return res
end]])
set_lines(example_lines)
child.lua([[MiniMap.open({
integrations = { _G.integration_many_matches },
symbols = { encode = { ' ', '█', resolution = { row = 1, col = 1 } } }
})]])
child.expect_screenshot()
end
T['open()']['respects `MiniMapNormal` highlight group'] = function()
set_lines(example_lines)
child.cmd('hi MiniMapNormal ctermfg=black')
map_open({ window = { winblend = 0 } })
-- Open separate floating window for comparison
local buf_id = child.api.nvim_create_buf(true, false)
child.api.nvim_buf_set_lines(buf_id, 0, -1, true, tbl_repeat('bbbbb', 10))
child.api.nvim_open_win(buf_id, false, {
relative = 'editor',
anchor = 'NW',
row = 0,
col = 0,
width = 5,
height = 10,
})
-- Highlighting of map and other floating window should differ
child.expect_screenshot()
end
T['open()']['respects `vim.{g,b}.minimap_disable`'] = new_set({
parametrize = { { 'g' }, { 'b' } },
}, {
test = function(var_type)
child[var_type].minimap_disable = true
map_open()
eq(#child.api.nvim_tabpage_list_wins(0), 1)
end,
})
T['refresh()'] =
new_set({ hooks = {
pre_case = function()
child.set_size(30, 30)
disable_map_updates()
end,
} })
T['refresh()']['works'] = function()
map_open()
set_lines(example_lines)
set_cursor(15, 0)
mock_test_integration()
child.expect_screenshot()
map_refresh()
child.expect_screenshot()
end
T['refresh()']['works without opened map'] = function() expect.no_error(map_refresh) end
T['refresh()']['respects `opts.integrations` argument'] = function()
set_lines(example_lines)
map_open()
source_test_integration()
child.lua('MiniMap.refresh({ integrations = { _G.test_integration } })')
child.expect_screenshot()
-- Updates current data accordingly
eq(child.lua_get('MiniMap.current.opts.integrations[1] == _G.test_integration'), true)
end
T['refresh()']['respects `opts.symbols` argument'] = function()
set_lines(example_lines)
source_test_encode_symbols()
map_open()
child.lua([[MiniMap.refresh({
symbols = { encode = _G.test_encode_symbols, scroll_line = '>', scroll_view = '+' },
})]])
child.expect_screenshot()
-- Updates current data accordingly
eq(child.lua_get('MiniMap.current.opts.symbols.scroll_line'), '>')
end
T['refresh()']['respects `opts.window` argument'] = function()
set_lines(example_lines)
map_open()
local opts =
{ window = { focusable = true, side = 'left', show_integration_count = false, width = 15, winblend = 50 } }
map_refresh(opts)
child.expect_screenshot()
eq(child.api.nvim_win_get_option(get_map_win_id(), 'winblend'), 50)
-- Updates current data accordingly
eq(child.lua_get('MiniMap.current.opts.window'), opts.window)
end
T['refresh()']['respects `parts` argument'] = function()
map_open()
child.expect_screenshot()
set_lines(tbl_repeat('aa aa aa', #example_lines))
set_cursor(15, 0)
mock_test_integration()
-- Nothing should have changed in map window
map_refresh({}, { integrations = false, lines = false, scrollbar = false })
child.expect_screenshot()
-- Only lines should have changed in map window
map_refresh({}, { integrations = false, lines = true, scrollbar = false })
child.expect_screenshot()
-- Only scrollbar should have changed in map window
set_lines(example_lines)
map_refresh({}, { integrations = false, lines = false, scrollbar = true })
child.expect_screenshot()
-- Only integration highlights should have changed in map window
set_cursor(1, 0)
map_refresh({}, { integrations = true, lines = false, scrollbar = false })
child.expect_screenshot()
end
T['refresh()']['is not affected by `MiniMap.config.window`'] = function()
child.lua('MiniMap.config.window.width = 10')
map_open()
eq(get_map_width(), 10)
child.lua('MiniMap.config.window.width = 20')
map_refresh()
eq(get_map_width(), 10)
end
T['refresh()']['updates `MiniMap.current`'] = function()
child.lua('MiniMap.config.window.width = 20')
map_open()
eq(child.api.nvim_win_get_width(get_map_win_id()), 20)
eq(child.lua_get('MiniMap.current.opts.window.width'), 20)
end
T['refresh()']['respects `MiniMap.current`'] = function()
-- Check that any interactive refresh keeps all windows with synced options
map_open()
child.lua([[MiniMap.current.opts.window.side = 'left']])
eq(get_map_win_side(), 'right')
map_refresh()
eq(get_map_win_side(), 'left')
end
T['refresh()']['respects `vim.{g,b}.minimap_disable`'] = new_set({
parametrize = { { 'g' }, { 'b' } },
}, {
test = function(var_type)
disable_map_updates()
map_open()
child[var_type].minimap_disable = true
set_lines(example_lines)
map_refresh()
-- Shouldn't show any map lines
child.expect_screenshot()
end,
})
T['close()'] = new_set()
T['close()']['works'] = function()
map_open()
eq(#child.api.nvim_tabpage_list_wins(0), 2)
map_close()
eq(#child.api.nvim_tabpage_list_wins(0), 1)
end
T['close()']['resets `MiniMap.current.opts` after closing last map window'] = function()
local is_current_opts_reset = function() return child.lua_get('vim.tbl_count(MiniMap.current.opts)') == 0 end
map_open()
local init_tabpage = child.api.nvim_get_current_tabpage()
child.cmd('tabedit')
local second_tabpage = child.api.nvim_get_current_tabpage()
map_open()
eq(#child.api.nvim_list_wins(), 4)
eq(is_current_opts_reset(), false)
child.api.nvim_set_current_tabpage(init_tabpage)
map_close()
eq(#child.api.nvim_list_wins(), 3)
eq(is_current_opts_reset(), false)
child.api.nvim_set_current_tabpage(second_tabpage)
map_close()
eq(#child.api.nvim_list_wins(), 2)
eq(is_current_opts_reset(), true)
end
T['close()']['does not error if window was closed manually'] = function()
map_open()
eq(#child.api.nvim_tabpage_list_wins(0), 2)
child.api.nvim_win_close(get_map_win_id(), true)
eq(#child.api.nvim_tabpage_list_wins(0), 1)
-- Should not error and make proper clean up
eq(#get_current().win_data, 1)
expect.no_error(map_close)
eq(#get_current().win_data, 0)
end
T['close()']['disrespects `vim.{g,b}.minimap_disable`'] = new_set({
parametrize = { { 'g' }, { 'b' } },
}, {
test = function(var_type)
map_open()
eq(get_n_shown_windows(), 2)
child[var_type].minimap_disable = true
map_close()
eq(get_n_shown_windows(), 1)
end,
})
T['toggle()'] = new_set()
T['toggle()']['works'] = function()
eq(get_n_shown_windows(), 1)
child.lua('MiniMap.toggle()')
eq(get_n_shown_windows(), 2)
eq(vim.tbl_count(get_current().win_data), 1)
child.lua('MiniMap.toggle()')
eq(get_n_shown_windows(), 1)
eq(vim.tbl_count(get_current().win_data), 0)
end
T['toggle_focus()'] = new_set()
T['toggle_focus()']['works'] = function()
set_lines(example_lines)
set_cursor(15, 10)
local init_win = child.api.nvim_get_current_win()
map_open()
eq(child.api.nvim_get_current_win(), init_win)
-- Should move focus to map window
child.lua('MiniMap.toggle_focus()')
eq(child.api.nvim_get_current_win(), get_map_win_id())
-- Moving cursor in map window should move cursor in previous window (line -
-- first one encoded by the current map line; column - first non-blank)
-- More tests in integration tests
set_cursor(1, 0)
child.lua('MiniMap.toggle_focus()')
eq(child.api.nvim_get_current_win(), init_win)
eq(get_cursor(), { 1, 2 })
end
T['toggle_focus()']['respects `use_previous_cursor`'] = function()
set_lines(example_lines)
set_cursor(15, 10)
map_open()
child.lua('MiniMap.toggle_focus()')
set_cursor(1, 0)
child.lua('MiniMap.toggle_focus(true)')
eq(get_cursor(), { 15, 10 })
end
T['toggle_focus()']['does not move to first non-blank in source if no movement in map'] = function()
set_lines(example_lines)
set_cursor(15, 10)
local init_win = child.api.nvim_get_current_win()
map_open()
child.lua('MiniMap.toggle_focus()')
eq(child.api.nvim_get_current_win(), get_map_win_id())
child.lua('MiniMap.toggle_focus()')
eq(child.api.nvim_get_current_win(), init_win)
eq(get_cursor(), { 15, 10 })
end
T['toggle_side()'] = new_set()
T['toggle_side()']['works'] = function()
-- Can be used without opened window
expect.no_error(function() child.lua('MiniMap.toggle_side()') end)
map_open()
eq(get_map_win_side(), 'right')
child.lua('MiniMap.toggle_side()')
eq(get_map_win_side(), 'left')
child.lua('MiniMap.toggle_side()')
eq(get_map_win_side(), 'right')
end
T['current'] = new_set()
T['current']['has initial value'] = function()
eq(get_current(), {
buf_data = {},
opts = {},
win_data = {},
})
end
T['current']['has correct `buf_data` structure'] = function()
local init_buf = child.api.nvim_get_current_buf()
map_open()
local all_bufs = child.api.nvim_list_bufs()
eq(#all_bufs, 2)
local map_buf_id = all_bufs[1] == init_buf and all_bufs[2] or all_bufs[1]
eq(child.lua_get('MiniMap.current.buf_data'), { source = init_buf, map = map_buf_id })
end
T['current']['has correct `opts` structure'] = function()
map_open()
eq(child.lua_get('vim.deep_equal(MiniMap.current.opts, MiniMap.config)'), true)
end
T['current']['has correct `win_data` structure'] = function()
map_open()
local init_tabpage = child.api.nvim_get_current_tabpage()
child.cmd('tabedit')
local second_tabpage = child.api.nvim_get_current_tabpage()
map_open()
local opts_win_data = child.lua_get('MiniMap.current.win_data')
eq_keys(opts_win_data, { init_tabpage, second_tabpage })
local buf_1 = child.api.nvim_win_get_buf(opts_win_data[init_tabpage])
eq(child.api.nvim_buf_get_option(buf_1, 'filetype'), 'minimap')
local buf_2 = child.api.nvim_win_get_buf(opts_win_data[init_tabpage])
eq(child.api.nvim_buf_get_option(buf_2, 'filetype'), 'minimap')
end
local validate_gen_encode_symbols = function(field, id)
child.lua(string.format([[_G.symbols = MiniMap.gen_encode_symbols['%s']('%s')]], field, id))
local cmd = string.format(
[[MiniMap.encode_strings(vim.fn.readfile('%s'), { symbols = _G.symbols })]],
get_resolution_test_file(id)
)
eq(child.lua_get(cmd), child.lua_get('{ table.concat(_G.symbols) }'))
end
T['gen_encode_symbols'] = new_set()
T['gen_encode_symbols']['can be used as `MiniMap.config.symbols.encode`'] = function()
child.set_size(30, 30)
unload_module()
child.lua([[_G.map = require('mini.map')]])
child.lua([[_G.map.setup({ symbols = { encode = _G.map.gen_encode_symbols.block('1x2') } })]])
set_lines(example_lines)
map_open()
child.expect_screenshot()
end
T['gen_encode_symbols']['can be used as part of `opts.symbols.encode`'] = function()
child.set_size(30, 30)
set_lines(example_lines)
child.lua([[MiniMap.open({ symbols = { encode = MiniMap.gen_encode_symbols.block('1x2') } })]])
child.expect_screenshot()
end
T['gen_encode_symbols']['block()'] = function()
validate_gen_encode_symbols('block', '1x2')
validate_gen_encode_symbols('block', '2x1')
validate_gen_encode_symbols('block', '2x2')
validate_gen_encode_symbols('block', '3x2')
end
T['gen_encode_symbols']['dot()'] = function()
validate_gen_encode_symbols('dot', '3x2')
validate_gen_encode_symbols('dot', '4x2')
end
T['gen_encode_symbols']['shade()'] = function()
validate_gen_encode_symbols('shade', '1x2')
validate_gen_encode_symbols('shade', '2x1')
end
local map_open_with_integration = function(integration)
local cmd = string.format('MiniMap.open({ integrations = { MiniMap.gen_integration.%s() } })', integration)
child.lua(cmd)
end
T['gen_integration'] = new_set({
hooks = {
pre_case = function()
child.set_size(30, 30)
set_lines(example_lines)
set_cursor(15, 0)
end,
},
})
T['gen_integration']['builtin_search()'] = new_set({
hooks = {
pre_case = function() end,
},
})
T['gen_integration']['builtin_search()']['works'] = function()
map_open_with_integration('builtin_search')
-- It should show counts for actual matches, not matched lines
type_keys('/', ' a', '<CR>')
child.expect_screenshot()
-- Should not affect cursor
local cur_pos = get_cursor()
map_refresh()
eq(get_cursor(), cur_pos)
-- Should respect 'hlsearch' option
child.o.hlsearch = false
map_refresh()
child.expect_screenshot()
end
T['gen_integration']['builtin_search()']['respects `hl_groups` argument'] = function()
map_open_with_integration('builtin_search')
child.lua([[MiniMap.config.integrations = {
MiniMap.gen_integration.builtin_search({ search = 'MiniMapSymbolLine' })
}]])
type_keys('/', ' a', '<CR>')
local output = child.lua_get('MiniMap.config.integrations[1]()')
for _, v in ipairs(output) do
eq(v.hl_group, 'MiniMapSymbolLine')
end