diff options
author | sanine-a <sanine.not@pm.me> | 2021-05-23 16:47:59 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-05-23 16:47:59 -0500 |
commit | e06165088c13cd31819a7b1bb1a2ba9e5aaf689e (patch) | |
tree | a1772bfb552202af710bddc0ebf5231376c845e4 /src/test.c | |
parent | 4dc4d9f1d20a73d1e4c399a56c86bf70589b13d9 (diff) |
create initial files and write three hs_pushstring() tests
Diffstat (limited to 'src/test.c')
-rw-r--r-- | src/test.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..5275d4d --- /dev/null +++ b/src/test.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <string.h> + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + +#include "honeysuckle.h" + +/* 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++; \ + } \ + } while (0) + +int tests_run = 0; +int tests_failed = 0; + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_pushstring + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +static char* push_noformat(lua_State *L) +{ + hs_pushstring(L, "a string"); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != 'a string'", strcmp(string, "a string") == 0); + return 0; +} + +static char* push_formatint(lua_State *L) +{ + hs_pushstring(L, "%d is 5", 5); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != '5 is 5'", strcmp(string, "5 is 5") == 0); + return 0; +} + +static char* push_formatstring(lua_State *L) +{ + hs_pushstring(L, "%s is 'hello'", "hello"); + 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); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * RUN TESTS + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int main() +{ + printf("================ start tests ================\n\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=============== tests finished ===============\n\n"); + printf("ran %d tests, %d failed\n", tests_run, tests_failed); + return 0; +} |