Skip to content

Assertions and dumping values

A Core\Test::assert... method that fails throws Core\Test\Failure. Core\Debug::render returns a readable view of any value as a Core\Cli\Text. Its text() method gives the string.

A failed assertion

An assertion that fails throws Core\Test\Failure.

Every Core\Test::assert… method either returns and lets your code carry on, or throws. The message names the method that failed and the values it compared, so you can read what went wrong without opening the file again. The message option puts your own sentence in front of that text.

Core\Test\Failure is not a RuntimeError. It sits directly under Throwable, so a catch (RuntimeError $e) in the code you are testing never catches it by accident. A catch (Throwable $e) does catch it.

Inside a #[Test] method the test runner catches the failure and reports that test as failed. Outside one it is an ordinary throw: catch it, or it ends your program.

The examples below show the message, the catch arm that takes it, and a check outside a test.

What a failure says

<?nvs
// Shows what a failed assertion throws, and what its message says.
// Every `Core\Test::assert...` method throws `Core\Test\Failure` when it fails.
try {
Core\Test::assertEquals(41, 42);
} catch (Core\Test\Failure $failure) {
// The message names the method and both values it compared.
echo $failure->message, "\n";
}
// The `message` option puts your own sentence in front of that text.
try {
Core\Test::assertTrue(false, { message: 'the flag should be set' });
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
// `location` is the file and the line of the assertion that failed.
try {
Core\Test::assertCount(['order', 'customer'], 3);
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
echo 'raised at ', $failure->location, "\n";
}
Output
Core\Test::assertEquals failed: `$actual` is 41, `$expected` is 42
the flag should be set: Core\Test::assertTrue failed: `$actual` is false
Core\Test::assertCount failed: `$actual` holds 2 entries, `$expected` is 3
raised at docs/examples/lang/errors/assertion-failures/01-what-a-failure-says.nvs:21

Only a throwable arm catches it

<?nvs
// Shows which `catch` arm takes a failed assertion. `Core\Test\Failure` is not
// a `RuntimeError`.
class Cart
{
public static function total(array<int> $prices): int
{
int $sum = 0;
foreach ($prices as int $price) {
$sum = $sum + $price;
}
return $sum;
}
}
// The `RuntimeError` arm does not match a failure. The `Throwable` arm is the
// one that runs.
try {
Core\Test::assertEquals(Cart::total([100, 250]), 999);
} catch (RuntimeError $error) {
echo "the RuntimeError arm caught it\n";
} catch (Throwable $failure) {
echo "the Throwable arm caught it\n";
echo $failure->message, "\n";
}
Output
the Throwable arm caught it
Core\Test::assertEquals failed: `$actual` is 350, `$expected` is 999

A check that ends the script

<?nvs
// Shows a check outside a test. Nothing catches the failure, so the program
// ends with exit status 1.
// proof: exit 1
array<string> $config = ['host' => 'localhost', 'port' => '5432'];
echo "checking the configuration\n";
// This assertion passes, so the program carries on.
Core\Test::assertCount($config, 2);
// This one fails. Outside a `#[Test]` method a failure is an ordinary throw,
// so it ends the program here and the error output gets the message.
Core\Test::assertTrue(
Core\Arr::hasKey($config, 'password'),
{ message: 'the password is missing' },
);
echo "this line never runs\n";
Output
checking the configuration

Details

$failure is RuntimeError and $failure is LogicError are both false. Only a catch for Core\Test\Failure or for Throwable takes a failed assertion.

An assertion that passes returns nothing and prints nothing.

You can create a failure yourself with new Core\Test\Failure("…"). Your own class cannot extend it.

A failure that nothing catches ends the program with status 1, like any uncaught error. The report on standard error has the class Core\Test\Failure and the message.

Seeing what a value is

Two members show you what a value really is while you are working on a program. Core\Debug::dump writes one rendering per argument to standard error. Core\Debug::render builds the same rendering and returns it as a Core\Cli\Text value. You can print it or put it in a log line.

A rendering names the type of every value, the length of every string and the class of every object. Control bytes such as a tab are made visible, a structure that is very deep or very long is cut short, a value that points back at itself is marked, and a property declared secret is shown as [redacted].

dump never writes to standard output, and a captured buffer never swallows it. What your program prints stays exactly what it printed, so a dump you forget to remove cannot change a response.

The examples below show a rendering printed, a dump beside a program's own output, and a password that stays hidden.

Seeing what a value is

<?nvs
// Shows `Core\Debug::render`. It returns the rendering of a value as text, so
// this program prints it. Every value says what type it is, and a string says
// how many bytes it has.
final class Order
{
public function constructor(public int $id, public string $customer)
{
}
}
echo Core\Debug::render(new Order(1042, 'Ada')), "\n";
// A tab inside a string is shown as `\t`, so you can see it.
echo Core\Debug::render(['paid' => true, 'note' => "two\tcolumns"]), "\n";
Output
Order#1 (2) {
$id => int(1042)
$customer => string(3) "Ada"
}
array(2) [
"paid" => bool(true)
"note" => string(11) "two\tcolumns"
]

