|
2 | 2 |
|
3 | 3 | namespace Smoren\ArrayView\Tests\Unit\ArrayView; |
4 | 4 |
|
5 | | -use Smoren\ArrayView\Exceptions\ValueError; |
6 | 5 | use Smoren\ArrayView\Selectors\IndexListSelector; |
7 | 6 | use Smoren\ArrayView\Selectors\MaskSelector; |
8 | 7 | use Smoren\ArrayView\Selectors\SliceSelector; |
9 | 8 | use Smoren\ArrayView\Structs\Slice; |
10 | | -use Smoren\ArrayView\Views\ArrayIndexListView; |
11 | | -use Smoren\ArrayView\Views\ArrayMaskView; |
12 | | -use Smoren\ArrayView\Views\ArraySliceView; |
13 | 9 | use Smoren\ArrayView\Views\ArrayView; |
14 | 10 |
|
15 | 11 | class WriteTest extends \Codeception\Test\Unit |
@@ -132,6 +128,21 @@ public function testWriteBySlice(array $source, callable $viewGetter, $toWrite, |
132 | 128 | $this->assertSame($expected, $source); |
133 | 129 | } |
134 | 130 |
|
| 131 | + /** |
| 132 | + * @dataProvider dataProviderForApply |
| 133 | + */ |
| 134 | + public function testApply(array $source, callable $viewGetter, callable $mapper, array $expected) |
| 135 | + { |
| 136 | + // Given |
| 137 | + $view = $viewGetter($source); |
| 138 | + |
| 139 | + // When |
| 140 | + $view->apply($mapper); |
| 141 | + |
| 142 | + // Then |
| 143 | + $this->assertSame($expected, $source); |
| 144 | + } |
| 145 | + |
135 | 146 | public function dataProviderForArrayWrite(): array |
136 | 147 | { |
137 | 148 | return [ |
@@ -257,4 +268,62 @@ public function dataProviderForWriteCombine(): array |
257 | 268 | ], |
258 | 269 | ]; |
259 | 270 | } |
| 271 | + |
| 272 | + public function dataProviderForApply(): array |
| 273 | + { |
| 274 | + return [ |
| 275 | + [ |
| 276 | + [], |
| 277 | + fn (array &$source) => ArrayView::toView($source), |
| 278 | + fn (int $item) => $item + 1, |
| 279 | + [], |
| 280 | + ], |
| 281 | + [ |
| 282 | + [1], |
| 283 | + fn (array &$source) => ArrayView::toView($source), |
| 284 | + fn (int $item) => $item + 1, |
| 285 | + [2], |
| 286 | + ], |
| 287 | + [ |
| 288 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 289 | + fn (array &$source) => ArrayView::toView($source), |
| 290 | + fn (int $item) => $item, |
| 291 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 292 | + ], |
| 293 | + [ |
| 294 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 295 | + fn (array &$source) => ArrayView::toView($source), |
| 296 | + fn (int $item) => $item + 1, |
| 297 | + [2, 3, 4, 5, 6, 7, 8, 9, 10, 11], |
| 298 | + ], |
| 299 | + [ |
| 300 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 301 | + fn (array &$source) => ArrayView::toView($source), |
| 302 | + fn (int $item, int $index) => $item + $index, |
| 303 | + [1, 3, 5, 7, 9, 11, 13, 15, 17, 19], |
| 304 | + ], |
| 305 | + [ |
| 306 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 307 | + fn (array &$source) => ArrayView::toView($source) |
| 308 | + ->subview('::2'), |
| 309 | + fn (int $item, int $index) => $item + $index, |
| 310 | + [1, 2, 4, 4, 7, 6, 10, 8, 13, 10], |
| 311 | + ], |
| 312 | + [ |
| 313 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 314 | + fn (array &$source) => ArrayView::toView($source) |
| 315 | + ->subview('1::2'), |
| 316 | + fn (int $item) => $item * 2, |
| 317 | + [1, 4, 3, 8, 5, 12, 7, 16, 9, 20], |
| 318 | + ], |
| 319 | + [ |
| 320 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 321 | + fn (array &$source) => ArrayView::toView($source) |
| 322 | + ->subview('1::2') |
| 323 | + ->subview(new IndexListSelector([0, 1, 2])), |
| 324 | + fn (int $item) => $item * 2, |
| 325 | + [1, 4, 3, 8, 5, 12, 7, 8, 9, 10], |
| 326 | + ], |
| 327 | + ]; |
| 328 | + } |
260 | 329 | } |
0 commit comments