Repository navigation
Expand file tree
/
Copy pathtest_visits.lua
More file actions
2314 lines (1890 loc) · 75.9 KB
/
Copy pathtest_visits.lua
File metadata and controls
2314 lines (1890 loc) · 75.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<
10BC8
/div>
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
local load_module = function(config) child.mini_load('visits', config) end
local unload_module = function() child.mini_unload('visits') end
local type_keys = function(...) return child.type_keys(...) end
local sleep = function(ms) helpers.sleep(ms, child, true) end
local forward_lua = function(fun_str) return helpers.forward_lua(child, fun_str) end
local edit = function(path) child.cmd('edit ' .. child.fn.fnameescape(path)) end
local child_time = function() return child.lua_get('os.time()') end
-- Test paths helpers
local join_path = function(...) return table.concat({ ... }, '/') end
local full_path = function(x) return (child.fn.fnamemodify(x, ':p'):gsub('\\', '/'):gsub('/+', '/'):gsub('(.)/$', '%1')) end
local test_dir = 'tests/dir-visits'
local test_dir_absolute = vim.fn.fnamemodify(test_dir, ':p'):gsub('\\', '/'):gsub('(.)/$', '%1')
local xdg_data_home = test_dir_absolute
local data_std_path = xdg_data_home .. (helpers.is_windows() and '/nvim-data' or '/nvim')
local make_testpath = function(...) return join_path(test_dir_absolute, ...) end
local make_temppath = function(name)
local temp_dir = make_testpath('temp')
vim.fn.mkdir(temp_dir, 'p')
MiniTest.finally(function() vim.fn.delete(temp_dir, 'rf') end)
return join_path(temp_dir, name)
end
local cleanup = function()
-- Remove possibly written index file as it affects multiple test cases
local default_index_path = data_std_path .. '/mini-visits-index'
if vim.fn.filereadable(default_index_path) == 0 then return end
vim.fn.delete(default_index_path)
-- Remove mocked `data` directory
vim.fn.delete(data_std_path, 'rf')
-- Remove temporary directory
vim.fn.delete(make_testpath('temp'), 'rf')
end
-- Common test wrappers
local get_index = forward_lua('MiniVisits.get_index')
local set_index = forward_lua('MiniVisits.set_index')
local getcwd = function() return (child.fn.getcwd():gsub('\\', '/')) end
local new_uri_scratch_buf = function()
local buf_id = child.api.nvim_create_buf(false, true)
child.api.nvim_buf_set_name(buf_id, 'xxx://hello/world')
return buf_id
end
-- Common test helpers
local validate_buf_name = function(buf_id, ref_name)
buf_id = buf_id or child.api.nvim_get_current_buf()
local name = child.api.nvim_buf_get_name(buf_id):gsub('\\', '/'):gsub('/+$', '')
ref_name = ref_name ~= '' and full_path(ref_name) or ''
ref_name = ref_name:gsub('\\', '/'):gsub('/+$', '')
eq(name, ref_name)
end
local validate_partial_equal = function(test_tbl, ref_tbl)
eq(type(test_tbl), 'table')
local test_with_ref_keys = {}
for key, _ in pairs(ref_tbl) do
test_with_ref_keys[key] = test_tbl[key]
end
eq(test_with_ref_keys, ref_tbl)
end
local validate_index_entry = function(cwd, path, ref)
local index = child.lua_get('MiniVisits.get_index()')
local out = (index[full_path(cwd)] or {})[full_path(path)]
if ref == nil then
eq(out, nil)
else
validate_partial_equal(out, ref)
end
end
local make_ref_index_full = function(ref_index)
local ref_full = {}
for cwd, cwd_tbl in pairs(ref_index) do
local cwd_tbl_full = {}
for path, path_tbl in pairs(cwd_tbl) do
cwd_tbl_full[make_testpath(path)] = path_tbl
end
ref_full[make_testpath(cwd)] = cwd_tbl_full
end
return ref_full
end
local set_index_from_ref = function(ref) set_index(make_ref_index_full(ref)) end
-- Common mocks
local mock_ui_select = function(choice_index)
local lua_cmd = string.format(
[[
_G.ui_select_log = {}
vim.ui.select = function(items, opts, on_choice)
table.insert(_G.ui_select_log, { items = items, prompt = opts.prompt })
on_choice(items[%s], %s)
end]],
choice_index,
choice_index
)
child.lua(lua_cmd)
end
local mock_ui_input = function(input)
child.lua('_G.mock_input = ' .. vim.inspect(input))
child.lua([[
_G.ui_input_orig = _G.ui_input_orig or vim.ui.input
_G.ui_input_log = {}
vim.ui.input = function(opts, on_confirm)
table.insert(_G.ui_input_log, opts.prompt)
on_confirm(_G.mock_input)
end
]])
end
local get_ui_select_log = function() return child.lua_get('_G.ui_select_log') end
-- Time constants
local default_track_delay = 1000
local small_time = helpers.get_time_const(10)
local test_track_delay = 2 * small_time
-- Output test set ============================================================
local T = new_set({
hooks = {
pre_case = function()
pcall(child.lua, 'MiniVisits.config.autowrite = false')
child.setup()
cleanup()
-- Make `stdpath('data')` point to test directory
local lua_cmd = string.format([[vim.loop.os_setenv('XDG_DATA_HOME', %s)]], vim.inspect(xdg_data_home))
child.lua(lua_cmd)
-- Load module
load_module()
-- Make more comfortable screenshots
child.set_size(5, 60)
child.o.laststatus = 0
end,
post_once = function()
pcall(child.lua, 'MiniVisits.config.autowrite = false')
child.stop()
cleanup()
end,
},
n_retry = helpers.get_n_retry(2),
})
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
-- Global variable
eq(child.lua_get('type(_G.MiniVisits)'), 'table')
-- Autocommand group
eq(child.fn.exists('#MiniVisits'), 1)
end
T['setup()']['creates `config` field'] = function()
eq(child.lua_get('type(_G.MiniVisits.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniVisits.config.' .. field), value) end
expect_config('list.filter', vim.NIL)
expect_config('list.filter', vim.NIL)
expect_config('silent', false)
expect_config('store.autowrite', true)
expect_config('store.normalize', vim.NIL)
expect_config('store.path', child.fn.stdpath('data') .. '/mini-visits-index')
expect_config('track.event', 'BufEnter')
expect_config('track.delay', 1000)
end
T['setup()']['respects `config` argument'] = function()
unload_module()
load_module({ silent = true })
eq(child.lua_get('MiniVisits.config.silent'), true)
end
T['setup()']['validates `config` argument'] = function()
unload_module()
local expect_config_error = function(config, name, target_type)
expect.error(function() load_module(config) end, vim.pesc(name) .. '.*' .. vim.pesc(target_type))
end
expect_config_error('a', 'config', 'table')
expect_config_error({ list = 'a' }, 'list', 'table')
expect_config_error({ list = { filter = 'a' } }, 'list.filter', 'function')
expect_config_error({ list = { sort = 'a' } }, 'list.sort', 'function')
expect_config_error({ silent = 'a' }, 'silent', 'boolean')
expect_config_error({ store = 'a' }, 'store', 'table')
expect_config_error({ store = { autowrite = 'a' } }, 'store.autowrite', 'boolean')
expect_config_error({ store = { normalize = 'a' } }, 'store.normalize', 'function')
expect_config_error({ store = { path = 1 } }, 'store.path', 'string')
expect_config_error({ track = 'a' }, 'track', 'table')
expect_config_error({ track = { event = 1 } }, 'track.event', 'string')
expect_config_error({ track = { delay = 'a' } }, 'track.delay', 'number')
end
T['register_visit()'] = new_set()
local register_visit = forward_lua('MiniVisits.register_visit')
T['register_visit()']['works'] = function()
-- Should not check if arguments represent present paths on disk
local file_full, file_2_full = full_path('file'), full_path('dir/file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
-- Should create entry if it is not present treating input as file system
-- entries (relative to current directory in this case)
eq(get_index(), {})
register_visit('file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 1, latest = child_time() } } })
register_visit('file', 'dir')
local latest_1 = child_time()
eq(get_index(), { [dir_full] = { [file_full] = { count = 2, latest = latest_1 } } })
register_visit('dir/file-2', 'dir')
local latest_2 = child_time()
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 2, latest = latest_1 },
[file_2_full] = { count = 1, latest = latest_2 },
},
})
register_visit('file', 'dir-2')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 2, latest = latest_1 },
[file_2_full] = { count = 1, latest = latest_2 },
},
[dir_2_full] = {
[file_full] = { count = 1, latest = child_time() },
},
})
end
T['register_visit()']['uses current data as defaults'] = function()
local path = make_testpath('file')
edit(path)
register_visit()
eq(get_index(), { [getcwd()] = { [path] = { count = 1, latest = child_time() } } })
-- Errors on non-normal buffer
child.api.nvim_set_current_buf(new_uri_scratch_buf())
expect.error(register_visit, 'not for a regular file')
end
T['register_visit()']['handles paths with "~" for home directory'] = function()
register_visit('~/file', '~/dir')
local home_dir = child.loop.os_homedir():gsub('\\', '/')
eq(
get_index(),
{ [join_path(home_dir, 'dir')] = { [join_path(home_dir, 'file')] = { count = 1, latest = child_time() } } }
)
end
T['register_visit()']['does not affect other stored data'] = function()
local path, cwd = make_testpath('file'), test_dir_absolute
set_index({ [cwd] = { [path] = { count = 0, latest = 0, aaa = { bbb = true } } } })
register_visit(path, cwd)
eq(get_index(), { [cwd] = { [path] = { count = 1, latest = child_time(), aaa = { bbb = true } } } })
end
T['register_visit()']['validates arguments'] = function()
local validate = function(error_pattern, ...)
local args = { ... }
expect.error(function() register_visit(unpack(args)) end, error_pattern)
end
validate('`path`.*string', 1, 'dir')
validate('`cwd`.*string', 'file', 1)
validate('`path` and `cwd`.*not.*empty', '', 'dir')
validate('`path` and `cwd`.*not.*empty', 'file', '')
end
T['add_path()'] = new_set()
local add_path = forward_lua('MiniVisits.add_path')
T['add_path()']['works'] = function()
-- Should not check if arguments represent present paths on disk
local dir_full = full_path('dir')
local file_full, file_2_full = full_path('file'), full_path('file-2')
add_path('file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, latest = 0 } } })
-- Should do nothing if path-cwd already exists
add_path('file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, latest = 0 } } })
add_path('file-2', 'dir')
eq(
get_index(),
{ [dir_full] = { [file_full] = { count = 0, latest = 0 }, [file_2_full] = { count = 0, latest = 0 } } }
)
end
T['add_path()']['works with empty string arguments'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
local init_tbl = { count = 0, latest = 0 }
-- If no visits, should result into no added paths
add_path('', 'dir')
eq(get_index(), {})
add_path('file', '')
eq(get_index(), {})
add_path('', '')
eq(get_index(), {})
-- Empty string for `path` should mean "add all present paths in cwd".
-- Not useful, but should be allowed for consistency with other functions.
add_path('file', 'dir')
add_path('', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = init_tbl } })
-- Empty string for `cwd` should mean "add path to all visited cwds".
add_path('file', 'dir-2')
add_path('file-2', '')
eq(get_index(), {
[dir_full] = { [file_full] = init_tbl, [file_2_full] = init_tbl },
[dir_2_full] = { [file_full] = init_tbl, [file_2_full] = init_tbl },
})
end
T['add_path()']['uses current data as defaults'] = function()
local path = make_testpath('file')
edit(path)
add_path()
eq(get_index(), { [getcwd()] = { [path] = { count = 0, latest = 0 } } })
-- Errors on non-normal buffer
child.api.nvim_set_current_buf(new_uri_scratch_buf())
expect.error(add_path, 'not for a regular file')
end
T['add_path()']['does not affect other stored data'] = function()
local path, cwd = make_testpath('file'), test_dir_absolute
set_index({ [cwd] = { [path] = { count = 0, latest = 0, aaa = { bbb = true } } } })
add_path(path, cwd)
eq(get_index(), { [cwd] = { [path] = { count = 0, latest = 0, aaa = { bbb = true } } } })
end
T['add_path()']['validates arguments'] = function()
expect.error(function() add_path(1, 'dir') end, '`path`.*string')
expect.error(function() add_path('file', 1) end, '`cwd`.*string')
end
T['add_label()'] = new_set()
local add_label = forward_lua('MiniVisits.add_label')
T['add_label()']['works'] = function()
-- Should not check if arguments represent present paths on disk
local file_full, dir_full = full_path('file'), full_path('dir')
-- Should add path if it is not present
add_label('aaa', 'file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, labels = { aaa = true }, latest = 0 } } })
-- Should show feedback message
child.expect_screenshot()
-- Should add to already existing path-cwd pair
add_label('bbb', 'file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, labels = { aaa = true, bbb = true }, latest = 0 } } })
-- Should not affect already present data
register_visit('file', 'dir')
add_label('ccc', 'file', 'dir')
eq(get_index(), {
[dir_full] = { [file_full] = { count = 1, labels = { aaa = true, bbb = true, ccc = true }, latest = child_time() } },
})
end
T['add_label()']['works with empty string arguments'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
-- If no visits, should result into no added paths and labels
add_path('', 'dir')
eq(get_index(), {})
add_path('file', '')
eq(get_index(), {})
add_path('', '')
eq(get_index(), {})
-- Empty string for `path` should mean "add to all present paths in cwd"
add_path('file', 'dir')
add_path('file-2', 'dir')
add_label('aaa', '', 'dir')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, labels = { aaa = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { aaa = true }, latest = 0 },
},
})
-- Empty string for `cwd` should mean "add to path in all present cwds"
add_path('file', 'dir-2')
add_label('bbb', 'file', '')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, labels = { aaa = true, bbb = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { aaa = true }, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, labels = { bbb = true }, latest = 0 },
},
})
-- Both empty strings should mean "add to all present paths in present cwds"
add_label('ccc', '', '')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, labels = { aaa = true, bbb = true, ccc = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { aaa = true, ccc = true }, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, labels = { bbb = true, ccc = true }, latest = 0 },
},
})
end
T['add_label()']['uses current data as defaults for path and cwd'] = function()
local path = make_testpath('file')
edit(path)
add_label('aaa')
eq(get_index(), { [getcwd()] = { [path] = { count = 0, labels = { aaa = true }, latest = 0 } } })
-- Errors on non-normal buffer
child.api.nvim_set_current_buf(new_uri_scratch_buf())
expect.error(function() add_label('aaa') end, 'not for a regular file')
end
T['add_label()']['asks user for label if it is not supplied'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
-- Should use `vim.ui.input`
mock_ui_input('aaa')
child.l
10BC8
ua_notify('MiniVisits.add_label(nil, "file", "dir")')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, labels = { aaa = true }, latest = 0 } } })
eq(child.lua_get('_G.ui_input_log'), { '(mini.visits) Enter label to add ' })
-- Has completion with all labels from target cwd
add_label('abb', 'file-2', 'dir')
add_label('bbb', 'file', 'dir')
add_label('ccc', 'file', 'dir-2')
add_label('ddd', 'file-2', 'dir-2')
-- - Unmock to fully test completion
child.lua('vim.ui.input = _G.ui_input_orig')
child.lua_notify('MiniVisits.add_label(nil, "file", "dir")')
type_keys('<Tab>')
child.expect_screenshot()
-- - Should properly filter it
type_keys('<C-e>', 'a', '<Tab>')
child.expect_screenshot()
-- - Can be canceled without adding any label
type_keys('<C-c>')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, labels = { aaa = true, bbb = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { abb = true }, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, labels = { ccc = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { ddd = true }, latest = 0 },
},
})
end
T['add_label()']['does not affect other stored data'] = function()
local path, cwd = make_testpath('file'), test_dir_absolute
set_index({ [cwd] = { [path] = { count = 0, latest = 0, aaa = { bbb = true } } } })
add_label('xxx', path, cwd)
eq(get_index(), { [cwd] = { [path] = { count = 0, labels = { xxx = true }, latest = 0, aaa = { bbb = true } } } })
end
T['add_label()']['validates arguments'] = function()
expect.error(function() add_label(1, 'file', 'dir') end, '`label`.*string')
expect.error(function() add_label('aaa', 1, 'dir') end, '`path`.*string')
expect.error(function() add_label('aaa', 'file', 1) end, '`cwd`.*string')
end
T['remove_path()'] = new_set()
local remove_path = forward_lua('MiniVisits.remove_path')
T['remove_path()']['works'] = function()
-- Should not check if arguments represent present paths on disk
local dir_full = full_path('dir')
local file_full, file_2_full = full_path('file'), full_path('file-2')
add_path('file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, latest = 0 } } })
remove_path('file', 'dir')
eq(get_index(), {})
-- Should do nothing if path-cwd already absent
remove_path('file', 'dir')
eq(get_index(), {})
end
T['remove_path()']['works with empty string arguments'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
local init_tbl = { count = 0, latest = 0 }
-- If no visits, should result into no errors
remove_path('', 'dir')
eq(get_index(), {})
remove_path('file', '')
eq(get_index(), {})
remove_path('', '')
eq(get_index(), {})
-- Empty string for `path` should mean "remove all present paths in cwd".
add_path('file', 'dir')
add_path('file-2', 'dir')
add_path('file', 'dir-2')
remove_path('', 'dir')
eq(get_index(), { [dir_2_full] = { [file_full] = init_tbl } })
-- Empty string for `cwd` should mean "remove path from all visited cwds".
add_path('file', 'dir')
add_path('file-2', 'dir')
remove_path('file', '')
eq(get_index(), { [dir_full] = { [file_2_full] = init_tbl } })
-- Both empty strings should essentially mean "remove all present"
add_path('file', 'dir')
add_path('file-2', 'dir')
remove_path('', '')
eq(get_index(), {})
end
T['remove_path()']['uses current data as defaults'] = function()
local path = make_testpath('file')
edit(path)
add_path()
eq(get_index(), { [getcwd()] = { [path] = { count = 0, latest = 0 } } })
remove_path()
eq(get_index(), {})
-- Errors on non-normal buffer
child.api.nvim_set_current_buf(new_uri_scratch_buf())
expect.error(remove_path, 'not for a regular file')
end
T['remove_path()']['validates arguments'] = function()
expect.error(function() remove_path(1, 'dir') end, '`path`.*string')
expect.error(function() remove_path('file', 1) end, '`cwd`.*string')
end
T['remove_label()'] = new_set()
local remove_label = forward_lua('MiniVisits.remove_label')
T['remove_label()']['works'] = function()
-- Should not check if arguments represent present paths on disk
local file_full, dir_full = full_path('file'), full_path('dir')
add_label('aaa', 'file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, labels = { aaa = true }, latest = 0 } } })
remove_label('aaa', 'file', 'dir')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, latest = 0 } } })
-- Should show feedback message
child.expect_screenshot()
-- Should not affect already present data
register_visit('file', 'dir')
add_label('bbb', 'file', 'dir')
add_label('ccc', 'file', 'dir')
remove_label('bbb', 'file', 'dir')
eq(get_index(), {
[dir_full] = { [file_full] = { count = 1, labels = { ccc = true }, latest = child_time() } },
})
end
T['remove_label()']['works with empty string arguments'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
-- If no visits, should result into no error
remove_path('', 'dir')
eq(get_index(), {})
remove_path('file', '')
eq(get_index(), {})
remove_path('', '')
eq(get_index(), {})
-- Empty string for `path` should mean "remove from all present paths in cwd"
add_label('aaa', 'file', 'dir')
add_label('aaa', 'file-2', 'dir')
add_label('aaa', 'file', 'dir-2')
remove_label('aaa', '', 'dir')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, latest = 0 },
[file_2_full] = { count = 0, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, labels = { aaa = true }, latest = 0 },
},
})
-- Empty string for `cwd` should mean "remove from path in all present cwds"
add_label('aaa', 'file', 'dir')
add_label('aaa', 'file-2', 'dir')
remove_label('aaa', 'file', '')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, latest = 0 },
[file_2_full] = { count = 0, labels = { aaa = true }, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, latest = 0 },
},
})
-- Both empty should mean "remove from all present paths in present cwds"
add_label('aaa', 'file', 'dir')
add_label('aaa', 'file', 'dir-2')
remove_label('aaa', '', '')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, latest = 0 },
[file_2_full] = { count = 0, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, latest = 0 },
},
})
end
T['remove_label()']['uses current data as defaults for path and cwd'] = function()
local path = make_testpath('file')
edit(path)
add_label('aaa')
eq(get_index(), { [getcwd()] = { [path] = { count = 0, labels = { aaa = true }, latest = 0 } } })
remove_label('aaa')
eq(get_index(), { [getcwd()] = { [path] = { count = 0, latest = 0 } } })
-- Errors on non-normal buffer
child.api.nvim_set_current_buf(new_uri_scratch_buf())
expect.error(function() remove_label('aaa') end, 'not for a regular file')
end
T['remove_label()']['asks user for label if it is not supplied'] = function()
local file_full, file_2_full = full_path('file'), full_path('file-2')
local dir_full, dir_2_full = full_path('dir'), full_path('dir-2')
add_label('aaa', 'file', 'dir')
add_label('bbb', 'file', 'dir')
mock_ui_input('aaa')
child.lua_notify('MiniVisits.remove_label(nil, "file", "dir")')
eq(get_index(), { [dir_full] = { [file_full] = { count = 0, labels = { bbb = true }, latest = 0 } } })
eq(child.lua_get('_G.ui_input_log'), { '(mini.visits) Enter label to remove ' })
-- Has completion with all labels from target path-cwd pair
add_label('aaa', 'file', 'dir')
add_label('abb', 'file', 'dir')
add_label('bbb', 'file', 'dir')
add_label('ccc', 'file-2', 'dir')
add_label('ddd', 'file', 'dir-2')
-- - Unmock to fully test completion
child.lua('vim.ui.input = _G.ui_input_orig')
child.lua_notify('MiniVisits.remove_label(nil, "file", "dir")')
type_keys('<Tab>')
child.expect_screenshot()
-- - Should properly filter it
type_keys('<C-e>', 'a', '<Tab>')
child.expect_screenshot()
-- - Can be canceled without adding any label
type_keys('<C-c>')
eq(get_index(), {
[dir_full] = {
[file_full] = { count = 0, labels = { aaa = true, abb = true, bbb = true }, latest = 0 },
[file_2_full] = { count = 0, labels = { ccc = true }, latest = 0 },
},
[dir_2_full] = {
[file_full] = { count = 0, labels = { ddd = true }, latest = 0 },
},
})
end
T['remove_label()']['does not affect other stored data'] = function()
local path, cwd = make_testpath('file'), test_dir_absolute
set_index({ [cwd] = { [path] = { count = 0, labels = { xxx = true }, latest = 0, aaa = { bbb = true } } } })
remove_label('xxx', path, cwd)
eq(get_index(), { [cwd] = { [path] = { count = 0, latest = 0, aaa = { bbb = true } } } })
end
T['remove_label()']['validates arguments'] = function()
expect.error(function() remove_label(1, 'file', 'dir') end, '`label`.*string')
expect.error(function() remove_label('aaa', 1, 'dir') end, '`path`.*string')
expect.error(function() remove_label('aaa', 'file', 1) end, '`cwd`.*string')
end
T['list_paths()'] = new_set()
local list_paths = forward_lua('MiniVisits.list_paths')
T['list_paths()']['works'] = function()
local cur_time = child_time()
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 2, latest = cur_time - 3 },
['dir_1/file_1-2'] = { count = 1, latest = cur_time - 4 },
},
dir_2 = {
['dir_2/file_2-1'] = { count = 3, latest = cur_time - 2 },
['dir_1/file_1-2'] = { count = 4, latest = cur_time - 1 },
},
}
set_index_from_ref(ref_index)
local validate_dir_1 = function(cwd)
eq(list_paths(cwd), { make_testpath('dir_1', 'file_1-1'), make_testpath('dir_1', 'file_1-2') })
end
-- Should work with relative path `cwd`
local rel_cwd = join_path(test_dir, 'dir_1')
validate_dir_1(rel_cwd)
-- Should work with absolute path `cwd`
local abs_cwd = full_path(rel_cwd)
validate_dir_1(abs_cwd)
-- Should use current working directory by default
child.fn.chdir(make_testpath('dir_1'))
validate_dir_1(nil)
-- Should work with empty string `cwd` meaning "all visited cwds"
eq(
list_paths(''),
{ make_testpath('dir_1', 'file_1-2'), make_testpath('dir_2', 'file_2-1'), make_testpath('dir_1', 'file_1-1') }
)
-- Should not affect the index
eq(get_index(), make_ref_index_full(ref_index))
end
T['list_paths()']['respects `opts.filter`'] = function()
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 2, latest = 10 },
['dir_1/file_1-2'] = { count = 1, latest = 9 },
},
}
set_index_from_ref(ref_index)
child.lua([[
_G.filter_args_log = {}
_G.filter = function(...)
table.insert(_G.filter_args_log, { ... })
local path_data = ({ ... })[1]
return path_data.count > 1
end]])
child.lua('_G.cwd = ' .. vim.inspect(make_testpath('dir_1')))
eq(child.lua_get([[MiniVisits.list_paths(cwd, { filter = _G.filter })]]), { make_testpath('dir_1', 'file_1-1') })
local args_log = child.lua_get('_G.filter_args_log')
-- - Ensure same order in test, as there is no guarantee of order
table.sort(args_log, function(a, b) return a[1].path < b[1].path end)
eq(args_log, {
{ { count = 2, latest = 10, path = make_testpath('dir_1', 'file_1-1') } },
{ { count = 1, latest = 9, path = make_testpath('dir_1', 'file_1-2') } },
})
end
T['list_paths()']['respects `opts.sort`'] = function()
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 2, latest = 10 },
['dir_1/file_1-2'] = { count = 1, latest = 9 },
},
}
set_index_from_ref(ref_index)
child.lua([[
_G.sort_args_log = {}
_G.sort = function(...)
table.insert(_G.sort_args_log, { ... })
local path_data_arr = vim.deepcopy(({ ... })[1])
table.sort(path_data_arr, function(a, b) return a.count < b.count end)
return path_data_arr
end]])
child.lua('_G.cwd = ' .. vim.inspect(make_testpath('dir_1')))
eq(
child.lua_get([[MiniVisits.list_paths(_G.cwd, { sort = _G.sort })]]),
{ make_testpath('dir_1', 'file_1-2'), make_testpath('dir_1', 'file_1-1') }
)
local args_log = child.lua_get('_G.sort_args_log')
eq(vim.tbl_count(args_log), 1)
local path_data_arr = args_log[1][1]
-- - Ensure same order in test, as there is no guarantee of order
table.sort(path_data_arr, function(a, b) return a.path < b.path end)
eq(path_data_arr, {
{ count = 2, latest = 10, path = make_testpath('dir_1', 'file_1-1') },
{ count = 1, latest = 9, path = make_testpath('dir_1', 'file_1-2') },
})
end
T['list_paths()']['allows `opts.sort` to return non-related array'] = function()
child.lua([[_G.sort = function() return { { path = 'bb' }, { path = 'aa' } } end]])
eq(child.lua_get([[MiniVisits.list_paths('', { sort = _G.sort })]]), { 'bb', 'aa' })
end
T['list_paths()']['properly merges visit data for filter and sort'] = function()
--stylua: ignore
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 2, latest = 10, aaa = 1 },
['dir_1/file_1-2'] = { count = 1, latest = 9, bbb = 'a' },
['file'] = { count = 1, latest = 8, data = { bool = true, num = 2, str = 'b' } },
},
dir_2 = {
['dir_1/file_1-1'] = { count = 11, latest = 100, ccc = 3 },
['file'] = { count = 10, latest = 1, data = { bool2 = true, num2 = 2, str2 = 'b' } },
}
}
set_index_from_ref(ref_index)
child.lua([[
_G.filter_args_log, _G.sort_args_log = {}, {}
_G.filter = function(...)
table.insert(_G.filter_args_log, { ... })
return true
end
_G.sort = function(...)
table.insert(_G.sort_args_log, { ... })
return ({...})[1]
end]])
child.lua_get([[MiniVisits.list_paths('', { filter = _G.filter, sort = _G.sort })]])
-- Filter
local filter_args_log = child.lua_get('_G.filter_args_log')
-- - Ensure same order in test, as there is no guarantee of order
table.sort(filter_args_log, function(a, b) return a[1].path < b[1].path end)
eq(filter_args_log, {
-- `count` should be summed, `latest` should be reduced with `math.max`,
-- non-related fields should be merged
{ { count = 13, latest = 100, path = make_testpath('dir_1', 'file_1-1'), aaa = 1, ccc = 3 } },
{ { count = 1, latest = 9, path = make_testpath('dir_1', 'file_1-2'), bbb = 'a' } },
{
{
count = 11,
latest = 8,
path = make_testpath('file'),
data = { bool = true, bool2 = true, num = 2, num2 = 2, str = 'b', str2 = 'b' },
},
},
})
-- Sort
local sort_args_log = child.lua_get('_G.sort_args_log')
eq(vim.tbl_count(sort_args_log), 1)
local path_data_arr = sort_args_log[1][1]
-- - Ensure same order in test, as there is no guarantee of order
table.sort(path_data_arr, function(a, b) return a.path < b.path end)
eq(path_data_arr, {
{ count = 13, latest = 100, path = make_testpath('dir_1', 'file_1-1'), aaa = 1, ccc = 3 },
{ count = 1, latest = 9, path = make_testpath('dir_1', 'file_1-2'), bbb = 'a' },
{
count = 11,
latest = 8,
path = make_testpath('file'),
data = { bool = true, bool2 = true, num = 2, num2 = 2, str = 'b', str2 = 'b' },
},
})
end
T['list_paths()']['respects `config.list`'] = function()
set_index_from_ref({ dir_1 = { file = { count = 1, latest = 10 } } })
child.lua([[
_G.filter = function()
_G.filter_been_here = true
return true
end
_G.sort = function(...)
_G.sort_been_here = true
return ({...})[1]
end]])
child.lua('MiniVisits.config.list = { filter = _G.filter, sort = _G.sort }')
list_paths('')
eq(child.lua_get('_G.filter_been_here'), true)
eq(child.lua_get('_G.sort_been_here'), true)
end
T['list_paths()']['validates arguments'] = function()
expect.error(function() list_paths(1) end, '`cwd`.*string')
end
T['list_labels()'] = new_set()
local list_labels = forward_lua('MiniVisits.list_labels')
T['list_labels()']['works'] = function()
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 0, labels = { xxx = true, bbb = true }, latest = 0 },
['dir_1/file_1-2'] = { count = 0, labels = { xxx = true, aaa = true }, latest = 0 },
},
dir_2 = {
['dir_2/file_2-1'] = { count = 0, latest = 0 },
['dir_1/file_1-2'] = { count = 0, labels = { xxx = true, yyy = true }, latest = 0 },
},
}
set_index_from_ref(ref_index)
-- Should first sort by decreasing frequency of appearance for different
-- paths. Ties should be resolved alphabetically.
-- Should work with relative paths
local rel_path = join_path(test_dir, 'dir_1', 'file_1-1')
local rel_cwd = join_path(test_dir, 'dir_1')
eq(list_labels(rel_path, rel_cwd), { 'bbb', 'xxx' })
-- Should work with absolute paths
local abs_path = full_path(rel_path)
local abs_cwd = full_path(rel_cwd)
eq(list_labels(abs_path, abs_cwd), { 'bbb', 'xxx' })
-- Should use current file and directory as defaults
child.fn.chdir(make_testpath('dir_1'))
edit('file_1-1')
eq(list_labels(), { 'bbb', 'xxx' })
-- Should work with empty string `path` meaning "all paths in target cwd"
eq(list_labels('', abs_cwd), { 'xxx', 'aaa', 'bbb' })
-- Should work with empty string `cwd` meaning "path in all cwds"
-- - NOTE: Although `xxx` label happens twice, it is still counted as one
-- because it is for the same path.
eq(list_labels(make_testpath('dir_1', 'file_1-2'), ''), { 'aaa', 'xxx', 'yyy' })
-- Should work with both empty strings meaning "all visits"
eq(list_labels('', ''), { 'xxx', 'aaa', 'bbb', 'yyy' })
-- Should work with empty string `cwd` meaning "all visited cwds"
eq(get_index(), make_ref_index_full(ref_index))
end
T['list_labels()']['respects `opts.filter`'] = function()
local ref_index = {
dir_1 = {
['dir_1/file_1-1'] = { count = 2, labels = { xxx = true, aaa = true }, latest = 10 },