-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayTest.php
More file actions
100 lines (77 loc) · 2.75 KB
/
Copy pathArrayTest.php
File metadata and controls
100 lines (77 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
use PHPUnit\Framework\TestCase;
use TMciver\Functional\AssociativeArray;
use TMciver\Functional\Either\Either;
use TMciver\Functional\Either\Monad\RightFavoringEitherMonad;
class ArrayTest extends TestCase {
private $monad;
protected function setUp() {
$this->monad = new RightFavoringEitherMonad();
}
public function testTraverseSuccessForArrayOfInt() {
$dividend = 12;
$divisors = [2, 4, 6];
$intsArray = new AssociativeArray($divisors);
$eitherResults = $intsArray->traverse(function ($i) use ($dividend) {
return divide($dividend, $i);
}, $this->monad);
$expected = Either::fromValue([6, 3, 2]);
$this->assertEquals($expected, $eitherResults);
}
public function testTraverseFailureForArrayOfInt() {
$dividend = 12;
$divisors = [2, 0, 6];
$intsArray = new AssociativeArray($divisors);
$eitherResults = $intsArray->traverse(function ($i) use ($dividend) {
return divide($dividend, $i);
}, $this->monad);
$expected = Either::left('Division by zero!');
$this->assertEquals($expected, $eitherResults);
}
public function testTraverseForEmptyArray() {
$arr = new AssociativeArray([]);
$eitherResult = $arr->traverse(function ($ignore) {
throw new \Exception('This should not affect the traversal as it should not be called!');
}, $this->monad);
$expected = Either::fromValue([]);
$this->assertEquals($expected, $eitherResult);
}
public function testTraverseForThrownException() {
$intsArray = new AssociativeArray([2, 0, 6]);
$eitherResults = $intsArray->traverse(function ($i) {
if ($i == 0) {
throw new \Exception('Found zero!');
} else {
return Either::fromValue($i);
}
}, $this->monad);
$expected = $this->monad->fail();
$this->assertInstanceOf(TMciver\Functional\Either\Left::class, $expected);
}
public function testTraverseForReturningNull() {
$intsArray = new AssociativeArray([2, 0, 6]);
$eitherResults = $intsArray->traverse(function ($ignore) {
return null;
}, $this->monad);
$expected = $this->monad->fail();
$this->assertInstanceOf(TMciver\Functional\Either\Left::class, $expected);
}
public function testHandlingOfNullArgument() {
$arr = new AssociativeArray(null);
$expected = Either::fromValue([]);
$this->assertEquals($arr->sequence($this->monad), $expected);
}
}
/**
* @param x (number) The dividend
* @param y (number) The divisor
* @return Either number; Left if the divisor is zero, Right otherwise.
*/
function divide($x, $y) {
if ($y == 0) {
$eitherResult = Either::left('Division by zero!');
} else {
$eitherResult = Either::fromValue($x/$y);
}
return $eitherResult;
}