Skip to content

Commit

Permalink
IndexListView tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 12, 2024
1 parent 4b66272 commit bcd9132
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/unit/ArrayIndexListView/ReadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Views\ArrayIndexListView;
use Smoren\ArrayView\Views\ArrayView;

class ReadTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForRead
*/
public function testReadByMethod(array $source, array $indexes, array $expected)
{
$view = ArrayView::toView($source);
$subview = $view->subview(new IndexListSelector($indexes));

$this->assertInstanceOf(ArrayIndexListView::class, $subview);

$this->assertSame($expected, [...$subview]);
$this->assertSame(\count($expected), \count($subview));

for ($i = 0; $i < \count($subview); ++$i) {
$this->assertSame($expected[$i], $subview[$i]);
}

for ($i = 0; $i < \count($view); ++$i) {
$this->assertSame($source[$i], $view[$i]);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($expected, $subview->toArray());

$this->assertSame($source, [...$view]);
$this->assertSame($expected, [...$subview]);
}

/**
* @dataProvider dataProviderForRead
*/
public function testReadByIndex(array $source, array $mask, array $expected)
{
$view = ArrayView::toView($source);
$subArray = $view[new IndexListSelector($mask)];

$this->assertSame($expected, $subArray);
$this->assertSame(\count($expected), \count($subArray));

for ($i = 0; $i < \count($subArray); ++$i) {
$this->assertSame($expected[$i], $subArray[$i]);
}

for ($i = 0; $i < \count($view); ++$i) {
$this->assertSame($source[$i], $view[$i]);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($source, [...$view]);
$this->assertSame($expected, $subArray);
}

public function dataProviderForRead(): array
{
return [
[[], [], []],
[[1], [], []],
[[1, 2, 3], [], []],
[[1], [0], [1]],
[[1], [0, 0], [1, 1]],
[[1], [0, 0, 0], [1, 1, 1]],
[[1, 2], [0], [1]],
[[1, 2], [1], [2]],
[[1, 2], [0, 1], [1, 2]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, 7], [2, 4, 6, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [7, 5, 3, 1], [8, 6, 4, 2]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 3, 7], [2, 6, 4, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 7, 8], [1, 2, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 5, 5, 3], [2, 2, 6, 6, 4]],
];
}
}
126 changes: 126 additions & 0 deletions tests/unit/ArrayIndexListView/WriteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Views\ArrayView;

class WriteTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForMaskSubviewWrite
*/
public function testWriteByIndex(array $source, array $config, array $toWrite, array $expected)
{
$view = ArrayView::toView($source);

$view[new IndexListSelector($config)] = $toWrite;

$this->assertSame($expected, [...$view]);
$this->assertSame($expected, $source);
}

/**
* @dataProvider dataProviderForMaskSubviewWrite
*/
public function testWriteBySubview(array $source, $config, array $toWrite, array $expected)
{
$view = ArrayView::toView($source);

$view->subview(new IndexListSelector($config))[':'] = $toWrite;

$this->assertSame($expected, [...$view]);
$this->assertSame($expected, $source);
}

public function dataProviderForMaskSubviewWrite(): array
{
return [
[
[],
[],
[],
[],
],
[
[1],
[],
[],
[1],
],
[
[1, 2, 3],
[],
[],
[1, 2, 3],
],
[
[1],
[0],
[2],
[2],
],
[
[1],
[0, 0],
[3, 3],
[3],
],
[
[1],
[0, 0, 0],
[4, 4, 4],
[4],
],
[
[1, 2],
[0],
[2],
[2, 2],
],
[
[1, 2],
[1],
[3],
[1, 3],
],
[
[1, 2],
[0, 1],
[2, 3],
[2, 3],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 3, 5, 7],
[3, 5, 7, 9],
[1, 3, 3, 5, 5, 7, 7, 9, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[7, 5, 3, 1],
[9, 7, 5, 3],
[1, 3, 3, 5, 5, 7, 7, 9, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 5, 3, 7],
[3, 7, 5, 9],
[1, 3, 3, 5, 5, 7, 7, 9, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 7, 8],
[2, 3, 9, 10],
[2, 3, 3, 4, 5, 6, 7, 9, 10],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 1, 5, 5, 3],
[4, 4, 8, 8, 5],
[1, 4, 3, 5, 5, 8, 7, 8, 9],
],
];
}
}

0 comments on commit bcd9132

Please sign in to comment.