summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2021-09-23 22:06:41 -0500
committersanine <sanine.not@pm.me>2021-09-23 22:06:41 -0500
commit6f92e9da2f6b0223c34728da34be65d76d5db485 (patch)
tree932760a75acd33dfe4e303e1c46fd8bdd153501f
parent34603bb6d538ab1b0edbe10feeef27b80b1b7f7a (diff)
add basic test framework
-rw-r--r--CMakeLists.txt12
-rw-r--r--src/test/logging/logging_tests.c6
-rw-r--r--src/test/minunit.h41
-rw-r--r--src/test/suites.h11
-rw-r--r--src/test/test_main.c14
5 files changed, 84 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1b11d68..2934ec2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -40,3 +40,15 @@ endif()
target_link_libraries(honey ${LIBRARIES})
+# build tests (optional)
+
+set(TEST_ROOT ${CMAKE_SOURCE_DIR}/src/test)
+set(TEST_SOURCES
+ ${TEST_ROOT}/test_main.c
+ ${TEST_ROOT}/logging/logging_tests.c
+ )
+add_executable(test EXCLUDE_FROM_ALL ${TEST_SOURCES})
+set_target_properties(test PROPERTIES
+ C_STANDARD 99
+ CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic")
+target_link_libraries(test ${LUA_LIBRARIES} honeysuckle)
diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c
new file mode 100644
index 0000000..2e92d68
--- /dev/null
+++ b/src/test/logging/logging_tests.c
@@ -0,0 +1,6 @@
+#include "../minunit.h"
+#include "../suites.h"
+
+void honey_logging_tests()
+{
+}
diff --git a/src/test/minunit.h b/src/test/minunit.h
new file mode 100644
index 0000000..b1bfda7
--- /dev/null
+++ b/src/test/minunit.h
@@ -0,0 +1,41 @@
+#ifndef MINUNIT_H
+#define MINUNIT_H
+
+#include <stdio.h>
+
+#define STR_IMPL(x) #x
+#define STR(x) STR_IMPL(x)
+
+#define MU_INDENT " "
+
+/* minunit testing macros from /www.jera.com/techinfo/jtns/jtn002.html */
+#define mu_assert(test, message) do { if (!(test)) return message "\n" MU_INDENT "[" __FILE__ ":" STR(__LINE__) "]"; } while (0)
+#define mu_assert_equal(a, b) mu_assert(a == b, "'" #a "' is not equal to '" #b "'")
+#define mu_assert_unequal(a, b) mu_assert(a != b, "'" #a "' is equal to '" #b "'")
+#define mu_assert_streq(a, b) mu_assert(strcmp(a, b) == 0, "'" #a "' is not equal to '" #b "'")
+
+#define mu_run_test(name, test) do { \
+ const char *message = test(); \
+ tests_run++; \
+ if (message) { \
+ printf(MU_INDENT "test '%s' failed: %s\n", name, message); \
+ tests_failed++; \
+ } \
+ } while (0)
+#define mu_run_suite(suite) do { \
+ int tests_run_old = tests_run; \
+ printf("suite: " #suite "\n"); \
+ suite(); \
+ printf(MU_INDENT "ran %d tests\n\n", \
+ tests_run - tests_run_old); \
+ } while(0)
+#define mu_tests_finished() do { \
+ printf("ran %d tests, %d failed\n", tests_run, tests_failed); \
+ return tests_failed; \
+ } while(0)
+
+#define mu_test const char *
+
+extern int tests_run, tests_failed;
+
+#endif
diff --git a/src/test/suites.h b/src/test/suites.h
new file mode 100644
index 0000000..1dec589
--- /dev/null
+++ b/src/test/suites.h
@@ -0,0 +1,11 @@
+#ifndef HONEY_TESTS_SUITES
+#define HONEY_TESTS_SUITES
+
+#include "minunit.h"
+
+void honey_logging_tests();
+
+#define RUN_TESTS \
+ mu_run_suite(honey_logging_tests);
+
+#endif
diff --git a/src/test/test_main.c b/src/test/test_main.c
new file mode 100644
index 0000000..a4e776a
--- /dev/null
+++ b/src/test/test_main.c
@@ -0,0 +1,14 @@
+#include "minunit.h"
+#include "suites.h"
+
+int tests_run = 0;
+int tests_failed = 0;
+
+int main()
+{
+ printf("~~~~~~~~ running tests ~~~~~~~~\n\n");
+ RUN_TESTS;
+ printf("~~~~~~~~~ tests done ~~~~~~~~~~\n\n");
+
+ mu_tests_finished();
+}