mirror of
https://github.com/danog/amp.git
synced 2025-01-22 13:21:16 +01:00
Add tests for InvalidYieldError, improve key display
Without something like var_export, null and false are casted to an empty string.
This commit is contained in:
parent
a66f25e4f6
commit
1299e51ca6
@ -12,7 +12,7 @@ class InvalidYieldError extends \Error {
|
||||
$prefix .= \sprintf(
|
||||
"; %s yielded at key %s",
|
||||
\is_object($yielded) ? \get_class($yielded) : \gettype($yielded),
|
||||
$generator->key()
|
||||
\var_export($generator->key(), true)
|
||||
);
|
||||
|
||||
if (!$generator->valid()) {
|
||||
|
35
test/InvalidYieldErrorTest.php
Normal file
35
test/InvalidYieldErrorTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\InvalidYieldError;
|
||||
|
||||
class InvalidYieldErrorTest extends \PHPUnit_Framework_TestCase {
|
||||
public function testWithInvalidGenerator() {
|
||||
/** @var \Generator $gen */
|
||||
$gen = (function () {
|
||||
if (false) {
|
||||
yield;
|
||||
}
|
||||
})();
|
||||
|
||||
$gen->current();
|
||||
|
||||
$error = new InvalidYieldError($gen, "prefix message");
|
||||
$this->assertSame("prefix message; NULL yielded at key NULL", $error->getMessage());
|
||||
}
|
||||
|
||||
public function testSubgenerator() {
|
||||
$subgen = (function () {
|
||||
yield "foo" => 42;
|
||||
})();
|
||||
|
||||
/** @var \Generator $gen */
|
||||
$gen = (function () use ($subgen) {
|
||||
yield from $subgen;
|
||||
})();
|
||||
|
||||
$error = new InvalidYieldError($gen, "prefix");
|
||||
$this->assertSame("prefix; integer yielded at key 'foo' on line " . (__LINE__ - 8) . " in " . __FILE__, $error->getMessage());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user