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

@@ -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
{