@@ -6,161 +6,160 @@ Helpers.expect = vim.deepcopy(MiniTest.expect)
66
77-- The error message returned when a test fails.
88local function errorMessage (str , pattern )
9- return string.format (" Pattern: %s\n Observed string: %s" , vim .inspect (pattern ), str )
9+ return string.format (" Pattern: %s\n Observed string: %s" , vim .inspect (pattern ), str )
1010end
1111
1212-- Check equality of a global `field` against `value` in the given `child` process.
1313-- @usage global_equality(child, "_G.CommitMsgSgLoaded", true)
1414Helpers .expect .global_equality = MiniTest .new_expectation (
15- " variable in child process matches" ,
16- function (child , field , value )
17- return Helpers .expect .equality (child .lua_get (field ), value )
18- end ,
19- errorMessage
15+ " variable in child process matches" ,
16+ function (child , field , value )
17+ return Helpers .expect .equality (child .lua_get (field ), value )
18+ end ,
19+ errorMessage
2020)
2121
2222-- Check type equality of a global `field` against `value` in the given `child` process.
2323-- @usage global_type_equality(child, "_G.CommitMsgSgLoaded", "boolean")
2424Helpers .expect .global_type_equality = MiniTest .new_expectation (
25- " variable type in child process matches" ,
26- function (child , field , value )
27- return Helpers .expect .global_equality (child , " type(" .. field .. " )" , value )
28- end ,
29- errorMessage
25+ " variable type in child process matches" ,
26+ function (child , field , value )
27+ return Helpers .expect .global_equality (child , " type(" .. field .. " )" , value )
28+ end ,
29+ errorMessage
3030)
3131
3232-- Check equality of a config `field` against `value` in the given `child` process.
3333-- @usage option_equality(child, "debug", true)
3434Helpers .expect .config_equality = MiniTest .new_expectation (
35- " config option matches" ,
36- function (child , field , value )
37- return Helpers .expect .global_equality (child , " _G.CommitMsgSg.config." .. field , value )
38- end ,
39- errorMessage
35+ " config option matches" ,
36+ function (child , field , value )
37+ return Helpers .expect .global_equality (child , " _G.CommitMsgSg.config." .. field , value )
38+ end ,
39+ errorMessage
4040)
4141
4242-- Check type equality of a config `field` against `value` in the given `child` process.
4343-- @usage config_type_equality(child, "debug", "boolean")
4444Helpers .expect .config_type_equality = MiniTest .new_expectation (
45- " config option type matches" ,
46- function (child , field , value )
47- return Helpers .expect .global_equality (
48- child ,
49- " type(_G.CommitMsgSg.config." .. field .. " )" ,
50- value
51- )
52- end ,
53- errorMessage
45+ " config option type matches" ,
46+ function (child , field , value )
47+ return Helpers .expect .global_equality (
48+ child ,
49+ " type(_G.CommitMsgSg.config." .. field .. " )" ,
50+ value
51+ )
52+ end ,
53+ errorMessage
5454)
5555
5656-- Check equality of a state `field` against `value` in the given `child` process.
5757-- @usage state_equality(child, "enabled", true)
5858Helpers .expect .state_equality = MiniTest .new_expectation (
59- " state matches" ,
60- function (child , field , value )
61- return Helpers .expect .global_equality (child , " _G.CommitMsgSg.enabled." .. field , value )
62- end ,
63- errorMessage
59+ " state matches" ,
60+ function (child , field , value )
61+ return Helpers .expect .global_equality (child , " _G.CommitMsgSg.enabled." .. field , value )
62+ end ,
63+ errorMessage
6464)
6565
6666-- Check type equality of a state `field` against `value` in the given `child` process.
6767-- @usage state_type_equality(child, "enabled", "boolean")
6868Helpers .expect .state_type_equality = MiniTest .new_expectation (
69- " state type matches" ,
70- function (child , field , value )
71- return Helpers .expect .global_equality (
72- child ,
73- " type(_G.CommitMsgSg.state." .. field .. " )" ,
74- value
75- )
76- end ,
77- errorMessage
69+ " state type matches" ,
70+ function (child , field , value )
71+ return Helpers .expect .global_equality (
72+ child ,
73+ " type(_G.CommitMsgSg.state." .. field .. " )" ,
74+ value
75+ )
76+ end ,
77+ errorMessage
7878)
7979
8080Helpers .expect .match = MiniTest .new_expectation (" string matching" , function (str , pattern )
81- return str :find (pattern ) ~= nil
81+ return str :find (pattern ) ~= nil
8282end , errorMessage )
8383
8484Helpers .expect .no_match = MiniTest .new_expectation (" no string matching" , function (str , pattern )
85- return str :find (pattern ) == nil
85+ return str :find (pattern ) == nil
8686end , errorMessage )
8787
8888-- Monkey-patch `MiniTest.new_child_neovim` with helpful wrappers
8989Helpers .new_child_neovim = function ()
90- local child = MiniTest .new_child_neovim ()
90+ local child = MiniTest .new_child_neovim ()
9191
92- local prevent_hanging = function (method )
92+ local prevent_hanging = function (method )
9393 -- stylua: ignore
9494 if not child .is_blocked () then return end
9595
96- local msg =
97- string.format (" Can not use `child.%s` because child process is blocked." , method )
98- error (msg )
99- end
100-
101- child .setup = function ()
102- child .restart ({ " -u" , " scripts/minimal_init.lua" })
96+ local msg = string.format (" Can not use `child.%s` because child process is blocked." , method )
97+ error (msg )
98+ end
10399
104- -- Change initial buffer to be readonly. This not only increases execution
105- -- speed, but more closely resembles manually opened Neovim.
106- child .bo .readonly = false
107- end
100+ child .setup = function ()
101+ child .restart ({ " -u" , " scripts/minimal_init.lua" })
108102
109- child .set_lines = function (arr , start , finish )
110- prevent_hanging (" set_lines" )
103+ -- Change initial buffer to be readonly. This not only increases execution
104+ -- speed, but more closely resembles manually opened Neovim.
105+ child .bo .readonly = false
106+ end
111107
112- if type (arr ) == " string" then
113- arr = vim .split (arr , " \n " )
114- end
108+ child .set_lines = function (arr , start , finish )
109+ prevent_hanging (" set_lines" )
115110
116- child .api .nvim_buf_set_lines (0 , start or 0 , finish or - 1 , false , arr )
111+ if type (arr ) == " string" then
112+ arr = vim .split (arr , " \n " )
117113 end
118114
119- child .get_lines = function ( start , finish )
120- prevent_hanging ( " get_lines " )
115+ child .api . nvim_buf_set_lines ( 0 , start or 0 , finish or - 1 , false , arr )
116+ end
121117
122- return child .api . nvim_buf_get_lines ( 0 , start or 0 , finish or - 1 , false )
123- end
118+ child .get_lines = function ( start , finish )
119+ prevent_hanging ( " get_lines " )
124120
125- child .set_cursor = function ( line , column , win_id )
126- prevent_hanging ( " set_cursor " )
121+ return child .api . nvim_buf_get_lines ( 0 , start or 0 , finish or - 1 , false )
122+ end
127123
128- child .api . nvim_win_set_cursor ( win_id or 0 , { line , column } )
129- end
124+ child .set_cursor = function ( line , column , win_id )
125+ prevent_hanging ( " set_cursor " )
130126
131- child .get_cursor = function (win_id )
132- prevent_hanging ( " get_cursor " )
127+ child .api . nvim_win_set_cursor (win_id or 0 , { line , column } )
128+ end
133129
134- return child .api . nvim_win_get_cursor ( win_id or 0 )
135- end
130+ child .get_cursor = function ( win_id )
131+ prevent_hanging ( " get_cursor " )
136132
137- child .set_size = function ( lines , columns )
138- prevent_hanging ( " set_size " )
133+ return child .api . nvim_win_get_cursor ( win_id or 0 )
134+ end
139135
140- if type (lines ) == " number" then
141- child .o .lines = lines
142- end
136+ child .set_size = function (lines , columns )
137+ prevent_hanging (" set_size" )
143138
144- if type (columns ) == " number" then
145- child .o .columns = columns
146- end
139+ if type (lines ) == " number" then
140+ child .o .lines = lines
147141 end
148142
149- child .get_size = function ()
150- prevent_hanging (" get_size" )
151-
152- return { child .o .lines , child .o .columns }
143+ if type (columns ) == " number" then
144+ child .o .columns = columns
153145 end
146+ end
154147
155- child .expect_screenshot = function (opts , path , screenshot_opts )
156- if child .fn .has (" nvim-0.8" ) == 0 then
157- MiniTest .skip (" Screenshots are tested for Neovim>=0.8 (for simplicity)." )
158- end
148+ child .get_size = function ()
149+ prevent_hanging (" get_size" )
159150
160- MiniTest .expect .reference_screenshot (child .get_screenshot (screenshot_opts ), path , opts )
151+ return { child .o .lines , child .o .columns }
152+ end
153+
154+ child .expect_screenshot = function (opts , path , screenshot_opts )
155+ if child .fn .has (" nvim-0.8" ) == 0 then
156+ MiniTest .skip (" Screenshots are tested for Neovim>=0.8 (for simplicity)." )
161157 end
162158
163- return child
159+ MiniTest .expect .reference_screenshot (child .get_screenshot (screenshot_opts ), path , opts )
160+ end
161+
162+ return child
164163end
165164
166165return Helpers
0 commit comments