From 102b166c467427c6351c83d382696c06fc1b4023 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 13 Jul 2024 12:32:54 +0200 Subject: [PATCH] refactor: rename --- lua/neotest-golang/lib/cmd.lua | 2 +- lua/neotest-golang/lib/json.lua | 28 ++++++++++++---------------- lua/neotest-golang/process.lua | 2 +- tests/unit/json_spec.lua | 8 ++++---- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index cde3aadd..1ebc0d2e 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -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) diff --git a/lua/neotest-golang/lib/json.lua b/lua/neotest-golang/lib/json.lua index 984baaad..f569f4e1 100644 --- a/lua/neotest-golang/lib/json.lua +++ b/lua/neotest-golang/lib/json.lua @@ -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 @@ -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 diff --git a/lua/neotest-golang/process.lua b/lua/neotest-golang/process.lua index 2b237b0c..1c36682b 100644 --- a/lua/neotest-golang/process.lua +++ b/lua/neotest-golang/process.lua @@ -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 diff --git a/tests/unit/json_spec.lua b/tests/unit/json_spec.lua index 302c1a73..11f61d50 100644 --- a/tests/unit/json_spec.lua +++ b/tests/unit/json_spec.lua @@ -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) @@ -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) @@ -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() @@ -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)