From 6fb491ab320680435a96c54d04989ee7686e574a Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Mon, 25 May 2026 21:20:14 -0400 Subject: [PATCH 1/2] test: add coverage for expected util helper and remove dead diagnostic code --- lua/json/decode.lua | 4 ---- tests/lunit-error-reporting.lua | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lua/json/decode.lua b/lua/json/decode.lua index da01c01..7453053 100644 --- a/lua/json/decode.lua +++ b/lua/json/decode.lua @@ -104,9 +104,6 @@ local function generateDecoder(lexer, options) local function diagnostic_decoder(data) local state = decode_state.create(options) local parsed = diagnostic_parser:match(data) - if not parsed then - error("Invalid JSON data") - end local last_pos = 1 local i = 0 local ok, err = pcall(function() @@ -133,7 +130,6 @@ local function generateDecoder(lexer, options) local line, col, char, last_line = util.get_invalid_character_info(data, last_pos) error(err .. (" @ character: %i %i:%i [%s] line:\n%s"):format(last_pos, line, col, char, last_line)) end - return state.previous end local decoder = function(data) diff --git a/tests/lunit-error-reporting.lua b/tests/lunit-error-reporting.lua index a03c9a7..d3a0af7 100644 --- a/tests/lunit-error-reporting.lua +++ b/tests/lunit-error-reporting.lua @@ -126,3 +126,25 @@ end function test_missing_array_value() assert_decode_error('[1,, 2]', "Expected value in array", nil, 1, 4) end + +-- Test: expected utility helper with multiple arguments +function test_util_expected_helper() + local util = require("json.decode.util") + local pattern = util.expected("foo", "bar") + local ok, err = pcall(function() + pattern:match("data") + end) + assert_false(ok) + assert_not_nil(string_find(tostring(err), "expected one of 'foo','bar'", 1, true)) +end + +-- Test: expected utility helper with single argument +function test_util_expected_single_helper() + local util = require("json.decode.util") + local pattern = util.expected("baz") + local ok, err = pcall(function() + pattern:match("data") + end) + assert_false(ok) + assert_not_nil(string_find(tostring(err), "expected 'baz'", 1, true)) +end From 2e4b56b3442363f09699cbb1c09b8618341a1fa6 Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Mon, 25 May 2026 21:22:57 -0400 Subject: [PATCH 2/2] fix: add unreachable assert guard to end of diagnostic_decoder --- lua/json/decode.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/json/decode.lua b/lua/json/decode.lua index 7453053..c9dd457 100644 --- a/lua/json/decode.lua +++ b/lua/json/decode.lua @@ -130,6 +130,8 @@ local function generateDecoder(lexer, options) local line, col, char, last_line = util.get_invalid_character_info(data, last_pos) error(err .. (" @ character: %i %i:%i [%s] line:\n%s"):format(last_pos, line, col, char, last_line)) end + -- This line should never be reached since diagnostic_decoder is only run on failure paths + assert(false, "Diagnostic decoder finished without throwing an error") end local decoder = function(data)