diff options
Diffstat (limited to 'src/tests/hs_throw_error_tests.c')
-rw-r--r-- | src/tests/hs_throw_error_tests.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/tests/hs_throw_error_tests.c b/src/tests/hs_throw_error_tests.c new file mode 100644 index 0000000..1c5af6a --- /dev/null +++ b/src/tests/hs_throw_error_tests.c @@ -0,0 +1,57 @@ +#include "hs_tests.h" + +const char *err_string = ""; + +int set_err_string(lua_State *L) +{ + if (lua_isstring(L, -1)) + err_string = lua_tostring(L, -1); + return 0; +} + + +int throw_const_error(lua_State *L) +{ + hs_throw_error(L, "a constant error"); + return 0; +} + +TEST(hs_throw_error_constant) +{ + lua_pushcfunction(L, throw_const_error); + int pos = lua_gettop(L); + int result = lua_pcall(L, 0, 0, pos); + mu_assert_equal(result, LUA_ERRRUN); + mu_assert_str_not_equal(err_string, ""); + mu_assert_str_equal(err_string, "a constant error"); + return 0; +} + + +int throw_number_error(lua_State *L) +{ + hs_throw_error(L, "%s number %d", "error", 10); + return 0; +} + +TEST(hs_throw_error_format) +{ + lua_pushcfunction(L, throw_number_error); + int pos = lua_gettop(L); + int result = lua_pcall(L, 0, 0, pos); + mu_assert_equal(result, LUA_ERRRUN); + mu_assert_str_not_equal(err_string, ""); + mu_assert_str_equal(err_string, "error number 10"); + return 0; +} + + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void hs_throw_error_tests() +{ + printf("running hs_throw_error() tests...\n"); + + mu_run_test("throw constant error string", hs_throw_error_constant); + mu_run_test("throw error with format string", hs_throw_error_format); +} |