diff --git a/libs/tests/tests.c b/libs/tests/tests.c new file mode 100644 index 0000000..aceb9e9 --- /dev/null +++ b/libs/tests/tests.c @@ -0,0 +1,50 @@ +#include "uthash.h" +#include +#include +#include +#include + +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"); +}