Updated testing library to make writing tests easier
Success is printed by the RUN_TEST_SUITE macro so we don't need to add that boilerplate.
This commit is contained in:
@@ -36,7 +36,6 @@ void testing_lib_bytes_to_hword(void)
|
|||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SUCCESS(__func__, "%s\n", "Test succeeded");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testing_lib_bytes_to_word(void)
|
void testing_lib_bytes_to_word(void)
|
||||||
@@ -60,9 +59,9 @@ void testing_lib_bytes_to_word(void)
|
|||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SUCCESS(__func__, "%s\n", "Test succeeded");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_SUITE(test_lib, testing_lib_bytes_to_hword, testing_lib_bytes_to_word);
|
TEST_SUITE(test_lib, CREATE_TEST(testing_lib_base_bytes_to_hword),
|
||||||
|
CREATE_TEST(testing_lib_base_bytes_to_word));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -18,21 +18,37 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define MESSAGE(COLOUR, NAME, FORMAT, ...) \
|
#define MESSAGE(FILE, COLOUR, NAME, FORMAT, ...) \
|
||||||
printf("\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
fprintf(FILE, "\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__)
|
||||||
|
|
||||||
#define INFO(NAME, FORMAT, ...) MESSAGE(TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
|
#define INFO(NAME, FORMAT, ...) \
|
||||||
#define FAIL(NAME, FORMAT, ...) MESSAGE(TERM_RED, NAME, FORMAT, __VA_ARGS__)
|
MESSAGE(stdout, TERM_YELLOW, NAME, FORMAT, __VA_ARGS__)
|
||||||
|
#define FAIL(NAME, FORMAT, ...) \
|
||||||
|
MESSAGE(stderr, TERM_RED, NAME, FORMAT, __VA_ARGS__)
|
||||||
#define SUCCESS(NAME, FORMAT, ...) \
|
#define SUCCESS(NAME, FORMAT, ...) \
|
||||||
MESSAGE(TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
|
MESSAGE(stdout, TERM_GREEN, NAME, FORMAT, __VA_ARGS__)
|
||||||
|
|
||||||
typedef void (*test_fn)(void);
|
typedef void (*test_fn)(void);
|
||||||
|
struct Test
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
test_fn src;
|
||||||
|
};
|
||||||
|
|
||||||
#define TEST_SUITE(NAME, ...) test_fn NAME[] = {__VA_ARGS__}
|
#define CREATE_TEST(NAME) \
|
||||||
#define RUN_TEST_SUITE(NAME) \
|
(struct Test) \
|
||||||
INFO(#NAME, "%s", "Starting test suite...\n"); \
|
{ \
|
||||||
for (size_t i = 0; i < ARR_SIZE(NAME); ++i) \
|
.name = #NAME, .src = NAME \
|
||||||
NAME[i](); \
|
}
|
||||||
SUCCESS(#NAME, "%s", "Finished test suite!\n")
|
|
||||||
|
#define TEST_SUITE(NAME, ...) struct Test NAME[] = {__VA_ARGS__}
|
||||||
|
#define RUN_TEST_SUITE(SUITE) \
|
||||||
|
INFO(#SUITE, "%s", "Starting test suite...\n"); \
|
||||||
|
for (size_t i = 0; i < ARR_SIZE(SUITE); ++i) \
|
||||||
|
{ \
|
||||||
|
SUITE[i].src(); \
|
||||||
|
SUCCESS(SUITE[i].name, "%s\n", "Test succeeded"); \
|
||||||
|
} \
|
||||||
|
SUCCESS(#SUITE, "%s", "Finished test suite!\n")
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user