More support for test. Panic function update.

This commit is contained in:
Christoffer Lerno
2022-11-14 11:14:45 +01:00
committed by Christoffer Lerno
parent 450113d161
commit 49eacb8824
21 changed files with 269 additions and 78 deletions

View File

@@ -62,6 +62,11 @@ extern fn LongDivResult ldiv(long number, long denom);
extern fn int rand();
extern fn void srand(uint seed);
define JmpBuf = CInt[$$JMP_BUF_SIZE];
extern fn CInt setjmp(JmpBuf* buffer);
extern fn void longjmp(JmpBuf* buffer, CInt value);
// MB functions omitted
// string
@@ -131,6 +136,7 @@ $default:
macro CFile stderr() { return (CFile*)(uptr)2; }
$endswitch;
const HAS_MALLOC_SIZE =
env::OS_TYPE == OsType.LINUX
|| env::OS_TYPE == OsType.WIN32
@@ -233,6 +239,7 @@ define SignalFunction = fn void(int);
extern fn SignalFunction signal(int sig, SignalFunction function);
// Incomplete
module libc::errno;
const Errno EPERM = 1; /* Operation not permitted */
@@ -368,3 +375,5 @@ const Errno EKEYREJECTED = 129; /* Key was rejected by service */
const Errno EOWNERDEAD = 130; /* Owner died */
const Errno ENOTRECOVERABLE = 131; /* State not recoverable */

View File

@@ -28,33 +28,72 @@ struct VarArrayHeader
void *allocator;
}
define TestFn = fn void!();
struct TestRunner
{
char** test_names;
void** test_fns;
uint count;
char[][] test_names;
TestFn[] test_fns;
JmpBuf buf;
}
fn TestRunner test_runner_create()
{
return TestRunner {
.test_names = $$TEST_NAMES,
.test_fns = $$TEST_FNS,
.count = $$TEST_COUNT,
.test_names = $$TEST_NAMES,
};
}
extern fn void puts(char* c);
import libc;
fn void TestRunner.run(TestRunner* runner)
private TestRunner* current_runner;
fn void test_panic(char[] message, char[] file, char[] function, uint line)
{
for (uint i = 0; i < runner.count; i++)
{
puts(runner.test_names[i]);
}
io::println("[error]");
io::printfln("\n Error: %s", message);
io::printfln(" - in %s %s:%s.\n", function, file, line);
libc::longjmp(&current_runner.buf, 1);
}
fn void run_test_runner()
fn bool TestRunner.run(TestRunner* runner)
{
test_runner_create().run();
current_runner = runner;
PanicFn old_panic = builtin::panic;
defer builtin::panic = old_panic;
builtin::panic = &test_panic;
int tests_passed = 0;
int tests = runner.test_names.len;
io::println("----- TESTS -----");
foreach(i, char[] name : runner.test_names)
{
io::printf("Testing %s ... ", name);
if (libc::setjmp(&runner.buf) == 0)
{
if (catch err = runner.test_fns[i]())
{
io::println("[failed]");
continue;
}
io::println("[ok]");
tests_passed++;
}
}
io::printfln("\n%d test(s) run.\n", tests);
io::print("Test Result: ");
if (tests_passed < tests)
{
io::print("FAILED");
}
else
{
io::print("ok");
}
io::printfln(". %d passed, %d failed.", tests_passed, tests - tests_passed);
return tests == tests_passed;
}
fn bool __run_default_test_runner()
{
return test_runner_create().run();
}