diff options
author | sanine-a <sanine.not@pm.me> | 2021-07-03 01:20:01 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-07-03 01:20:01 -0500 |
commit | f83e8d242b8a719733498181f4cad3fbb9c97ca7 (patch) | |
tree | edc44f1e16499f0138d62ed45fb12bba42840570 | |
parent | db2b31a335b531d7e54ab3e7d494bbf33050ac60 (diff) |
create hs_process_table_tests.c and add basic table processing test
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | README.md | 20 | ||||
-rw-r--r-- | src/honeysuckle.c | 8 | ||||
l--------- | src/tests/.#hs_create_table_tests.c | 1 | ||||
-rw-r--r-- | src/tests/hs_create_enum_tests.c | 1 | ||||
-rw-r--r-- | src/tests/hs_create_table_tests.c | 4 | ||||
-rw-r--r-- | src/tests/hs_process_table_tests.c | 77 | ||||
-rw-r--r-- | src/tests/tests_main.c | 2 |
8 files changed, 101 insertions, 14 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f6b491f..0e73664 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ set(TEST_SOURCES ${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_process_table_tests.c # ${TEST_ROOT}/hs_throw_error_tests.c # ${TEST_ROOT}/hs_traceback_tests.c # ${TEST_ROOT}/hs_call_tests.c @@ -86,7 +86,7 @@ messages. `HS_TBL`, `HS_FUNC`, `HS_NIL`, and `HS_ANY` take a pointer to an integer that will be filled with the stack index of the argument. -```C +```c int some_lua_binding(lua_State *L) { bool b; @@ -116,7 +116,7 @@ each `HS_END` you may provide another argument list, and after the final argument list you must provide another `HS_END`. This function throws an error only if none of the argument lists match the provided arguments. -```C +```c int overloaded_lua_binding(lua_State *L) { int i; @@ -149,7 +149,7 @@ etc.) but `HS_FUNC` and `HS_TBL` expect an integer stack index. You cannot use `HS_NIL` or `HS_ANY` with this function. `hs_create_table()` automatically pops any stack indices provided to it. -```C +```c int *ptr; // some pointer hs_create_table(L, @@ -173,7 +173,7 @@ literal to enum value and `hs_enum_to_string()` converts enums to strings. In case the value does not exist, converting to a string results in NULL and converting to an enum results in a negative value. -```C +```c enum { VALUE_1, VALUE_2, VALUE_3 } example_t; int index = hs_create_enum(L, @@ -194,7 +194,7 @@ char *str = hs_enum_to_string(L, index, VALUE_3); // "three" like `set_config{ debug=true, verbosity=2, logfile='run.log' }`. It can only operate on stringly-keyed values. -```C +```c void set_verbosity(int value, void *data) { // set verbosity level } void set_debug(bool value, void *data) { // set debug } void set_logfile(char *value, void *data) { // set log filename } @@ -216,14 +216,14 @@ hs_process_table(L, tbl_index, NULL, `hs_throw_error()` constructs and throws error messages like printf. -```C +```c hs_throw_error(L, "ERROR: %d is not within range", 10); ``` `hs_traceback()` can be supplied to `lua_pcall()` to get a helpful stack trace error message: -```C +```c lua_pushcfunction(L, hs_traceback); int tr_pos = lua_gettop(); @@ -238,7 +238,7 @@ which provides the same helpful, contained error messages as `hs_call()` but takes arguments explicitly in the call for both function arguments and returns: -```C +```c // some_function accepts int, str and returns number, boolean lua_pushcfunction(L, some_function); @@ -256,7 +256,7 @@ hs_call_args(L, `hs_pushstring()` allows you to push string values using printf format strings. -```C +```c hs_pushstring(L, "hello there, %s. I hear the current time is %d:%02d", "dylan", 10, 5); ``` @@ -269,7 +269,7 @@ rdel()` to make registry access easier. These are mostly just thin wrappers around `luaL_ref()`, `luaL_unref()` and `lua_rawgeti()` but they come up frequently enough that I think these are helpful. -```C +```c lua_pushstring(L, "some string"); int my_string_ref = hs_rstore(L); // ... diff --git a/src/honeysuckle.c b/src/honeysuckle.c index 8cb8194..11f3f53 100644 --- a/src/honeysuckle.c +++ b/src/honeysuckle.c @@ -34,6 +34,14 @@ int hs_create_enum(lua_State *L, ...) } +void hs_process_table(lua_State *L, int table_index, void *data, ...) +{ + L = L; + table_index = table_index; + data = data; +} + + void hs_pushstring(lua_State *L, const char *format_string, ...) { lua_pushnumber(L, 0.4); diff --git a/src/tests/.#hs_create_table_tests.c b/src/tests/.#hs_create_table_tests.c deleted file mode 120000 index 68ed537..0000000 --- a/src/tests/.#hs_create_table_tests.c +++ /dev/null @@ -1 +0,0 @@ -kate@amalthea.194602:1624721630
\ No newline at end of file diff --git a/src/tests/hs_create_enum_tests.c b/src/tests/hs_create_enum_tests.c index 0cc3d0b..b24ec71 100644 --- a/src/tests/hs_create_enum_tests.c +++ b/src/tests/hs_create_enum_tests.c @@ -13,6 +13,7 @@ lua_pop(L, -1); \ } while(0) + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * tests diff --git a/src/tests/hs_create_table_tests.c b/src/tests/hs_create_table_tests.c index 8a8ebe2..460832f 100644 --- a/src/tests/hs_create_table_tests.c +++ b/src/tests/hs_create_table_tests.c @@ -1,6 +1,6 @@ #include "hs_tests.h" -int testfunc(lua_State *L) { return 0; } +static int testfunc(lua_State *L) { return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * @@ -29,7 +29,7 @@ TEST(table_correct_index) #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_string(field, expected) check_table_element(field, lua_isstring, "string", const 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) diff --git a/src/tests/hs_process_table_tests.c b/src/tests/hs_process_table_tests.c new file mode 100644 index 0000000..eedf600 --- /dev/null +++ b/src/tests/hs_process_table_tests.c @@ -0,0 +1,77 @@ +#include "hs_tests.h" + +struct process_types { + bool boolean; + lua_Integer integer; + lua_Number number; + const char *string; +}; + +void set_boolean(bool value, void *data) +{ + struct process_types *d = data; + d->boolean = value; +} + +void set_integer(lua_Integer value, void *data) +{ + struct process_types *d = data; + d->integer = value; +} + +void set_number(lua_Number value, void *data) +{ + struct process_types *d = data; + d->number = value; +} + +void set_string(const char *value, void *data) +{ + struct process_types *d = data; + d->string = value; +} + + +TEST(process_basic_types) +{ + lua_createtable(L, 0, 0); + lua_pushboolean(L, true); + lua_setfield(L, -2, "boolean"); + + lua_pushinteger(L, 14); + lua_setfield(L, -2, "integer"); + + lua_pushnumber(L, 44.66); + lua_setfield(L, -2, "number"); + + lua_pushstring(L, "c: c: c:"); + lua_setfield(L, -2, "string"); + + struct process_types data = { false, 0, 0, "" }; + + hs_process_table(L, -1, &data, + "boolean", HS_BOOL, set_boolean, + "integer", HS_INT, set_integer, + "number", HS_NUM, set_number, + "string", HS_STR, set_string, + HS_END); + + mu_assert("failed to set boolean!", data.boolean == true); + mu_assert("failed to set integer!", data.integer == 14); + mu_assert("failed to set number!", data.number == 44.66); + mu_assert("failed to set string!", strcmp(data.string, "c: c: c:") == 0); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * test suite + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +void hs_process_table_tests() +{ + printf("running hs_process_table() tests...\n"); + mu_run_test("process basic types", process_basic_types); +} diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c index 207e233..51c2462 100644 --- a/src/tests/tests_main.c +++ b/src/tests/tests_main.c @@ -21,6 +21,8 @@ int main() hs_create_table_tests(); hs_create_enum_tests(); + hs_process_table_tests(); + hs_pushstring_tests(); printf("\n=============== tests finished ===============\n\n"); |