forked from nvimdev/guard.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint_spec.lua
More file actions
190 lines (172 loc) · 4.29 KB
/
lint_spec.lua
File metadata and controls
190 lines (172 loc) · 4.29 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
---@diagnostic disable: undefined-field, undefined-global
local api = vim.api
local same = assert.are.same
local ft = require('guard.filetype')
local lint = require('guard.lint')
local gapi = require('guard.api')
local ns = api.nvim_get_namespaces()['Guard']
local vd = vim.diagnostic
describe('lint module', function()
local bufnr
before_each(function()
for k, _ in pairs(ft) do
ft[k] = nil
end
vd.reset(ns, bufnr)
vim.iter(api.nvim_get_autocmds({ group = 'Guard' })):each(function(it)
api.nvim_del_autocmd(it.id)
end)
bufnr = api.nvim_create_buf(true, false)
vim.bo[bufnr].filetype = 'lua'
api.nvim_set_current_buf(bufnr)
vim.cmd('silent! write! /tmp/lint_spec_test.lua')
end)
teardown(function()
vd.reset()
end)
local mock_linter_regex = {
fn = function()
return '/tmp/lint_spec_test.lua:1:1: warning: Very important error message [error code 114514]'
end,
parse = lint.from_regex({
source = 'mock_linter_regex',
regex = ':(%d+):(%d+):%s+(%w+):%s+(.-)%s+%[(.-)%]',
groups = { 'lnum', 'col', 'severity', 'message', 'code' },
offset = 0,
severities = {
information = lint.severities.info,
hint = lint.severities.info,
note = lint.severities.style,
},
}),
}
local mock_linter_json = {
fn = function()
return vim.json.encode({
source = 'mock_linter_json',
bufnr = bufnr,
col = 1,
end_col = 9,
lnum = 1,
end_lnum = 0,
message = 'Very important error message',
namespace = ns,
severity = 'warning',
})
end,
parse = lint.from_json({
get_diagnostics = function(...)
return { vim.json.decode(...) }
end,
attributes = {
lnum = 'lnum',
end_lnum = 'end_lnum',
col = 'col',
end_col = 'end_col',
message = 'message',
code = 'severity',
},
source = 'mock_linter_json',
}),
}
local mock_linter = {
fn = function()
return 'some stuff'
end,
parse = function()
return {
{
bufnr = bufnr,
col = 1,
end_col = 1,
lnum = 1,
end_lnum = 1,
message = 'foo',
namespace = 42,
severity = vd.severity.HINT,
source = 'bar',
},
}
end,
}
it('can lint with single linter', function()
if true then
return
end
ft('lua'):lint(mock_linter_regex)
gapi.lint()
vim.wait(100)
same({
{
source = 'mock_linter_regex',
bufnr = bufnr,
col = 1,
end_col = 1,
lnum = 1,
end_lnum = 1,
message = 'Very important error message',
code = 'error code 114514',
namespace = ns,
severity = 2,
},
}, vd.get())
end)
it('can lint with multiple linters', function()
if true then
return
end
ft('lua'):lint(mock_linter_regex):append(mock_linter_json)
gapi.lint()
vim.wait(100)
same({
{
source = 'mock_linter_regex',
bufnr = bufnr,
col = 1,
end_col = 1,
lnum = 1,
end_lnum = 1,
message = 'Very important error message',
code = 'error code 114514',
namespace = ns,
severity = 2,
},
{
bufnr = bufnr,
col = 0,
end_col = 0,
end_lnum = 0,
lnum = 0,
message = 'Very important error message',
code = 'warning',
namespace = ns,
severity = 2,
source = 'mock_linter_json',
},
}, vd.get())
end)
it('can define a linter for all filetypes', function()
ft('*'):lint(mock_linter)
gapi.lint()
vim.wait(100)
same('foo', vd.get()[1].message)
end)
it('can lint on custom user events', function()
local mock_linter_custom = vim.deepcopy(mock_linter, true)
mock_linter_custom.events = {
{ name = 'ColorScheme', opt = { pattern = 'blue' } },
}
ft('*'):lint(mock_linter_custom)
-- should have been overridden
vim.cmd('silent! write!')
vim.wait(100)
same({}, vd.get())
-- did not match pattern
vim.cmd('colorscheme vim')
vim.wait(100)
same({}, vd.get())
vim.cmd('colorscheme blue')
vim.wait(500)
same('foo', vd.get()[1].message)
end)
end)