summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt16
-rw-r--r--src/honeysuckle.c25
-rw-r--r--src/honeysuckle.h85
-rw-r--r--src/hs_parse_args.c111
-rw-r--r--src/hs_pushstring.c31
-rw-r--r--src/hs_throw_error.c21
-rw-r--r--src/hs_type_to_string.c31
-rw-r--r--src/tests/hs_parse_args_tests.c882
-rw-r--r--src/tests/tests_main.c8
-rw-r--r--src/va_nargs.h75
11 files changed, 775 insertions, 511 deletions
diff --git a/.gitignore b/.gitignore
index bdc5af0..588f95f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
*~
build
+src/va_nargs.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6bc86a7..e629ec9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,14 @@ find_package(Lua51 REQUIRED)
include_directories(${LUA_INCLUDE_DIR})
# build and link the library
-add_library(honeysuckle src/honeysuckle.c)
+set(HS_ROOT ${CMAKE_SOURCE_DIR}/src)
+set(HONEYSUCKLE_SOURCES
+ ${HS_ROOT}/hs_type_to_string.c
+ ${HS_ROOT}/hs_pushstring.c
+ ${HS_ROOT}/hs_throw_error.c
+ ${HS_ROOT}/honeysuckle.c
+ )
+add_library(honeysuckle ${HONEYSUCKLE_SOURCES})
set_target_properties(honeysuckle PROPERTIES CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic")
target_link_libraries(honeysuckle ${LUA_LIBRARIES})
set_target_properties(honeysuckle PROPERTIES
@@ -20,10 +27,9 @@ set(TEST_SOURCES
${TEST_ROOT}/tests_main.c
${TEST_ROOT}/hs_type_to_string_tests.c
${TEST_ROOT}/hs_parse_args_tests.c
- ${TEST_ROOT}/hs_parse_overloaded_tests.c
- ${TEST_ROOT}/hs_create_table_tests.c
- ${TEST_ROOT}/hs_create_enum_tests.c
- ${TEST_ROOT}/hs_process_table_tests.c
+ # ${TEST_ROOT}/hs_parse_overloaded_tests.c
+ # ${TEST_ROOT}/hs_create_table_tests.c
+ # ${TEST_ROOT}/hs_process_table_tests.c
${TEST_ROOT}/hs_throw_error_tests.c
# ${TEST_ROOT}/hs_traceback_tests.c
# ${TEST_ROOT}/hs_call_tests.c
diff --git a/src/honeysuckle.c b/src/honeysuckle.c
index 339b278..1d8da6b 100644
--- a/src/honeysuckle.c
+++ b/src/honeysuckle.c
@@ -1,18 +1,11 @@
#include "honeysuckle.h"
-const char* hs_type_to_string(int type)
-{
- type = type;
- return "mock";
-}
-
-
-void hs_parse_args(lua_State *L, ...)
+void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments)
{
L = L;
}
-int hs_parse_overloaded(lua_State *L, ...)
+int hs_parse_overloaded_(lua_State *L, ...)
{
L = L;
return -1;
@@ -45,17 +38,3 @@ void hs_pt_set_boolean(bool value, void *data) { value=value; data=data; }
void hs_pt_set_integer(lua_Integer value, void *data) { value=value; data=data; }
void hs_pt_set_number(lua_Number value, void *data) { value=value; data=data; }
void hs_pt_set_string(const char *value, void *data) { value=value; data=data; }
-
-
-void hs_throw_error(lua_State *L, const char *format_string, ...)
-{
- L = L;
- format_string = format_string;
-}
-
-
-void hs_pushstring(lua_State *L, const char *format_string, ...)
-{
- lua_pushnumber(L, 0.4);
- format_string = format_string;
-}
diff --git a/src/honeysuckle.h b/src/honeysuckle.h
index 1890c40..9aecddf 100644
--- a/src/honeysuckle.h
+++ b/src/honeysuckle.h
@@ -7,28 +7,66 @@
#include <lualib.h>
#include <lauxlib.h>
-#define HS_END 0
+#include "va_nargs.h"
/* type constants */
-#define HS_BOOL 1
-#define HS_INT 2
-#define HS_NUM 3
-#define HS_STR 4
-#define HS_TBL 5
-#define HS_FUNC 6
-#define HS_CFUNC 7
-#define HS_USER 8
-#define HS_LIGHT 9
-#define HS_NIL 10
-#define HS_ANY 11
-
-const char* hs_type_to_string(int type);
-
-void hs_parse_args(lua_State *L, ...);
-int hs_parse_overloaded(lua_State *L, ...);
+typedef enum
+ { HS_BOOL,
+ HS_INT,
+ HS_NUM,
+ HS_STR,
+ HS_TBL,
+ HS_FUNC,
+ HS_CFUNC,
+ HS_USER,
+ HS_LIGHT,
+ HS_NIL,
+ HS_ANY,
+ } hs_type;
+
+const char* hs_type_to_string(hs_type type);
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * hs_parse_args and hs_parse_overloaded
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+struct hs_arg {
+ hs_type type;
+ union {
+ bool *boolean;
+ lua_Integer *integer;
+ lua_Number *number;
+ char **string;
+ int *stack_index;
+ lua_CFunction *function;
+ void **userdata;
+ } ptr;
+};
+
+#define hs_bool(x) { .type=HS_BOOL, .ptr.boolean = &(x) }
+#define hs_int(x) { .type=HS_INT, .ptr.integer = &(x) }
+#define hs_num(x) { .type=HS_NUM, .ptr.number = &(x) }
+#define hs_str(x) { .type=HS_STR, .ptr.string = &(x) }
+#define hs_tbl(x) { .type=HS_TBL, .ptr.stack_index = &(x) }
+#define hs_func(x) { .type=HS_FUNC, .ptr.stack_index = &(x) }
+#define hs_cfunc(x) { .type=HS_CFUNC, .ptr.function = &(x) }
+#define hs_user(x) { .type=HS_USER, .ptr.userdata = &(x) }
+#define hs_light(x) { .type=HS_LIGHT, .ptr.userdata = &(x) }
+#define hs_nil(x) { .type=HS_NIL, .ptr.stack_index = &(x) }
+#define hs_any(x) { .type=HS_ANY, .ptr.stack_index = &(x) }
+
+void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments);
+
+#define hs_parse_args(L, ...) \
+ hs_parse_args_(L, VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ })
+
+
+//int hs_parse_overloaded_(lua_State *L, ...);
int hs_create_table(lua_State *L, ...);
-int hs_create_enum(lua_State *L, ...);
void hs_process_table(lua_State *L, int table_index, void *data, ...);
// default processors
@@ -37,12 +75,15 @@ void hs_pt_set_integer(lua_Integer value, void *data);
void hs_pt_set_number(lua_Number value, void *data);
void hs_pt_set_string(const char *value, void *data);
+void hs_vpushstring(lua_State *L, const char *format_string, va_list args);
+void hs_pushstring(lua_State *L, const char *format_string, ...);
+
+
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
void hs_throw_error(lua_State *L, const char *format_string, ...);
int hs_traceback(lua_State *L);
-int hs_call(lua_State *L);
-int hs_call_args(lua_State *L, ...);
-
-void hs_pushstring(lua_State *L, const char *format_string, ...);
+int hs_call(lua_State *L, int nargs, int nret);
#define hs_rstore(L) luaL_ref(L, LUA_REGISTRYINDEX);
#define hs_rload(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c
new file mode 100644
index 0000000..6fb8f31
--- /dev/null
+++ b/src/hs_parse_args.c
@@ -0,0 +1,111 @@
+#include "honeysuckle.h"
+
+static bool check_parse(lua_State *L, int index, struct hs_arg *expected)
+{
+ switch(expected->type) {
+ case HS_BOOL:
+ if (!lua_isboolean(L, index))
+ return false;
+ *(expected->ptr.boolean) = lua_toboolean(L, index);
+ return true;
+
+ case HS_INT:
+ if (!lua_isnumber(L, index))
+ return false;
+ *(expected->ptr.integer) = lua_tointeger(L, index);
+ return true;
+
+ case HS_NUM:
+ if (!lua_isnumber(L, index))
+ return false;
+ *(expected->ptr.number) = lua_tonumber(L, index);
+ return true;
+
+ case HS_STR:
+ if (!lua_isstring(L, index))
+ return false;
+ *(expected->ptr.string) = (char *) lua_tostring(L, index);
+ return true;
+
+ case HS_TBL:
+ if (!lua_istable(L, index))
+ return false;
+ *(expected->ptr.stack_index) = index;
+ return true;
+
+ case HS_FUNC:
+ if (!lua_isfunction(L, index))
+ return false;
+ *(expected->ptr.stack_index) = index;
+ return true;
+
+ case HS_CFUNC:
+ if (!lua_iscfunction(L, index))
+ return false;
+ *(expected->ptr.function) = lua_tocfunction(L, index);
+ return true;
+
+ case HS_USER:
+ if (!lua_isuserdata(L, index))
+ return false;
+ *(expected->ptr.userdata) = lua_touserdata(L, index);
+ return true;
+
+ case HS_LIGHT:
+ if (!lua_islightuserdata(L, index))
+ return false;
+ *(expected->ptr.userdata) = lua_touserdata(L, index);
+ return true;
+
+ case HS_NIL:
+ if (!lua_isnil(L, index))
+ return false;
+ *(expected->ptr.stack_index) = index;
+
+ case HS_ANY:
+ *(expected->ptr.stack_index) = index;
+ return true;
+
+ default:
+ return false;
+ }
+}
+
+
+static bool try_parse_args(lua_State *L, int n_args, struct hs_arg *arguments)
+{
+ int args_provided = lua_gettop(L);
+ if (args_provided != n_args)
+ return false;
+
+ for (int i=0; i<n_args; i++) {
+ bool success = check_parse(L, i+1, arguments + i);
+ if (!success)
+ return false;
+ }
+ return true;
+}
+
+
+void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments)
+{
+ bool success = try_parse_args(L, n_args, arguments);
+ if (!success) {
+ lua_pushstring(L, "expected arguments of the form (");
+ for (int i=0; i<n_args; i++) {
+ lua_pushstring(L, hs_type_to_string(arguments[i].type));
+ lua_pushstring(L, ", ");
+ }
+ lua_pop(L, 1);
+ lua_pushstring(L, "); received (");
+ const int n_provided = lua_gettop(L);
+ for (int i=0; i<n_provided; i++) {
+ lua_pushstring(L, lua_typename(L, lua_type(L, i+1)));
+ lua_pushstring(L, ", ");
+ }
+ lua_pop(L, 1);
+ lua_pushstring(") instead");
+ lua_concat(L, 1 + (2*n_args) + (2*n_provided));
+ lua_error(L);
+ }
+}
diff --git a/src/hs_pushstring.c b/src/hs_pushstring.c
new file mode 100644
index 0000000..f7da0d0
--- /dev/null
+++ b/src/hs_pushstring.c
@@ -0,0 +1,31 @@
+#include <stdlib.h>
+
+#include "honeysuckle.h"
+
+void hs_vpushstring(lua_State *L, const char *format_string, va_list args)
+{
+ va_list args_;
+ va_copy(args_, args);
+
+ int string_size = vsnprintf(NULL, 0, format_string, args_);
+ va_end(args_);
+
+ char *string = malloc((string_size+1) * sizeof(char));
+ if (string == NULL) {
+ lua_pushstring(L, "there was an error allocating memory for a string");
+ lua_error(L);
+ }
+
+ vsnprintf(string, string_size+1, format_string, args);
+ lua_pushstring(L, string);
+ free(string);
+}
+
+
+void hs_pushstring(lua_State *L, const char *format_string, ...)
+{
+ va_list args;
+ va_start(args, format_string);
+ hs_vpushstring(L, format_string, args);
+ va_end(args);
+}
diff --git a/src/hs_throw_error.c b/src/hs_throw_error.c
index b2377dd..20a4c6d 100644
--- a/src/hs_throw_error.c
+++ b/src/hs_throw_error.c
@@ -4,20 +4,9 @@
void hs_throw_error(lua_State *L, const char *format_string, ...)
{
- va_list args, args_;
- va_start(args, format_string);
- va_copy(args_, args);
-
- int string_size = vsnprintf(NULL, 0, format_string, args_);
- va_end(args_);
-
- char *string = malloc((string_size+1) * sizeof(char));
- if (string == NULL)
- lua_pushstring(L, "there was an error allocating memory for an error message");
- else {
- vsnprintf(string, string_size+1, format_string, args);
- lua_pushstring(L, string);
- free(string);
- }
- lua_error(L);
+ va_list args;
+ va_start(args, format_string);
+ hs_vpushstring(L, format_string, args);
+ va_end(args);
+ lua_error(L);
}
diff --git a/src/hs_type_to_string.c b/src/hs_type_to_string.c
new file mode 100644
index 0000000..d0a580c
--- /dev/null
+++ b/src/hs_type_to_string.c
@@ -0,0 +1,31 @@
+#include "honeysuckle.h"
+
+const char * hs_type_to_string(hs_type type)
+{
+ switch(type) {
+ case HS_BOOL:
+ return "boolean";
+ case HS_INT:
+ return "integer";
+ case HS_NUM:
+ return "number";
+ case HS_STR:
+ return "string";
+ case HS_TBL:
+ return "table";
+ case HS_FUNC:
+ return "function";
+ case HS_CFUNC:
+ return "C function";
+ case HS_USER:
+ return "userdata";
+ case HS_LIGHT:
+ return "light userdata";
+ case HS_NIL:
+ return "nil";
+ case HS_ANY:
+ return "any";
+ default:
+ return "(unknown)";
+ }
+}
diff --git a/src/tests/hs_parse_args_tests.c b/src/tests/hs_parse_args_tests.c
index e7b331d..7b03c24 100644
--- a/src/tests/hs_parse_args_tests.c
+++ b/src/tests/hs_parse_args_tests.c
@@ -10,321 +10,321 @@ static int testfunc(lua_State *L) { return 0; }
*/
// 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(name, type, macro) \
+ int name ## _testfunc(lua_State *L) { \
+ type test; hs_parse_args(L, macro(test)); \
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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)
+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;
+ 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)
+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;
+ 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)
+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;
+ 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)
+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;
+ 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(parse_table_typecheck, int, HS_TBL)
+PARSE_TYPECHECK_TEST(parse_table_typecheck, int, 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;
+ 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(parse_func_typecheck, int, HS_FUNC)
+PARSE_TYPECHECK_TEST(parse_func_typecheck, int, 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;
+ 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)
+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;
+ 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)
+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;
+ 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)
+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;
+ 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(parse_nil_typecheck, int, HS_NIL)
+PARSE_TYPECHECK_TEST(parse_nil_typecheck, int, 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;
+ 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;
}
-PARSE_TYPECHECK_TEST(parse_any_typecheck, int, HS_ANY)
+PARSE_TYPECHECK_TEST(parse_any_typecheck, int, hs_any)
{
- lua_pushcfunction(L, parse_bool_typecheck_testfunc);
- lua_pushboolean(L, true);
- mu_assert("typecheck for bool failed!",
- lua_pcall(L, 1, 0, 0) == 0);
-
- lua_pushcfunction(L, parse_int_typecheck_testfunc);
- lua_pushinteger(L, 5);
- mu_assert("typecheck for int failed!",
- lua_pcall(L, 1, 0, 0) == 0);
-
- 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);
-
- lua_pushcfunction(L, parse_string_typecheck_testfunc);
- lua_pushstring(L, "hello!");
- mu_assert("typecheck for string failed!",
- lua_pcall(L, 1, 0, 0) == 0);
-
- lua_pushcfunction(L, parse_table_typecheck_testfunc);
- lua_getglobal(L, "debug");
- mu_assert("typecheck for table failed!",
- lua_pcall(L, 1, 0, 0) == 0);
-
- lua_pushcfunction(L, parse_func_typecheck_testfunc);
- lua_getglobal(L, "test");
- mu_assert("typecheck for function failed!",
- lua_pcall(L, 1, 0, 0) == 0);
-
- 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);
-
- 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);
-
- 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;
+ lua_pushcfunction(L, parse_bool_typecheck_testfunc);
+ lua_pushboolean(L, true);
+ mu_assert("typecheck for bool failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ lua_pushcfunction(L, parse_int_typecheck_testfunc);
+ lua_pushinteger(L, 5);
+ mu_assert("typecheck for int failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ 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);
+
+ lua_pushcfunction(L, parse_string_typecheck_testfunc);
+ lua_pushstring(L, "hello!");
+ mu_assert("typecheck for string failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ lua_pushcfunction(L, parse_table_typecheck_testfunc);
+ lua_getglobal(L, "debug");
+ mu_assert("typecheck for table failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ lua_pushcfunction(L, parse_func_typecheck_testfunc);
+ lua_getglobal(L, "test");
+ mu_assert("typecheck for function failed!",
+ lua_pcall(L, 1, 0, 0) == 0);
+
+ 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);
+
+ 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);
+
+ 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;
}
@@ -337,212 +337,212 @@ PARSE_TYPECHECK_TEST(parse_any_typecheck, int, HS_ANY)
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;
+ lua_pushboolean(L, true);
+ bool b = false;
+ hs_parse_args(L, hs_bool(b));
+ 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;
+ lua_pushinteger(L, 420);
+ int i = 0;
+ hs_parse_args(L, hs_int(i));
+ 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;
+ lua_pushnumber(L, 40.5f);
+ lua_Number n = 0;
+ hs_parse_args(L, hs_num(n));
+ 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;
+ lua_pushstring(L, "hello, world!");
+ char *s = "";
+ hs_parse_args(L, hs_str(s));
+ mu_assert("failed to properly parse string!",
+ strcmp(s, "hello, world!") == 0);
+ return 0;
}
TEST(parse_tbl)
{
- lua_getglobal(L, "debug");
- int correct_index = lua_gettop(L);
- int tbl_index;
- hs_parse_args(L, HS_TBL, &tbl_index, HS_END);
- mu_assert("failed to properly parse table!", tbl_index == correct_index);
- return 0;
+ lua_getglobal(L, "debug");
+ int correct_index = lua_gettop(L);
+ int tbl_index;
+ hs_parse_args(L, hs_tbl(tbl_index));
+ mu_assert("failed to properly parse table!", tbl_index == correct_index);
+ return 0;
}
TEST(parse_func)
{
- lua_getglobal(L, "type");
- int correct_index = lua_gettop(L);
- int index = 0;
- hs_parse_args(L, HS_FUNC, &index, HS_END);
- mu_assert("failed to properly parse function!", index == correct_index);
- return 0;
+ lua_getglobal(L, "type");
+ int correct_index = lua_gettop(L);
+ int index = 0;
+ hs_parse_args(L, hs_func(index));
+ mu_assert("failed to properly parse function!", index == correct_index);
+ 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;
+ lua_pushcfunction(L, testfunc);
+ lua_CFunction f;
+ hs_parse_args(L, hs_cfunc(f));
+ 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;
+ lua_CFunction f;
+ hs_parse_args(L, hs_cfunc(f));
+ return 0;
}
TEST(fail_parse_noncfunc)
{
- lua_pushcfunction(L, should_fail);
- lua_getglobal(L, "type");
- mu_assert("incorrectly parsed non-C function!",
- lua_pcall(L, 1, 0, 0) != 0);
- return 0;
+ lua_pushcfunction(L, should_fail);
+ lua_getglobal(L, "type");
+ mu_assert("incorrectly parsed non-C function!",
+ lua_pcall(L, 1, 0, 0) != 0);
+ 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;
+ void *userdata = lua_newuserdata(L, sizeof(char));
+ void *parsed = NULL;
+ hs_parse_args(L, hs_user(parsed));
+ 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 five = 5;
+ lua_pushlightuserdata(L, &five);
+
+ void *data;
+ hs_parse_args(L, hs_light(data));
+ mu_assert("failed to properly parse light userdata!",
+ data == &five);
+ return 0;
}
TEST(parse_nil)
{
- lua_pushnil(L);
- int correct_index = lua_gettop(L);
- int index = 0;
- hs_parse_args(L, HS_NIL, &index, HS_END);
- mu_assert("failed to properly parse nil!",
- index == correct_index);
- return 0;
+ lua_pushnil(L);
+ int correct_index = lua_gettop(L);
+ int index = 0;
+ hs_parse_args(L, hs_nil(index));
+ mu_assert("failed to properly parse nil!",
+ index == correct_index);
+ return 0;
}
TEST(parse_any)
{
- lua_pushnil(L);
- int correct_index = lua_gettop(L);
- int index = 0;
- hs_parse_args(L, HS_ANY, &index, HS_END);
- mu_assert("failed to properly parse [any]!",
- index == correct_index);
- return 0;
+ lua_pushnil(L);
+ int correct_index = lua_gettop(L);
+ int index = 0;
+ hs_parse_args(L, hs_any(index));
+ mu_assert("failed to properly parse [any]!",
+ index == correct_index);
+ return 0;
}
TEST(parse_all)
{
- lua_pushboolean(L, true);
- lua_pushinteger(L, 420);
- lua_pushnumber(L, 40.5f);
- lua_pushstring(L, "hello, world!");
- lua_getglobal(L, "debug");
- int tbl_index = lua_gettop(L);
- lua_getglobal(L, "type");
- int func_index = lua_gettop(L);
- lua_pushcfunction(L, testfunc);
- void *userdata = lua_newuserdata(L, sizeof(char));
- int five = 5;
- lua_pushlightuserdata(L, &five);
- lua_pushnil(L);
- int nil_index = lua_gettop(L);
- lua_pushnil(L);
- int any_index = lua_gettop(L);
-
- bool b;
- int i;
- float f;
- char *str;
- int i_tbl;
- int i_func;
- lua_CFunction fn;
- void *user;
- void *light;
- int i_nil;
- int i_any;
- hs_parse_args
- (L,
- HS_BOOL, &b,
- HS_INT, &i,
- HS_NUM, &f,
- HS_STR, &str,
- HS_TBL, &i_tbl,
- HS_FUNC, &i_func,
- HS_CFUNC, &fn,
- HS_USER, &user,
- HS_LIGHT, &light,
- HS_NIL, &i_nil,
- HS_ANY, &i_any,
- HS_END);
- mu_assert("failed to properly parse boolean!", b);
- mu_assert("failed to properly parse integer!", i == 420);
- mu_assert("failed to properly parse number!", f == 40.5f);
- mu_assert("failed to properly parse string!",
- strcmp(str, "hello, world!") == 0);
- mu_assert("failed to properly parse table!", i_tbl == tbl_index);
- mu_assert("failed to properly parse function!", func_index == i_func);
- mu_assert("failed to properly parse C function!", fn == testfunc);
- mu_assert("failed to properly parse userdata!", user == userdata);
- mu_assert("failed to properly parse light userdata!", light == &five);
- mu_assert("failed to properly parse nil!", nil_index == i_nil);
- mu_assert("failed to properly parse [any]!", any_index == i_any);
- return 0;
+ lua_pushboolean(L, true);
+ lua_pushinteger(L, 420);
+ lua_pushnumber(L, 40.5f);
+ lua_pushstring(L, "hello, world!");
+ lua_getglobal(L, "debug");
+ int tbl_index = lua_gettop(L);
+ lua_getglobal(L, "type");
+ int func_index = lua_gettop(L);
+ lua_pushcfunction(L, testfunc);
+ void *userdata = lua_newuserdata(L, sizeof(char));
+ int five = 5;
+ lua_pushlightuserdata(L, &five);
+ lua_pushnil(L);
+ int nil_index = lua_gettop(L);
+ lua_pushnil(L);
+ int any_index = lua_gettop(L);
+
+ bool b;
+ int i;
+ float f;
+ char *str;
+ int i_tbl;
+ int i_func;
+ lua_CFunction fn;
+ void *user;
+ void *light;
+ int i_nil;
+ int i_any;
+ hs_parse_args
+ (L,
+ hs_bool(b),
+ hs_int(i),
+ hs_num(f),
+ hs_str(str),
+ hs_tbl(i_tbl),
+ hs_func(i_func),
+ hs_cfunc(fn),
+ hs_user(user),
+ hs_light(light),
+ hs_nil(i_nil),
+ hs_any(i_any));
+
+ mu_assert("failed to properly parse boolean!", b);
+ mu_assert("failed to properly parse integer!", i == 420);
+ mu_assert("failed to properly parse number!", f == 40.5f);
+ mu_assert("failed to properly parse string!",
+ strcmp(str, "hello, world!") == 0);
+ mu_assert("failed to properly parse table!", i_tbl == tbl_index);
+ mu_assert("failed to properly parse function!", func_index == i_func);
+ mu_assert("failed to properly parse C function!", fn == testfunc);
+ mu_assert("failed to properly parse userdata!", user == userdata);
+ mu_assert("failed to properly parse light userdata!", light == &five);
+ mu_assert("failed to properly parse nil!", nil_index == i_nil);
+ mu_assert("failed to properly parse [any]!", any_index == i_any);
+ return 0;
}
-TEST(parse_readme_example)
+/*TEST(parse_readme_example)
{
- lua_pushboolean(L, true);
- lua_pushinteger(L, 7);
- lua_getglobal(L, "debug");
- int expected = lua_gettop(L);
- lua_pushnumber(L, 3.1415f);
- lua_pushstring(L, "c: c: c:");
- void *userdata = lua_newuserdata(L, sizeof(char));
-
- bool b; int i; int table_index; float f; char *str; void *user;
- hs_parse_args
- (L, HS_BOOL, &b, HS_INT, &i, HS_TBL, &table_index,
- HS_NUM, &f, HS_STR, &str, HS_USER, &user, HS_END);
-
- mu_assert("failed to properly parse boolean!", b);
- mu_assert("failed to properly parse integer!", i == 7);
- mu_assert("failed to properly parse table!", table_index == expected);
- mu_assert("failed to properly parse number!", f == 3.1415f);
- mu_assert("failed to properly parse string!", strcmp(str, "c: c: c:") == 0);
- mu_assert("failed to properly parse userdata!", user == userdata);
- return 0;
-}
+ lua_pushboolean(L, true);
+ lua_pushinteger(L, 7);
+ lua_getglobal(L, "debug");
+ int expected = lua_gettop(L);
+ lua_pushnumber(L, 3.1415f);
+ lua_pushstring(L, "c: c: c:");
+ void *userdata = lua_newuserdata(L, sizeof(char));
+
+ bool b; int i; int table_index; float f; char *str; void *user;
+ hs_parse_args
+ (L, HS_BOOL, &b, HS_INT, &i, HS_TBL, &table_index,
+ HS_NUM, &f, HS_STR, &str, HS_USER, &user, HS_END);
+
+ mu_assert("failed to properly parse boolean!", b);
+ mu_assert("failed to properly parse integer!", i == 7);
+ mu_assert("failed to properly parse table!", table_index == expected);
+ mu_assert("failed to properly parse number!", f == 3.1415f);
+ mu_assert("failed to properly parse string!", strcmp(str, "c: c: c:") == 0);
+ mu_assert("failed to properly parse userdata!", user == userdata);
+ return 0;
+ }*/
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
@@ -553,32 +553,32 @@ TEST(parse_readme_example)
void hs_parse_args_tests()
{
- printf("running hs_parse_args() typechecking 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 any typecheck", parse_any_typecheck);
-
- printf("running hs_parse_args() parsing 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 tbl", parse_tbl);
- mu_run_test("parse func", parse_func);
- mu_run_test("parse cfunc", parse_cfunc);
- mu_run_test("fail parse noncfunc", fail_parse_noncfunc);
- mu_run_test("parse userdata", parse_userdata);
- mu_run_test("parse lightuserdata", parse_lightuserdata);
- mu_run_test("parse nil", parse_nil);
- mu_run_test("parse any", parse_any);
- mu_run_test("parse all", parse_all);
- mu_run_test("parse readme example", parse_readme_example);
+ printf("running hs_parse_args() typechecking 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 any typecheck", parse_any_typecheck);
+
+ printf("running hs_parse_args() parsing 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 tbl", parse_tbl);
+ mu_run_test("parse func", parse_func);
+ mu_run_test("parse cfunc", parse_cfunc);
+ mu_run_test("fail parse noncfunc", fail_parse_noncfunc);
+ mu_run_test("parse userdata", parse_userdata);
+ mu_run_test("parse lightuserdata", parse_lightuserdata);
+ mu_run_test("parse nil", parse_nil);
+ mu_run_test("parse any", parse_any);
+ mu_run_test("parse all", parse_all);
+ // mu_run_test("parse readme example", parse_readme_example);
}
diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c
index 171b253..cf18928 100644
--- a/src/tests/tests_main.c
+++ b/src/tests/tests_main.c
@@ -17,10 +17,10 @@ int main()
mu_run_suite(hs_type_to_string_tests);
mu_run_suite(hs_parse_args_tests);
- mu_run_suite(hs_parse_overloaded_tests);
- mu_run_suite(hs_create_table_tests);
- mu_run_suite(hs_create_enum_tests);
- mu_run_suite(hs_process_table_tests);
+ //mu_run_suite(hs_parse_overloaded_tests);
+ //mu_run_suite(hs_create_table_tests);
+ //mu_run_suite(hs_create_enum_tests);
+ //mu_run_suite(hs_process_table_tests);
mu_run_suite(hs_throw_error_tests);
mu_run_suite(hs_pushstring_tests);
diff --git a/src/va_nargs.h b/src/va_nargs.h
new file mode 100644
index 0000000..0037445
--- /dev/null
+++ b/src/va_nargs.h
@@ -0,0 +1,75 @@
+#ifndef VA_NARGS
+
+#define VA_NARGS(...) VA_NARGS_SHIFT_COUNT(__VA_ARGS__, VA_NARGS_COUNTS)
+
+#define VA_NARGS_COUNTS \
+ 255, 254, 253, 252, 251, 250, 249, 248, \
+ 247, 246, 245, 244, 243, 242, 241, 240, \
+ 239, 238, 237, 236, 235, 234, 233, 232, \
+ 231, 230, 229, 228, 227, 226, 225, 224, \
+ 223, 222, 221, 220, 219, 218, 217, 216, \
+ 215, 214, 213, 212, 211, 210, 209, 208, \
+ 207, 206, 205, 204, 203, 202, 201, 200, \
+ 199, 198, 197, 196, 195, 194, 193, 192, \
+ 191, 190, 189, 188, 187, 186, 185, 184, \
+ 183, 182, 181, 180, 179, 178, 177, 176, \
+ 175, 174, 173, 172, 171, 170, 169, 168, \
+ 167, 166, 165, 164, 163, 162, 161, 160, \
+ 159, 158, 157, 156, 155, 154, 153, 152, \
+ 151, 150, 149, 148, 147, 146, 145, 144, \
+ 143, 142, 141, 140, 139, 138, 137, 136, \
+ 135, 134, 133, 132, 131, 130, 129, 128, \
+ 127, 126, 125, 124, 123, 122, 121, 120, \
+ 119, 118, 117, 116, 115, 114, 113, 112, \
+ 111, 110, 109, 108, 107, 106, 105, 104, \
+ 103, 102, 101, 100, 99, 98, 97, 96, \
+ 95, 94, 93, 92, 91, 90, 89, 88, \
+ 87, 86, 85, 84, 83, 82, 81, 80, \
+ 79, 78, 77, 76, 75, 74, 73, 72, \
+ 71, 70, 69, 68, 67, 66, 65, 64, \
+ 63, 62, 61, 60, 59, 58, 57, 56, \
+ 55, 54, 53, 52, 51, 50, 49, 48, \
+ 47, 46, 45, 44, 43, 42, 41, 40, \
+ 39, 38, 37, 36, 35, 34, 33, 32, \
+ 31, 30, 29, 28, 27, 26, 25, 24, \
+ 23, 22, 21, 20, 19, 18, 17, 16, \
+ 15, 14, 13, 12, 11, 10, 9, 8, \
+ 7, 6, 5, 4, 3, 2, 1, 0
+
+#define VA_NARGS_GET_COUNT( \
+ _255, _254, _253, _252, _251, _250, _249, _248, \
+ _247, _246, _245, _244, _243, _242, _241, _240, \
+ _239, _238, _237, _236, _235, _234, _233, _232, \
+ _231, _230, _229, _228, _227, _226, _225, _224, \
+ _223, _222, _221, _220, _219, _218, _217, _216, \
+ _215, _214, _213, _212, _211, _210, _209, _208, \
+ _207, _206, _205, _204, _203, _202, _201, _200, \
+ _199, _198, _197, _196, _195, _194, _193, _192, \
+ _191, _190, _189, _188, _187, _186, _185, _184, \
+ _183, _182, _181, _180, _179, _178, _177, _176, \
+ _175, _174, _173, _172, _171, _170, _169, _168, \
+ _167, _166, _165, _164, _163, _162, _161, _160, \
+ _159, _158, _157, _156, _155, _154, _153, _152, \
+ _151, _150, _149, _148, _147, _146, _145, _144, \
+ _143, _142, _141, _140, _139, _138, _137, _136, \
+ _135, _134, _133, _132, _131, _130, _129, _128, \
+ _127, _126, _125, _124, _123, _122, _121, _120, \
+ _119, _118, _117, _116, _115, _114, _113, _112, \
+ _111, _110, _109, _108, _107, _106, _105, _104, \
+ _103, _102, _101, _100, _99, _98, _97, _96, \
+ _95, _94, _93, _92, _91, _90, _89, _88, \
+ _87, _86, _85, _84, _83, _82, _81, _80, \
+ _79, _78, _77, _76, _75, _74, _73, _72, \
+ _71, _70, _69, _68, _67, _66, _65, _64, \
+ _63, _62, _61, _60, _59, _58, _57, _56, \
+ _55, _54, _53, _52, _51, _50, _49, _48, \
+ _47, _46, _45, _44, _43, _42, _41, _40, \
+ _39, _38, _37, _36, _35, _34, _33, _32, \
+ _31, _30, _29, _28, _27, _26, _25, _24, \
+ _23, _22, _21, _20, _19, _18, _17, _16, \
+ _15, _14, _13, _12, _11, _10, _9, _8, \
+ _7, _6, _5, _4, _3, _2, _1, N, ...) N
+
+#define VA_NARGS_SHIFT_COUNT(...) VA_NARGS_GET_COUNT(__VA_ARGS__)
+
+#endif