A dump beside your own output

<?nvs
// Shows `Core\Debug::dump`. It writes to standard error, so it appears in your
// terminal while the program's own output stays unchanged. The lines below are
// the output; the two renderings arrive on standard error.
final class Basket
{
public function constructor(public int $items, public int $cents)
{
}
}
var $basket = new Basket(3, 4599);
Core\Debug::dump($basket);
// One call takes several values and renders each of them.
Core\Debug::dump($basket->items, $basket->cents, 'still here');
echo 'total: ', $basket->cents, " cents\n";
echo 'items: ', $basket->items, "\n";
Output
total: 4599 cents
items: 3

A password that stays hidden

<?nvs
// Shows what happens to a password in a rendering. A property declared `secret`
// is printed as `[redacted]`. You can inspect a configuration object without
// putting its password in your terminal or your log file.
final class DbConfig
{
public function constructor(
public string $host,
public string $user,
public secret string $password,
)
{
}
}
var $config = new DbConfig('db.internal', 'orders', 'hunter2');
// The host and the user are shown. The password is not.
echo Core\Debug::render($config), "\n";
Output
DbConfig#1 (3) {
$host => string(11) "db.internal"
$user => string(6) "orders"
$password => [redacted]
}

Details

Core\Debug::render returns a Core\Cli\Text, not a string. echo prints it. Call ->text() to get the string, for example to compare it: Core\Debug::render(1)->text() == "int(1)" is true. Two Core\Cli\Text values are objects, so == on them compares the objects, not the text.

Each value shows its type. null is null, and a number is int(1), uint(3), float(1.5) or decimal(2.50). An enum case shows its number, such as int(1). A function shows the number of its parameters, such as callable(1 parameter). A string shows its length in bytes, so "café" is string(5).

A newline is shown as \n and a tab as \t. Other control bytes and the characters that change the direction of text are shown as visible symbols.

An array shows its first 100 entries, then [elided: 200 of 300 entries] for the rest. A string over 1024 bytes shows its first 1024 bytes, as string(1024+) "…" [elided: 3976 bytes]. A structure shows eight levels, and a deeper value is [elided: below the depth cap]. An object that was already shown higher up is [cycle -> #1], with the number of that object.

Core\Debug::dump() with no value writes nothing. Output from dump inside Core\Out::capture is not captured. It still goes to standard error.

A secret property is shown as [redacted]. A secret variable cannot be passed to dump or render at all: that is the error E0724.

Inside a web request, dump writes a log record at the Debug level. In production, that record is kept only when [log] level is "Debug".

Core\Debug::dump($value) writes the same view to standard error. It never goes into the normal output, so a web page or a captured output does not change.

With [debug] store = true, each dump is also recorded. The Debug page of the debug viewer lists the dumps with the file and line that called dump. Its By site tab groups the dumps of each line. A production server records the type and size of each value, and not the value.

  • Do use Core\Debug::dump while you look for a bug.
  • Do use Core\Debug::render when you need the text, for example in a log line.
  • Don’t catch Core\Test\Failure in a test to hide a failure.
  • Don’t print render output to a web page. Use it for logs and debugging.