diff --git a/src/main/php/test/assert/Assertable.class.php b/src/main/php/test/assert/Assertable.class.php index 7e74c17..76a2542 100755 --- a/src/main/php/test/assert/Assertable.class.php +++ b/src/main/php/test/assert/Assertable.class.php @@ -8,7 +8,7 @@ /** @test test.unittest.AssertableTest */ class Assertable { public static $TRUE, $FALSE, $NULL; - private $value; + public $value; static function __static() { @@ -70,7 +70,13 @@ public function mappedBy($mapper): self { foreach ($this->value as $key => $element) { $m= $mapper($element, $key); if ($m instanceof Traversable) { - $self->value+= iterator_to_array($m); + foreach ($m as $key => $value) { + if (null === $key) { + $self->value[]= $value; + } else { + $self->value[$key]= $value; + } + } } else if (is_string($key)) { $self->value[$key]= $m; } else { diff --git a/src/test/php/test/unittest/AssertableTest.class.php b/src/test/php/test/unittest/AssertableTest.class.php index 58b82bd..0b24528 100755 --- a/src/test/php/test/unittest/AssertableTest.class.php +++ b/src/test/php/test/unittest/AssertableTest.class.php @@ -79,8 +79,22 @@ public function map_traversable_with_yield_from($mapper) { #[Test] public function map_can_transform_keys_via_yield() { Assert::equals( - new Assertable([1 => 'a', 2 => 'b']), - (new Assertable(['a' => 1, 'b' => 2]))->mappedBy(function($v, $k) { yield $v => $k; }) + new Assertable([1 => 'a', 2 => 'b', 0 => 'c']), + (new Assertable(['a' => 1, 'b' => 2, 'c' => 0]))->mappedBy(function($v, $k) { yield $v => $k; }) + ); + } + + #[Test] + public function yield_with_explicit_null_appends_to_result() { + $opcodes= function() { + yield 'const' => 1; + yield 'add' => 1; + yield 'multiply' => 2; + yield 'add' => -1; + }; + Assert::equals( + new Assertable([['const', 1], ['add', 1], ['multiply', 2], ['add', -1]]), + (new Assertable($opcodes()))->mappedBy(function($arg, $op) { yield null => [$op, $arg]; }) ); }