diff --git a/src/Collection/Iterator/SingletonIterator.php b/src/Collection/Iterator/SingletonIterator.php index 8ad88ba..eed6ff3 100644 --- a/src/Collection/Iterator/SingletonIterator.php +++ b/src/Collection/Iterator/SingletonIterator.php @@ -64,4 +64,9 @@ public function rewind(): void { $this->hasNext = true; } + + public function key(): mixed + { + return 0; + } } diff --git a/src/Control/Either.php b/src/Control/Either.php index e79a327..f320f04 100644 --- a/src/Control/Either.php +++ b/src/Control/Either.php @@ -17,10 +17,6 @@ */ abstract class Either extends Value { - private function __construct() - { - } - /** * @template L1 * diff --git a/tests/Collection/GenericListTest.php b/tests/Collection/GenericListTest.php index c5fcdc7..6c7fe67 100644 --- a/tests/Collection/GenericListTest.php +++ b/tests/Collection/GenericListTest.php @@ -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(); + } } diff --git a/tests/Collection/Iterator/SingletonIteratorTest.php b/tests/Collection/Iterator/SingletonIteratorTest.php new file mode 100644 index 0000000..b137cf4 --- /dev/null +++ b/tests/Collection/Iterator/SingletonIteratorTest.php @@ -0,0 +1,37 @@ +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()); + } +} diff --git a/tests/Collection/IteratorTest.php b/tests/Collection/IteratorTest.php index 7cb244b..1db2918 100644 --- a/tests/Collection/IteratorTest.php +++ b/tests/Collection/IteratorTest.php @@ -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; @@ -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()); + } } diff --git a/tests/Collection/StreamTest.php b/tests/Collection/StreamTest.php index 4b78391..885a640 100644 --- a/tests/Collection/StreamTest.php +++ b/tests/Collection/StreamTest.php @@ -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 @@ -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()); @@ -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(); + } } diff --git a/tests/Control/EitherTest.php b/tests/Control/EitherTest.php index ee1cd87..827a920 100644 --- a/tests/Control/EitherTest.php +++ b/tests/Control/EitherTest.php @@ -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()); + } } diff --git a/tests/Control/OptionTest.php b/tests/Control/OptionTest.php index 4d5c1fe..3cd6e9a 100644 --- a/tests/Control/OptionTest.php +++ b/tests/Control/OptionTest.php @@ -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(); + } } diff --git a/tests/Control/TryToTest.php b/tests/Control/TryToTest.php index c667d7b..b6ae4d9 100644 --- a/tests/Control/TryToTest.php +++ b/tests/Control/TryToTest.php @@ -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(); + } } diff --git a/tests/LazyTest.php b/tests/LazyTest.php index 51fb5fa..937a372 100644 --- a/tests/LazyTest.php +++ b/tests/LazyTest.php @@ -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); }); @@ -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()); + } }