summaryrefslogtreecommitdiff
path: root/source/kit_test
diff options
context:
space:
mode:
authorMitya Selivanov <0x7fffff@guattari.ru>2022-08-08 02:50:03 +0400
committerMitya Selivanov <0x7fffff@guattari.ru>2022-08-08 02:50:03 +0400
commitcd1e69e13d8dc1df57b5f371edc3f23ceb9bafc1 (patch)
tree89f5a8af881ad026b2fa3a5c5e5774dc4c7bf17f /source/kit_test
parentccf5e49b969c7643cd67ac678a7f13f4639db745 (diff)
downloadkit-cd1e69e13d8dc1df57b5f371edc3f23ceb9bafc1.zip
Add tests
Diffstat (limited to 'source/kit_test')
-rw-r--r--source/kit_test/CMakeLists.txt2
-rw-r--r--source/kit_test/test.c (renamed from source/kit_test/run_tests.c)43
-rw-r--r--source/kit_test/test.h14
3 files changed, 36 insertions, 23 deletions
diff --git a/source/kit_test/CMakeLists.txt b/source/kit_test/CMakeLists.txt
index f7315a1..25b9d81 100644
--- a/source/kit_test/CMakeLists.txt
+++ b/source/kit_test/CMakeLists.txt
@@ -1,6 +1,6 @@
target_sources(
${KIT_TEST_LIBRARY}
PRIVATE
- run_tests.c
+ test.c
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/test.h>)
diff --git a/source/kit_test/run_tests.c b/source/kit_test/test.c
index 1bb6e67..db3c68c 100644
--- a/source/kit_test/run_tests.c
+++ b/source/kit_test/test.c
@@ -39,8 +39,17 @@ static void color_code(int term_color, int c) {
}
}
+void kit_test_register(char const *name, kit_test_run_fn fn) {
+ int n = kit_tests_list.size++;
+ if (n < KIT_TESTS_SIZE_LIMIT) {
+ kit_tests_list.tests[n].test_fn = fn;
+ strcpy(kit_tests_list.tests[n].test_name, name);
+ kit_tests_list.tests[n].assertions = 0;
+ }
+}
+
int kit_run_tests(int argc, char **argv) {
- int fail_test_count = 0;
+ int success_count = 0;
int fail_assertion_count = 0;
int total_assertion_count = 0;
int status = 0;
@@ -50,7 +59,8 @@ int kit_run_tests(int argc, char **argv) {
if (strcmp("--no-term-color", argv[i]) == 0)
term_color = 0;
- for (int i = 0; i < kit_tests_list.size; i++) {
+ for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT;
+ i++) {
color_code(term_color, yellow);
printf("[ RUN... ] %s ", kit_tests_list.tests[i].test_name);
color_code(term_color, white);
@@ -74,7 +84,7 @@ int kit_run_tests(int argc, char **argv) {
test_status = 0;
}
- if (kit_tests_list.tests[i].assertions >=
+ if (kit_tests_list.tests[i].assertions >
KIT_TEST_ASSERTIONS_LIMIT)
test_status = 0;
@@ -83,30 +93,35 @@ int kit_run_tests(int argc, char **argv) {
if (test_status == 0) {
color_code(term_color, red);
printf("[ RUN ] %s\n", kit_tests_list.tests[i].test_name);
- printf("[ FAILED ] %s - %d ms\n",
- kit_tests_list.tests[i].test_name, duration);
+ printf("[ FAILED ] %s", kit_tests_list.tests[i].test_name);
color_code(term_color, white);
- fail_test_count++;
+ if (duration > 0)
+ printf(" - %d ms", duration);
+ printf("\n");
status = 1;
} else {
color_code(term_color, green);
printf("[ RUN ] %s\n", kit_tests_list.tests[i].test_name);
- printf("[ OK ] %s - %d ms\n",
- kit_tests_list.tests[i].test_name, duration);
+ printf("[ OK ] %s", kit_tests_list.tests[i].test_name);
color_code(term_color, white);
+ if (duration > 0)
+ printf(" - %d ms", duration);
+ printf("\n");
+ success_count++;
}
}
- printf("\n%d of %d tests passed.\n",
- kit_tests_list.size - fail_test_count, kit_tests_list.size);
+ printf("\n%d of %d tests passed.\n", success_count,
+ kit_tests_list.size);
printf("%d of %d assertions passed.\n\n",
total_assertion_count - fail_assertion_count,
total_assertion_count);
if (status != 0) {
- for (int i = 0; i < kit_tests_list.size; i++) {
- if (kit_tests_list.tests[i].assertions >=
+ for (int i = 0;
+ i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) {
+ if (kit_tests_list.tests[i].assertions >
KIT_TEST_ASSERTIONS_LIMIT)
printf("Too many assertions for \"%s\" in \"%s\"!\n",
kit_tests_list.tests[i].test_name,
@@ -123,8 +138,8 @@ int kit_run_tests(int argc, char **argv) {
printf("\n");
}
- if (kit_tests_list.size == KIT_TESTS_SIZE_LIMIT) {
- printf("Too many tests!");
+ if (kit_tests_list.size > KIT_TESTS_SIZE_LIMIT) {
+ printf("Too many tests!\n\n");
status = 1;
}
diff --git a/source/kit_test/test.h b/source/kit_test/test.h
index fdbfbdc..4dc7aac 100644
--- a/source/kit_test/test.h
+++ b/source/kit_test/test.h
@@ -79,20 +79,17 @@ extern kit_tests_list_t kit_tests_list;
# endif
#endif
+void kit_test_register(char const *name, kit_test_run_fn fn);
+
#define KIT_TEST(name) \
static void KIT_TEST_CONCAT3_(kit_test_run_, __LINE__, \
KIT_TEST_FILE)(int, \
kit_test_report_fn); \
KIT_TEST_ON_START_( \
KIT_TEST_CONCAT3_(kit_test_case_, __LINE__, KIT_TEST_FILE)) { \
- int n = kit_tests_list.size; \
- if (n < KIT_TESTS_SIZE_LIMIT) { \
- kit_tests_list.size++; \
- kit_tests_list.tests[n].test_fn = KIT_TEST_CONCAT3_( \
- kit_test_run_, __LINE__, KIT_TEST_FILE); \
- strcpy(kit_tests_list.tests[n].test_name, name); \
- kit_tests_list.tests[n].assertions = 0; \
- } \
+ kit_test_register( \
+ name, \
+ KIT_TEST_CONCAT3_(kit_test_run_, __LINE__, KIT_TEST_FILE)); \
} \
static void KIT_TEST_CONCAT3_(kit_test_run_, __LINE__, \
KIT_TEST_FILE)( \
@@ -108,6 +105,7 @@ int kit_run_tests(int argc, char **argv);
# define TEST KIT_TEST
# define REQUIRE KIT_REQUIRE
+# define test_register kit_test_register
# define run_tests kit_run_tests
#endif