Skip to content

Commit

Permalink
new tests added, error test renamed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 10, 2024
1 parent 5c71355 commit 6333610
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 73 additions & 4 deletions tests/unit/ArrayView/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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],
],
];
}
}

0 comments on commit 6333610

Please sign in to comment.