diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/honeysuckle.h | 14 | ||||
-rw-r--r-- | src/hs_parse_args.c | 11 | ||||
-rw-r--r-- | src/tests/hs_parse_overloaded_tests.c | 212 | ||||
-rw-r--r-- | src/tests/tests_main.c | 2 |
4 files changed, 134 insertions, 105 deletions
diff --git a/src/honeysuckle.h b/src/honeysuckle.h index 9aecddf..584c4bd 100644 --- a/src/honeysuckle.h +++ b/src/honeysuckle.h @@ -64,7 +64,19 @@ void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments); hs_parse_args_(L, VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ }) -//int hs_parse_overloaded_(lua_State *L, ...); +#define hs_overload(...) VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ } + +int hs_parse_overloaded_(lua_State *L, ...); + +#define hs_parse_overloaded(L, ...) \ + hs_parse_overloaded_(L, __VA_ARGS__, -1) + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * hs_create_table + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ int hs_create_table(lua_State *L, ...); diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c index 5a14bd0..685561e 100644 --- a/src/hs_parse_args.c +++ b/src/hs_parse_args.c @@ -111,3 +111,14 @@ void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments) lua_error(L); } } + +int hs_parse_overloaded_(lua_State *L, ...) +{ + va_list args, args_error; + va_start(args, L); + va_copy(args_error, args); + + int choice = 0; + va_end(args); + va_end(args_error); +} diff --git a/src/tests/hs_parse_overloaded_tests.c b/src/tests/hs_parse_overloaded_tests.c index 812aab5..784807a 100644 --- a/src/tests/hs_parse_overloaded_tests.c +++ b/src/tests/hs_parse_overloaded_tests.c @@ -9,143 +9,149 @@ static int testfunc(lua_State *L) { return 0; } * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -#define PARSE_OVERLOADED \ - bool b; int i, ti, fi, ni; float f; char *str; \ - lua_CFunction fn; void *user, *light; \ - int choice = hs_parse_overloaded \ - (L, \ - HS_BOOL, &b, HS_END, HS_INT, &i, HS_END, HS_NUM, &f, HS_END, \ - HS_STR, &str, HS_END, HS_TBL, &ti, HS_END, HS_FUNC, &fi, \ - HS_END, HS_CFUNC, &fn, HS_END, HS_USER, &user, HS_END, \ - HS_LIGHT, &light, HS_END, HS_NIL, &ni, HS_END, HS_END); +#define PARSE_OVERLOADED \ + bool b; long int i; int ti, fi, ni; double f; \ + char *str; lua_CFunction fn; void *user, *light; \ + int choice = hs_parse_overloaded \ + (L, \ + hs_overload(hs_bool(b)), \ + hs_overload(hs_int(i)), \ + hs_overload(hs_num(f)), \ + hs_overload(hs_str(str)), \ + hs_overload(hs_tbl(ti)), \ + hs_overload(hs_func(fi)), \ + hs_overload(hs_cfunc(fn)), \ + hs_overload(hs_user(user)), \ + hs_overload(hs_light(light)), \ + hs_overload(hs_nil(ni))); TEST(parse_bool_overloaded) { - lua_pushboolean(L, true); - PARSE_OVERLOADED; - mu_assert("boolean option was not chosen!", choice == 0); - mu_assert("failed to properly parse boolean!", b); - return 0; + lua_pushboolean(L, true); + PARSE_OVERLOADED; + mu_assert("boolean option was not chosen!", choice == 0); + mu_assert("failed to properly parse boolean!", b); + return 0; } TEST(parse_integer_overloaded) { - lua_pushinteger(L, 5); - PARSE_OVERLOADED; - mu_assert("integer option was not chosen!", choice == 1); - mu_assert("failed to properly parse integer!", i == 5); - return 0; + lua_pushinteger(L, 5); + PARSE_OVERLOADED; + mu_assert("integer option was not chosen!", choice == 1); + mu_assert("failed to properly parse integer!", i == 5); + return 0; } TEST(parse_number_overloaded) { - lua_pushnumber(L, 42.0f); - PARSE_OVERLOADED; - mu_assert("number option was not chosen!", choice == 2); - mu_assert("failed to properly parse boolean!", f == 42.0f); - return 0; + lua_pushnumber(L, 42.0f); + PARSE_OVERLOADED; + mu_assert("number option was not chosen!", choice == 2); + mu_assert("failed to properly parse boolean!", f == 42.0f); + return 0; } TEST(parse_string_overloaded) { - lua_pushstring(L, "hello, world!"); - PARSE_OVERLOADED; - mu_assert("string option was not chosen!", choice == 3); - mu_assert("failed to properly parse string!", - strcmp(str, "hello, world!") == 0); - return 0; + lua_pushstring(L, "hello, world!"); + PARSE_OVERLOADED; + mu_assert("string option was not chosen!", choice == 3); + mu_assert("failed to properly parse string!", + strcmp(str, "hello, world!") == 0); + return 0; } TEST(parse_table_overloaded) { - lua_getglobal(L, "debug"); - int expected = lua_gettop(L); - PARSE_OVERLOADED; - mu_assert("table option was not chosen!", choice == 4); - mu_assert("failed to properly parse table!", ti == expected); - return 0; + lua_getglobal(L, "debug"); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("table option was not chosen!", choice == 4); + mu_assert("failed to properly parse table!", ti == expected); + return 0; } TEST(parse_function_overloaded) { - lua_getglobal(L, "type"); - int expected = lua_gettop(L); - PARSE_OVERLOADED; - mu_assert("function option was not chosen!", choice == 5); - mu_assert("failed to properly parse function!", fi == expected); - return 0; + luaL_loadstring(L, "print('hello, world!')"); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("function option was not chosen!", choice == 5); + mu_assert("failed to properly parse function!", fi == expected); + return 0; } TEST(parse_cfunction_overloaded) { - lua_pushcfunction(L, testfunc); - PARSE_OVERLOADED; - mu_assert("C function option was not chosen!", choice == 6); - mu_assert("failed to properly parse C function!", fn == testfunc); - return 0; + lua_pushcfunction(L, testfunc); + PARSE_OVERLOADED; + mu_assert("C function option was not chosen!", choice == 6); + mu_assert("failed to properly parse C function!", fn == testfunc); + return 0; } TEST(parse_userdata_overloaded) { - void *userdata = lua_newuserdata(L, sizeof(char)); - PARSE_OVERLOADED; - mu_assert("userdata option was not chosen!", choice == 7); - mu_assert("failed to properly parse userdata!", user == userdata); - return 0; + void *userdata = lua_newuserdata(L, sizeof(char)); + PARSE_OVERLOADED; + mu_assert("userdata option was not chosen!", choice == 7); + mu_assert("failed to properly parse userdata!", user == userdata); + return 0; } TEST(parse_lightuserdata_overloaded) { - int five = 5; - lua_pushlightuserdata(L, &five); - PARSE_OVERLOADED; - mu_assert("light userdata option was not chosen!", choice == 8); - mu_assert("failed to properly parse light userdata!", light == &five); - return 0; + int five = 5; + lua_pushlightuserdata(L, &five); + PARSE_OVERLOADED; + mu_assert("light userdata option was not chosen!", choice == 8); + mu_assert("failed to properly parse light userdata!", light == &five); + return 0; } TEST(parse_nil_overloaded) { - lua_pushnil(L); - int expected = lua_gettop(L); - PARSE_OVERLOADED; - mu_assert("nil option was not chosen!", choice == 9); - mu_assert("failed to properly parse nil!", ni == expected); - return 0; + lua_pushnil(L); + int expected = lua_gettop(L); + PARSE_OVERLOADED; + mu_assert("nil option was not chosen!", choice == 9); + mu_assert("failed to properly parse nil!", ni == expected); + return 0; } TEST(parse_2_3_overload_0) { - lua_pushinteger(L, 69); - lua_pushstring(L, "foo"); - int i; char *str; void *data; - int choice = hs_parse_overloaded - (L, HS_INT, &i, HS_STR, &str, HS_END, - HS_INT, &i, HS_STR, &str, HS_USER, &data, HS_END, - HS_END); - - mu_assert("incorrectly selected option other than 0!", choice == 0); - mu_assert("failed to properly parse integer!", i == 69); - mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); - return 0; + lua_pushinteger(L, 69); + lua_pushstring(L, "foo"); + long int i; char *str; void *data; + int choice = hs_parse_overloaded + (L, + hs_overload(hs_int(i), hs_str(str)), + hs_overload(hs_int(i), hs_str(str), hs_user(data))); + + mu_assert("incorrectly selected option other than 0!", choice == 0); + mu_assert("failed to properly parse integer!", i == 69); + mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); + return 0; } TEST(parse_2_3_overload_1) { - lua_pushinteger(L, 69); - lua_pushstring(L, "foo"); - void *userdata = lua_newuserdata(L, sizeof(char)); - int i; char *str; void *data; - int choice = hs_parse_overloaded - (L, HS_INT, &i, HS_STR, &str, HS_END, - HS_INT, &i, HS_STR, &str, HS_USER, &data, HS_END, - HS_END); - - mu_assert("incorrectly selected option other than 1!", choice == 1); - mu_assert("failed to properly parse integer!", i == 69); - mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); - mu_assert("failed to properly parse userdata!", userdata == data); - return 0; + lua_pushinteger(L, 69); + lua_pushstring(L, "foo"); + void *userdata = lua_newuserdata(L, sizeof(char)); + long int i; char *str; void *data; + int choice = hs_parse_overloaded + (L, + hs_overload(hs_int(i), hs_str(str)), + hs_overload(hs_int(i), hs_str(str), hs_user(data))); + + mu_assert("incorrectly selected option other than 1!", choice == 1); + mu_assert("failed to properly parse integer!", i == 69); + mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0); + mu_assert("failed to properly parse userdata!", userdata == data); + return 0; } @@ -158,17 +164,17 @@ TEST(parse_2_3_overload_1) void hs_parse_overloaded_tests() { - printf("running hs_parse_overloaded() parsing tests...\n"); - mu_run_test("parse bool overloaded", parse_bool_overloaded); - mu_run_test("parse integer overloaded", parse_integer_overloaded); - mu_run_test("parse number overloaded", parse_number_overloaded); - mu_run_test("parse string overloaded", parse_string_overloaded); - mu_run_test("parse table overloaded", parse_table_overloaded); - mu_run_test("parse function overloaded", parse_function_overloaded); - mu_run_test("parse cfunction overloaded", parse_cfunction_overloaded); - mu_run_test("parse userdata overloaded", parse_userdata_overloaded); - mu_run_test("parse lightuserdata overloaded", parse_lightuserdata_overloaded); - 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_parse_overloaded() parsing tests...\n"); + mu_run_test("parse bool overloaded", parse_bool_overloaded); + mu_run_test("parse integer overloaded", parse_integer_overloaded); + mu_run_test("parse number overloaded", parse_number_overloaded); + mu_run_test("parse string overloaded", parse_string_overloaded); + mu_run_test("parse table overloaded", parse_table_overloaded); + mu_run_test("parse function overloaded", parse_function_overloaded); + mu_run_test("parse cfunction overloaded", parse_cfunction_overloaded); + mu_run_test("parse userdata overloaded", parse_userdata_overloaded); + mu_run_test("parse lightuserdata overloaded", parse_lightuserdata_overloaded); + 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); } diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c index cf18928..43a5dc9 100644 --- a/src/tests/tests_main.c +++ b/src/tests/tests_main.c @@ -17,7 +17,7 @@ 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_parse_overloaded_tests); //mu_run_suite(hs_create_table_tests); //mu_run_suite(hs_create_enum_tests); //mu_run_suite(hs_process_table_tests); |