public function testEitherErrorMessage() { $errMsg = 'No power in the \'verse can stop me.'; $either = Either::fromValue(null, $errMsg); $expected = Either::left($errMsg); $this->assertEquals($expected, $either); }
public function testFlatMapForLeft() { $eitherInt = Either::left("Error!"); $eitherIntPlusOne = $eitherInt->flatMap(function ($i) { return Either::fromValue($i + 1); }); $this->assertInstanceOf('TMciver\\Functional\\Either\\Left', $eitherIntPlusOne); }
public function testEitherToMaybeForRight() { $right = Either::fromValue("Hello"); $conversionVisitor = new EitherToMaybe(); $actualMaybe = $right->accept($conversionVisitor); $expectedMaybe = Maybe::fromValue("Hello"); $this->assertEquals($expectedMaybe, $actualMaybe); }
public function testMaybeToEitherForJust() { $just = Maybe::fromValue("Hello"); $conversionVisitor = new MaybeToEither(); $actualEither = $just->accept($conversionVisitor); $expectedEither = Either::fromValue("Hello"); $this->assertEquals($expectedEither, $actualEither); }
public function testPure() { // create a MaybeT that represents an `Either Maybe` $mt = new MaybeT(Either::left("I am Error.")); // create a pure value $newMt = $mt->pure("Hello!"); $expectedMt = new MaybeT(Either::fromValue(Maybe::fromValue("Hello!"))); $this->assertEquals($newMt, $expectedMt); }
public function testMapRight() { $toUpper = function ($str) { return strtoupper($str); }; $eitherStr = Either::fromValue("hello"); $mappedEitherStr = $eitherStr->map($toUpper); $expectedEitherStr = Either::fromValue("HELLO"); $this->assertEquals($mappedEitherStr, $expectedEitherStr); }
public function map(callable $f) { // Since we don't know if $f will throw an exception, we wrap the call // in a try/catch. The result wiil be Left if there's an exception. try { $eitherResult = Either::fromValue($f($this->val)); } catch (\Exception $e) { $eitherResult = self::fail($e->getMessage()); } return $eitherResult; }
public function testApplicativeForLeft() { // The function $eitherFn = Either::left('There is no function!'); // The args $eitherStr = Either::fromValue('Hello world!'); $eitherStart = Either::fromValue(6); $eitherLength = Either::fromValue(5); // Apply the function applicatively $eitherResult = $eitherFn($eitherStr, $eitherStart, $eitherLength); $expectedResult = $eitherFn; $this->assertEquals($expectedResult, $eitherResult); }
public function testTraverseForThrownException() { $intsArray = new AssociativeArray([2, 0, 6]); $instance = Either::left(''); $eitherResults = $intsArray->traverse(function ($i) { if ($i == 0) { throw new \Exception('Found zero!'); } else { return Either::fromValue($i); } }, $instance); $expected = $instance->fail(); $this->assertInstanceOf(TMciver\Functional\Either\Left::class, $expected); }
public function testAssociativity() { // we'll combine four values in two different ways and make sure the // results are the same. $right1 = Either::fromValue(1); $right2 = Either::fromValue(2); $right3 = Either::fromValue(3); $right4 = Either::fromValue(4); // First, combine the first two, then the last two and finally combine // the two results. $firstTwo = $right1->append($right2); $secondTwo = $right3->append($right4); $result1 = $firstTwo->append($secondTwo); // Next, combine the first two with the third and then combine that // result with the fourth. $firstThree = $firstTwo->append($right3); $result2 = $firstThree->append($right4); $this->assertEquals($result1, $result2); }
public function visitJust($just) { return Either::fromValue($just->get()); }