132 lines
4.1 KiB
Lua
132 lines
4.1 KiB
Lua
local t = require("test")
|
|
local cmd = require("git.cmd")
|
|
|
|
t.test("parse_args splits on whitespace", function()
|
|
t.eq(
|
|
cmd.parse_args("config user.name value"),
|
|
{ "config", "user.name", "value" }
|
|
)
|
|
end)
|
|
|
|
t.test("parse_args preserves spaces inside double quotes", function()
|
|
t.eq(
|
|
cmd.parse_args([[config user.name "Oscar Wallberg"]]),
|
|
{ "config", "user.name", "Oscar Wallberg" }
|
|
)
|
|
end)
|
|
|
|
t.test("parse_args preserves spaces inside single quotes", function()
|
|
t.eq(
|
|
cmd.parse_args([[log --grep='bug fix' --author=Oscar]]),
|
|
{ "log", "--grep=bug fix", "--author=Oscar" }
|
|
)
|
|
end)
|
|
|
|
t.test("parse_args handles backslash-escaped space", function()
|
|
t.eq(cmd.parse_args([[a\ b c]]), { "a b", "c" })
|
|
end)
|
|
|
|
t.test("parse_args handles escaped quote inside double quotes", function()
|
|
t.eq(cmd.parse_args([["a\"b" c]]), { 'a"b', "c" })
|
|
end)
|
|
|
|
t.test(
|
|
"parse_args treats backslash literally inside single quotes",
|
|
function()
|
|
t.eq(cmd.parse_args([['a\b' c]]), { "a\\b", "c" })
|
|
end
|
|
)
|
|
|
|
t.test("parse_args concatenates adjacent quoted segments", function()
|
|
t.eq(cmd.parse_args([[foo"bar"baz]]), { "foobarbaz" })
|
|
end)
|
|
|
|
t.test("parse_args handles tabs as separators", function()
|
|
t.eq(cmd.parse_args("a\tb\tc"), { "a", "b", "c" })
|
|
end)
|
|
|
|
t.test("parse_args returns empty list for empty or whitespace input", function()
|
|
t.eq(cmd.parse_args(""), {})
|
|
t.eq(cmd.parse_args(" \t "), {})
|
|
end)
|
|
|
|
t.test("parse_args preserves empty quoted token", function()
|
|
t.eq(cmd.parse_args([[a "" b]]), { "a", "", "b" })
|
|
end)
|
|
|
|
t.test("parse_args expands %% on unquoted token", function()
|
|
local buf = vim.api.nvim_create_buf(false, false)
|
|
t.defer(function()
|
|
pcall(vim.api.nvim_buf_delete, buf, { force = true })
|
|
end)
|
|
vim.api.nvim_buf_set_name(buf, vim.fn.getcwd() .. "/some-file.lua")
|
|
vim.api.nvim_set_current_buf(buf)
|
|
|
|
t.eq(
|
|
cmd.parse_args("add %"),
|
|
{ "add", vim.fn.getcwd() .. "/some-file.lua" }
|
|
)
|
|
end)
|
|
|
|
t.test("parse_args does not expand %% inside double quotes", function()
|
|
local buf = vim.api.nvim_create_buf(false, false)
|
|
t.defer(function()
|
|
pcall(vim.api.nvim_buf_delete, buf, { force = true })
|
|
end)
|
|
vim.api.nvim_buf_set_name(buf, vim.fn.getcwd() .. "/some-file.lua")
|
|
vim.api.nvim_set_current_buf(buf)
|
|
|
|
t.eq(cmd.parse_args([[log -- "%"]]), { "log", "--", "%" })
|
|
end)
|
|
|
|
t.test("parse_args does not expand %% inside single quotes", function()
|
|
local buf = vim.api.nvim_create_buf(false, false)
|
|
t.defer(function()
|
|
pcall(vim.api.nvim_buf_delete, buf, { force = true })
|
|
end)
|
|
vim.api.nvim_buf_set_name(buf, vim.fn.getcwd() .. "/some-file.lua")
|
|
vim.api.nvim_set_current_buf(buf)
|
|
|
|
t.eq(cmd.parse_args([[log -- '%']]), { "log", "--", "%" })
|
|
end)
|
|
|
|
t.test("parse_args does not treat mid-token tilde as expansion", function()
|
|
t.eq(cmd.parse_args("checkout HEAD~3"), { "checkout", "HEAD~3" })
|
|
end)
|
|
|
|
t.test("parse_args expands leading ~/ to home", function()
|
|
t.eq(cmd.parse_args("add ~/foo"), { "add", vim.fn.expand("~/foo") })
|
|
end)
|
|
|
|
t.test("parse_complete_state with trailing space", function()
|
|
local s = cmd._parse_complete_state("G push origin ")
|
|
t.eq(s.prior, { "push", "origin" })
|
|
t.falsy(s.after_separator)
|
|
end)
|
|
|
|
t.test("parse_complete_state mid-token", function()
|
|
local s = cmd._parse_complete_state("G push or")
|
|
t.eq(s.prior, { "push" })
|
|
t.falsy(s.after_separator)
|
|
end)
|
|
|
|
t.test("parse_complete_state empty after command", function()
|
|
local s = cmd._parse_complete_state("G ")
|
|
t.eq(s.prior, {})
|
|
t.falsy(s.after_separator)
|
|
end)
|
|
|
|
t.test("parse_complete_state detects -- separator", function()
|
|
local s = cmd._parse_complete_state("G log -- foo")
|
|
t.eq(s.prior, { "log", "--" })
|
|
t.truthy(s.after_separator)
|
|
end)
|
|
|
|
t.test("positional_index ignores flags", function()
|
|
t.eq(cmd._positional_index({ "push" }), 1)
|
|
t.eq(cmd._positional_index({ "push", "origin" }), 2)
|
|
t.eq(cmd._positional_index({ "push", "--force" }), 1)
|
|
t.eq(cmd._positional_index({ "push", "--force", "origin" }), 2)
|
|
t.eq(cmd._positional_index({ "checkout", "-b", "feature" }), 2)
|
|
end)
|