forked from ground/ground
most basic tests lib on the planet
This commit is contained in:
50
libs/tests/tests.c
Normal file
50
libs/tests/tests.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include "uthash.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <groundvm.h>
|
||||||
|
#include <groundext.h>
|
||||||
|
#include <ansii.h>
|
||||||
|
|
||||||
|
GroundValue testsRun(GroundScope* scope, List args) {
|
||||||
|
GroundObject* testsStruct = args.values[0].data.customVal;
|
||||||
|
|
||||||
|
int totalTests = 0, successful = 0, fail = 0;
|
||||||
|
|
||||||
|
GroundObjectField* entry, *temp;
|
||||||
|
HASH_ITER(hh, testsStruct->fields, entry, temp) {
|
||||||
|
if (entry) {
|
||||||
|
|
||||||
|
totalTests++;
|
||||||
|
|
||||||
|
if (entry->value.type != FUNCTION) {
|
||||||
|
ERROR("The given tests struct can't have any struct fields, only methods!", "WrongType");
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundValue success = groundRunFunction(entry->value.data.fnVal, 0);
|
||||||
|
if (success.type != BOOL) {
|
||||||
|
ERROR("Your tests should return a boolean.", "ReturnTypeMismatch");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success.data.boolVal) {
|
||||||
|
successful++;
|
||||||
|
printf(ESC_GREEN_FG ESC_BOLD ".");
|
||||||
|
} else {
|
||||||
|
fail++;
|
||||||
|
printf(ESC_RED_FG ESC_BOLD ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(ESC_RESET "\n\n/============================\\\n");
|
||||||
|
printf("- Total : %d\n", totalTests);
|
||||||
|
printf("- Successful: %d\n", successful);
|
||||||
|
printf("- Fail : %d\n", fail);
|
||||||
|
printf("\\============================/\n");
|
||||||
|
|
||||||
|
|
||||||
|
return groundCreateValue(INT, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ground_init(GroundScope* scope) {
|
||||||
|
groundAddNativeFunction(scope, "tests_Run", testsRun, INT, 1, CUSTOM, "testsStruct");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user