diff --git a/lua/json/decode.lua b/lua/json/decode.lua index da01c01..c9dd457 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,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 - return state.previous + -- 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) 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