From afa6b4b6195483d73cb6a91bc757652301275ed6 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Sun, 8 Oct 2023 11:52:40 +0200 Subject: Add test cases --- test/plugin_spec.lua | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/plugin_spec.lua (limited to 'test/plugin_spec.lua') 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\\"', 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\\"', 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\\"', 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) -- cgit v1.2.3