Skip to content

Core\Test\Failure

The error a failed check throws.

Every check Core\Test offers does two things when it does not hold: it throws this, and it records the outcome in a ledger for the test it is in. The runner reads the ledger rather than the throw, so a test that catches its own failure and carries on has still failed, and nothing it can call will say otherwise.

That is what makes catching one safe rather than a loophole. A helper that checks every field of a record and reports all the wrong ones at once, a block that keeps going after the first disappointment, a test written about a check of your own: each of those has to intercept a failure, and none of them can hide one. Core\Test::expectFailure is the single spelling for a failure consumed on purpose.

Good to know: it hangs off the root of the error tree directly rather than under the errors a running program raises, so a catch written for those never picks one up by accident.

Examples

What a failed check tells you

<?nvs
// A check that fails throws a `Core\Test\Failure`. Its message names the check
// and says why it failed. Your own `message` comes first.
use Core\Test;
use Core\Test\Failure;
try {
Test::assertTrue(false, { message: 'the basket should have been emptied' });
} catch (Failure $failed) {
echo $failed->message, "\n";
}
// `Failure` extends `Throwable` directly. It is not a `LogicError` or a `RuntimeError`,
// so only the `Throwable` block catches it.
try {
Test::assertEquals(2, 3);
} catch (LogicError $notAMistakeInTheCode) {
echo "a LogicError clause\n";
} catch (RuntimeError $notTheWorldSayingNo) {
echo "a RuntimeError clause\n";
} catch (Throwable $theRoot) {
echo "only a Throwable clause\n";
}
Output
the basket should have been emptied: Core\Test::assertTrue failed: `$actual` is false
only a Throwable clause

Every field and not only the first

<?nvs
// This program checks three fields and reports every wrong one in a single run.
// Without the `catch`, the first failed check would stop the others.
// A caught failure still counts. Each check records its result before it throws,
// so the test has still failed twice.
use Core\Test;
use Core\Test\Failure;
final class Fields
{
// Returns the field's name when the value is wrong, and an empty string when
// it is right. The caller joins the names into one report.
public static function wrong(string $name, string $expected, string $actual): string
{
try {
Test::assertEquals($actual, $expected, { message: $name });
return '';
} catch (Failure $mismatch) {
return $name;
}
}
}
string $report = '';
$report = $report . Fields::wrong('street', 'Kärntner Ring 1', 'Kärntner Ring 1');
$report = $report . Fields::wrong(' city', 'Vienna', 'Wien');
$report = $report . Fields::wrong(' postcode', '1010', '1100');
echo 'wrong fields:', $report, "\n";
Output
wrong fields: city postcode

Testing a check of your own

<?nvs
// This program tests a check function that you wrote yourself.
// `Test::expectFailure` runs a function and passes only if a check in it failed.
// It then removes that failure from the test's result.
use Core\Test;
use Core\Test\Failure;
final class Money
{
// This is the check under test. An amount in cents must not be
// below zero.
public static function assertValidCents(int $amount): void
{
Test::assertTrue($amount >= 0, { message: 'an amount cannot be negative' });
}
}
// A valid amount passes, and the test records no failure.
Money::assertValidCents(1250);
echo "1250 is accepted\n";
// An invalid amount must fail the check. `expectFailure` tests this,
// and the test itself still passes.
Test::expectFailure(fn(): void => {
Money::assertValidCents(-1);
});
echo "-1 is refused\n";
// If no check in the function fails, `expectFailure` throws a `Failure`.
// So a check that stops working makes this test fail.
try {
Test::expectFailure(fn(): void => {
Money::assertValidCents(0);
});
echo "nothing was refused\n";
} catch (Failure $nothingFailed) {
echo "0 is accepted, so there was nothing to refuse\n";
}
Output
1250 is accepted
-1 is refused
0 is accepted, so there was nothing to refuse