1
1
--- @diagnostic disable : undefined-field , undefined-global
2
2
local api = vim .api
3
- local equal = assert .equal
3
+ local equal , same = assert .equal , assert . are . same
4
4
local ft = require (' guard.filetype' )
5
5
local gapi = require (' guard.api' )
6
+ local group = require (' guard.events' ).group
6
7
7
8
describe (' format module' , function ()
8
9
local bufnr
10
+ local ill_lua = {
11
+ ' local a' ,
12
+ ' = "test"' ,
13
+ }
9
14
before_each (function ()
10
15
for k , _ in pairs (ft ) do
11
16
ft [k ] = nil
@@ -15,6 +20,9 @@ describe('format module', function()
15
20
vim .bo [bufnr ].filetype = ' lua'
16
21
api .nvim_set_current_buf (bufnr )
17
22
vim .cmd (' silent! write! /tmp/fmt_spec_test.lua' )
23
+ vim .iter (api .nvim_get_autocmds ({ group = group })):each (function (it )
24
+ api .nvim_del_autocmd (it .id )
25
+ end )
18
26
end )
19
27
20
28
it (' can format with single formatter' , function ()
@@ -23,10 +31,7 @@ describe('format module', function()
23
31
args = { ' -' },
24
32
stdin = true ,
25
33
})
26
- api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , {
27
- ' local a' ,
28
- ' = "test"' ,
29
- })
34
+ api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , ill_lua )
30
35
gapi .fmt ()
31
36
vim .wait (500 )
32
37
local line = api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )[1 ]
@@ -43,10 +48,7 @@ describe('format module', function()
43
48
args = { ' -s' , ' ' },
44
49
stdin = true ,
45
50
})
46
- api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , {
47
- ' local a' ,
48
- ' = "test"' ,
49
- })
51
+ api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , ill_lua )
50
52
gapi .fmt ()
51
53
vim .wait (500 )
52
54
local lines = api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
@@ -59,10 +61,7 @@ describe('format module', function()
59
61
return table.concat (vim .split (acc , ' \n ' ), ' ' ) .. vim .inspect (range )
60
62
end ,
61
63
})
62
- api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , {
63
- ' local a' ,
64
- ' = "test"' ,
65
- })
64
+ api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , ill_lua )
66
65
gapi .fmt ()
67
66
vim .wait (500 )
68
67
local lines = api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
@@ -109,4 +108,27 @@ describe('format module', function()
109
108
lines = api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
110
109
assert .are .same ({ ' def' }, lines )
111
110
end )
111
+
112
+ it (' can format on custom events' , function ()
113
+ ft (' lua' ):fmt ({
114
+ cmd = ' stylua' ,
115
+ args = { ' -' },
116
+ stdin = true ,
117
+ autocmds = {
118
+ { event = ' ColorScheme' , opt = {} },
119
+ },
120
+ })
121
+
122
+ api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , ill_lua )
123
+
124
+ vim .cmd (' silent! write! /tmp/fmt_spec_test.lua' )
125
+ vim .wait (500 )
126
+
127
+ same (ill_lua , api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ))
128
+
129
+ vim .cmd .colorscheme (' blue' )
130
+ vim .wait (500 )
131
+
132
+ equal ([[ local a = 'test']] , api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )[1 ])
133
+ end )
112
134
end )
0 commit comments