Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/std/core/test.c3
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fn void? test_div() @test
test::le(2, 3);
test::eq_approx(m::divide(1, 3)!, 0.333, places: 3);
test::@check(2 == 2, "divide: %d", divide(6, 3)!);
test::@ok(m::divide(3, 1));
test::@catch(m::divide(3, 0));
test::@error(m::divide(3, 0), MathError.DIVISION_BY_ZERO);
}
Expand Down Expand Up @@ -77,6 +79,33 @@ macro @check(#condition, String format = "", args...)
}
}

<*
Check if function returns error
@param #funcresult : `result of function execution`
@require runtime::test_context != null : "Only allowed in @test functions"
*>
macro @catch(#funcresult)
{
if (catch err = #funcresult) return;
print_panicf("`%s` unexpectedly did not return error.", $stringify(#funcresult));
}

<*
Check if function doesn't return error
@param #funcresult : `result of function execution`
@require runtime::test_context != null : "Only allowed in @test functions"
*>
macro @ok(#funcresult)
{
if (catch err = #funcresult)
{
print_panicf("`%s` returned unexpected error [%s].",
$stringify(#funcresult), err);
}
}

<*
Check if function returns specific error
Expand Down
70 changes: 70 additions & 0 deletions test/unit/stdlib/core/test_test.c3
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,76 @@ alias TestFailFn = fn void? (bool to_fail);

faultdef FOO;

fn void test_catch()
{
TestFailFn ffail_void = fn void?(bool to_fail)
{
if (to_fail) return io::FILE_NOT_FOUND?;
};
TestIntFn ffail_int = fn int?(int a, int b)
{
if (b == 0) return io::FILE_NOT_FOUND?;
return a / b;
};
test::@setup(state.setup_fn, state.teardown_fn);

test::@catch(ffail_void(true));
test::@catch(ffail_int(1, 0));
}

fn void test_catch_fail()
{
TestFailFn ffail_void = fn void?(bool to_fail)
{
if (to_fail) return io::FILE_NOT_FOUND?;
};
TestIntFn ffail_int = fn int?(int a, int b)
{
if (b == 0) return io::FILE_NOT_FOUND?;
return a / b;
};
test::@setup(state.setup_fn, state.teardown_fn);
state.expected_fail = true;

test::@catch(ffail_void(false));
test::@catch(ffail_int(1, 2));
}

fn void test_ok()
{
TestFailFn ffail_void = fn void?(bool to_fail)
{
if (to_fail) return io::FILE_NOT_FOUND?;
};
TestIntFn ffail_int = fn int?(int a, int b)
{
if (b == 0) return io::FILE_NOT_FOUND?;
return a / b;
};
test::@setup(state.setup_fn, state.teardown_fn);

test::@ok(ffail_void(false));
test::@ok(ffail_int(1, 2));
}

fn void test_ok_fail()
{
TestFailFn ffail_void = fn void?(bool to_fail)
{
if (to_fail) return io::FILE_NOT_FOUND?;
};
TestIntFn ffail_int = fn int?(int a, int b)
{
if (b == 0) return io::FILE_NOT_FOUND?;
return a / b;
};
test::@setup(state.setup_fn, state.teardown_fn);
state.expected_fail = true;

test::@ok(ffail_void(true));
test::@ok(ffail_int(1, 0));
}

fn void test_error()
{
TestFailFn ffail_void = fn void?(bool to_fail)
Expand Down
Loading