summaryrefslogtreecommitdiff
path: root/src/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test.c')
-rw-r--r--src/test.c123
1 files changed, 109 insertions, 14 deletions
diff --git a/src/test.c b/src/test.c
index f52fa99..7c4f144 100644
--- a/src/test.c
+++ b/src/test.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
+#include <stdbool.h>
#include <lua.h>
#include <lualib.h>
@@ -22,6 +23,8 @@
} \
} while (0)
+#define TEST(name) static char* name(lua_State *L)
+
int tests_run = 0;
int tests_failed = 0;
@@ -32,77 +35,77 @@ int tests_failed = 0;
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
-static char* hs_bool_to_string(lua_State *L)
+TEST(hs_bool_to_string)
{
mu_assert("HS_BOOL does not result in 'boolean'!",
strcmp(hs_type_to_string(HS_BOOL), "boolean") == 0);
return 0;
}
-static char* hs_int_to_string(lua_State *L)
+TEST(hs_int_to_string)
{
mu_assert("HS_INT does not result in 'integer'!",
strcmp(hs_type_to_string(HS_INT), "integer") == 0);
return 0;
}
-static char* hs_num_to_string(lua_State *L)
+TEST(hs_num_to_string)
{
mu_assert("HS_NUM does not result in 'number'!",
strcmp(hs_type_to_string(HS_NUM), "number") == 0);
return 0;
}
-static char* hs_str_to_string(lua_State *L)
+TEST(hs_str_to_string)
{
mu_assert("HS_STR does not result in 'string'!",
strcmp(hs_type_to_string(HS_STR), "string") == 0);
return 0;
}
-static char* hs_tbl_to_string(lua_State *L)
+TEST(hs_tbl_to_string)
{
mu_assert("HS_TBL does not result in 'table'!",
strcmp(hs_type_to_string(HS_TBL), "table") == 0);
return 0;
}
-static char* hs_func_to_string(lua_State *L)
+TEST(hs_func_to_string)
{
mu_assert("HS_FUNC does not result in 'function'!",
strcmp(hs_type_to_string(HS_FUNC), "function") == 0);
return 0;
}
-static char* hs_cfunc_to_string(lua_State *L)
+TEST(hs_cfunc_to_string)
{
mu_assert("HS_CFUNC does not result in 'C function'!",
strcmp(hs_type_to_string(HS_CFUNC), "C function") == 0);
return 0;
}
-static char* hs_user_to_string(lua_State *L)
+TEST(hs_user_to_string)
{
mu_assert("HS_USER does not result in 'userdata'!",
strcmp(hs_type_to_string(HS_USER), "userdata") == 0);
return 0;
}
-static char* hs_light_to_string(lua_State *L)
+TEST(hs_light_to_string)
{
mu_assert("HS_LIGHT does not result in 'light userdata'!",
strcmp(hs_type_to_string(HS_LIGHT), "light userdata") == 0);
return 0;
}
-static char* hs_nil_to_string(lua_State *L)
+TEST(hs_nil_to_string)
{
mu_assert("HS_NIL does not result in 'nil'!",
strcmp(hs_type_to_string(HS_NIL), "nil") == 0);
return 0;
}
-static char* hs_any_to_string(lua_State *L)
+TEST(hs_any_to_string)
{
mu_assert("HS_ANY does not result in 'any'!",
strcmp(hs_type_to_string(HS_ANY), "any") == 0);
@@ -112,12 +115,92 @@ static char* hs_any_to_string(lua_State *L)
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
+ * tests for hs_parse_args
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+TEST(parse_bool)
+{
+ lua_pushboolean(L, true);
+ bool b = false;
+ hs_parse_args(L, HS_BOOL, &b, HS_END);
+ mu_assert("failed to properly parse boolean!", b);
+ return 0;
+}
+
+TEST(parse_int)
+{
+ lua_pushinteger(L, 420);
+ int i = 0;
+ hs_parse_args(L, HS_INT, &i, HS_END);
+ mu_assert("failed to properly parse integer!", i == 420);
+ return 0;
+}
+
+TEST(parse_num)
+{
+ lua_pushnumber(L, 40.5f);
+ lua_Number n = 0;
+ hs_parse_args(L, HS_NUM, &n, HS_END);
+ mu_assert("failed to properly parse number!", n == 40.5f);
+ return 0;
+}
+
+TEST(parse_str)
+{
+ lua_pushstring(L, "hello, world!");
+ char *s = "";
+ hs_parse_args(L, HS_STR, &s, HS_END);
+ mu_assert("failed to properly parse string!",
+ strcmp(s, "hello, world!") == 0);
+ return 0;
+}
+
+int testfunc(lua_State *L) { return 0; }
+
+TEST(parse_func)
+{
+ lua_pushcfunction(L, testfunc);
+ lua_CFunction f;
+ hs_parse_args(L, HS_FUNC, &f, HS_END);
+ mu_assert("failed to properly parse function!", f == testfunc);
+ return 0;
+}
+
+TEST(parse_cfunc)
+{
+ lua_pushcfunction(L, testfunc);
+ lua_CFunction f;
+ hs_parse_args(L, HS_CFUNC, &f, HS_END);
+ mu_assert("failed to properly parse C function!", f == testfunc);
+ return 0;
+}
+
+int should_fail(lua_State *L)
+{
+ lua_CFunction f;
+ hs_parse_args(L, HS_CFUNC, &f, HS_END);
+ return 0;
+}
+
+TEST(fail_parse_noncfunc)
+{
+ lua_pushcfunction(L, should_fail);
+ lua_getglobal(L, "assert");
+ mu_assert("incorrectly parsed non-C function!",
+ lua_pcall(L, 1, 0, 0) != 0);
+ return 0;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
* tests for hs_pushstring
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
-static char* push_noformat(lua_State *L)
+TEST(push_noformat)
{
hs_pushstring(L, "a string");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
@@ -126,7 +209,7 @@ static char* push_noformat(lua_State *L)
return 0;
}
-static char* push_formatint(lua_State *L)
+TEST(push_formatint)
{
hs_pushstring(L, "%d is 5", 5);
mu_assert("no string at top of stack!", lua_isstring(L, -1));
@@ -135,7 +218,7 @@ static char* push_formatint(lua_State *L)
return 0;
}
-static char* push_formatstring(lua_State *L)
+TEST(push_formatstring)
{
hs_pushstring(L, "%s is 'hello'", "hello");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
@@ -168,11 +251,23 @@ int main()
mu_run_test("light to string", hs_light_to_string);
mu_run_test("nil to string", hs_nil_to_string);
mu_run_test("any to string", hs_any_to_string);
+ printf("\n");
+
+ printf("running hs_parse_args() tests...\n");
+ mu_run_test("parse bool", parse_bool);
+ mu_run_test("parse int", parse_int);
+ mu_run_test("parse num", parse_num);
+ mu_run_test("parse str", parse_str);
+ mu_run_test("parse func", parse_func);
+ 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);