Add tap for Option and Result
This commit is contained in:
@@ -197,4 +197,28 @@ class ResultTest extends TestCase
|
||||
$value = Result::toOption(Result::Error('file not found'));
|
||||
$this->assertTrue(Option::isNone($value), 'An "Error" result should map to a "None" option');
|
||||
}
|
||||
|
||||
#[TestDox('Tap succeeds for OK result')]
|
||||
public function testTapSucceedsForOKResult(): void
|
||||
{
|
||||
$value = '';
|
||||
$original = Result::OK('working');
|
||||
$tapped = Result::tap(function (Result $it) use (&$value) {
|
||||
$value = Result::isOK($it) ? 'OK: ' . $it->getOK() : 'Error: ' . $it->getError();
|
||||
}, $original);
|
||||
$this->assertEquals('OK: working', $value, 'The tapped function was not called');
|
||||
$this->assertSame($original, $tapped, 'The same result should have been returned');
|
||||
}
|
||||
|
||||
#[TestDox('Tap succeeds for Error result')]
|
||||
public function testTapSucceedsForErrorResult(): void
|
||||
{
|
||||
$value = '';
|
||||
$original = Result::Error('failed');
|
||||
$tapped = Result::tap(function (Result $it) use (&$value) {
|
||||
$value = Result::isOK($it) ? 'OK: ' . $it->getOK() : 'Error: ' . $it->getError();
|
||||
}, $original);
|
||||
$this->assertEquals('Error: failed', $value, 'The tapped function was not called');
|
||||
$this->assertSame($original, $tapped, 'The same result should have been returned');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user