-
Notifications
You must be signed in to change notification settings - Fork 201
/
test_test.lua
1381 lines (1091 loc) · 46 KB
/
test_test.lua
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
-- This is intended to mostly cover general API. In a sense, all tests in this
-- plugin also test 'mini.test'.
local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()
local expect, eq = helpers.expect, helpers.expect.equality
local new_set, finally = MiniTest.new_set, MiniTest.finally
local mark_flaky = helpers.mark_flaky
-- Helpers with child processes
--stylua: ignore start
local load_module = function(config) child.mini_load('test', config) end
local unload_module = function() child.mini_unload('test') end
local set_cursor = function(...) return child.set_cursor(...) end
local set_lines = function(...) return child.set_lines(...) end
local get_lines = function(...) return child.get_lines(...) end
--stylua: ignore end
-- TODO: Remove after compatibility with Neovim=0.9 is dropped
local islist = vim.fn.has('nvim-0.10') == 1 and vim.islist or vim.tbl_islist
local get_latest_message = function() return child.cmd_capture('1messages') end
local get_ref_path = function(name) return string.format('tests/dir-test/%s', name) end
local get_current_all_cases = function()
-- Encode functions inside child. Works only for "simple" functions.
local command = [[vim.tbl_map(function(case)
case.hooks = { pre = vim.tbl_map(string.dump, case.hooks.pre), post = vim.tbl_map(string.dump, case.hooks.post) }
case.test = string.dump(case.test)
return case
end, MiniTest.current.all_cases)]]
local res = child.lua_get(command)
-- Decode functions in current process
res = vim.tbl_map(function(case)
case.hooks = { pre = vim.tbl_map(loadstring, case.hooks.pre), post = vim.tbl_map(loadstring, case.hooks.post) }
---@diagnostic disable-next-line:param-type-mismatch
case.test = loadstring(case.test)
return case
end, res)
-- Update array to enable getting element by last entry of `desc` field
return setmetatable(res, {
__index = function(t, key)
return vim.tbl_filter(function(case_output)
local last_desc = case_output.desc[#case_output.desc]
return last_desc == key
end, t)[1]
end,
})
end
local testrun_ref_file = function(name)
local find_files_command = string.format([[_G.find_files = function() return { '%s' } end]], get_ref_path(name))
child.lua(find_files_command)
child.lua('MiniTest.run({ collect = { find_files = _G.find_files }, execute = { reporter = {} } })')
return get_current_all_cases()
end
local filter_by_desc = function(cases, id, value)
return vim.tbl_filter(function(c) return c.desc[id] == value end, cases)
end
local expect_all_state = function(cases, state)
local res = true
for _, c in ipairs(cases) do
if type(c.exec) ~= 'table' or c.exec.state ~= state then res = false end
end
eq(res, true)
end
-- Output test set
local T = new_set({
hooks = {
pre_case = function()
child.setup()
-- Mock UI so that default reporter is as if used interactively
child.lua('vim.api.nvim_list_uis = function() return { 1 } end')
load_module()
end,
post_once = child.stop,
},
})
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
-- Global variable
eq(child.lua_get('type(_G.MiniTest)'), 'table')
-- Highlight groups
child.cmd('hi clear')
load_module()
expect.match(child.cmd_capture('hi MiniTestFail'), 'gui=bold')
expect.match(child.cmd_capture('hi MiniTestPass'), 'gui=bold')
expect.match(child.cmd_capture('hi MiniTestEmphasis'), 'gui=bold')
end
T['setup()']['creates `config` field'] = function()
eq(child.lua_get('type(_G.MiniTest.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniTest.config.' .. field), value) end
expect_config('collect.emulate_busted', true)
eq(child.lua_get('type(_G.MiniTest.config.collect.find_files)'), 'function')
eq(child.lua_get('type(_G.MiniTest.config.collect.filter_cases)'), 'function')
expect_config('execute.reporter', vim.NIL)
expect_config('execute.stop_on_error', false)
expect_config('script_path', 'scripts/minitest.lua')
expect_config('silent', false)
end
T['setup()']['respects `config` argument'] = function()
unload_module()
load_module({ script_path = 'a' })
eq(child.lua_get('MiniTest.config.script_path'), 'a')
end
T['setup()']['validates `config` argument'] = function()
unload_module()
local expect_config_error = function(config, name, target_type)
local pattern = vim.pesc(name) .. '.*' .. vim.pesc(target_type)
expect.error(load_module, pattern, config)
end
expect_config_error('a', 'config', 'table')
expect_config_error({ collect = 'a' }, 'collect', 'table')
expect_config_error({ collect = { emulate_busted = 'a' } }, 'collect.emulate_busted', 'boolean')
expect_config_error({ collect = { find_files = 'a' } }, 'collect.find_files', 'function')
expect_config_error({ collect = { filter_cases = 'a' } }, 'collect.filter_cases', 'function')
expect_config_error({ execute = 'a' }, 'execute', 'table')
expect_config_error({ execute = { reporter = 'a' } }, 'execute.reporter', 'function')
expect_config_error({ execute = { stop_on_error = 'a' } }, 'execute.stop_on_error', 'boolean')
expect_config_error({ script_path = 1 }, 'script_path', 'string')
expect_config_error({ silent = 1 }, 'silent', 'boolean')
end
T['setup()']['defines non-linked default highlighting on `ColorScheme`'] = function()
child.cmd('colorscheme blue')
expect.match(child.cmd_capture('hi MiniTestFail'), 'gui=bold')
expect.match(child.cmd_capture('hi MiniTestPass'), 'gui=bold')
expect.match(child.cmd_capture('hi MiniTestEmphasis'), 'gui=bold')
end
T['new_set()'] = new_set()
T['new_set()']['tracks field order'] = function()
local res = testrun_ref_file('testref_new-set.lua')
-- Check order
local order_cases = vim.tbl_map(
function(c) return c.desc[#c.desc] end,
vim.tbl_filter(function(c) return c.desc[2] == 'order' end, res)
)
eq(order_cases, { 'From initial call', 'zzz First added', 'aaa Second added', 1 })
end
T['new_set()']['stores `opts`'] = function()
local opts = { parametrize = { { 'a' } } }
child.lua([[_G.set = MiniTest.new_set(...)]], { opts })
eq(child.lua_get([[getmetatable(_G.set).opts]]), opts)
end
T['case helpers'] = new_set()
T['case helpers']['work'] = function()
local res = testrun_ref_file('testref_case-helpers.lua')
-- `finally()`
eq(res['finally() with error; check'].exec.state, 'Pass')
eq(res['finally() no error; check'].exec.state, 'Pass')
-- `skip()`
eq(res['skip(); no message'].exec.state, 'Pass with notes')
eq(res['skip(); no message'].exec.notes, { 'Skip test' })
eq(res['skip(); with message'].exec.state, 'Pass with notes')
eq(res['skip(); with message'].exec.notes, { 'This is a custom skip message' })
-- `add_note()`
eq(res['add_note()'].exec.state, 'Pass with notes')
eq(res['add_note()'].exec.notes, { 'This note should be appended' })
end
T['run()'] = new_set()
T['run()']['respects `opts` argument'] = function()
child.lua('MiniTest.run({ collect = { find_files = function() return {} end } })')
eq(#get_current_all_cases(), 0)
-- Should also use buffer local config
local general_file = get_ref_path('testref_general.lua')
local command = string.format(
[[vim.b.minitest_config = { collect = { find_files = function() return { '%s' } end } }]],
general_file
)
child.lua(command)
child.lua('MiniTest.run()')
eq(#get_current_all_cases() > 0, true)
end
T['run()']['tries to execute script if no arguments are supplied'] = function()
local validate = function()
local cache_local_config = child.b.minitest_config
eq(child.lua_get('_G.custom_script_result'), vim.NIL)
child.lua('MiniTest.run()')
eq(child.lua_get('_G.custom_script_result'), 'This actually ran')
-- Global and buffer local config should be restored
eq(child.lua_get('type(MiniTest.config.aaa)'), 'nil')
eq(child.b.minitest_config, cache_local_config)
end
local script_path = get_ref_path('testref_custom-script.lua')
child.lua('MiniTest.config.script_path = ' .. vim.inspect(script_path))
validate()
-- Should also use buffer local config
child.lua([[MiniTest.config.script_path = '']])
child.lua('_G.custom_script_result = nil')
child.b.minitest_config = { script_path = script_path }
validate()
end
T['run()']['handles `parametrize`'] = function()
local res = testrun_ref_file('testref_run-parametrize.lua')
eq(#res, 10)
local short_res = vim.tbl_map(function(c)
local desc = vim.list_slice(c.desc, 2)
return { args = c.args, desc = desc, passed_args = c.exec.fails[1]:match('Passed arguments: (.-)\n Traceback.*$') }
end, res)
eq(short_res[1], { args = { 'a' }, desc = { 'parametrize', 'first level' }, passed_args = '"a"' })
eq(short_res[2], { args = { 'b' }, desc = { 'parametrize', 'first level' }, passed_args = '"b"' })
eq(short_res[3], { args = { 'a', 1 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"a", 1' })
eq(short_res[4], { args = { 'a', 2 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"a", 2' })
eq(short_res[5], { args = { 'b', 1 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"b", 1' })
eq(short_res[6], { args = { 'b', 2 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"b", 2' })
--stylua: ignore start
eq(short_res[7], { args = { 'a', 'a', 1, 1 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"a", "a", 1, 1' })
eq(short_res[8], { args = { 'a', 'a', 2, 2 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"a", "a", 2, 2' })
eq(short_res[9], { args = { 'b', 'b', 1, 1 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"b", "b", 1, 1' })
eq(short_res[10], { args = { 'b', 'b', 2, 2 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"b", "b", 2, 2' })
--stylua: ignore end
end
T['run()']['validates `parametrize`'] = function()
expect.error(
function() testrun_ref_file('testref_run-parametrize-error.lua') end,
[[`parametrize` should have only tables. Got "a"]]
)
end
T['run()']['handles `data`'] = function()
local res = testrun_ref_file('testref_run-data.lua')
local short_res = vim.tbl_map(function(c) return { data = c.data, desc = vim.list_slice(c.desc, 2) } end, res)
eq(#short_res, 2)
eq(short_res[1], {
data = { a = 1, b = 2 },
desc = { 'data', 'first level' },
})
eq(short_res[2], {
data = { a = 10, b = 2, c = 30 },
desc = { 'data', 'nested', 'should override' },
})
end
T['run()']['handles `hooks`'] = function()
local res = testrun_ref_file('testref_run-hooks.lua')
--stylua: ignore
eq(child.lua_get('_G.log'), {
-- Test order
"pre_once_1",
"pre_case_1", "First level test", "post_case_1",
"pre_once_2",
"pre_case_1", "pre_case_2", "Nested #1", "post_case_2", "post_case_1",
"pre_case_1", "pre_case_2", "Nested #2", "post_case_2", "post_case_1",
"post_once_2",
"post_once_1",
-- Test skip case on hook error. All hooks should still be called.
"pre_case_3", "post_case_3", "post_once_3",
"pre_once_4", "post_case_4", "post_once_4",
-- Using same function in `*_once` hooks should still lead to its multiple
-- execution.
"Same function",
"Same function",
"Same hook test",
"Same function",
"Same function",
})
-- Skipping test case due to hook errors should add a note
expect.match(filter_by_desc(res, 2, 'skip_case_on_hook_error #1')[1].exec.notes[1], '^Skip.*error.*hooks')
end
T['run()']['appends traceback to fails'] = function()
local res = testrun_ref_file('testref_general.lua')
local ref_path = get_ref_path('testref_general.lua')
local n = 0
for _, case in ipairs(res) do
if #case.exec.fails > 0 then
expect.match(case.exec.fails[1], 'Traceback:%s+' .. vim.pesc(ref_path))
n = n + 1
end
end
if n == 0 then error('No actual fails was tested', 0) end
end
T['run_file()'] = new_set()
T['run_file()']['works'] = function()
child.lua([[MiniTest.run_file(...)]], { get_ref_path('testref_run.lua') })
local last_desc =
child.lua_get([[vim.tbl_map(function(case) return case.desc[#case.desc] end, MiniTest.current.all_cases)]])
eq(last_desc, { 'run_at_location()', 'extra case' })
end
T['run_file()']['normalizes input path'] = function()
child.lua('MiniTest.run_file(...)', { './' .. get_ref_path('testref_run.lua') })
eq(child.lua_get('MiniTest.current.all_cases[1].desc[1]'), 'tests/dir-test/testref_run.lua')
end
T['run_at_location()'] = new_set()
T['run_at_location()']['works with non-default input'] = new_set({ parametrize = { { 3 }, { 4 }, { 5 } } }, {
function(line)
local path = get_ref_path('testref_run.lua')
local command = string.format([[MiniTest.run_at_location({ file = '%s', line = %s })]], path, line)
child.lua(command)
local all_cases = get_current_all_cases()
eq(#all_cases, 1)
eq(all_cases[1].desc, { path, 'run_at_location()' })
end,
})
T['run_at_location()']['uses cursor position by default'] = function()
local path = get_ref_path('testref_run.lua')
child.cmd('edit ' .. path)
set_cursor(4, 0)
child.lua('MiniTest.run_at_location()')
local all_cases = get_current_all_cases()
eq(#all_cases, 1)
eq(all_cases[1].desc, { path, 'run_at_location()' })
end
local collect_general = function()
local path = get_ref_path('testref_general.lua')
local command = string.format([[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end })]], path)
child.lua(command)
end
T['collect()'] = new_set()
T['collect()']['works'] = function()
child.lua('_G.cases = MiniTest.collect()')
-- TODO: Remove after compatibility with Neovim=0.9 is dropped
child.lua([[_G.islist = vim.fn.has('nvim-0.10') == 1 and vim.islist or vim.tbl_islist]])
-- Should return array of cases
eq(child.lua_get('_G.islist(_G.cases)'), true)
local keys = child.lua_get('vim.tbl_keys(_G.cases[1])')
table.sort(keys)
eq(keys, { 'args', 'data', 'desc', 'hooks', 'test' })
end
T['collect()']['respects `emulate_busted` option'] = function()
local res = testrun_ref_file('testref_collect-busted.lua')
-- All descriptions should be prepended with file name
eq(#filter_by_desc(res, 1, get_ref_path('testref_collect-busted.lua')), #res)
-- `describe()/it()`
eq(#filter_by_desc(res, 2, 'describe()/it()'), 3)
-- `setup()/teardown()`
expect_all_state(filter_by_desc(res, 2, 'setup()/teardown()'), 'Pass')
-- `before_each()/after_each()`
expect_all_state(filter_by_desc(res, 2, 'before_each()/after_each()'), 'Pass')
-- `MiniTest.skip()`
expect_all_state(filter_by_desc(res, 2, 'MiniTest.skip()'), 'Pass with notes')
-- `MiniTest.finally()`
local cases_finally = filter_by_desc(res, 2, 'MiniTest.finally()')
-- all_have_state(filter_by_desc(cases_finally, 3, 'works with no error'))
expect_all_state(filter_by_desc(cases_finally, 3, 'works with no error'), 'Pass')
expect_all_state(filter_by_desc(cases_finally, 3, 'works with error'), 'Pass')
-- Should also use buffer local config
child.lua([[vim.b.minitest_config = { collect = { emulate_busted = false } }]])
local busted_file = get_ref_path('testref_collect-busted.lua')
local command = string.format([[MiniTest.collect({ find_files = function() return { '%s' } end })]], busted_file)
expect.error(function() child.lua(command) end, 'attempt to call global')
end
T['collect()']['respects `find_files` option'] = function()
local command = string.format(
[[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end })]],
get_ref_path('testref_general.lua')
)
child.lua(command)
eq(child.lua_get('#_G.cases'), 2)
eq(child.lua_get('_G.cases[1].desc[1]'), 'tests/dir-test/testref_general.lua')
child.lua([[vim.b.minitest_config = { collect = { find_files = function() return {} end } }]])
child.lua('_G.cases = MiniTest.collect()')
eq(child.lua_get('#_G.cases'), 0)
end
T['collect()']['respects `filter_cases` option'] = function()
local command = string.format(
[[_G.cases = MiniTest.collect({
find_files = function() return { '%s' } end,
filter_cases = function(case) return case.desc[2] == 'case 2' end,
})]],
get_ref_path('testref_general.lua')
)
child.lua(command)
eq(child.lua_get('#_G.cases'), 1)
eq(child.lua_get('_G.cases[1].desc[2]'), 'case 2')
child.lua([[vim.b.minitest_config = { collect = { filter_cases = function() return false end } }]])
child.lua('_G.cases = MiniTest.collect()')
eq(child.lua_get('#_G.cases'), 0)
end
T['execute()'] = new_set()
T['execute()']['respects `reporter` option'] = new_set()
T['execute()']['respects `reporter` option']['empty'] = function()
collect_general()
child.lua('MiniTest.execute(_G.cases, { reporter = {} })')
end
T['execute()']['respects `reporter` option']['partial'] = function()
collect_general()
child.lua([[MiniTest.execute(
_G.cases,
{ reporter = {
start = function() _G.was_in_start = true end,
finish = function() _G.was_in_finish = true end,
} }
)]])
eq(child.lua_get('_G.was_in_start'), true)
eq(child.lua_get('_G.was_in_finish'), true)
child.lua('vim.b.minitest_config = { execute = { reporter = { update = function() _G.was_in_update = true end } } }')
child.lua('MiniTest.execute(_G.cases)')
eq(child.lua_get('_G.was_in_update'), true)
end
T['execute()']['respects `stop_on_error` option'] = function()
collect_general()
child.lua('MiniTest.execute(_G.cases, { stop_on_error = true })')
eq(child.lua_get('type(_G.cases[1].exec)'), 'table')
eq(child.lua_get('_G.cases[1].exec.state'), 'Fail')
eq(child.lua_get('type(_G.cases[2].exec)'), 'nil')
-- Should also use buffer local config
child.lua('vim.b.minitest_config = { execute = { stop_on_error = false } }')
child.lua('MiniTest.execute(_G.cases)')
eq(child.lua_get('type(_G.cases[2].exec)'), 'table')
end
T['execute()']['properly calls `reporter` methods'] = function()
collect_general()
child.lua([[
_G.update_history = {}
_G.reporter = {
start = function(all_cases) _G.all_cases = all_cases end,
update = function(case_num)
table.insert(_G.update_history, { case_num = case_num, state = _G.all_cases[case_num].exec.state })
end,
finish = function() _G.was_in_finish = true end,
}]])
child.lua([[MiniTest.execute(_G.cases, { reporter = _G.reporter })]])
eq(child.lua_get('#_G.all_cases'), 2)
eq(child.lua_get('_G.update_history'), {
{ case_num = 1, state = "Executing 'pre' hook #1" },
{ case_num = 1, state = "Executing 'pre' hook #2" },
{ case_num = 1, state = 'Executing test' },
{ case_num = 1, state = "Executing 'post' hook #1" },
{ case_num = 1, state = 'Fail' },
{ case_num = 2, state = "Executing 'pre' hook #1" },
{ case_num = 2, state = 'Executing test' },
{ case_num = 2, state = "Executing 'post' hook #1" },
{ case_num = 2, state = "Executing 'post' hook #2" },
{ case_num = 2, state = 'Pass' },
})
eq(child.lua_get('_G.was_in_finish'), true)
end
T['execute()']['handles no cases'] = function()
child.lua('MiniTest.execute({})')
eq(child.lua_get('MiniTest.current.all_cases'), {})
-- Should throw message
eq(get_latest_message(), '(mini.test) No cases to execute.')
end
T['execute()']['respects `config.silent`'] = function()
child.lua('MiniTest.config.silent = true')
child.lua('MiniTest.execute({})')
eq(child.lua_get('MiniTest.current.all_cases'), {})
-- Should not throw message
eq(get_latest_message(), '')
end
T['stop()'] = new_set()
T['stop()']['works'] = function()
collect_general()
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()')
child.lua('MiniTest.execute(_G.cases, { reporter = { start = function() MiniTest.stop() end } })')
eq(child.lua_get('type(_G.cases[1].exec)'), 'nil')
eq(child.lua_get('type(_G.cases[2].exec)'), 'nil')
-- Should close all opened child processed by default
eq(child.lua_get('_G.grandchild.is_running()'), false)
end
T['stop()']['respects `close_all_child_neovim` option'] = function()
collect_general()
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()')
-- Register cleanup
finally(function() child.lua('_G.grandchild.stop()') end)
child.lua([[MiniTest.execute(
_G.cases,
{ reporter = { start = function() MiniTest.stop({ close_all_child_neovim = false }) end } }
)]])
-- Shouldn't close as per option
eq(child.lua_get('_G.grandchild.is_running()'), true)
end
T['is_executing()'] = new_set()
T['is_executing()']['works'] = function()
collect_general()
-- Tests are executing all the time while reporter is active, but not before
-- or after
eq(child.lua_get('MiniTest.is_executing()'), false)
child.lua([[
_G.executing_states = {}
local track_is_executing = function() table.insert(_G.executing_states, MiniTest.is_executing()) end
MiniTest.execute(
_G.cases,
{ reporter = { start = track_is_executing, update = track_is_executing, finish = track_is_executing } }
)]])
local all_true = true
for _, s in ipairs(child.lua_get('_G.executing_states')) do
if s ~= true then all_true = false end
end
eq(all_true, true)
eq(child.lua_get('MiniTest.is_executing()'), false)
end
T['expect'] = new_set()
T['expect']['equality()/no_equality()'] = new_set()
T['expect']['equality()/no_equality()']['work when equal'] = function()
local f, empty_tbl = function() end, {}
local validate = function(x, y)
expect.no_error(MiniTest.expect.equality, x, y)
expect.error(MiniTest.expect.no_equality, '%*no%* equality.*Object:', x, y)
end
validate(1, 1)
validate('a', 'a')
validate(f, f)
validate(empty_tbl, empty_tbl)
-- Tables should be compared "deeply per elements"
validate(empty_tbl, {})
validate({ 1 }, { 1 })
validate({ a = 1 }, { a = 1 })
validate({ { b = 2 } }, { { b = 2 } })
end
T['expect']['equality()/no_equality()']['work when not equal'] = function()
local f = function() end
local validate = function(x, y)
expect.error(MiniTest.expect.equality, 'equality.*Left: .*Right: ', x, y)
expect.no_error(MiniTest.expect.no_equality, x, y)
end
validate(1, 2)
validate(1, '1')
validate('a', 'b')
validate(f, function() end)
-- Tables should be compared "deeply per elements"
validate({ 1 }, { 2 })
validate({ a = 1 }, { a = 2 })
validate({ a = 1 }, { b = 1 })
validate({ a = 1 }, { { a = 1 } })
validate({ { b = 2 } }, { { b = 3 } })
validate({ { b = 2 } }, { { c = 2 } })
end
T['expect']['equality()/no_equality()']['return `true` on success'] = function()
eq(MiniTest.expect.equality(1, 1), true)
eq(MiniTest.expect.no_equality(1, 2), true)
end
T['expect']['error()'] = new_set()
T['expect']['error()']['works'] = function()
expect.error(function()
MiniTest.expect.error(function() end)
end, 'error%..*Observed no error')
expect.error(function()
MiniTest.expect.error(function() end, 'aa')
end, 'error matching pattern "aa"%..*Observed no error')
expect.error(function() MiniTest.expect.error(error, 'bb') end, 'error matching pattern "bb"%..*Observed error:')
end
T['expect']['error()']['respects `pattern` argument'] = function()
---@diagnostic disable-next-line:param-type-mismatch
expect.error(function() MiniTest.expect.error(error, 1) end, 'pattern.*expected string')
-- `nil` and `''` are placeholders for 'any error'
expect.no_error(function() MiniTest.expect.error(error, '') end)
expect.no_error(function() MiniTest.expect.error(error, nil) end)
end
T['expect']['error()']['accepts function arguments'] = function()
--stylua: ignore
local f = function(x, y)
if x ~= y then error('`x` and `y` are not equal') end
end
expect.no_error(function() MiniTest.expect.error(f, 'not equal', 1, 2) end)
expect.error(function() MiniTest.expect.error(f, 'not equal', 1, 1) end)
end
T['expect']['error()']['returns `true` on success'] = function() eq(MiniTest.expect.error(error), true) end
T['expect']['no_error()'] = new_set()
T['expect']['no_error()']['works'] = function()
expect.error(function() MiniTest.expect.no_error(error) end, '%*no%* error%..*Observed error:')
expect.no_error(function()
MiniTest.expect.no_error(function() end)
end)
end
T['expect']['no_error()']['accepts function arguments'] = function()
--stylua: ignore
local f = function(x, y)
if x ~= y then error('`x` and `y` are not equal') end
end
expect.error(function() MiniTest.expect.no_error(f, 1, 2) end)
expect.no_error(function() MiniTest.expect.no_error(f, 1, 1) end)
end
T['expect']['no_error()']['returns `true` on success'] = function()
eq(MiniTest.expect.no_error(function() end), true)
end
T['expect']['reference_screenshot()'] = new_set()
T['expect']['reference_screenshot()']['works'] = function()
local path = get_ref_path('reference-screenshot')
child.set_size(5, 12)
set_lines({ 'aaa' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path), true)
set_lines({ 'bbb' })
expect.error(
function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path) end,
'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*Reference:.*Observed:'
)
-- Should pass if supplied `nil` (like in case of no reasonable screenshot)
eq(MiniTest.expect.reference_screenshot(nil), true)
end
T['expect']['reference_screenshot()']['locates problem'] = function()
local path = get_ref_path('reference-screenshot')
local validate = function(screen, pattern)
expect.error(function() MiniTest.expect.reference_screenshot(screen, path) end, pattern)
end
child.set_size(5, 12)
set_lines({ 'aaa' })
local screen = child.get_screenshot()
-- Number of lines
local screen_text_lines = vim.deepcopy(screen)
table.remove(screen_text_lines.text, 1)
validate(screen_text_lines, 'Different number of `text` lines%. Reference: 5%. Observed: 4%.')
local screen_attr_lines = vim.deepcopy(screen)
table.remove(screen_attr_lines.attr, 1)
validate(screen_attr_lines, 'Different number of `attr` lines%. Reference: 5%. Observed: 4%.')
-- Number of columns
local screen_text_columns = vim.deepcopy(screen)
table.remove(screen_text_columns.text[1], 1)
validate(screen_text_columns, 'Different number of columns in `text` line 1%. Reference: 12%. Observed: 11%.')
local screen_attr_columns = vim.deepcopy(screen)
table.remove(screen_attr_columns.attr[1], 1)
validate(screen_attr_columns, 'Different number of columns in `attr` line 1%. Reference: 12%. Observed: 11%.')
-- Cells
local screen_text_cell = vim.deepcopy(screen)
screen_text_cell.text[1][2] = 'X'
validate(screen_text_cell, 'Different `text` cell at line 1 column 2%. Reference: "a"%. Observed: "X"%.')
local screen_attr_cell = vim.deepcopy(screen)
screen_attr_cell.attr[1][2] = 'X'
validate(screen_attr_cell, 'Different `attr` cell at line 1 column 2%. Reference: "0"%. Observed: "X"%.')
end
T['expect']['reference_screenshot()']['correctly infers reference path'] = function()
child.set_size(5, 20)
set_lines({ 'This path should be correctly inferred without suffix' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
set_lines({ 'This path should have suffix 002' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
set_lines({ 'This is a call to `reference_screenshot()` with manual path' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), 'tests/dir-test/intermediate-screenshot'), true)
set_lines({ 'This path should have suffix 004' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
end
local validate_path_sanitize = function()
child.set_size(5, 12)
set_lines({ 'Path should be correctly sanitized' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
end
local useful_punctuation = [=[_-+{}()[]'"]=]
local linux_forbidden = [[/]]
local windows_forbidden = [[<>:"/\|?*]]
local whitespace = '\t '
local special_characters = string.char(0) .. string.char(1) .. string.char(31)
local suffix = useful_punctuation .. linux_forbidden .. windows_forbidden .. whitespace .. special_characters
-- Don't permanently create reference file because its name is very long. This
-- might hurt Windows users which are not interested in testing this plugin.
T['expect']['reference_screenshot()']['correctly sanitizes path ' .. suffix] = new_set(
{ parametrize = { { suffix } } },
{
test = function()
local expected_filename = table.concat({
'tests/screenshots/',
'tests-test_test.lua---',
'expect---',
'reference_screenshot()---',
'correctly-sanitizes-path-',
[[_-+{}()[]''----'-------------]],
'test-+-args-',
[[{-'_-+{}()[]'-'-----'-------t--0-1-31'-}]],
}, '')
finally(function()
MiniTest.current.case.exec.notes = {}
vim.fn.delete(expected_filename)
end)
eq(vim.fn.filereadable(expected_filename), 0)
validate_path_sanitize()
eq(vim.fn.filereadable(expected_filename), 1)
end,
}
)
-- Paths should not end with whitespace or dot
T['expect']['reference_screenshot()']['correctly sanitizes path for Windows '] = validate_path_sanitize
T['expect']['reference_screenshot()']['correctly sanitizes path for Windows #2.'] = validate_path_sanitize
T['expect']['reference_screenshot()']['creates reference if it does not exist'] = function()
local path = get_ref_path('nonexistent-reference-screenshot')
child.fn.delete(path)
finally(function()
child.fn.delete(path)
MiniTest.current.case.exec.notes = {}
end)
set_lines({ 'nonexistent' })
local screenshot = child.get_screenshot()
eq(MiniTest.expect.reference_screenshot(screenshot, path), true)
eq(MiniTest.current.case.exec.notes, { 'Created reference screenshot at path ' .. vim.inspect(path) })
MiniTest.current.case.exec.notes = {}
eq(MiniTest.expect.reference_screenshot(screenshot, path), true)
eq(MiniTest.current.case.exec.notes, {})
end
T['expect']['reference_screenshot()']['respects `opts.force` argument'] = function()
local path = get_ref_path('force-reference-screenshot')
local notes = { 'Created reference screenshot at path ' .. vim.inspect(path) }
child.fn.delete(path)
finally(function()
child.fn.delete(path)
MiniTest.current.case.exec.notes = {}
end)
set_lines({ 'First run' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path), true)
eq(MiniTest.current.case.exec.notes, notes)
MiniTest.current.case.exec.notes = {}
set_lines({ 'This should be forced' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { force = true }), true)
eq(MiniTest.current.case.exec.notes, notes)
end
T['expect']['reference_screenshot()']['respects `opts.ignore_lines`'] = function()
local path = get_ref_path('reference-screenshot')
child.set_size(5, 12)
local validate = function(ignore_lines, ref)
eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_lines = ignore_lines }), ref)
end
set_lines({ 'aaa' })
validate(nil, true)
set_lines({ 'aaa', 'bbb' })
validate({ 2 }, true)
validate({ 1, 2, 3 }, true)
set_lines({ 'ccc', 'bbb' })
expect.error(
function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_lines = { 2 } }) end,
'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*Reference:.*Observed:'
)
end
T['expect']['reference_screenshot()']['works with multibyte characters'] = function()
child.set_size(5, 12)
set_lines({ ' 1 2' })
expect.no_error(function() MiniTest.expect.reference_screenshot(child.get_screenshot()) end)
end
T['new_expectation()'] = new_set()
T['new_expectation()']['works'] = function()
local expect_truthy = MiniTest.new_expectation(
'truthy',
function(x) return x end,
function(x) return 'Object: ' .. vim.inspect(x) end
)
expect.error(expect_truthy, 'truthy%..*Object:', false)
expect.no_error(expect_truthy, 1)
end
T['new_expectation()']['allows string or function arguments'] = function()
local expect_truthy = MiniTest.new_expectation(
function() return 'func_truthy' end,
function(x) return x end,
'Not truthy'
)
expect.error(expect_truthy, 'func_truthy%..*Not truthy', false)
expect.no_error(expect_truthy, 1)
end
T['new_child_neovim()'] = new_set()
T['new_child_neovim()']['works'] = function()
finally(function() child.lua('_G.grandchild.stop()') end)
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()')
eq(child.lua_get('_G.grandchild.is_running()'), true)
end
T['child'] = new_set()
T['child']['job'] = function()
eq(type(child.job), 'table')
child.stop()
eq(child.job, nil)
end
T['child']['start()'] = new_set()
T['child']['start()']['respects `args` argument'] = function()
child.stop()
child.start({ '-c', 'lua _G.inside_args = true' })
eq(child.lua_get('_G.inside_args'), true)
end
T['child']['start()']['does nothing if already running'] = function()
finally(function() child.lua('_G.grandchild.stop()') end)
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()')
child.lua('_G.should_be_present = true')
child.lua('_G.grandchild.start()')
eq(child.lua_get('_G.should_be_present'), true)
eq(get_latest_message(), '(mini.test) Child process is already running. Use `child.restart()`.')
end
T['child']['stop()'] = function()
eq(child.is_running(), true)
child.stop()
eq(child.is_running(), false)
end
T['child']['restart()'] = new_set()
T['child']['restart()']['respects `args` argument'] = function()
eq(child.lua_get('_G.inside_args'), vim.NIL)
child.restart({ '-c', 'lua _G.inside_args = true' })
eq(child.lua_get('_G.inside_args'), true)
end
T['child']['restart()']['uses `args` from `start()` by default'] = function()
child.stop()
child.start({ '-c', 'lua _G.inside_args = true' })
eq(child.lua_get('_G.inside_args'), true)
child.restart()
eq(child.lua_get('_G.inside_args'), true)
end
local validate_child_method = function(method, opts)
opts = vim.tbl_deep_extend('force', { prevent_hanging = true }, opts or {})
-- Validate presence of method
expect.no_error(method)
-- Validate hanging prevention
if opts.prevent_hanging then
child.type_keys('di')
expect.error(method, opts.name .. '.*child process is blocked')
-- Unblock for faster test execution
child.type_keys('<Esc>')
end
-- Validate ensuring running
child.stop()
expect.error(method, 'Child process is not running')
end
local validate_child_field = function(tbl_name, field_name, value)
local var = string.format('vim[%s][%s]', vim.inspect(tbl_name), vim.inspect(field_name))
-- Setting
child[tbl_name][field_name] = value
eq(child.lua_get(var), value)
-- Getting
eq(child[tbl_name][field_name], child.lua_get(var))
end
T['child']['api'] = function()