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
|
local assert = require("luassert")
local function buf(input)
local b = vim.api.nvim_create_buf(false, false)
vim.api.nvim_command("buffer " .. b)
vim.api.nvim_buf_set_lines(b, 0, -1, true, vim.split(input, "\n"))
return b
end
describe("plugin spec", function()
it("loads", function()
assert.is_true(vim.g.loaded_promqlfmt == 1)
assert.is_true(vim.fn.exists(":Promqlfmt") == 2)
end)
local tests = {
{
name = "formats whole buffer",
input = 'sum(rate(foo{bar="baz"}[5m])) by (bar)',
expected = 'sum by (bar) (rate(foo{bar="baz"}[5m]))',
fmtfn = function()
vim.api.nvim_command(":Promqlfmt")
end,
},
{
name = "formats visual selection of whole lines",
input = 'sum(rate(foo{bar="baz"}[5m])) by (bar)\nsum(rate(foo{bar="baz"}[5m])) by (bar)',
expected = 'sum(rate(foo{bar="baz"}[5m])) by (bar)\nsum by (bar) (rate(foo{bar="baz"}[5m]))',
fmtfn = function()
vim.api.nvim_command(":2")
vim.api.nvim_exec('execute "normal V\\<Esc>"', false)
vim.api.nvim_command(":'<,'>Promqlfmt")
end,
},
{
name = "formats visual selection of whole lines with padding",
input = [[
---
foo:
bar: |
((foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m-foo:bar:baz:by_action:rate5m{foo="bar"})/foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m)]],
expected = [[
---
foo:
bar: |
(
(foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"})
/
foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m
)]],
fmtfn = function()
vim.api.nvim_command(":4")
vim.api.nvim_exec('execute "normal V\\<Esc>"', false)
vim.api.nvim_command(":'<,'>Promqlfmt")
end,
},
{
name = "formats visual selection of multine whole lines with padding",
input = [[
---
foo:
bar: |
((foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"})
/
foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m)]],
expected = [[
---
foo:
bar: |
(
(foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"})
/
foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m
)]],
fmtfn = function()
vim.api.nvim_command(":4")
vim.api.nvim_exec('execute "normal V2j\\<Esc>"', false)
vim.api.nvim_command(":'<,'>Promqlfmt")
end,
},
}
for _, test in ipairs(tests) do
it(test.name, function()
buf(test.input)
test.fmtfn()
local result = vim.api.nvim_buf_get_lines(0, 0, -1, true)
assert.are.same(test.expected, table.concat(result, "\n"))
end)
end
end)
|