summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/honeysuckle.c7
-rw-r--r--src/tests/hs_create_enum_tests.c73
-rw-r--r--src/tests/tests_main.c3
4 files changed, 84 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0e3eda3..f6b491f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,7 +22,7 @@ set(TEST_SOURCES
${TEST_ROOT}/hs_parse_args_tests.c
${TEST_ROOT}/hs_parse_overloaded_tests.c
${TEST_ROOT}/hs_create_table_tests.c
- # ${TEST_ROOT}/hs_create_enum_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_traceback_tests.c
diff --git a/src/honeysuckle.c b/src/honeysuckle.c
index 1015343..8cb8194 100644
--- a/src/honeysuckle.c
+++ b/src/honeysuckle.c
@@ -26,6 +26,13 @@ int hs_create_table(lua_State *L, ...)
return 0;
}
+int hs_create_enum(lua_State *L, ...)
+{
+ lua_createtable(L, 0, 0);
+ L = L;
+ return 0;
+}
+
void hs_pushstring(lua_State *L, const char *format_string, ...)
{
diff --git a/src/tests/hs_create_enum_tests.c b/src/tests/hs_create_enum_tests.c
new file mode 100644
index 0000000..0cc3d0b
--- /dev/null
+++ b/src/tests/hs_create_enum_tests.c
@@ -0,0 +1,73 @@
+#include "hs_tests.h"
+
+#define check(string, value) do { \
+ lua_getfield(L, index, string); \
+ mu_assert("field '" string "' is not an integer!", lua_isnumber(L, -1)); \
+ int enum_value = lua_tointeger(L, -1); \
+ mu_assert("field '" string "' does not match expected value!", enum_value == (value)); \
+ lua_pop(L, -1); \
+ lua_rawgeti(L, index, value); \
+ mu_assert("enum field for '" string "' is not a string!", lua_isstring(L, -1)); \
+ const char *str = lua_tostring(L, -1); \
+ mu_assert("enum field for '" string "' does not match expected value!", strcmp(str, string) == 0); \
+ lua_pop(L, -1); \
+ } while(0)
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * tests
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+enum test_enum { VAL0, VAL1, VAL2, VAL3, N_VALS };
+
+
+TEST(enum_test)
+{
+ int index = hs_create_enum
+ (L,
+ "zero", VAL0,
+ "one", VAL1,
+ "two", VAL2,
+ "three", VAL3,
+ HS_END);
+ mu_assert("failed to return correct index!", index == lua_gettop(L));
+ check("zero", VAL0);
+ check("one", VAL1);
+ check("two", VAL2);
+ check("three", VAL3);
+ return 0;
+}
+
+TEST(int_test)
+{
+ int index = hs_create_enum
+ (L,
+ "someValue", 23,
+ "anotherValue", 444,
+ "???", -5,
+ "217", 217,
+ HS_END);
+ mu_assert("failed to return correct index!", index == lua_gettop(L));
+ check("someValue", 23);
+ check("anotherValue", 444);
+ check("???", -5);
+ check("217", 217);
+ return 0;
+}
+
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * test suite
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+void hs_create_enum_tests()
+{
+ printf("running hs_create_enum() tests...\n");
+ mu_run_test("create enum table from enum", enum_test);
+ mu_run_test("create enum table from integers", int_test);
+}
diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c
index 06c6940..207e233 100644
--- a/src/tests/tests_main.c
+++ b/src/tests/tests_main.c
@@ -18,6 +18,9 @@ int main()
hs_parse_args_tests();
hs_parse_overloaded_tests();
+ hs_create_table_tests();
+ hs_create_enum_tests();
+
hs_pushstring_tests();
printf("\n=============== tests finished ===============\n\n");