Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests added. #20

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Interfaces/ArraySelectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ interface ArraySelectorInterface
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;

/**
* Checks if the selector is compatible with the given view.
*
* @template T View elements type.
*
* @param ArrayViewInterface<T> $view the view to check compatibility with.
*
* @return bool true if the element is compatible, false otherwise
*/
public function compatibleWith(ArrayViewInterface $view): bool;

/**
* Return value of the selector.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Selectors/IndexListSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
}

/**
* Checks if the selector is compatible with the given view.
*
* @template T View elements type.
*
* @param ArrayViewInterface<T> $view the view to check compatibility with.
*
* @return bool true if the element is compatible, false otherwise
*
* {@inheritDoc}
*/
public function compatibleWith(ArrayViewInterface $view): bool
{
return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view);
}

/**
* {@inheritDoc}
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Selectors/MaskSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
return new ArrayMaskView($source, $this->value, $readonly ?? $source->isReadonly());
}

/**
* Checks if the selector is compatible with the given view.
*
* @template T View elements type.
*
* @param ArrayViewInterface<T> $view the view to check compatibility with.
*
* @return bool true if the element is compatible, false otherwise
*
* {@inheritDoc}
*/
public function compatibleWith(ArrayViewInterface $view): bool
{
return \count($this->value) === \count($view);
}

/**
* {@inheritDoc}
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Selectors/SliceSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
}

/**
* Checks if the selector is compatible with the given view.
*
* @template T View elements type.
*
* @param ArrayViewInterface<T> $view the view to check compatibility with.
*
* @return bool true if the element is compatible, false otherwise
*
* {@inheritDoc}
*/
public function compatibleWith(ArrayViewInterface $view): bool
{
return true;
}

/**
* {@inheritDoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ArrayViewAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function offsetExists($offset): bool
}

if ($offset instanceof ArraySelectorInterface) {
return true;
return $offset->compatibleWith($this);
}

return false;
Expand Down
149 changes: 149 additions & 0 deletions tests/unit/ArrayIndexListView/IssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;

use Smoren\ArrayView\Exceptions\IndexError;
use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Views\ArrayView;

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

$existIndexes = [
...range(0, \count($indexes) - 1),
...range(-1, -\count($indexes)),
];

foreach ($existIndexes as $index) {
$this->assertTrue(isset($subview[$index]), $index);
}
}

/**
* @dataProvider dataProviderForIssetSingleFalse
*/
public function testIssetSingleFalse(array $source, array $indexes, array $expected)
{
$view = ArrayView::toView($source);
$subview = $view->subview(new IndexListSelector($indexes));

foreach ($expected as $index) {
$this->assertFalse(isset($subview[$index]), $index);
}
}

/**
* @dataProvider dataProviderForIssetSelectorTrue
*/
public function testIssetSelectorTrue(array $source, array $indexes)
{
$view = ArrayView::toView($source);

$this->assertTrue(isset($view[new IndexListSelector($indexes)]));

$subview = $view->subview(new IndexListSelector($indexes));
$this->assertSame(\count($indexes), \count($subview));

$subview = $view[new IndexListSelector($indexes)];
$this->assertSame(\count($indexes), \count($subview));
}

/**
* @dataProvider dataProviderForIssetSelectorFalse
*/
public function testIssetSelectorFalse(array $source, array $indexes)
{
$view = ArrayView::toView($source);

$this->assertFalse(isset($view[new IndexListSelector($indexes)]));

$this->expectException(IndexError::class);
$_ = $view[new IndexListSelector($indexes)];
}

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

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

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

public function dataProviderForIssetSelectorFalse(): array
{
return [
[[1], [0, 1]],
[[1], [1, -1, -2]],
[[1], [0, 1, 0, -1, -2]],
[[1], [1, -1]],
[[1], [0, 0, -2]],
[[1, 2], [2]],
[[1, 2], [1, 2]],
[[1, 2], [0, 1, 2]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, -10]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 5, 3, 1]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 10, 9, 7]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [-10, 1, 7, 10]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 50, 5, 3]],
];
}
}
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]],
];
}
}
Loading
Loading