Commit 4495c462 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Added some testdox.

parent 026682b0
......@@ -70,7 +70,7 @@
"test:phpcpd": "vendor/bin/phpcpd --fuzzy src tests",
"test:phpmd": "vendor/bin/phpmd src,tests text ./phpmd-ruleset.xml",
"test:phpstan": "vendor/bin/phpstan analyse --ansi --no-progress src tests",
"test:phpunit": "vendor/bin/phpunit",
"test:phpunit": "vendor/bin/phpunit --colors=always --testdox",
"test:composer-validate": "composer validate",
"test:composer-require": "vendor/bin/composer-require-checker check",
"test:security": "test '!' -s composer.lock || vendor/bin/security-checker security:check"
......
......@@ -93,12 +93,11 @@ abstract class Maybe implements \IteratorAggregate
{
Assertion::allIsInstanceOf($maybes, self::class);
return array_merge(
...array_map(
static function (Maybe $maybe) {
return $maybe->toArray();
},
$maybes
return array_map(
static function (Just $just) { return $just->value(); },
array_filter(
$maybes,
static function (Maybe $maybe) { return $maybe instanceof Just; }
)
);
}
......
......@@ -13,6 +13,9 @@ use PHPUnit\Framework\TestCase;
*/
class JustTest extends TestCase
{
/**
* @testdox ->map() should call the callback with the value and pass the result.
*/
public function testMap()
{
self::assertEquals(
......@@ -25,6 +28,9 @@ class JustTest extends TestCase
);
}
/**
* @testdox ->resolve() should call the first callback with the value and pass the result.
*/
public function testResolve()
{
self::assertEquals(
......@@ -40,11 +46,17 @@ class JustTest extends TestCase
);
}
/**
* @testdox ->toArray() should return an array of one item.
*/
public function testToArray()
{
self::assertEquals([2], Just::from(2)->toArray());
}
/**
* @testdox ->value() should return the value.
*/
public function testValue()
{
self::assertEquals(
......@@ -53,26 +65,41 @@ class JustTest extends TestCase
);
}
/**
* @testdox ->filter() should filter should return Just when the predicate returns true.
*/
public function testFilterAccepted()
{
self::assertEquals([2], Just::from(2)->filter('is_integer')->toArray());
}
/**
* @testdox ->filter() should filter should return Nothing when the predicate returns false.
*/
public function testFilterRejected()
{
self::assertNotEquals([2], Just::from(2)->filter('is_string')->toArray());
}
/**
* @testdox ->valueOr() should return the value.
*/
public function testValueOr()
{
self::assertEquals(2, Just::from(2)->valueOr(5));
}
/**
* @testdox ->value() should return the value.
*/
public function testConcat()
{
self::assertEquals(2, Just::from(2)->concat(Just::from(6))->value());
}
/**
* @testdox ->getIterator() should return an Iterator with a single item.
*/
public function testGetIterator()
{
$values = \iterator_to_array(Just::from(2));
......
......@@ -15,6 +15,9 @@ use PHPUnit\Framework\TestCase;
*/
class NothingTest extends TestCase
{
/**
* @testdox ->map() should ignore its argument and return itself.
*/
public function testMap()
{
self::assertEquals(
......@@ -27,6 +30,9 @@ class NothingTest extends TestCase
);
}
/**
* @testdox ->resolve() should always call the second callback.
*/
public function testResolve()
{
self::assertEquals(
......@@ -42,11 +48,17 @@ class NothingTest extends TestCase
);
}
/**
* @testdox ->toArray() should return an empty array.
*/
public function testToArray()
{
self::assertEquals([], Nothing::instance()->toArray());
}
/**
* @testdox ->value() should throw a NothingException.
*/
public function testValue()
{
$this->expectException(NothingException::class);
......@@ -54,21 +66,33 @@ class NothingTest extends TestCase
Nothing::instance()->value();
}
/**
* @testdox ->filter() should ignore its argument and return itself.
*/
public function testFilter()
{
self::assertEquals([], Nothing::instance()->filter('is_string')->toArray());
}
/**
* @testdox ->valueOr() should return its argument.
*/
public function testValueOr()
{
self::assertEquals(2, Nothing::instance()->valueOr(2));
}
/**
* @testdox ->concat() should return its argument.
*/
public function testConcat()
{
self::assertEquals([2], Nothing::instance()->concat(Just::from(2))->toArray());
}
/**
* @testdox ->getIterator() should return an empty Iterator.
*/
public function testGetIterator()
{
$values = \iterator_to_array(Nothing::instance());
......
......@@ -15,30 +15,45 @@ use PHPUnit\Framework\TestCase;
*/
class MaybeTest extends TestCase
{
/**
* @testdox ::just() should wrap any value with Just::from.
*/
public function testJustFromScalar()
{
self::assertEquals(42, Maybe::just(42)->value());
}
/**
* @testdox ::just() should return any Just instance as is.
*/
public function testJustFromJust()
{
self::assertEquals(42, Maybe::just(Just::from(42))->value());
}
/**
* @testdox ::just() should return Nothing as is.
*/
public function testJustFromNothing()
{
self::assertEquals([], Maybe::just(Nothing::instance())->toArray());
}
/**
* @testdox ::nothing() should return Nothing.
*/
public function testNothing()
{
self::assertEquals([], Maybe::nothing()->toArray());
}
/**
* @testdox ::some() should the value of Justs.
*/
public function testSome()
{
self::assertEquals(
[5, 'x'],
[1 => 5, 3 => 'x'],
Maybe::some(
[
Maybe::nothing(),
......@@ -51,6 +66,9 @@ class MaybeTest extends TestCase
);
}
/**
* @testdox ::run() should pass back Just values to the generator.
*/
public function testRunJusts()
{
$result = Maybe::run(static function () {
......@@ -64,6 +82,9 @@ class MaybeTest extends TestCase
self::assertEquals(20, $result->valueOr(0));
}
/**
* @testdox ::run() should interrupt the generator when yielded Nothing.
*/
public function testRunNothing()
{
$result = Maybe::run(static function () {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment