diff options
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/test.c | 69 |
2 files changed, 64 insertions, 11 deletions
@@ -76,20 +76,22 @@ performs typechecking and in case of an error throws with helpful error messages. `hs_parse_args_fast()` does not perform typechecking, and as a result has much less friendly error messages. -`HS_TBL`, `HS_FUNC`, `HS_NIL`, and `HS_ANY` do not take a pointer, and are -simply used to confirm the presence of an argument on the stack. +`HS_TBL`, `HS_FUNC`, `HS_NIL`, and `HS_ANY` take a pointer to an integer +that will be filled with the stack index of the argument. ```C int some_lua_binding(lua_State *L) { bool b; int i; + int tbl_index; lua_Number n; char *str; void* user; hs_parse_args(L, HS_BOOL, &b, HS_INT, &i, + HS_TBL, &tbl_index, HS_NUM, &n, HS_STR, &str, HS_USER, &user, @@ -127,11 +127,6 @@ TEST(hs_any_to_string) return 0; \ } \ TEST(name) -#define PARSE_TYPECHECK_TEST_NOVAL(name, constant) \ - int name ## _testfunc(lua_State *L) { \ - hs_parse_args(L, constant, HS_END); return 0; \ - } \ - TEST(name) #define CHECK_FAIL_BOOL(name) \ lua_pushcfunction(L, name ## _testfunc); lua_pushboolean(L, true); \ @@ -264,7 +259,7 @@ PARSE_TYPECHECK_TEST(parse_string_typecheck, char *, HS_STR) return 0; } -PARSE_TYPECHECK_TEST_NOVAL(parse_table_typecheck, HS_TBL) +PARSE_TYPECHECK_TEST(parse_table_typecheck, int, HS_TBL) { CHECK_FAIL_BOOL(parse_table_typecheck); CHECK_FAIL_INT(parse_table_typecheck); @@ -284,7 +279,7 @@ PARSE_TYPECHECK_TEST_NOVAL(parse_table_typecheck, HS_TBL) return 0; } -PARSE_TYPECHECK_TEST_NOVAL(parse_func_typecheck, HS_FUNC) +PARSE_TYPECHECK_TEST(parse_func_typecheck, int, HS_FUNC) { CHECK_FAIL_BOOL(parse_func_typecheck); CHECK_FAIL_INT(parse_func_typecheck); @@ -368,7 +363,7 @@ PARSE_TYPECHECK_TEST(parse_light_typecheck, void *, HS_LIGHT) return 0; } -PARSE_TYPECHECK_TEST_NOVAL(parse_nil_typecheck, HS_NIL) +PARSE_TYPECHECK_TEST(parse_nil_typecheck, int, HS_NIL) { CHECK_FAIL_BOOL(parse_nil_typecheck); CHECK_FAIL_INT(parse_nil_typecheck); @@ -388,6 +383,61 @@ PARSE_TYPECHECK_TEST_NOVAL(parse_nil_typecheck, HS_NIL) return 0; } +PARSE_TYPECHECK_TEST(parse_any_typecheck, int, HS_ANY) +{ + lua_pushcfunction(L, parse_bool_typecheck_testfunc); + lua_pushboolean(L, true); + mu_assert("typecheck for bool failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_int_typecheck_testfunc); + lua_pushinteger(L, 5); + mu_assert("typecheck for int failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_int_typecheck_testfunc); + lua_pushnumber(L, 42.0f); + mu_assert("typecheck for number failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_string_typecheck_testfunc); + lua_pushstring(L, "hello!"); + mu_assert("typecheck for string failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_table_typecheck_testfunc); + lua_getglobal(L, "debug"); + mu_assert("typecheck for table failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_func_typecheck_testfunc); + lua_getglobal(L, "test"); + mu_assert("typecheck for function failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_cfunc_typecheck_testfunc); + lua_pushvalue(L, -1); + mu_assert("typecheck for C function failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_user_typecheck_testfunc); + lua_newuserdata(L, sizeof(char)); + mu_assert("typecheck for userdata failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_user_typecheck_testfunc); + int testdata = 5; lua_pushlightuserdata(L, &testdata); + mu_assert("typecheck for light userdata failed!", + lua_pcall(L, 1, 0, 0) == 0); + + lua_pushcfunction(L, parse_nil_typecheck_testfunc); + lua_pushnil(L); + mu_assert("typecheck for nil failed!", + lua_pcall(L, 1, 0, 0) == 0); + + return 0; +} + TEST(parse_bool) { lua_pushboolean(L, true); @@ -565,7 +615,8 @@ int main() mu_run_test("parse cfunc typecheck", parse_cfunc_typecheck); mu_run_test("parse user typecheck", parse_user_typecheck); mu_run_test("parse light typecheck", parse_light_typecheck); - mu_run_test("parse nil typecheck", parse_nil_typecheck); + mu_run_test("parse nil typecheck", parse_nil_typecheck); + mu_run_test("parse any typecheck", parse_any_typecheck); mu_run_test("parse bool", parse_bool); mu_run_test("parse int", parse_int); mu_run_test("parse num", parse_num); |