From 8c66847bfc4a6af1ca86e916b678a6b8ce58be2f Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 11 Jun 2021 17:01:18 -0500 Subject: add initial tests for hs_create_table --- src/honeysuckle.c | 8 +++++++ src/test.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/honeysuckle.c b/src/honeysuckle.c index e45da48..1015343 100644 --- a/src/honeysuckle.c +++ b/src/honeysuckle.c @@ -19,6 +19,14 @@ int hs_parse_overloaded(lua_State *L, ...) } +int hs_create_table(lua_State *L, ...) +{ + lua_createtable(L, 0, 0); + L = L; + return 0; +} + + void hs_pushstring(lua_State *L, const char *format_string, ...) { lua_pushnumber(L, 0.4); diff --git a/src/test.c b/src/test.c index 4ae48ee..816500b 100644 --- a/src/test.c +++ b/src/test.c @@ -806,6 +806,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); -- cgit v1.2.1