/
CandyTest.php
123 lines (103 loc) · 3.83 KB
/
CandyTest.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
require_once 'PHPUnit/Framework.php';
include_once __DIR__ . '/../../../recess/lang/Candy.class.php';
use recess\lang\Candy;
class CandyTest extends PHPUnit_Framework_TestCase {
function testConstructor() {
$candy = new Candy(function() { return 'Hello world!'; });
$this->assertEquals('Hello world!',$candy());
}
function testWrap() {
$candy = new Candy(function() { return 'link'; });
$candy->wrap(function($candy) { return 'wrapper' . $candy(); });
$this->assertEquals('wrapperlink', $candy());
}
function testWrapCandy() {
$candy = new Candy(function() { return 'middle'; });
$wrap = function($candy) {return 'front' . $candy() . 'end';};
$candy->wrap($wrap)->wrap($wrap);
$this->assertEquals('frontfrontmiddleendend', $candy());
}
function testArgs() {
$candy = new Candy(function($num) { return $num + 1; });
$candy->wrap(function($candy, $num) { return $candy() + 1; });
$this->assertEquals(3, $candy(1));
}
function testManyArgs() {
$candy = new Candy(function() { return array_sum(func_get_args()); });
$candy->wrap(function($candy) { return $candy() + 1; });
$this->assertEquals(9, $candy(1,1,1,1,1,1,1,1));
}
function testArgManipulation() {
$candy = new Candy(function($num) { return $num; });
$candy->wrap(function($candy, $num) { $num += 1; return $candy(); });
$this->assertEquals(1, $candy(1));
$candy = new Candy(function($num) { return $num; });
$candy->wrap(function($candy, &$num) { $num += 1; return $candy(); });
$this->assertEquals(2, $candy(1));
$candy = new Candy(function($num) { return $num; });
$candy->wrap(function($candy, $num) { $num += 1; return $candy($num); });
$this->assertEquals(2, $candy(1));
}
function testShortCircuit() {
$candy = new Candy(function($num) { return $num; });
$candy->wrap(function($candy, $num) { return 2; });
$this->assertEquals(2, $candy(1));
}
function testLIFO() {
$candy = new Candy(function($num) { return $num + 1; });
$candy->wrap(function($candy, $num) { return $candy($num * 2); })
->wrap(function($candy, $num) { return $candy($num - 1); });
$this->assertEquals(5, $candy(3));
}
function testLazyLinks() {
$candy = new Candy(function($num) { return $num + 1; });
$candy->wrap(function($candy, &$num) { $num += 1; })
->wrap(function($candy, $num) { /* echo or log something */ });
$this->assertEquals(3, $candy(1));
}
function testNonCallableException() {
try {
$candy = new Candy('unknownFunc');
$this->assertTrue(false);
} catch(Exception $e) {
$this->assertTrue(true);
return;
}
$this->assertTrue(false);
}
function testPlainOldFunction() {
$candy = new Candy('callMe');
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}
function testStaticMethod() {
$candy = new Candy(array('SomeStaticClass','callMe'));
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}
function testMethod() {
$candy = new Candy(array(new SomeStaticClass,'callMe'));
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}
function testManyArgsUnwrapped() {
$candy = new Candy(function() { return array_sum(func_get_args()); });
$this->assertEquals(0, $candy());
$this->assertEquals(1, $candy(1));
$this->assertEquals(2, $candy(1,1));
$this->assertEquals(3, $candy(1,1,1));
$this->assertEquals(4, $candy(1,1,1,1));
$this->assertEquals(5, $candy(1,1,1,1,1));
$this->assertEquals(6, $candy(1,1,1,1,1,1));
$this->assertEquals(7, $candy(1,1,1,1,1,1,1));
}
}
class SomeStaticClass {
static function callMe() { return 'pass'; }
function callMeToo() { return 'pass'; }
}
function callMe() { return 'pass'; }