diff options
Diffstat (limited to 'src/test.c')
-rw-r--r-- | src/test.c | 66 |
1 files changed, 65 insertions, 1 deletions
@@ -808,6 +808,66 @@ TEST(parse_2_3_overload_1) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * + * tests for hs_create_table + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +TEST(table_correct_index) +{ + int index = hs_create_table(L, HS_END); + mu_assert("returned incorrect index!", index == lua_gettop(L)); + return 0; +} + + +#define check_table_element(field, is_type, typestring, type, conversion, comparison) \ + do { \ + lua_getfield(L, -1, field); \ + if (!is_type(L, -1)) { lua_pop(L, 1); return "field '" field "' is not type " typestring "!"; } \ + type value = conversion(L, -1); \ + lua_pop(L, 1); \ + mu_assert("field '" field "' does not match the expected value!", comparison); \ + } while(0) + +#define check_bool(field, expected) check_table_element(field, lua_isboolean, "boolean", bool, lua_toboolean, value == expected) +#define check_int(field, expected) check_table_element(field, lua_isnumber, "integer", int, lua_tointeger, value == expected) +#define check_num(field, expected) check_table_element(field, lua_isnumber, "number", float, lua_tonumber, value == expected) +#define check_string(field, expected) check_table_element(field, lua_isstring, "string", char*, lua_tostring, strcmp(value, expected)) +#define check_cfunc(field, expected) check_table_element(field, lua_iscfunction, "C function", lua_CFunction, lua_tocfunction, value == expected) +#define check_user(field, expected) check_table_element(field, lua_isuserdata, "userdata", void*, lua_touserdata, value == expected) +#define check_light(field, expected) check_table_element(field, lua_islightuserdata, "light userdata", void*, lua_touserdata, value == expected) + +TEST(table_create_basic_types) +{ + void *userdata = lua_newuserdata(L, sizeof(char)); + int user_index = lua_gettop(L); + char lightuserdata = 'F'; + + hs_create_table + (L, + "boolValue", HS_BOOL, false, + "intValue", HS_INT, 15, + "numValue", HS_NUM, 33.66, + "stringValue", HS_STR, "goober", + "cfuncValue", HS_CFUNC, testfunc, + "userValue", HS_USER, user_index, + "lightValue", HS_LIGHT, &lightuserdata, + HS_END); + + check_bool("boolValue", false); + check_int("intValue", 15); + check_num("numValue", 33.66); + check_string("stringValue", "goober"); + check_cfunc("cfuncValue", testfunc); + check_user("userValue", userdata); + check_light("lightValue", &lightuserdata); + return 0; +} + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * * tests for hs_pushstring * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -907,7 +967,11 @@ int main() 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); - + + printf("running hs_create_table() tests...\n"); + mu_run_test("return correct stack index", table_correct_index); + mu_run_test("check creation of basic types", table_create_basic_types); + 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); |