From 1299e51ca6066983b0f543601ec8dbafb8c48569 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sun, 11 Dec 2016 17:08:17 +0100 Subject: [PATCH] Add tests for InvalidYieldError, improve key display Without something like var_export, null and false are casted to an empty string. --- lib/InvalidYieldError.php | 2 +- test/InvalidYieldErrorTest.php | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/InvalidYieldErrorTest.php diff --git a/lib/InvalidYieldError.php b/lib/InvalidYieldError.php index b9d4530..dc02a16 100644 --- a/lib/InvalidYieldError.php +++ b/lib/InvalidYieldError.php @@ -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()) { diff --git a/test/InvalidYieldErrorTest.php b/test/InvalidYieldErrorTest.php new file mode 100644 index 0000000..9cbea7b --- /dev/null +++ b/test/InvalidYieldErrorTest.php @@ -0,0 +1,35 @@ +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()); + } +} \ No newline at end of file