Skip to content

Commit

Permalink
More example tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 20, 2024
1 parent 9bafaae commit f19bd1a
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Traits/ArrayViewAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function offsetExists($offset): bool
*
* $view['::2']; // [1, 3, 5]
* $view[[0, 2, 4]]; // [1, 3, 5]
* $view[[true, true, false, false, true]]; // [1, 3, 5]
* $view[[true, true, false, false, true]]; // [1, 2, 5]
* ```
*
* @param numeric|S $offset The offset to get the value at.
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/ArrayViewOperationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function map(callable $mapper): array
*
* $data = [9, 27, 45, 63, 81];
*
* $subview->mapWith($data, fn ($lhs, $rhs) => $lhs * $rhs); // [10, 30, 50, 70, 90]
* $subview->mapWith($data, fn ($lhs, $rhs) => $lhs + $rhs); // [10, 30, 50, 70, 90]
* ```
*
* @template U The type rhs of a binary operation.
Expand Down Expand Up @@ -257,7 +257,7 @@ public function apply(callable $mapper): self
*
* $data = [9, 27, 45, 63, 81];
*
* $subview->applyWith($data, fn ($lhs, $rhs) => $lhs * $rhs);
* $subview->applyWith($data, fn ($lhs, $rhs) => $lhs + $rhs);
* $subview->toArray(); // [10, 30, 50, 70, 90]
*
* $source; // [10, 2, 30, 4, 50, 6, 70, 8, 90, 10]
Expand Down
261 changes: 261 additions & 0 deletions tests/unit/Examples/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,265 @@ public function testSelectorsPipeNested()
$subview[':'] = [55, 77];
$this->assertSame([1, 2, 3, 4, 55, 6, 77, 8, 9, 10], $originalArray);
}

public function testMap()
{
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2');

$actual = $subview->map(fn ($x) => $x * 10);
$this->assertSame([10, 30, 50, 70, 90], $actual);
}

public function testMapWith()
{
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2');
$this->assertSame([1, 3, 5, 7, 9], $subview->toArray());

$data = [9, 27, 45, 63, 81];

$actual = $subview->mapWith($data, fn ($lhs, $rhs) => $lhs + $rhs);
$this->assertSame([10, 30, 50, 70, 90], $actual);
}

public function testIs()
{
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$mask = $view->is(fn ($x) => $x % 2 === 0);
$this->assertSame([false, true, false, true, false, true], $mask->getValue());

$this->assertSame([2, 4, 6], $view->subview($mask)->toArray());
$this->assertSame([2, 4, 6], $view[$mask]);

$view[$mask] = [20, 40, 60];
$this->assertSame([1, 20, 3, 40, 5, 60], $source);
}

public function testMatch()
{
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$mask = $view->match(fn ($x) => $x % 2 === 0);
$this->assertSame([false, true, false, true, false, true], $mask->getValue());

$this->assertSame([2, 4, 6], $view->subview($mask)->toArray());
$this->assertSame([2, 4, 6], $view[$mask]);

$view[$mask] = [20, 40, 60];
$this->assertSame([1, 20, 3, 40, 5, 60], $source);
}

public function testMatchWith()
{
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$data = [6, 5, 4, 3, 2, 1];

$mask = $view->matchWith($data, fn ($lhs, $rhs) => $lhs > $rhs);
$this->assertSame([false, false, false, true, true, true], $mask->getValue());

$this->assertSame([4, 5, 6], $view->subview($mask)->toArray());
$this->assertSame([4, 5, 6], $view[$mask]);

$view[$mask] = [40, 50, 60];
$this->assertSame([1, 2, 3, 40, 50, 60], $source);
}

public function testApply()
{
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2');

$this->assertSame([1, 3, 5, 7, 9], $subview->toArray());

$subview->apply(fn ($x) => $x * 10);

$this->assertSame([10, 30, 50, 70, 90], $subview->toArray());
$this->assertSame([10, 2, 30, 4, 50, 6, 70, 8, 90, 10], $source);
}

public function testApplyWith()
{
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2');

$this->assertSame([1, 3, 5, 7, 9], $subview->toArray());

$data = [9, 27, 45, 63, 81];

$subview->applyWith($data, fn ($lhs, $rhs) => $lhs + $rhs);
$this->assertSame([10, 30, 50, 70, 90], $subview->toArray());

$this->assertSame([10, 2, 30, 4, 50, 6, 70, 8, 90, 10], $source);
}

public function testFilter()
{
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$filtered = $view->filter(fn ($x) => $x % 2 === 0);
$this->assertSame([2, 4, 6], $filtered->toArray());

$filtered[':'] = [20, 40, 60];
$this->assertSame([20, 40, 60], $filtered->toArray());

$this->assertSame([1, 20, 3, 40, 5, 60], $source);
}

