From 48c4b0a6e46a364b4a5d687141e0aec896333f3d Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 8 Aug 2021 13:29:02 -0500 Subject: write initial tests for hs_throw_error() --- CMakeLists.txt | 2 +- src/honeysuckle.c | 7 +++++ src/tests/hs_throw_error_tests.c | 57 ++++++++++++++++++++++++++++++++++++++++ src/tests/tests_main.c | 1 + 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/tests/hs_throw_error_tests.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e73664..6bc86a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ set(TEST_SOURCES ${TEST_ROOT}/hs_create_table_tests.c ${TEST_ROOT}/hs_create_enum_tests.c ${TEST_ROOT}/hs_process_table_tests.c - # ${TEST_ROOT}/hs_throw_error_tests.c + ${TEST_ROOT}/hs_throw_error_tests.c # ${TEST_ROOT}/hs_traceback_tests.c # ${TEST_ROOT}/hs_call_tests.c # ${TEST_ROOT}/hs_call_args_tests.c diff --git a/src/honeysuckle.c b/src/honeysuckle.c index 97c60c4..339b278 100644 --- a/src/honeysuckle.c +++ b/src/honeysuckle.c @@ -47,6 +47,13 @@ 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); 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); +} diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c index 9a871c1..171b253 100644 --- a/src/tests/tests_main.c +++ b/src/tests/tests_main.c @@ -21,6 +21,7 @@ int main() 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); -- cgit v1.2.1