diff options
Diffstat (limited to 'src/tests/hs_parse_overloaded_tests.c')
-rw-r--r-- | src/tests/hs_parse_overloaded_tests.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/src/tests/hs_parse_overloaded_tests.c b/src/tests/hs_parse_overloaded_tests.c new file mode 100644 index 0000000..de364db --- /dev/null +++ b/src/tests/hs_parse_overloaded_tests.c @@ -0,0 +1,172 @@ +#include "hs_tests.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * 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; +} + +TEST(parse_2_3_overload_0) +{ + lua_pushinteger(L, 69); + lua_pushstring(L, "foo"); + int i; char *str; void *data; + int choice = hs_parse_overloaded + (L, HS_INT, &i, HS_STR, &str, HS_END, + HS_INT, &i, HS_STR, &str, HS_USER, &data, HS_END, + HS_END); + + mu_assert("incorrectly selected option other than 0!", choice == 0); + mu_assert("failed to properly parse integer!", i == 69); + mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); + return 0; +} + +TEST(parse_2_3_overload_1) +{ + lua_pushinteger(L, 69); + lua_pushstring(L, "foo"); + void *userdata = lua_newuserdata(L, sizeof(char)); + int i; char *str; void *data; + int choice = hs_parse_overloaded + (L, HS_INT, &i, HS_STR, &str, HS_END, + HS_INT, &i, HS_STR, &str, HS_USER, &data, HS_END, + HS_END); + + mu_assert("incorrectly selected option other than 1!", choice == 1); + mu_assert("failed to properly parse integer!", i == 69); + mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); + mu_assert("failed to properly parse userdata!", userdata == data); + return 0; +} + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * test suite + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +void hs_parse_overloaded_tests() +{ + 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); + mu_run_test("parse 2/3 overload 0", parse_2_3_overload_0); + mu_run_test("parse 2/3 overload 1", parse_2_3_overload_1); +} |