summaryrefslogtreecommitdiff
path: root/test/plugin_spec.lua
diff options
context:
space:
mode:
authorChristian Segundo2023-10-08 11:52:40 +0200
committerChristian Segundo2023-10-08 22:30:00 +0200
commitafa6b4b6195483d73cb6a91bc757652301275ed6 (patch)
tree2c121e4de865febbe809379c6ec0fc725c92125d /test/plugin_spec.lua
parenta4644e03b1aa7daf0f2d4c0474d04c3b3aabebbd (diff)
downloadpromqlfmt-afa6b4b6195483d73cb6a91bc757652301275ed6.tar.gz
Add test cases
Diffstat (limited to 'test/plugin_spec.lua')
-rw-r--r--test/plugin_spec.lua91
1 files changed, 91 insertions, 0 deletions
diff --git a/test/plugin_spec.lua b/test/plugin_spec.lua
new file mode 100644
index 0000000..b24c8da
--- /dev/null
+++ b/test/plugin_spec.lua
@@ -0,0 +1,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)