Skip to content

Commit

Permalink
A lot of tests, some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 12, 2024
1 parent 7877f23 commit 2ff6647
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ private function numericOffsetExists($offset): bool
return false;
}

if (\is_numeric($offset) && !\is_integer($offset + 0)) {
return false;
}

try {
$index = $this->convertIndex(intval($offset));
} catch (IndexError $e) {
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/ArrayIndexListView/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class ReadTest extends \Codeception\Test\Unit
public function testReadByMethod(array $source, array $indexes, array $expected)
{
$view = ArrayView::toView($source);
$subview = $view->subview(new IndexListSelector($indexes));

$selector = new IndexListSelector($indexes);
$this->assertSame($indexes, $selector->getValue());

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

$this->assertSame($expected, [...$subview]);
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/ArrayMaskView/IssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayMaskView;

use Smoren\ArrayView\Exceptions\SizeError;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Views\ArrayView;

class IssetTest extends \Codeception\Test\Unit
{

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

$this->assertTrue(isset($view[new MaskSelector($boolMask)]));
}

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

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

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

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

public function dataProviderForIssetSelectorFalse(): array
{
return [
[[], [0], []],
[[], [1], []],
[[], [0, 1], []],
[[1], [], []],
[[1], [0, 0], []],
[[1], [1, 0], []],
[[1], [1, 1, 1], []],
[[1, 2, 3], [], []],
[[1, 2, 3], [0], []],
[[1, 2, 3], [0, 0], []],
[[1, 2, 3], [0, 0, 0, 0], []],
[[1, 2, 3], [0, 0, 0, 0, 0], []],
[[1, 2, 3], [1], []],
[[1, 2, 3], [1, 1], []],
[[1, 2, 3], [1, 1, 1, 1], []],
[[1, 2, 3], [1, 1, 1, 1, 1], []],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 0, 1, 0, 1, 0, 1], [2, 4, 6, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 1, 0, 0, 0, 0, 0, 1, 0], [1, 2, 3, 9]],
];
}
}
5 changes: 4 additions & 1 deletion tests/unit/ArrayMaskView/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class ReadTest extends \Codeception\Test\Unit
public function testReadByMethod(array $source, array $mask, array $expected)
{
$view = ArrayView::toView($source);
$subview = $view->subview(new MaskSelector($mask));

$selector = new MaskSelector($mask);
$this->assertSame($mask, $selector->getValue());

$subview = $view->subview($selector);
$this->assertInstanceOf(ArrayMaskView::class, $subview);

$this->assertSame($expected, [...$subview]);
Expand Down
94 changes: 94 additions & 0 deletions tests/unit/ArraySliceView/IssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArraySliceView;

use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

class IssetTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForIssetSelectorStringTrue
* @dataProvider dataProviderForIssetSelectorArrayTrue
*/
public function testIssetSelectorObjectTrue(array $source, $slice)
{
$view = ArrayView::toView($source);
$this->assertTrue(isset($view[new SliceSelector($slice)]));
}

/**
* @dataProvider dataProviderForIssetSelectorStringTrue
*/
public function testIssetSelectorStringTrue(array $source, $slice)
{
$view = ArrayView::toView($source);
$this->assertTrue(isset($view[$slice]));
}

public function dataProviderForIssetSelectorStringTrue(): array
{
return [
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8:2'],

[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-1::-1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-1:0:-1'],

[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:9:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:9:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:9:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:10:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:10:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-9:9:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-9:9:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-10:10:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-10:10:2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-5:10:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-5:100:2'],

[[], '0:'],
[[], '0:0'],
[[], '0:0:1'],
[[], '1:-1'],
[[], '-1:-1'],
[[], '-2:-1'],
[[], '-2:-1:2'],
[[], '-1:0:-1'],
[[1], '0:'],
[[1], '0:1'],
[[1], '0:1:1'],
[[1], '0:1:2'],
[[1], '0:-1'],
[[1], '0:-1:1'],
[[1], '0:-1:2'],
[[1], '0:10:100'],
[[1], '1:10:100'],
[[1], '0:'],
[[1, 2, 3], '0:0:1'],
[[1], '1:'],
[[1, 2], '1:0'],
[[1, 2], '1::-1'],
[[1, 2], '0:1'],
[[1, 2], '1:1'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1::2'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '::2'],
];
}

public function dataProviderForIssetSelectorArrayTrue(): array
{
return [
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6,1]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6,2]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8,1]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8,2]],
];
}
}
4 changes: 3 additions & 1 deletion tests/unit/ArraySliceView/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public function testReadByIndex(array $source, string $config, array $expected)
public function testReadByMethodAndIndex(array $source, $config, array $expected)
{
$view = ArrayView::toView($source);
$slicedArray = $view->subview(new SliceSelector($config))[':'];
$selector = new SliceSelector($config);
$this->assertSame($selector, $selector->getValue());

$slicedArray = $view->subview($selector)[':'];
$this->assertSame($expected, $slicedArray);
$this->assertSame(\count($expected), \count($slicedArray));

Expand Down
52 changes: 52 additions & 0 deletions tests/unit/ArrayView/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Smoren\ArrayView\Exceptions\IndexError;
use Smoren\ArrayView\Exceptions\KeyError;
use Smoren\ArrayView\Exceptions\NotSupportedError;
use Smoren\ArrayView\Exceptions\SizeError;
use Smoren\ArrayView\Exceptions\ValueError;
use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;
Expand Down Expand Up @@ -186,6 +188,18 @@ public function testWriteSizeError(array $source, callable $viewGetter, array $t
$view[':'] = $toWrite;
}

/**
* @dataProvider dataProviderForUnsetError
*/
public function testUnsetError(array $source, $index)
{
$view = ArrayView::toView($source);

$this->expectException(NotSupportedError::class);

unset($view[$index]);
}

/**
* @dataProvider dataProviderForNonSequentialError
*/
Expand Down Expand Up @@ -327,6 +341,44 @@ public function dataProviderForWriteSizeError(): array
];
}

