From 8f2b1bb21725d5d228ac8b506cffd739336b36e5 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Thu, 27 May 2021 21:32:27 -0500 Subject: add hs_parse_overloaded tests --- src/honeysuckle.c | 6 +++ src/test.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 145 insertions(+), 13 deletions(-) diff --git a/src/honeysuckle.c b/src/honeysuckle.c index 9cc3720..e45da48 100644 --- a/src/honeysuckle.c +++ b/src/honeysuckle.c @@ -12,6 +12,12 @@ void hs_parse_args(lua_State *L, ...) L = L; } +int hs_parse_overloaded(lua_State *L, ...) +{ + L = L; + return -1; +} + void hs_pushstring(lua_State *L, const char *format_string, ...) { diff --git a/src/test.c b/src/test.c index 63dc637..9d202cc 100644 --- a/src/test.c +++ b/src/test.c @@ -187,7 +187,7 @@ PARSE_TYPECHECK_TEST(parse_bool_typecheck, bool, HS_BOOL) lua_pushboolean(L, true); mu_assert("typecheck for bool failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_INT(parse_bool_typecheck); CHECK_FAIL_NUM(parse_bool_typecheck); CHECK_FAIL_STRING(parse_bool_typecheck); @@ -208,7 +208,7 @@ PARSE_TYPECHECK_TEST(parse_int_typecheck, int, HS_INT) lua_pushinteger(L, 5); mu_assert("typecheck for int failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_NUM(parse_int_typecheck); CHECK_FAIL_STRING(parse_int_typecheck); CHECK_FAIL_TABLE(parse_int_typecheck); @@ -229,7 +229,7 @@ PARSE_TYPECHECK_TEST(parse_num_typecheck, lua_Number, HS_NUM) lua_pushnumber(L, 42.0f); mu_assert("typecheck for number failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_STRING(parse_num_typecheck); CHECK_FAIL_TABLE(parse_num_typecheck); CHECK_FAIL_FUNC(parse_num_typecheck); @@ -250,7 +250,7 @@ PARSE_TYPECHECK_TEST(parse_string_typecheck, char *, HS_STR) lua_pushstring(L, "hello!"); mu_assert("typecheck for string failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_TABLE(parse_string_typecheck); CHECK_FAIL_FUNC(parse_string_typecheck); CHECK_FAIL_CFUNC(parse_string_typecheck); @@ -271,7 +271,7 @@ PARSE_TYPECHECK_TEST(parse_table_typecheck, int, HS_TBL) lua_getglobal(L, "debug"); mu_assert("typecheck for table failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_FUNC(parse_table_typecheck); CHECK_FAIL_CFUNC(parse_table_typecheck); CHECK_FAIL_USER(parse_table_typecheck); @@ -292,7 +292,7 @@ PARSE_TYPECHECK_TEST(parse_func_typecheck, int, HS_FUNC) lua_getglobal(L, "test"); mu_assert("typecheck for function failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_CFUNC(parse_func_typecheck); CHECK_FAIL_USER(parse_func_typecheck); CHECK_FAIL_LIGHT(parse_func_typecheck); @@ -313,7 +313,7 @@ PARSE_TYPECHECK_TEST(parse_cfunc_typecheck, lua_CFunction, HS_CFUNC) lua_pushvalue(L, -1); mu_assert("typecheck for C function failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_USER(parse_cfunc_typecheck); CHECK_FAIL_LIGHT(parse_cfunc_typecheck); CHECK_FAIL_NIL(parse_cfunc_typecheck); @@ -334,12 +334,12 @@ PARSE_TYPECHECK_TEST(parse_user_typecheck, void *, HS_USER) 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); - + CHECK_FAIL_NIL(parse_user_typecheck); return 0; } @@ -359,7 +359,7 @@ PARSE_TYPECHECK_TEST(parse_light_typecheck, void *, HS_LIGHT) int testdata = 5; lua_pushlightuserdata(L, &testdata); mu_assert("typecheck for light userdata failed!", lua_pcall(L, 1, 0, 0) == 0); - + CHECK_FAIL_NIL(parse_light_typecheck); return 0; } @@ -380,7 +380,7 @@ PARSE_TYPECHECK_TEST(parse_nil_typecheck, int, HS_NIL) lua_pushnil(L); mu_assert("typecheck for nil failed!", lua_pcall(L, 1, 0, 0) == 0); - + return 0; } @@ -425,7 +425,7 @@ PARSE_TYPECHECK_TEST(parse_any_typecheck, int, HS_ANY) 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!", @@ -634,6 +634,120 @@ TEST(parse_all) return 0; } + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_parse_overloaded + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +#define PARSE_OVERLOADED \ + bool b; int i, ti, fi, ni; float f; char *str; \ + lua_CFunction fn; void *user, *light; \ + int choice = hs_parse_overloaded \ + (L, \ + HS_BOOL, &b, HS_END, HS_INT, &i, HS_END, HS_NUM, &f, HS_END, \ + HS_STR, &str, HS_END, HS_TBL, &ti, HS_END, HS_FUNC, &fi, \ + HS_END, HS_CFUNC, &fn, HS_END, HS_USER, &user, HS_END, \ + HS_LIGHT, &light, HS_END, HS_NIL, &ni, HS_END, HS_END); + +TEST(parse_bool_overloaded) +{ + lua_pushboolean(L, true); + PARSE_OVERLOADED; + mu_assert("boolean option was not chosen!", choice == 0); + mu_assert("failed to properly parse boolean!", b); + return 0; +} + +TEST(parse_integer_overloaded) +{ + lua_pushinteger(L, 5); + PARSE_OVERLOADED; + mu_assert("integer option was not chosen!", choice == 1); + mu_assert("failed to properly parse integer!", i == 5); + return 0; +} + +TEST(parse_number_overloaded) +{ + lua_pushnumber(L, 42.0f); + PARSE_OVERLOADED; + mu_assert("number option was not chosen!", choice == 2); + mu_assert("failed to properly parse boolean!", f == 42.0f); + return 0; +} + +TEST(parse_string_overloaded) +{ + lua_pushstring(L, "hello, world!"); + PARSE_OVERLOADED; + mu_assert("string option was not chosen!", choice == 3); + mu_assert("failed to properly parse string!", + strcmp(str, "hello, world!") == 0); + return 0; +} + +TEST(parse_table_overloaded) +{ + lua_getglobal(L, "debug"); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("table option was not chosen!", choice == 4); + mu_assert("failed to properly parse table!", ti == expected); + return 0; +} + +TEST(parse_function_overloaded) +{ + lua_getglobal(L, "type"); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("function option was not chosen!", choice == 5); + mu_assert("failed to properly parse function!", fi == expected); + return 0; +} + +TEST(parse_cfunction_overloaded) +{ + lua_pushcfunction(L, testfunc); + PARSE_OVERLOADED; + mu_assert("C function option was not chosen!", choice == 6); + mu_assert("failed to properly parse C function!", fn == testfunc); + return 0; +} + +TEST(parse_userdata_overloaded) +{ + void *userdata = lua_newuserdata(L, sizeof(char)); + PARSE_OVERLOADED; + mu_assert("userdata option was not chosen!", choice == 7); + mu_assert("failed to properly parse userdata!", user == userdata); + return 0; +} + +TEST(parse_lightuserdata_overloaded) +{ + int five = 5; + lua_pushlightuserdata(L, &five); + PARSE_OVERLOADED; + mu_assert("light userdata option was not chosen!", choice == 8); + mu_assert("failed to properly parse light userdata!", light == &five); + return 0; +} + +TEST(parse_nil_overloaded) +{ + lua_pushnil(L); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("nil option was not chosen!", choice == 9); + mu_assert("failed to properly parse nil!", ni == expected); + return 0; +} + + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * tests for hs_pushstring @@ -721,11 +835,23 @@ int main() mu_run_test("parse any", parse_any); mu_run_test("parse all", parse_all); + printf("running hs_parse_overloaded() parsing tests...\n"); + mu_run_test("parse bool overloaded", parse_bool_overloaded); + mu_run_test("parse integer overloaded", parse_integer_overloaded); + mu_run_test("parse number overloaded", parse_number_overloaded); + mu_run_test("parse string overloaded", parse_string_overloaded); + mu_run_test("parse table overloaded", parse_table_overloaded); + mu_run_test("parse function overloaded", parse_function_overloaded); + mu_run_test("parse cfunction overloaded", parse_cfunction_overloaded); + mu_run_test("parse userdata overloaded", parse_userdata_overloaded); + mu_run_test("parse lightuserdata overloaded", parse_lightuserdata_overloaded); + mu_run_test("parse nil overloaded", parse_nil_overloaded); + printf("running hs_pushstring() tests...\n"); mu_run_test("hs_pushstring (no printf formatting)", push_noformat); mu_run_test("hs_pushstring (integer formatting)", push_formatint); mu_run_test("hs_pushstring (string formatting)", push_formatstring); - + printf("\n=============== tests finished ===============\n\n"); const char *color = tests_failed == 0 ? GREEN : RED; -- cgit v1.2.1