summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2021-08-08 13:29:02 -0500
committersanine-a <sanine.not@pm.me>2021-08-08 13:29:02 -0500
commit48c4b0a6e46a364b4a5d687141e0aec896333f3d (patch)
tree2dab26e635e5803a14c8f585986b8ab0d9f3825a /src/tests
parentbc53998c018b23f3fd59efa00dd7631bed762ee7 (diff)
write initial tests for hs_throw_error()
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/hs_throw_error_tests.c57
-rw-r--r--src/tests/tests_main.c1
2 files changed, 58 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);
+}
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);