diff options
author | sanine-a <sanine.not@pm.me> | 2021-05-27 20:59:26 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-05-27 20:59:26 -0500 |
commit | 0ed58392868f1b17fe0ab1b6ddf598ec82eb7170 (patch) | |
tree | f4e76069ff931056f09ad17f28fb872cb4cbbc60 | |
parent | 198832796a909e9edff44d72f555f7db0177e686 (diff) |
add parse tests for table, any, and all types at once
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | src/honeysuckle.h | 1 | ||||
-rw-r--r-- | src/test.c | 84 |
3 files changed, 84 insertions, 4 deletions
@@ -73,8 +73,7 @@ const string representing the type. arguments a lua_State pointer, any number of alternating type constant and pointer pairs, and finally `HS_END` to close the arguments. This function 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. +messages. `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. diff --git a/src/honeysuckle.h b/src/honeysuckle.h index b996aed..43e5f51 100644 --- a/src/honeysuckle.h +++ b/src/honeysuckle.h @@ -23,7 +23,6 @@ const char* hs_type_to_string(int type); void hs_parse_args(lua_State *L, ...); -void hs_parse_args_fast(lua_State *L, ...); int hs_parse_overloaded(lua_State *L, ...); int hs_create_table(lua_State *L, ...); @@ -484,6 +484,16 @@ TEST(parse_str) return 0; } +TEST(parse_tbl) +{ + lua_getglobal(L, "debug"); + int correct_index = lua_gettop(L); + int tbl_index; + hs_parse_args(L, HS_TBL, &tbl_index, HS_END); + mu_assert("failed to properly parse table!", tbl_index == correct_index); + return 0; +} + TEST(parse_func) { lua_getglobal(L, "type"); @@ -554,6 +564,75 @@ TEST(parse_nil) return 0; } +TEST(parse_any) +{ + lua_pushnil(L); + int correct_index = lua_gettop(L); + int index = 0; + hs_parse_args(L, HS_ANY, &index, HS_END); + mu_assert("failed to properly parse [any]!", + index == correct_index); + return 0; +} + +TEST(parse_all) +{ + lua_pushboolean(L, true); + lua_pushinteger(L, 420); + lua_pushnumber(L, 40.5f); + lua_pushstring(L, "hello, world!"); + lua_getglobal(L, "debug"); + int tbl_index = lua_gettop(L); + lua_getglobal(L, "type"); + int func_index = lua_gettop(L); + lua_pushcfunction(L, testfunc); + void *userdata = lua_newuserdata(L, sizeof(char)); + int five = 5; + lua_pushlightuserdata(L, &five); + lua_pushnil(L); + int nil_index = lua_gettop(L); + lua_pushnil(L); + int any_index = lua_gettop(L); + + bool b; + int i; + float f; + char *str; + int i_tbl; + int i_func; + lua_CFunction fn; + void *user; + void *light; + int i_nil; + int i_any; + hs_parse_args + (L, + HS_BOOL, &b, + HS_INT, &i, + HS_NUM, &f, + HS_STR, &str, + HS_TBL, &i_tbl, + HS_FUNC, &i_func, + HS_CFUNC, &fn, + HS_USER, &user, + HS_LIGHT, &light, + HS_NIL, &i_nil, + HS_ANY, &i_any, + HS_END); + mu_assert("failed to properly parse boolean!", b); + mu_assert("failed to properly parse integer!", i == 420); + mu_assert("failed to properly parse number!", f == 40.5f); + mu_assert("failed to properly parse string!", + strcmp(str, "hello, world!") == 0); + mu_assert("failed to properly parse table!", i_tbl == tbl_index); + mu_assert("failed to properly parse function!", func_index == i_func); + mu_assert("failed to properly parse C function!", fn == testfunc); + mu_assert("failed to properly parse userdata!", user == userdata); + mu_assert("failed to properly parse light userdata!", light == &five); + mu_assert("failed to properly parse nil!", nil_index == i_nil); + mu_assert("failed to properly parse [any]!", any_index == i_any); + return 0; +} /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * @@ -632,12 +711,15 @@ int main() mu_run_test("parse int", parse_int); mu_run_test("parse num", parse_num); mu_run_test("parse str", parse_str); + mu_run_test("parse tbl", parse_tbl); mu_run_test("parse func", parse_func); mu_run_test("parse cfunc", parse_cfunc); mu_run_test("fail parse noncfunc", fail_parse_noncfunc); mu_run_test("parse userdata", parse_userdata); mu_run_test("parse lightuserdata", parse_lightuserdata); - mu_run_test("parse_nil", parse_nil); + mu_run_test("parse nil", parse_nil); + mu_run_test("parse any", parse_any); + mu_run_test("parse all", parse_all); printf("running hs_pushstring() tests...\n"); mu_run_test("hs_pushstring (no printf formatting)", push_noformat); |