Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/TestSimple/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public function ok(mixed $expression, string $description = '') : bool
return $this->pass($description);
} catch(\Throwable $err)
{
$error = $err->getMessage();
$trace = $this->format_call_location($err->getTrace());
$error = sprintf("\n%s in %s", get_class($err), $trace);
}
return $this->fail($call_location, $description, '');
return $this->fail($call_location, $description, $error);
}

public function is($expect, $actual, string $description = '') : bool
Expand Down
20 changes: 20 additions & 0 deletions t/output.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ $tests[] = [ 'script' => 'php t/res/array.php'
, 'name' => 'failed `is` on arrays shows human readable diff'
];

$expected = <<<EOL
not ok 1 - Thrown error
# Failed test 'Thrown error'
# at t/res/thrown_error.php:11
# DivisionByZeroError in src/TestSimple/Assert.php:51
not ok 2
# Failed test at t/res/thrown_error.php:12
# Error in src/TestSimple/Assert.php:51
not ok 3
# Failed test at t/res/thrown_error.php:13
# CustomThrowable in src/TestSimple/Assert.php:51
1..3
Looks like you failed 3 out of 3 tests
EOL;
$tests[] = [ 'script' => 'php t/res/thrown_error.php'
, 'expected_out' => $expected
, 'expected_exit' => 3
, 'name' => 'thrown errors are caught and error message is displayed'
];

foreach($tests as $test)
{
unset($got);
Expand Down
15 changes: 15 additions & 0 deletions t/res/thrown_error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once "vendor/autoload.php";
$assert = new TestSimple\Assert();

class CustomThrowable extends \Exception
{
protected $message = 'Custom throwable';
}

$assert->ok(function(){ 1 /0; }, 'Thrown error');
$assert->ok(function(){ throw new \Error('Custom error'); });
$assert->ok(function(){ throw new \CustomThrowable("Threw me for a loop\nmultiline error message"); });

$assert->done();
Loading