summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/test.c340
1 files changed, 326 insertions, 14 deletions
diff --git a/src/test.c b/src/test.c
index 7c4f144..ff14377 100644
--- a/src/test.c
+++ b/src/test.c
@@ -8,19 +8,19 @@
#include "honeysuckle.h"
-/* minunit testing macros modified from those at
+/* minunit testing macros modified from those at
www.jera.com/techinfo/jtns/jtn002.html */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(name, test) do { \
- lua_State *L = luaL_newstate(); \
- luaL_openlibs(L); \
- char *message = test(L); \
- lua_close(L); \
- tests_run++; \
- if (message) { \
- printf("test '%s' failed: %s\n", name, message); \
- tests_failed++; \
- } \
+ lua_State *L = luaL_newstate(); \
+ luaL_openlibs(L); \
+ char *message = test(L); \
+ lua_close(L); \
+ tests_run++; \
+ if (message) { \
+ printf("test '%s' failed: %s\n", name, message); \
+ tests_failed++; \
+ } \
} while (0)
#define TEST(name) static char* name(lua_State *L)
@@ -120,6 +120,274 @@ TEST(hs_any_to_string)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
+// typechecking macros
+#define PARSE_TYPECHECK_TEST(name, type, constant) \
+ int name ## _testfunc(lua_State *L) { \
+ type test; hs_parse_args(L, constant, &test, HS_END); \
+ return 0; \
+ } \
+ TEST(name)
+#define PARSE_TYPECHECK_TEST_NOVAL(name, constant) \
+ int name ## _testfunc(lua_State *L) { \
+ hs_parse_args(L, constant, HS_END); return 0; \
+ } \
+ TEST(name)
+
+#define CHECK_FAIL_BOOL(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushboolean(L, true); \
+ mu_assert("incorrectly succeeded in parsing boolean!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_INT(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushinteger(L, 5); \
+ mu_assert("incorrectly succeeded in parsing integer!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_NUM(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushnumber(L, 42.0f); \
+ mu_assert("incorrectly succeeded in parsing number!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_STRING(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushstring(L, "hello!"); \
+ mu_assert("incorrectly succeeded in parsing string!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_TABLE(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "debug"); \
+ mu_assert("incorrectly succeeded in parsing table!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_FUNC(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "test"); \
+ mu_assert("incorrectly succeeded in parsing function!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_CFUNC(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushvalue(L, -1); \
+ mu_assert("incorrectly succeeded in parsing C function!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_USER(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_newuserdata(L, sizeof(char)); \
+ mu_assert("incorrectly succeeded in parsing userdata!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_LIGHT(name) \
+ int test_data = 5; \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushlightuserdata(L, &test_data); \
+ mu_assert("incorrectly succeeded in parsing light userdata!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+#define CHECK_FAIL_NIL(name) \
+ lua_pushcfunction(L, name ## _testfunc); lua_pushnil(L); \
+ mu_assert("incorrectly succeeded in parsing nil!", \
+ lua_pcall(L, 1, 0, 0) != 0);
+
+
+PARSE_TYPECHECK_TEST(parse_bool_typecheck, bool, HS_BOOL)
+{
+ lua_pushcfunction(L, parse_bool_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_TABLE(parse_bool_typecheck);
+ CHECK_FAIL_FUNC(parse_bool_typecheck);
+ CHECK_FAIL_CFUNC(parse_bool_typecheck);
+ CHECK_FAIL_USER(parse_bool_typecheck);
+ CHECK_FAIL_LIGHT(parse_bool_typecheck);
+ CHECK_FAIL_NIL(parse_bool_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST(parse_int_typecheck, int, HS_INT)
+{
+ CHECK_FAIL_BOOL(parse_int_typecheck);
+
+ lua_pushcfunction(L, parse_int_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_FUNC(parse_int_typecheck);
+ CHECK_FAIL_CFUNC(parse_int_typecheck);
+ CHECK_FAIL_USER(parse_int_typecheck);
+ CHECK_FAIL_LIGHT(parse_int_typecheck);
+ CHECK_FAIL_NIL(parse_int_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST(parse_num_typecheck, lua_Number, HS_NUM)
+{
+ CHECK_FAIL_BOOL(parse_num_typecheck);
+ CHECK_FAIL_INT(parse_num_typecheck);
+
+ lua_pushcfunction(L, parse_int_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_CFUNC(parse_num_typecheck);
+ CHECK_FAIL_USER(parse_num_typecheck);
+ CHECK_FAIL_LIGHT(parse_num_typecheck);
+ CHECK_FAIL_NIL(parse_num_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST(parse_string_typecheck, char *, HS_STR)
+{
+ CHECK_FAIL_BOOL(parse_string_typecheck);
+ CHECK_FAIL_INT(parse_string_typecheck);
+ CHECK_FAIL_NUM(parse_string_typecheck);
+
+ lua_pushcfunction(L, parse_string_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_USER(parse_string_typecheck);
+ CHECK_FAIL_LIGHT(parse_string_typecheck);
+ CHECK_FAIL_NIL(parse_string_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST_NOVAL(parse_table_typecheck, HS_TBL)
+{
+ CHECK_FAIL_BOOL(parse_table_typecheck);
+ CHECK_FAIL_INT(parse_table_typecheck);
+ CHECK_FAIL_NUM(parse_table_typecheck);
+ CHECK_FAIL_STRING(parse_table_typecheck);
+
+ lua_pushcfunction(L, parse_table_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_LIGHT(parse_table_typecheck);
+ CHECK_FAIL_NIL(parse_table_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST_NOVAL(parse_func_typecheck, HS_FUNC)
+{
+ CHECK_FAIL_BOOL(parse_func_typecheck);
+ CHECK_FAIL_INT(parse_func_typecheck);
+ CHECK_FAIL_NUM(parse_func_typecheck);
+ CHECK_FAIL_STRING(parse_func_typecheck);
+ CHECK_FAIL_TABLE(parse_func_typecheck);
+
+ lua_pushcfunction(L, parse_func_typecheck_testfunc);
+ 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);
+ CHECK_FAIL_NIL(parse_func_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST(parse_cfunc_typecheck, lua_CFunction, HS_CFUNC)
+{
+ CHECK_FAIL_BOOL(parse_cfunc_typecheck);
+ CHECK_FAIL_INT(parse_cfunc_typecheck);
+ CHECK_FAIL_NUM(parse_cfunc_typecheck);
+ CHECK_FAIL_STRING(parse_cfunc_typecheck);
+ CHECK_FAIL_TABLE(parse_cfunc_typecheck);
+ CHECK_FAIL_FUNC(parse_cfunc_typecheck);
+
+ lua_pushcfunction(L, parse_cfunc_typecheck_testfunc);
+ 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);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST(parse_user_typecheck, void *, HS_USER)
+{
+ CHECK_FAIL_BOOL(parse_user_typecheck);
+ CHECK_FAIL_INT(parse_user_typecheck);
+ CHECK_FAIL_NUM(parse_user_typecheck);
+ CHECK_FAIL_STRING(parse_user_typecheck);
+ CHECK_FAIL_TABLE(parse_user_typecheck);
+ CHECK_FAIL_FUNC(parse_user_typecheck);
+ CHECK_FAIL_CFUNC(parse_user_typecheck);
+
+ lua_pushcfunction(L, parse_user_typecheck_testfunc);
+ 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;
+}
+
+PARSE_TYPECHECK_TEST(parse_light_typecheck, void *, HS_LIGHT)
+{
+ CHECK_FAIL_BOOL(parse_light_typecheck);
+ CHECK_FAIL_INT(parse_light_typecheck);
+ CHECK_FAIL_NUM(parse_light_typecheck);
+ CHECK_FAIL_STRING(parse_light_typecheck);
+ CHECK_FAIL_TABLE(parse_light_typecheck);
+ CHECK_FAIL_FUNC(parse_light_typecheck);
+ CHECK_FAIL_CFUNC(parse_light_typecheck);
+ CHECK_FAIL_USER(parse_light_typecheck);
+
+ lua_pushcfunction(L, parse_light_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_light_typecheck);
+ return 0;
+}
+
+PARSE_TYPECHECK_TEST_NOVAL(parse_nil_typecheck, HS_NIL)
+{
+ CHECK_FAIL_BOOL(parse_nil_typecheck);
+ CHECK_FAIL_INT(parse_nil_typecheck);
+ CHECK_FAIL_NUM(parse_nil_typecheck);
+ CHECK_FAIL_STRING(parse_nil_typecheck);
+ CHECK_FAIL_TABLE(parse_nil_typecheck);
+ CHECK_FAIL_FUNC(parse_nil_typecheck);
+ CHECK_FAIL_CFUNC(parse_nil_typecheck);
+ CHECK_FAIL_USER(parse_nil_typecheck);
+ CHECK_FAIL_LIGHT(parse_nil_typecheck);
+
+ lua_pushcfunction(L, parse_nil_typecheck_testfunc);
+ lua_pushnil(L);
+ mu_assert("typecheck for nil failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ return 0;
+}
+
TEST(parse_bool)
{
lua_pushboolean(L, true);
@@ -193,6 +461,40 @@ TEST(fail_parse_noncfunc)
return 0;
}
+TEST(parse_userdata)
+{
+ void *userdata = lua_newuserdata(L, sizeof(char));
+ void *parsed = NULL;
+ hs_parse_args(L, HS_USER, &parsed, HS_END);
+ mu_assert("failed to properly parse userdata!",
+ parsed == userdata);
+ return 0;
+}
+
+TEST(parse_lightuserdata)
+{
+ int five = 5;
+ lua_pushlightuserdata(L, &five);
+
+ void *data;
+ hs_parse_args(L, HS_LIGHT, &data, HS_END);
+ mu_assert("failed to properly parse light userdata!",
+ data == &five);
+ return 0;
+}
+
+int parse_nil_testfunc(lua_State *L) { hs_parse_args(L, HS_NIL, HS_END); }
+
+TEST(parse_nil)
+{
+ lua_pushcfunction(L, parse_nil_testfunc);
+ lua_pushnil(L);
+ mu_assert("failed to properly parse nil!",
+ lua_pcall(L, 1, 0, 0) == 0);
+ return 0;
+}
+
+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_pushstring
@@ -224,7 +526,7 @@ TEST(push_formatstring)
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != 'hello is 'hello''",
- strcmp(string, "hello is 'hello'") == 0);
+ strcmp(string, "hello is 'hello'") == 0);
return 0;
}
@@ -238,7 +540,7 @@ TEST(push_formatstring)
int main()
{
printf("================ start tests ================\n\n");
-
+
printf("running hs_type_to_string() tests...\n");
mu_run_test("bool to string", hs_bool_to_string);
mu_run_test("int to string", hs_int_to_string);
@@ -254,6 +556,16 @@ int main()
printf("\n");
printf("running hs_parse_args() tests...\n");
+ mu_run_test("parse bool typecheck", parse_bool_typecheck);
+ mu_run_test("parse int typecheck", parse_int_typecheck);
+ mu_run_test("parse num typecheck", parse_num_typecheck);
+ mu_run_test("parse string typecheck", parse_string_typecheck);
+ mu_run_test("parse table typecheck", parse_table_typecheck);
+ mu_run_test("parse func typecheck", parse_func_typecheck);
+ mu_run_test("parse cfunc typecheck", parse_cfunc_typecheck);
+ mu_run_test("parse user typecheck", parse_user_typecheck);
+ mu_run_test("parse light typecheck", parse_light_typecheck);
+ mu_run_test("parse nil typecheck", parse_nil_typecheck);
mu_run_test("parse bool", parse_bool);
mu_run_test("parse int", parse_int);
mu_run_test("parse num", parse_num);
@@ -262,13 +574,13 @@ int main()
mu_run_test("parse cfunc", parse_cfunc);
mu_run_test("fail parse noncfunc", fail_parse_noncfunc);
printf("\n");
-
+
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");
-
+
printf("\n=============== tests finished ===============\n\n");
printf("ran %d tests, %d failed\n", tests_run, tests_failed);
return 0;