115 lines
2.2 KiB
C
115 lines
2.2 KiB
C
#include <ft/test.h>
|
|
#include <stdarg.h>
|
|
#undef ft_assert
|
|
|
|
//---
|
|
// Test framework
|
|
//---
|
|
|
|
void ft_test_init(ft_test *test)
|
|
{
|
|
test->passed = 0;
|
|
test->skipped = 0;
|
|
test->failed = 0;
|
|
}
|
|
|
|
void ft_test_run(ft_test *test)
|
|
{
|
|
ft_test_init(test);
|
|
if(!test->function) return;
|
|
test->function(test);
|
|
}
|
|
|
|
bool ft_test_done(ft_test *test)
|
|
{
|
|
return test->passed + test->skipped + test->failed > 0;
|
|
}
|
|
|
|
void ft_assert(ft_test *test, int expression, char const *file, int line,
|
|
char const *str)
|
|
{
|
|
if(expression) {
|
|
test->passed++;
|
|
}
|
|
else {
|
|
test->failed++;
|
|
ft_log(test, "%s:%d: assertion '%s' failed", file, line, str);
|
|
}
|
|
}
|
|
|
|
void ft_log(ft_test *test, char const *format, ...)
|
|
{
|
|
}
|
|
|
|
//---
|
|
// Test lists (to organize by header)
|
|
//---
|
|
|
|
void ft_list_init(ft_list *list)
|
|
{
|
|
list->passed = 0;
|
|
list->skipped = 0;
|
|
list->failed = 0;
|
|
|
|
if(!list->children) return;
|
|
for(int i = 0; list->children[i]; i++) {
|
|
ft_test_init(list->children[i]);
|
|
}
|
|
}
|
|
|
|
void ft_list_run(ft_list *list)
|
|
{
|
|
ft_list_init(list);
|
|
|
|
if(!list->children) return;
|
|
for(int i = 0; list->children[i]; i++) {
|
|
ft_test_run(list->children[i]);
|
|
}
|
|
|
|
ft_list_aggregate(list);
|
|
}
|
|
|
|
void ft_list_aggregate(ft_list *l)
|
|
{
|
|
l->passed = 0;
|
|
l->skipped = 0;
|
|
l->failed = 0;
|
|
|
|
if(!l->children) return;
|
|
for(int i = 0; l->children[i]; i++) {
|
|
ft_test const *t = l->children[i];
|
|
l->passed += t->passed;
|
|
l->skipped += t->skipped;
|
|
l->failed += t->failed;
|
|
}
|
|
}
|
|
|
|
//---
|
|
// Utilities
|
|
//---
|
|
|
|
int ft_test_color(ft_test const *t)
|
|
{
|
|
if(t->failed > 0) return FT_TEST_FAILED;
|
|
if(t->skipped > 0) return FT_TEST_SKIPPED;
|
|
if(t->passed > 0) return FT_TEST_PASSED;
|
|
if(t->function) return FT_TEST_PENDING;
|
|
return FT_TEST_EMPTY;
|
|
}
|
|
|
|
int ft_list_color(ft_list const *l)
|
|
{
|
|
if(l->failed > 0) return FT_TEST_FAILED;
|
|
if(l->skipped > 0) return FT_TEST_SKIPPED;
|
|
|
|
if(l->children) {
|
|
for(int i = 0; l->children[i]; i++) {
|
|
if(l->children[i]->function && l->children[i]->passed == 0)
|
|
return FT_TEST_PENDING;
|
|
}
|
|
}
|
|
|
|
if(l->passed > 0) return FT_TEST_PASSED;
|
|
return FT_TEST_EMPTY;
|
|
}
|