public function dataProviderForUnsetError(): array
{
return [
[[], 0],
[[], 1],
[[], -1],
[[], null],
[[], true],
[[], false],
[[], []],
[[], [1, 2, 3]],
[[], ['test' => 123]],
[[], new \ArrayObject([1, 2, 3])],
[[], INF],
[[], -INF],
[[], NAN],
[[], new SliceSelector('0:2')],
[[], new MaskSelector([0, 1])],
[[], new IndexListSelector([0, 1])],
[[1, 2, 3], 0],
[[1, 2, 3], 1],
[[1, 2, 3], -1],
[[1, 2, 3], null],
[[1, 2, 3], true],
[[1, 2, 3], false],
[[1, 2, 3], []],
[[1, 2, 3], [1, 2, 3]],
[[1, 2, 3], ['test' => 123]],
[[1, 2, 3], new \ArrayObject([1, 2, 3])],
[[1, 2, 3], INF],
[[1, 2, 3], -INF],
[[1, 2, 3], NAN],
[[1, 2, 3], new SliceSelector('0:2')],
[[1, 2, 3], new MaskSelector([0, 1])],
[[1, 2, 3], new IndexListSelector([0, 1])],
];
}

public function dataProviderForNonSequentialError(): array
{
return [
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/ArrayView/IssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayView;

use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

class IssetTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForIssetSelectorFalse
*/
public function testIssetSelectorFalse(array $source, $slice)
{
$view = ArrayView::toView($source);
$this->assertFalse(isset($view[$slice]));
}

public function dataProviderForIssetSelectorFalse(): array
{
return [
[[1, 2, 3, 4, 5, 6, 7, 8, 9], null],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], true],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], false],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 1.1],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], INF],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], -INF],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 'asd'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], ['a' => 1]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], new \ArrayObject(['a' => 1])],
];
}
}
55 changes: 55 additions & 0 deletions tests/unit/Structs/NormalizedSliceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Tests\Unit\Structs;

use Smoren\ArrayView\Structs\Slice;

class NormalizedSliceTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForToSlice
*/
public function testToSlice($input, $containerSize, array $expected)
{
$actual = Slice::toSlice($input)->normalize($containerSize);
$expectedSlice = new Slice(...$expected);

$this->assertSame($expectedSlice->getStart(), $actual->getStart());
$this->assertSame($expectedSlice->getEnd(), $actual->getEnd());
$this->assertSame($expectedSlice->getStep(), $actual->getStep());
}

public function dataProviderForToSlice(): array
{
return [
[':', 0, [0, 0, 1]],
['::', 0, [0, 0, 1]],
['::', 1, [0, 1, 1]],
['::', 2, [0, 2, 1]],
['0:', 0, [0, 0, 1]],
['0:', 1, [0, 1, 1]],
['1:', 0, [0, 0, 1]],
['-1:', 0, [0, 0, 1]],
['-1:', 1, [0, 1, 1]],
['0:0:', 0, [0, 0, 1]],
['0:0:', 10, [0, 0, 1]],
['1:1:', 0, [0, 0, 1]],
['1:1:', 10, [1, 1, 1]],
['-1:-1:', 0, [0, 0, 1]],
['-1:-1:', 10, [9, 9, 1]],
['1:1:-1', 0, [0, 0, -1]],
['1:1:-1', 1, [0, 0, -1]],
['1:1:-1', 2, [1, 1, -1]],
['-1:-1:-1', 0, [0, 0, -1]],
['-1:-1:-1', 1, [0, 0, -1]],
['-1:-1:-1', 2, [1, 1, -1]],
['1:2:3', 0, [0, 0, 3]],
['1:2:3', 1, [0, 0, 3]],
['1:2:3', 2, [1, 2, 3]],
['1:2:3', 3, [1, 2, 3]],
['1:2:3', 10, [1, 2, 3]],
];
}
}

0 comments on commit 2ff6647

Please sign in to comment.