]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/test/lua/ale_diagnostics_spec.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit '56df844d3c39ec494dacc69eae34272b27db185a' as '.vim/bundle/asyncomplete'
[etc/vim.git] / .vim / bundle / ale / test / lua / ale_diagnostics_spec.lua
1 local eq = assert.are.same
2 local diagnostics
3
4 describe("ale.diagnostics.send", function()
5     local buffer_map
6     local signs_config
7     local diagnostic_set_calls
8
9     setup(function()
10         _G.vim = {
11             api = {
12                 nvim_buf_get_var = function(buffer, key)
13                     local buffer_table = buffer_map[buffer] or {}
14                     local value = buffer_table[key]
15
16                     if value == nil then
17                         error(key .. " is missing")
18                     end
19
20                     return value
21                 end,
22                 nvim_create_namespace = function()
23                     return 42
24                 end,
25             },
26             diagnostic = {
27                 severity = {ERROR = 1, WARN = 2, INFO = 3},
28                 config = function()
29                     return {signs = signs_config}
30                 end,
31                 set = function(namespace, bufnr, _diagnostics, opts)
32                     table.insert(diagnostic_set_calls, {
33                         namespace = namespace,
34                         bufnr = bufnr,
35                         diagnostics = _diagnostics,
36                         opts = opts,
37                     })
38                 end,
39             },
40             tbl_extend = function(behavior, ...)
41                 assert(behavior == "force", "We should only use `force`")
42
43                 local merged = {}
44
45                 for _, arg in ipairs({...}) do
46                     for key, value in pairs(arg) do
47                         merged[key] = value
48                     end
49                 end
50
51                 return merged
52             end,
53             g = {},
54         }
55
56         diagnostics = require("ale.diagnostics")
57     end)
58
59     teardown(function()
60         _G.vim = nil
61     end)
62
63     before_each(function()
64         buffer_map = {}
65         diagnostic_set_calls = {}
66         signs_config = false
67         _G.vim.g = {}
68     end)
69
70     it("should set an empty list of diagnostics correctly", function()
71         diagnostics.send(7, {})
72
73         eq(
74             {
75                 {
76                     namespace = 42,
77                     bufnr = 7,
78                     diagnostics = {},
79                     opts = {virtual_text = false}
80                 },
81             },
82             diagnostic_set_calls
83         )
84     end)
85
86     it("should handle basic case with all fields", function()
87         diagnostics.send(1, {
88             {
89                 bufnr = 1,
90                 lnum = 2,
91                 end_lnum = 3,
92                 col = 4,
93                 end_col = 5,
94                 type = "W",
95                 code = "123",
96                 text = "Warning message",
97                 linter_name = "eslint",
98             },
99         })
100         eq({
101             {
102                 lnum = 1,
103                 end_lnum = 2,
104                 col = 3,
105                 end_col = 5,
106                 severity = vim.diagnostic.severity.WARN,
107                 code = "123",
108                 message = "Warning message",
109                 source = "eslint",
110             },
111         }, diagnostic_set_calls[1].diagnostics)
112     end)
113
114     it("should default end_lnum to lnum when missing", function()
115         diagnostics.send(1, {
116             {
117                 bufnr = 1,
118                 lnum = 5,
119                 col = 2,
120                 end_col = 8,
121                 type = "E",
122                 text = "Error message",
123                 linter_name = "mylinter",
124             },
125         })
126         eq({
127             {
128                 lnum = 4,
129                 end_lnum = 4,
130                 col = 1,
131                 end_col = 8,
132                 severity = vim.diagnostic.severity.ERROR,
133                 code = nil,
134                 message = "Error message",
135                 source = "mylinter",
136             },
137         }, diagnostic_set_calls[1].diagnostics)
138     end)
139
140     it("should default col to 0 when missing", function()
141         diagnostics.send(1, {
142             {
143                 bufnr = 1,
144                 lnum = 10,
145                 end_lnum = 12,
146                 end_col = 6,
147                 type = "I",
148                 text = "Info message",
149             },
150         })
151         eq({
152             {
153                 lnum = 9,
154                 end_lnum = 11,
155                 col = 0,
156                 end_col = 6,
157                 severity = vim.diagnostic.severity.INFO,
158                 code = nil,
159                 message = "Info message",
160                 source = nil,
161             },
162         }, diagnostic_set_calls[1].diagnostics)
163     end)
164
165     it("should ignore non-matching buffers", function()
166         diagnostics.send(1, {
167             {
168                 bufnr = 2,
169                 lnum = 1,
170                 end_lnum = 2,
171                 col = 1,
172                 end_col = 4,
173                 type = "W",
174                 text = "Message",
175             },
176         })
177         eq({}, diagnostic_set_calls[1].diagnostics)
178     end)
179
180     for _, set_signs_value in ipairs {1, true} do
181         describe("signs with setting set_signs = " .. tostring(set_signs_value), function()
182             before_each(function()
183                 _G.vim.g.ale_set_signs = set_signs_value
184                 _G.vim.g.ale_sign_priority = 10
185             end)
186
187             it("and global config as `false` should enable signs with the given priority", function()
188                 diagnostics.send(7, {})
189                 eq({priority = 10}, diagnostic_set_calls[1].opts.signs)
190             end)
191
192             it("and global config as a table should enable signs with the given priority", function()
193                 signs_config = {foo = "bar", priority = 5}
194                 diagnostics.send(7, {})
195                 eq(
196                     {foo = "bar", priority = 10},
197                     diagnostic_set_calls[1].opts.signs
198                 )
199             end)
200
201             it("and global config as a function should enable signs with the given priority", function()
202                 signs_config = function()
203                     return {foo = "bar", priority = 5}
204                 end
205                 diagnostics.send(7, {})
206
207                 local local_signs = diagnostic_set_calls[1].opts.signs
208
209                 eq("function", type(local_signs))
210                 eq({foo = "bar", priority = 10}, local_signs())
211             end)
212         end)
213     end
214
215     it("should toggle virtual_text correctly", function()
216         for _, value in ipairs({"all", "2", 2, "current", "1", 1, true}) do
217             diagnostic_set_calls = {}
218             _G.vim.g.ale_virtualtext_cursor = value
219             diagnostics.send(7, {})
220
221             eq({virtual_text = true}, diagnostic_set_calls[1].opts)
222         end
223
224         for _, value in ipairs({"disabled", "0", 0, false, nil}) do
225             diagnostic_set_calls = {}
226             _G.vim.g.ale_virtualtext_cursor = value
227             diagnostics.send(7, {})
228
229             eq({virtual_text = false}, diagnostic_set_calls[1].opts)
230         end
231     end)
232 end)