summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorChristian Segundo2023-10-08 22:30:48 +0200
committerChristian Segundo2023-10-08 22:30:48 +0200
commit9ff53495505b8b70971d50ef05b2863a9ce27112 (patch)
tree2c121e4de865febbe809379c6ec0fc725c92125d /test
parenta4644e03b1aa7daf0f2d4c0474d04c3b3aabebbd (diff)
parentafa6b4b6195483d73cb6a91bc757652301275ed6 (diff)
downloadpromqlfmt-9ff53495505b8b70971d50ef05b2863a9ce27112.tar.gz
Merge branch 'ft/ci'
Diffstat (limited to 'test')
-rw-r--r--test/init.lua37
-rw-r--r--test/plugin_spec.lua91
2 files changed, 128 insertions, 0 deletions
diff --git a/test/init.lua b/test/init.lua
new file mode 100644
index 0000000..f05408e
--- /dev/null
+++ b/test/init.lua
@@ -0,0 +1,37 @@
+local M = {}
+
+function M.root(root)
+ local f = debug.getinfo(1, "S").source:sub(2)
+ return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
+end
+
+---@param plugin string
+function M.load(plugin)
+ local name = plugin:match(".*/(.*)")
+ local package_root = M.root(".test/site/pack/deps/start/")
+ if not vim.loop.fs_stat(package_root .. name) then
+ print("Installing " .. plugin)
+ vim.fn.mkdir(package_root, "p")
+ vim.fn.system({
+ "git",
+ "clone",
+ "--depth=1",
+ "https://github.com/" .. plugin .. ".git",
+ package_root .. "/" .. name,
+ })
+ end
+end
+
+function M.setup()
+ vim.cmd([[set runtimepath=$VIMRUNTIME]])
+ vim.opt.runtimepath:append(M.root())
+ vim.opt.packpath = { M.root(".test/site") }
+ M.load("nvim-lua/plenary.nvim")
+ vim.env.XDG_CONFIG_HOME = M.root(".test/config")
+ vim.env.XDG_DATA_HOME = M.root(".test/data")
+ vim.env.XDG_STATE_HOME = M.root(".test/state")
+ vim.env.XDG_CACHE_HOME = M.root(".test/cache")
+ vim.api.nvim_command([[source plugin/promql.lua]])
+end
+
+M.setup()
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)