public function testCount()
{
$source = [1, 2, 3, 4, 5];

$subview = ArrayView::toView($source)->subview('::2');

$this->assertSame([1, 3, 5], $subview->toArray());
$this->assertCount(3, $subview);
}

public function testIterator()
{
$source = [1, 2, 3, 4, 5];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5]

$actual = [];
foreach ($subview as $item) {
$actual[] = $item;
// 1, 3, 5
}
$this->assertSame([1, 3, 5], $actual);
$this->assertSame([1, 3, 5], [...$subview]);
}

public function testIsReadonly()
{
$source = [1, 2, 3, 4, 5];

$readonlyView = ArrayView::toView($source, true);
$this->assertTrue($readonlyView->isReadonly());

$readonlySubview = ArrayView::toView($source)->subview('::2', true);
$this->assertTrue($readonlySubview->isReadonly());

$view = ArrayView::toView($source);
$this->assertFalse($view->isReadonly());

$subview = ArrayView::toView($source)->subview('::2');
$this->assertFalse($subview->isReadonly());
}

public function testOffsetExists()
{
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$this->assertTrue(isset($view[0]));
$this->assertTrue(isset($view[-1]));
$this->assertFalse(isset($view[10]));

$this->assertTrue(isset($view[new SliceSelector('::2')]));
$this->assertTrue(isset($view[new IndexListSelector([0, 2, 4])]));
$this->assertFalse(isset($view[new IndexListSelector([0, 2, 10])]));
$this->assertTrue(isset($view[new MaskSelector([true, true, false, false, true])]));
$this->assertFalse(isset($view[new MaskSelector([true, true, false, false, true, true])]));

$this->assertTrue(isset($view['::2']));
$this->assertTrue(isset($view[[0, 2, 4]]));
$this->assertFalse(isset($view[[0, 2, 10]]));
$this->assertTrue(isset($view[[true, true, false, false, true]]));
$this->assertFalse(isset($view[[true, true, false, false, true, true]]));
}

public function testOffsetGet()
{
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$this->assertSame(1, $view[0]);
$this->assertSame(5, $view[-1]);

$this->assertSame([1, 3, 5], $view[new SliceSelector('::2')]);
$this->assertSame([1, 3, 5], $view[new IndexListSelector([0, 2, 4])]);
$this->assertSame([1, 2, 5], $view[new MaskSelector([true, true, false, false, true])]);

$this->assertSame([1, 3, 5], $view['::2']);
$this->assertSame([1, 3, 5], $view[[0, 2, 4]]);
$this->assertSame([1, 2, 5], $view[[true, true, false, false, true]]);
}

public function testOffsetSet()
{
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[0] = 11;
$view[-1] = 55;

$this->assertSame([11, 2, 3, 4, 55], $source);

$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[new SliceSelector('::2')] = [11, 33, 55];
$this->assertSame([11, 2, 33, 4, 55], $source);

$view[new IndexListSelector([1, 3])] = [22, 44];
$this->assertSame([11, 22, 33, 44, 55], $source);

$view[new MaskSelector([true, false, false, false, true])] = [111, 555];
$this->assertSame([111, 22, 33, 44, 555], $source);

$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view['::2'] = [11, 33, 55];
$this->assertSame([11, 2, 33, 4, 55], $source);

$view[[1, 3]] = [22, 44];
$this->assertSame([11, 22, 33, 44, 55], $source);

$view[[true, false, false, false, true]] = [111, 555];
$this->assertSame([111, 22, 33, 44, 555], $source);
}

public function testSet()
{
$source = [1, 2, 3, 4, 5];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5]

$subview->set([11, 33, 55]);
$this->assertSame([11, 33, 55], $subview->toArray());
$this->assertSame([11, 2, 33, 4, 55], $source);
}

public function testToUnlinkedView()
{
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toUnlinkedView($source);

$this->assertSame(1, $view[0]);
$this->assertSame([2, 4], $view['1::2']);
$view['1::2'] = [22, 44];

$this->assertSame([1, 22, 3, 44, 5], $view->toArray());
$this->assertSame([1, 2, 3, 4, 5], $source);
}

public function testToView()
{
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$this->assertSame(1, $view[0]);
$this->assertSame([2, 4], $view['1::2']);
$view['1::2'] = [22, 44];

$this->assertSame([1, 22, 3, 44, 5], $view->toArray());
$this->assertSame([1, 22, 3, 44, 5], $source);
}
}

0 comments on commit f19bd1a

Please sign in to comment.