Add bind() to option and result

This commit is contained in:
2024-07-28 22:50:59 -04:00
parent efb3a4461e
commit 57af645d87
5 changed files with 89 additions and 8 deletions

View File

@@ -85,6 +85,30 @@ class OptionTest extends TestCase
$this->assertEquals('passed', $value, 'The value should have been obtained from the option');
}
#[TestDox('Bind succeeds with None')]
public function testBindSucceedsWithNone(): void
{
$original = Option::None();
$bound = $original->bind(fn($it) => Option::Some('value'));
$this->assertTrue($bound->isNone(), 'The option should have been None');
$this->assertSame($original, $bound, 'The same None instance should have been returned');
}
#[TestDox('Bind succeeds with Some and Some')]
public function testBindSucceedsWithSomeAndSome(): void
{
$bound = Option::Some('hello')->bind(fn($it) => Option::Some('goodbye'));
$this->assertTrue($bound->isSome(), 'The option should have been Some');
$this->assertEquals('goodbye', $bound->get(), 'The bound function was not called');
}
#[TestDox('Bind succeeds with Some and None')]
public function testBindSucceedsWithSomeAndNone(): void
{
$bound = Option::Some('greetings')->bind(fn($it) => Option::None());
$this->assertTrue($bound->isNone(), 'The option should have been None');
}
#[TestDox('Map succeeds with None')]
public function testMapSucceedsWithNone(): void
{

View File

@@ -74,6 +74,31 @@ class ResultTest extends TestCase
$this->assertFalse($result->isError(), 'The check for "Error" should have returned false');
}
#[TestDox('Bind succeeds for OK with OK')]
public function testBindSucceedsForOKWithOK(): void
{
$result = Result::OK('one')->bind(fn($it) => Result::OK("$it two"));
$this->assertTrue($result->isOK(), 'The result should have been OK');
$this->assertEquals('one two', $result->getOK(), 'The bound function was not called');
}
#[TestDox('Bind succeeds for OK with Error')]
public function testBindSucceedsForOKWithError(): void
{
$result = Result::OK('three')->bind(fn($it) => Result::Error('back to two'));
$this->assertTrue($result->isError(), 'The result should have been Error');
$this->assertEquals('back to two', $result->getError(), 'The bound function was not called');
}
#[TestDox('Bind succeeds for Error')]
public function testBindSucceedsForError(): void
{
$original = Result::Error('oops');
$result = $original->bind(fn($it) => Result::OK('never mind - it worked!'));
$this->assertTrue($result->isError(), 'The result should have been Error');
$this->assertSame($original, $result, 'The same Error result should have been returned');
}
#[TestDox('Map succeeds for OK result')]
public function testMapSucceedsForOKResult(): void
{