|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace Tests\Pipeline; |
| 22 | + |
| 23 | +use ArrayIterator; |
| 24 | +use IteratorIterator; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Tests\Pipeline\Scenarios\PeekScenario; |
| 27 | + |
| 28 | +use function Pipeline\map; |
| 29 | +use function Pipeline\take; |
| 30 | +use function iterator_to_array; |
| 31 | +use function range; |
| 32 | + |
| 33 | +/** |
| 34 | + * @covers \Pipeline\Standard |
| 35 | + * |
| 36 | + * @internal |
| 37 | + */ |
| 38 | +final class PeekTest extends TestCase |
| 39 | +{ |
| 40 | + /** |
| 41 | + * @return iterable<PeekScenario> |
| 42 | + */ |
| 43 | + public static function providePeekData(): iterable |
| 44 | + { |
| 45 | + yield 'empty array' => new PeekScenario(); |
| 46 | + yield 'empty array, count 3' => new PeekScenario(count: 3, expected_remains: []); |
| 47 | + yield 'zero count' => new PeekScenario(count: 0, input: [1, 2, 3], expected_peeked: [], expected_remains: [1, 2, 3]); |
| 48 | + |
| 49 | + yield 'simple peek' => new PeekScenario(count: 3, input: [1, 2, 3, 4, 5], expected_peeked: [1, 2, 3], expected_remains: [3 => 4, 5]); |
| 50 | + |
| 51 | + yield 'preserve keys' => new PeekScenario(count: 2, input: ['a' => 1, 'b' => 2, 'c' => 3], expected_peeked: ['a' => 1, 'b' => 2], expected_remains: ['c' => 3]); |
| 52 | + |
| 53 | + yield 'peek more than available' => new PeekScenario(count: 10, input: [1, 2, 3], expected_peeked: [1, 2, 3], expected_remains: []); |
| 54 | + yield 'consume all' => new PeekScenario(count: 3, input: [1, 2, 3], expected_peeked: [1, 2, 3], expected_remains: []); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return iterable<PeekScenario> |
| 59 | + */ |
| 60 | + public static function providePeekIterables(): iterable |
| 61 | + { |
| 62 | + foreach (self::providePeekData() as $name => $item) { |
| 63 | + yield $name . ' (array)' => [$item]; |
| 64 | + yield $name . ' (ArrayIterator)' => [$item->withInput(new ArrayIterator($item->input))]; |
| 65 | + yield $name . ' (IteratorIterator)' => [$item->withInput(new IteratorIterator(new ArrayIterator($item->input)))]; |
| 66 | + yield $name . ' (stream)' => [$item->withInput(take($item->input)->stream())]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @dataProvider providePeekIterables |
| 72 | + */ |
| 73 | + public function testPeekWithProvider(PeekScenario $item): void |
| 74 | + { |
| 75 | + $pipeline = take($item->input); |
| 76 | + |
| 77 | + $peeked = $pipeline->peek($item->count); |
| 78 | + |
| 79 | + $this->assertSame( |
| 80 | + take($item->expected_peeked)->tuples()->toList(), |
| 81 | + take($peeked)->tuples()->toList(), |
| 82 | + ); |
| 83 | + |
| 84 | + if (null === $item->expected_remains) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $this->assertSame( |
| 89 | + take($item->expected_remains)->tuples()->toList(), |
| 90 | + take($pipeline)->tuples()->toList(), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function testPeekBasic(): void |
| 95 | + { |
| 96 | + $pipeline = take([1, 2, 3, 4, 5]); |
| 97 | + $peeked = iterator_to_array($pipeline->peek(3)); |
| 98 | + |
| 99 | + $this->assertSame([1, 2, 3], $peeked); |
| 100 | + $this->assertSame([4, 5], $pipeline->toList()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testPeekPreservesKeys(): void |
| 104 | + { |
| 105 | + $pipeline = take(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]); |
| 106 | + $peeked = iterator_to_array($pipeline->peek(2)); |
| 107 | + |
| 108 | + $this->assertSame(['a' => 1, 'b' => 2], $peeked); |
| 109 | + $this->assertSame(['c' => 3, 'd' => 4], $pipeline->toAssoc()); |
| 110 | + } |
| 111 | + |
| 112 | + public function testPeekWithDuplicateKeys(): void |
| 113 | + { |
| 114 | + $generator = map(static function () { |
| 115 | + yield 'a' => 1; |
| 116 | + yield 'a' => 2; |
| 117 | + yield 'b' => 3; |
| 118 | + }); |
| 119 | + |
| 120 | + $pipeline = take($generator); |
| 121 | + $peeked = $pipeline->peek(2); |
| 122 | + |
| 123 | + $this->assertSame( |
| 124 | + [['a', 1], ['a', 2]], |
| 125 | + take($peeked)->tuples()->toList(), |
| 126 | + ); |
| 127 | + |
| 128 | + $this->assertSame(['b' => 3], $pipeline->toAssoc()); |
| 129 | + } |
| 130 | + |
| 131 | + public function testPeekFromGenerator(): void |
| 132 | + { |
| 133 | + $pipeline = take(self::xrange(1, 5)); |
| 134 | + $peeked = iterator_to_array($pipeline->peek(3), false); |
| 135 | + |
| 136 | + $this->assertSame([1, 2, 3], $peeked); |
| 137 | + $this->assertSame([4, 5], $pipeline->toList()); |
| 138 | + } |
| 139 | + |
| 140 | + public function testPeekMoreThanAvailable(): void |
| 141 | + { |
| 142 | + $pipeline = take([1, 2, 3]); |
| 143 | + $peeked = iterator_to_array($pipeline->peek(10), false); |
| 144 | + |
| 145 | + $this->assertSame([1, 2, 3], $peeked); |
| 146 | + $this->assertSame([], $pipeline->toList()); |
| 147 | + } |
| 148 | + |
| 149 | + public function testPeekFromEmptyPipeline(): void |
| 150 | + { |
| 151 | + $pipeline = take([]); |
| 152 | + $peeked = iterator_to_array($pipeline->peek(5), false); |
| 153 | + |
| 154 | + $this->assertSame([], $peeked); |
| 155 | + $this->assertSame([], $pipeline->toList()); |
| 156 | + } |
| 157 | + |
| 158 | + public function testPeekWithZeroCount(): void |
| 159 | + { |
| 160 | + $pipeline = take([1, 2, 3]); |
| 161 | + $peeked = iterator_to_array($pipeline->peek(0), false); |
| 162 | + |
| 163 | + $this->assertSame([], $peeked); |
| 164 | + $this->assertSame([1, 2, 3], $pipeline->toList()); |
| 165 | + } |
| 166 | + |
| 167 | + public function testPeekWithNegativeCount(): void |
| 168 | + { |
| 169 | + $pipeline = take([1, 2, 3]); |
| 170 | + $peeked = iterator_to_array($pipeline->peek(-5), false); |
| 171 | + |
| 172 | + $this->assertSame([], $peeked); |
| 173 | + $this->assertSame([1, 2, 3], $pipeline->toList()); |
| 174 | + } |
| 175 | + |
| 176 | + public function testPeekNonDestructiveWithPrepend(): void |
| 177 | + { |
| 178 | + $pipeline = take([1, 2, 3, 4, 5]); |
| 179 | + $peeked = iterator_to_array($pipeline->peek(3), true); |
| 180 | + |
| 181 | + // Restore items manually with prepend() |
| 182 | + $pipeline->prepend($peeked); |
| 183 | + |
| 184 | + $this->assertSame([1, 2, 3], $peeked); |
| 185 | + $this->assertSame([1, 2, 3, 4, 5], $pipeline->toList()); |
| 186 | + } |
| 187 | + |
| 188 | + /** @dataProvider provideOneToFive */ |
| 189 | + public function testMultipleSequentialPeeks(iterable $input): void |
| 190 | + { |
| 191 | + $pipeline = take($input); |
| 192 | + |
| 193 | + $first = iterator_to_array($pipeline->peek(2), false); |
| 194 | + $this->assertSame([1, 2], $first); |
| 195 | + |
| 196 | + $second = iterator_to_array($pipeline->peek(2), false); |
| 197 | + $this->assertSame([3, 4], $second); |
| 198 | + |
| 199 | + $this->assertSame([5], $pipeline->toList()); |
| 200 | + } |
| 201 | + |
| 202 | + public static function provideOneToFive(): iterable |
| 203 | + { |
| 204 | + yield [range(1, 5)]; |
| 205 | + yield [self::xrange(1, 5)]; |
| 206 | + } |
| 207 | + |
| 208 | + /** @dataProvider provideOneToFive */ |
| 209 | + public function testMultipleSequentialPeeksOutOfOrder(iterable $input): void |
| 210 | + { |
| 211 | + $pipeline = take($input); |
| 212 | + |
| 213 | + $first = $pipeline->peek(2); |
| 214 | + $second = $pipeline->peek(2); |
| 215 | + |
| 216 | + $this->assertSame([5], $pipeline->toList()); |
| 217 | + |
| 218 | + $this->assertSame([3, 4], iterator_to_array($second, false)); |
| 219 | + $this->assertSame([1, 2], iterator_to_array($first, false)); |
| 220 | + } |
| 221 | + |
| 222 | + public function testPeekOneDefersCosts(): void |
| 223 | + { |
| 224 | + $pipeline = map(function () { |
| 225 | + yield 1; |
| 226 | + $this->fail('Should not be called'); |
| 227 | + }); |
| 228 | + |
| 229 | + $this->assertSame([1], iterator_to_array($pipeline->peek(1))); |
| 230 | + } |
| 231 | + |
| 232 | + private static function xrange(int $start, int $end): iterable |
| 233 | + { |
| 234 | + for ($i = $start; $i <= $end; $i++) { |
| 235 | + yield $i; |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments