Skip to content

Commit b49a921

Browse files
committed
Util::normalizeIndex() tested.
1 parent 371745b commit b49a921

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

tests/unit/Structs/SliceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testIsSliceFalse($input)
3434
public function testSliceError($input)
3535
{
3636
$this->expectException(ValueError::class);
37-
$strInput = \is_scalar($input) ? "{$input}" : gettype($input);
37+
$strInput = \is_scalar($input) ? "{$input}" : \gettype($input);
3838
$this->expectExceptionMessage("Invalid slice: \"{$strInput}\"");
3939

4040
Slice::toSlice($input);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Tests\Unit\Utils;
6+
7+
use Smoren\ArrayView\Exceptions\IndexError;
8+
use Smoren\ArrayView\Util;
9+
10+
class NormalizeIndexTest extends \Codeception\Test\Unit
11+
{
12+
/**
13+
* @dataProvider dataProviderForNormalizeIndexSuccess
14+
*/
15+
public function testNormalizeIndexSuccess(array $source, int $index, int $expected)
16+
{
17+
$normalizedIndex = Util::normalizeIndex($index, \count($source));
18+
$this->assertSame($expected, $source[$normalizedIndex]);
19+
}
20+
21+
/**
22+
* @dataProvider dataProviderForNormalizeIndexNotThrow
23+
*/
24+
public function testNormalizeIndexNotThrow(array $source, int $index, int $expected)
25+
{
26+
$normalizedIndex = Util::normalizeIndex($index, \count($source), false);
27+
$this->assertSame($expected, $normalizedIndex);
28+
}
29+
30+
/**
31+
* @dataProvider dataProviderForNormalizeIndexError
32+
*/
33+
public function testNormalizeIndexError(array $source, int $index)
34+
{
35+
$this->expectException(IndexError::class);
36+
$this->expectExceptionMessage("Index {$index} is out of range.");
37+
Util::normalizeIndex($index, \count($source));
38+
}
39+
40+
public function dataProviderForNormalizeIndexSuccess(): array
41+
{
42+
return [
43+
[[1], 0, 1],
44+
[[1], -1, 1],
45+
[[1, 2], 0, 1],
46+
[[1, 2], -1, 2],
47+
[[1, 2], 1, 2],
48+
[[1, 2], -2, 1],
49+
[[1, 2, 3], 0, 1],
50+
[[1, 2, 3], -1, 3],
51+
[[1, 2, 3], 1, 2],
52+
[[1, 2, 3], -2, 2],
53+
[[1, 2, 3], 2, 3],
54+
[[1, 2, 3], -3, 1],
55+
];
56+
}
57+
58+
public function dataProviderForNormalizeIndexNotThrow(): array
59+
{
60+
return [
61+
[[1], 0, 0],
62+
[[1], -1, 0],
63+
[[1, 2], 0, 0],
64+
[[1, 2], -1, 1],
65+
[[1, 2], 1, 1],
66+
[[1, 2], -2, 0],
67+
[[1, 2, 3], 0, 0],
68+
[[1, 2, 3], -1, 2],
69+
[[1, 2, 3], 1, 1],
70+
[[1, 2, 3], -2, 1],
71+
[[1, 2, 3], 2, 2],
72+
[[1, 2, 3], -3, 0],
73+
74+
[[], 0, 0],
75+
[[], -1, -1],
76+
[[], 2, 2],
77+
[[], -2, -2],
78+
[[1], 1, 1],
79+
[[1], -2, -1],
80+
[[1, 2], 2, 2],
81+
[[1, 2], -3, -1],
82+
[[1, 2, 3], 3, 3],
83+
[[1, 2, 3], -4, -1],
84+
[[1, 2, 3], 4, 4],
85+
[[1, 2, 3], -5, -2],
86+
[[1, 2, 3], 100, 100],
87+
[[1, 2, 3], -101, -98],
88+
];
89+
}
90+
91+
public function dataProviderForNormalizeIndexError(): array
92+
{
93+
return [
94+
[[], 0],
95+
[[], -1],
96+
[[], 2],
97+
[[], -2],
98+
[[1], 1],
99+
[[1], -2],
100+
[[1, 2], 2],
101+
[[1, 2], -3],
102+
[[1, 2, 3], 3],
103+
[[1, 2, 3], -4],
104+
[[1, 2, 3], 4],
105+
[[1, 2, 3], -5],
106+
[[1, 2, 3], 100],
107+
[[1, 2, 3], -101],
108+
];
109+
}
110+
}

0 commit comments

Comments
 (0)