From 63336102f787f6f6579651c647b732adc5913ab5 Mon Sep 17 00:00:00 2001 From: Smoren Date: Sun, 10 Mar 2024 22:50:24 +0300 Subject: [PATCH] new tests added, error test renamed. --- ...SequentialErrorTest.php => ErrorsTest.php} | 2 +- tests/unit/ArrayView/WriteTest.php | 77 ++++++++++++++++++- 2 files changed, 74 insertions(+), 5 deletions(-) rename tests/unit/ArrayView/{NonSequentialErrorTest.php => ErrorsTest.php} (96%) diff --git a/tests/unit/ArrayView/NonSequentialErrorTest.php b/tests/unit/ArrayView/ErrorsTest.php similarity index 96% rename from tests/unit/ArrayView/NonSequentialErrorTest.php rename to tests/unit/ArrayView/ErrorsTest.php index 2128776..8e748f7 100644 --- a/tests/unit/ArrayView/NonSequentialErrorTest.php +++ b/tests/unit/ArrayView/ErrorsTest.php @@ -5,7 +5,7 @@ use Smoren\ArrayView\Exceptions\ValueError; use Smoren\ArrayView\Views\ArrayView; -class NonSequentialErrorTest extends \Codeception\Test\Unit +class ErrorsTest extends \Codeception\Test\Unit { /** * @dataProvider dataProviderForNonSequentialError diff --git a/tests/unit/ArrayView/WriteTest.php b/tests/unit/ArrayView/WriteTest.php index 350dd57..59592ea 100644 --- a/tests/unit/ArrayView/WriteTest.php +++ b/tests/unit/ArrayView/WriteTest.php @@ -2,14 +2,10 @@ namespace Smoren\ArrayView\Tests\Unit\ArrayView; -use Smoren\ArrayView\Exceptions\ValueError; use Smoren\ArrayView\Selectors\IndexListSelector; use Smoren\ArrayView\Selectors\MaskSelector; use Smoren\ArrayView\Selectors\SliceSelector; use Smoren\ArrayView\Structs\Slice; -use Smoren\ArrayView\Views\ArrayIndexListView; -use Smoren\ArrayView\Views\ArrayMaskView; -use Smoren\ArrayView\Views\ArraySliceView; use Smoren\ArrayView\Views\ArrayView; class WriteTest extends \Codeception\Test\Unit @@ -132,6 +128,21 @@ public function testWriteBySlice(array $source, callable $viewGetter, $toWrite, $this->assertSame($expected, $source); } + /** + * @dataProvider dataProviderForApply + */ + public function testApply(array $source, callable $viewGetter, callable $mapper, array $expected) + { + // Given + $view = $viewGetter($source); + + // When + $view->apply($mapper); + + // Then + $this->assertSame($expected, $source); + } + public function dataProviderForArrayWrite(): array { return [ @@ -257,4 +268,62 @@ public function dataProviderForWriteCombine(): array ], ]; } + + public function dataProviderForApply(): array + { + return [ + [ + [], + fn (array &$source) => ArrayView::toView($source), + fn (int $item) => $item + 1, + [], + ], + [ + [1], + fn (array &$source) => ArrayView::toView($source), + fn (int $item) => $item + 1, + [2], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source), + fn (int $item) => $item, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source), + fn (int $item) => $item + 1, + [2, 3, 4, 5, 6, 7, 8, 9, 10, 11], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source), + fn (int $item, int $index) => $item + $index, + [1, 3, 5, 7, 9, 11, 13, 15, 17, 19], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source) + ->subview('::2'), + fn (int $item, int $index) => $item + $index, + [1, 2, 4, 4, 7, 6, 10, 8, 13, 10], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source) + ->subview('1::2'), + fn (int $item) => $item * 2, + [1, 4, 3, 8, 5, 12, 7, 16, 9, 20], + ], + [ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + fn (array &$source) => ArrayView::toView($source) + ->subview('1::2') + ->subview(new IndexListSelector([0, 1, 2])), + fn (int $item) => $item * 2, + [1, 4, 3, 8, 5, 12, 7, 8, 9, 10], + ], + ]; + } }