Skip to content

Commit

Permalink
fix singleton iterator key method
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed May 19, 2024
1 parent 6590c51 commit 687b405
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Collection/Iterator/SingletonIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public function rewind(): void
{
$this->hasNext = true;
}

public function key(): mixed
{
return 0;
}
}
4 changes: 0 additions & 4 deletions src/Control/Either.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
*/
abstract class Either extends Value
{
private function __construct()
{
}

/**
* @template L1
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,18 @@ public function testIndexOfOnEmptyList(): void
{
self::assertSame(-1, GenericList::empty()->indexOf('a'));
}

public function testHeadOfEmptyList(): void
{
$this->expectException(\RuntimeException::class);

GenericList::empty()->head();
}

public function testTailOfEmptyList(): void
{
$this->expectException(\RuntimeException::class);

GenericList::empty()->tail();
}
}
37 changes: 37 additions & 0 deletions tests/Collection/Iterator/SingletonIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Munus\Tests\Collection\Iterator;

use Munus\Collection\Iterator\SingletonIterator;
use Munus\Exception\NoSuchElementException;
use PHPUnit\Framework\TestCase;

final class SingletonIteratorTest extends TestCase
{
public function testCurrentIfThereIsNoNext(): void
{
$iterator = new SingletonIterator('a');
$iterator->next();

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

$iterator->current();
}

public function testRewind(): void
{
$iterator = new SingletonIterator('a');
self::assertSame('a', $iterator->next());
self::assertFalse($iterator->hasNext());

$iterator->rewind();

self::assertTrue($iterator->hasNext());
self::assertTrue($iterator->valid());
self::assertSame(0, $iterator->key());
self::assertSame('a', $iterator->next());
self::assertFalse($iterator->hasNext());
}
}
19 changes: 19 additions & 0 deletions tests/Collection/IteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Munus\Collection\Iterator;
use Munus\Collection\Iterator\CompositeIterator;
use Munus\Collection\Map;
use Munus\Collection\Set;
use Munus\Exception\NoSuchElementException;
use Munus\Tuple;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -80,4 +81,22 @@ public function testReduce(): void

self::assertEquals(6, $iterator->reduce(fn (int $a, int $b) => $a + $b));
}

public function testReduceOnEmpty(): void
{
$this->expectException(NoSuchElementException::class);

Iterator::empty()->reduce(fn (int $a, int $b) => $a + $b);
}

public function testIteratorKey(): void
{
$iterator = new Iterator(Set::of(1, 2, 3));

self::assertSame(0, $iterator->key());
$iterator->next();
self::assertSame(1, $iterator->key());
$iterator->next();
self::assertSame(2, $iterator->key());
}
}
28 changes: 25 additions & 3 deletions tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ public function testStreamFilterNot(): void

public function testStreamLength(): void
{
self::assertEquals(5, Stream::from(0)->take(5)->length());
self::assertEquals(5, Stream::range(6, 10)->length());
self::assertEquals(5, Stream::ofAll(str_split('Munus'))->length());
self::assertSame(5, Stream::from(0)->take(5)->length());
self::assertSame(5, Stream::range(6, 10)->length());
self::assertSame(5, Stream::ofAll(str_split('Munus'))->length());
self::assertSame(0, Stream::empty()->length());
}

public function testStreamContains(): void
Expand Down Expand Up @@ -263,6 +264,13 @@ public function testStreamPrependAll(): void
self::assertTrue(Stream::of('a', 'b', 'c', 'd', 'e')->equals(Stream::of('e')->prependAll(Stream::of('a', 'b', 'c', 'd'))));
}

public function testStreamPrependAllEmptyStream(): void
{
$stream = Stream::of(1, 2, 3);

self::assertSame($stream, $stream->prependAll(Stream::empty()));
}

public function testStreamToArray(): void
{
self::assertEquals([1, 2, 3, 4, 5], Stream::range(1, 5)->toArray());
Expand Down Expand Up @@ -306,4 +314,18 @@ public function testIndexOfOnEmptyStream(): void
{
self::assertSame(-1, Stream::empty()->indexOf('a'));
}

public function testHeadOfEmptyStream(): void
{
$this->expectException(\RuntimeException::class);

Stream::empty()->head();
}

public function testTailOfEmptyStream(): void
{
$this->expectException(\RuntimeException::class);

Stream::empty()->tail();
}
}
16 changes: 16 additions & 0 deletions tests/Control/EitherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,20 @@ public function testToArray(): void
self::assertEquals(['a'], Either::right('a')->toArray());
self::assertEquals([], Either::left('a')->toArray());
}

public function testEitherIteratorLeft(): void
{
$iterator = Either::left('left')->iterator();

self::assertFalse($iterator->hasNext());
}

public function testEitherIteratorRight(): void
{
$iterator = Either::right('right')->iterator();

self::assertTrue($iterator->hasNext());
self::assertSame('right', $iterator->next());
self::assertFalse($iterator->hasNext());
}
}
7 changes: 7 additions & 0 deletions tests/Control/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,11 @@ public function testOptionIfPresent(): void
Option::none()->ifPresent(fn ($v) => throw new \RuntimeException('impossible is nothing'));
Option::of('a')->ifPresent(fn ($v) => self::assertSame('a', $v));
}

public function testOptionOfNoneGet(): void
{
$this->expectException(\RuntimeException::class);

Option::none()->get();
}
}
30 changes: 30 additions & 0 deletions tests/Control/TryToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,34 @@ public function testSuccessPeek(): void
self::assertSame($try, $try->peek(function ($value) use (&$check) {$check = $value; }));
self::assertEquals('munus', $check);
}

public function testTryToIteratorSuccess(): void
{
$iterator = TryTo::run(function () {return 'munus'; })->iterator();

self::assertTrue($iterator->hasNext());
self::assertSame('munus', $iterator->next());
self::assertFalse($iterator->hasNext());
}

public function testTryToIteratorFailure(): void
{
$iterator = TryTo::run(fn () => throw new \RuntimeException())->iterator();

self::assertFalse($iterator->hasNext());
}

public function testTryToGetCauseOnSuccess(): void
{
$this->expectException(\BadMethodCallException::class);

TryTo::run(function () {return 'munus'; })->getCause();
}

public function testTryToGetOnFailure(): void
{
$this->expectException(\BadMethodCallException::class);

TryTo::run(fn () => throw new \RuntimeException())->get();
}
}
17 changes: 17 additions & 0 deletions tests/LazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function testEvaluation(): void
self::assertTrue($lazy->isEvaluated());
}

public function testInvoke(): void
{
$lazy = Lazy::of(function () {return 'Munus is awesome'; });

self::assertEquals('Munus is awesome', $lazy());
self::assertTrue($lazy->isEvaluated());
}

public function testEvaluationOnlyOnce(): void
{
$lazy = Lazy::of(function (): int {return random_int(1, 1000); });
Expand Down Expand Up @@ -81,4 +89,13 @@ public function testLazyPeek(): void
self::assertEquals('munus', $check);
self::assertTrue($lazy->isEvaluated());
}

public function testLazyIterator(): void
{
$iterator = Lazy::of(function () {return 'munus'; })->iterator();

self::assertTrue($iterator->hasNext());
self::assertSame('munus', $iterator->next());
self::assertFalse($iterator->hasNext());
}
}

0 comments on commit 687b405

Please sign in to comment.