diff --git a/src/TestSimple/Assert.php b/src/TestSimple/Assert.php index c920aef..f3e484f 100644 --- a/src/TestSimple/Assert.php +++ b/src/TestSimple/Assert.php @@ -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 diff --git a/t/output.phpt b/t/output.phpt index f6c768b..1f44d03 100755 --- a/t/output.phpt +++ b/t/output.phpt @@ -120,6 +120,26 @@ $tests[] = [ 'script' => 'php t/res/array.php' , 'name' => 'failed `is` on arrays shows human readable diff' ]; +$expected = << '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); diff --git a/t/res/thrown_error.php b/t/res/thrown_error.php new file mode 100755 index 0000000..f6c69ae --- /dev/null +++ b/t/res/thrown_error.php @@ -0,0 +1,15 @@ +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();