Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 13, 2024
1 parent e50f5fd commit 102b166
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function M.golist_data(cwd)
}
local output =
vim.fn.system("cd " .. cwd .. " && " .. table.concat(go_list_command, " "))
return json.process_golist_output(output)
return json.decode_from_string(output)
end

function M.test_command_in_package(package_or_path)
Expand Down
28 changes: 12 additions & 16 deletions lua/neotest-golang/lib/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

local M = {}

--- Process output from 'go test -json' and return an iterable table.
--- @param raw_output table
--- Decode JSON from a table of strings into a table of objects.
--- @param tbl table
--- @return table
function M.process_gotest_json_output(raw_output)
function M.decode_from_table(tbl)
local jsonlines = {}
for _, line in ipairs(raw_output) do
for _, line in ipairs(tbl) do
if string.match(line, "^%s*{") then -- must start with the `{` character
local status, json_data = pcall(vim.fn.json_decode, line)
if status then
Expand All @@ -23,26 +23,22 @@ function M.process_gotest_json_output(raw_output)
return jsonlines
end

--- Process output from 'go list -json' an iterable lua table.
--- @param raw_output string
--- Decode JSON from a string into a table of objects.
--- @param str string
--- @return table
function M.process_golist_output(raw_output)
function M.decode_from_string(str)
-- Split the input into separate JSON objects
local json_objects = {}
local tbl = {}
local current_object = ""
for line in raw_output:gmatch("[^\r\n]+") do
for line in str:gmatch("[^\r\n]+") do
if line:match("^%s*{") and current_object ~= "" then
table.insert(json_objects, current_object)
table.insert(tbl, current_object)
current_object = ""
end
current_object = current_object .. line
end
table.insert(json_objects, current_object)

local jsonlines = M.process_gotest_json_output(json_objects)

-- Return the table of objects
return jsonlines
table.insert(tbl, current_object)
return M.decode_from_table(tbl)
end

return M
2 changes: 1 addition & 1 deletion lua/neotest-golang/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function M.test_results(spec, result, tree)
raw_output = async.fn.readfile(context.test_output_json_filepath)
end

local gotest_output = lib.json.process_gotest_json_output(raw_output)
local gotest_output = lib.json.decode_from_table(raw_output)

--- The 'go list -json' output, converted into a lua table.
local golist_output = context.golist_data
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/json_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)

Expand All @@ -26,7 +26,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" }, { Dir = "bar" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)

Expand All @@ -45,7 +45,7 @@ describe("Go list", function()
local expected = { { Dir = "foo" }, { Dir = "bar" }, { Dir = "baz" } }
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)
it("Returns nested entries", function()
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("Go list", function()
}
assert.are_same(
vim.inspect(expected),
vim.inspect(lib.json.process_golist_output(input))
vim.inspect(lib.json.decode_from_string(input))
)
end)
end)

0 comments on commit 102b166

Please sign in to comment.