From 9782f9ff35b1fdfa67438ea620bd7c91497781a3 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 23 Aug 2021 11:26:46 -0500 Subject: update hs_parse_args_tests to reflect the lack of type distinction between integers and numbers --- CMakeLists.txt | 1 + src/hs_parse_args.c | 4 +++- src/tests/hs_parse_args_tests.c | 48 ++++++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e629ec9..08c28d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(HONEYSUCKLE_SOURCES ${HS_ROOT}/hs_type_to_string.c ${HS_ROOT}/hs_pushstring.c ${HS_ROOT}/hs_throw_error.c + ${HS_ROOT}/hs_parse_args.c ${HS_ROOT}/honeysuckle.c ) add_library(honeysuckle ${HONEYSUCKLE_SOURCES}) diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c index 6fb8f31..eee34e9 100644 --- a/src/hs_parse_args.c +++ b/src/hs_parse_args.c @@ -1,3 +1,5 @@ +#include + #include "honeysuckle.h" static bool check_parse(lua_State *L, int index, struct hs_arg *expected) @@ -104,7 +106,7 @@ void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments) lua_pushstring(L, ", "); } lua_pop(L, 1); - lua_pushstring(") instead"); + lua_pushstring(L, ") instead"); lua_concat(L, 1 + (2*n_args) + (2*n_provided)); lua_error(L); } diff --git a/src/tests/hs_parse_args_tests.c b/src/tests/hs_parse_args_tests.c index 7b03c24..39c40fb 100644 --- a/src/tests/hs_parse_args_tests.c +++ b/src/tests/hs_parse_args_tests.c @@ -20,53 +20,63 @@ static int testfunc(lua_State *L) { return 0; } #define CHECK_FAIL_BOOL(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushboolean(L, true); \ mu_assert("incorrectly succeeded in parsing boolean!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_INT(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushinteger(L, 5); \ mu_assert("incorrectly succeeded in parsing integer!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_NUM(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushnumber(L, 42.0f); \ mu_assert("incorrectly succeeded in parsing number!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_STRING(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushstring(L, "hello!"); \ mu_assert("incorrectly succeeded in parsing string!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_TABLE(name) \ lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "debug"); \ mu_assert("incorrectly succeeded in parsing table!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_FUNC(name) \ lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "test"); \ mu_assert("incorrectly succeeded in parsing function!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_CFUNC(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushvalue(L, -1); \ mu_assert("incorrectly succeeded in parsing C function!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_USER(name) \ lua_pushcfunction(L, name ## _testfunc); lua_newuserdata(L, sizeof(char)); \ mu_assert("incorrectly succeeded in parsing userdata!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_LIGHT(name) \ int test_data = 5; \ lua_pushcfunction(L, name ## _testfunc); lua_pushlightuserdata(L, &test_data); \ mu_assert("incorrectly succeeded in parsing light userdata!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); #define CHECK_FAIL_NIL(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushnil(L); \ mu_assert("incorrectly succeeded in parsing nil!", \ - lua_pcall(L, 1, 0, 0) != 0); + lua_pcall(L, 1, 0, 0) != 0); \ + lua_pop(L, 1); PARSE_TYPECHECK_TEST(parse_bool_typecheck, bool, hs_bool) @@ -88,7 +98,7 @@ PARSE_TYPECHECK_TEST(parse_bool_typecheck, bool, hs_bool) return 0; } -PARSE_TYPECHECK_TEST(parse_int_typecheck, int, hs_int) +PARSE_TYPECHECK_TEST(parse_int_typecheck, lua_Integer, hs_int) { CHECK_FAIL_BOOL(parse_int_typecheck); @@ -97,7 +107,9 @@ PARSE_TYPECHECK_TEST(parse_int_typecheck, int, hs_int) mu_assert("typecheck for int failed!", lua_pcall(L, 1, 0, 0) == 0); - CHECK_FAIL_NUM(parse_int_typecheck); + // we would check for num, but in lua 5.1 there + // isn't a separate integer type, so it would + // still work CHECK_FAIL_STRING(parse_int_typecheck); CHECK_FAIL_TABLE(parse_int_typecheck); CHECK_FAIL_FUNC(parse_int_typecheck); @@ -111,7 +123,9 @@ PARSE_TYPECHECK_TEST(parse_int_typecheck, int, hs_int) PARSE_TYPECHECK_TEST(parse_num_typecheck, lua_Number, hs_num) { CHECK_FAIL_BOOL(parse_num_typecheck); - CHECK_FAIL_INT(parse_num_typecheck); + // we would check for int, but in lua 5.1 there + // isn't a separate integer type, so it would + // still work lua_pushcfunction(L, parse_int_typecheck_testfunc); lua_pushnumber(L, 42.0f); @@ -347,7 +361,7 @@ TEST(parse_bool) TEST(parse_int) { lua_pushinteger(L, 420); - int i = 0; + lua_Integer i = 0; hs_parse_args(L, hs_int(i)); mu_assert("failed to properly parse integer!", i == 420); return 0; @@ -411,7 +425,7 @@ int should_fail(lua_State *L) TEST(fail_parse_noncfunc) { lua_pushcfunction(L, should_fail); - lua_getglobal(L, "type"); + lua_getglobal(L, "print"); mu_assert("incorrectly parsed non-C function!", lua_pcall(L, 1, 0, 0) != 0); return 0; @@ -481,8 +495,8 @@ TEST(parse_all) int any_index = lua_gettop(L); bool b; - int i; - float f; + lua_Integer i; + lua_Number f; char *str; int i_tbl; int i_func; -- cgit v1.2.1