Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions cv/cv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2420,17 +2420,17 @@ cv_check_array(lua_State *L, struct cv_ctx *ctx,
const struct cv_node *n)
{
if (lua_type(L, data_idx) != LUA_TTABLE) {
int det = cv_ctx_push_error(L, ctx, n,
"TYPE_ERROR",
"Wrong type, expected array,"
" got map");
char msg[256];
const char *actual_type =
lua_typename(L, lua_type(L, data_idx));
snprintf(msg, sizeof(msg), "Wrong type, expected array, got %s",
actual_type);
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
if (det != 0) {
lua_pushstring(L, "array");
lua_setfield(L, det,
"expected_type");
lua_pushstring(L,
lua_typename(L,
lua_type(L, data_idx)));
lua_pushstring(L, actual_type);
lua_setfield(L, det,
"actual_type");
lua_pushvalue(L, data_idx);
Expand Down Expand Up @@ -2590,17 +2590,17 @@ cv_check_map(lua_State *L, struct cv_ctx *ctx,
const struct cv_node *n)
{
if (lua_type(L, data_idx) != LUA_TTABLE) {
int det = cv_ctx_push_error(L, ctx, n,
"TYPE_ERROR",
"Wrong type, expected map, got"
" non-table");
char msg[256];
const char *actual_type =
lua_typename(L, lua_type(L, data_idx));
snprintf(msg, sizeof(msg), "Wrong type, expected map, got %s",
actual_type);
int det = cv_ctx_push_error(L, ctx, n, "TYPE_ERROR", msg);
if (det != 0) {
lua_pushstring(L, "map");
lua_setfield(L, det,
"expected_type");
lua_pushstring(L,
lua_typename(L,
lua_type(L, data_idx)));
lua_pushstring(L, actual_type);
lua_setfield(L, det,
"actual_type");
lua_pushvalue(L, data_idx);
Expand Down
27 changes: 27 additions & 0 deletions test/compat_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,31 @@ function g.test_array_map_rejected()
t.assert_equals(e2[1].type, 'ARRAY_EXPECTED')
end

-- -------------------------------------------------------
-- array: make sure the error message is correct if a non-array
-- was provided instead of an array.
-- -------------------------------------------------------

function g.test_array_type_error_message()
local r, e = cv.check(1, {type = 'array'})
local exp = {
details = {actual_type = "number", expected_type = "array", value = 1},
message = "Wrong type, expected array, got number",
path = "$",
type = "TYPE_ERROR",
}
t.assert_equals(r, nil)
t.assert_equals(e, {exp})

r, e = cv.check(1, {type = 'map'})
exp = {
details = {actual_type = "number", expected_type = "map", value = 1},
message = "Wrong type, expected map, got number",
path = "$",
type = "TYPE_ERROR",
}
t.assert_equals(r, nil)
t.assert_equals(e, {exp})
end

-- vim: ts=4 sts=4 sw=4 et
Loading