forked from brianhaveri/Underscore.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionsTest.php
176 lines (152 loc) · 6.46 KB
/
FunctionsTest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
include_once(__DIR__ . '/../underscore.php');
class UnderscoreFunctionsTest extends PHPUnit_Framework_TestCase {
public function testMemoize() {
// from js
$fib = function($n) use (&$fib) {
return $n < 2 ? $n : $fib($n - 1) + $fib($n - 2);
};
$fastFib = __::memoize($fib);
$this->assertEquals(55, $fib(10), 'a memoized version of fibonacci produces identical results');
$this->assertEquals(55, $fastFib(10), 'a memoized version of fibonacci produces identical results');
$o = function($str) { return $str; };
$fastO = __::memoize($o);
$this->assertEquals('toString', $o('toString'), 'checks hasOwnProperty');
$this->assertEquals('toString', $fastO('toString'), 'checks hasOwnProperty');
// extra
$name = function() { return 'moe'; };
$fastName = __::memoize($name);
$this->assertEquals('moe', $name(), 'works with no parameters');
$this->assertEquals('moe', $fastName(), 'works with no parameters');
$names = function($one, $two, $three) {
return join(', ', array($one, $two, $three));
};
$fastNames = __::memoize($names);
$this->assertEquals('moe, larry, curly', $names('moe', 'larry', 'curly'), 'works with multiple parameters');
$this->assertEquals('moe, larry, curly', $fastNames('moe', 'larry', 'curly'), 'works with multiple parameters');
$foo = function() { return 'foo'; };
$fastFoo = __($foo)->memoize();
$this->assertEquals('foo', $foo(), 'can handle OO-style calls');
$this->assertEquals('foo', $fastFoo(), 'can handle OO-style calls');
$bar = function() { return 'bar'; };
$fastBar = __::memoize($bar, function($function, $args) {
return sha1(join('x', array(
var_export($function, 1),
var_export($args, 1)
)));
});
$this->assertEquals('bar', $bar(), 'can custom hash function');
$this->assertEquals('bar', $fastBar(), 'can use custom hash function');
// docs
$fibonacci = function($n) use (&$fibonacci) {
return $n < 2 ? $n : $fibonacci($n - 1) + $fibonacci($n - 2);
};
$fastFibonacci = __::memoize($fibonacci);
$this->assertEquals($fibonacci(2), $fastFibonacci(2));
}
public function testThrottle() {
// from js
$counter = 0;
$incr = function() use (&$counter) { $counter++; };
$throttledIncr = __::throttle($incr, 100);
$throttledIncr(); $throttledIncr(); $throttledIncr();
usleep(120 * 1000); $throttledIncr();
usleep(140 * 1000); $throttledIncr();
usleep(220 * 1000); $throttledIncr();
usleep(240 * 1000); $throttledIncr();
$this->assertEquals(5, $counter, 'incr was throttled');
// extra
$counter = 0;
$incr = function() use (&$counter) { $counter++; };
$throttledIncr = __($incr)->throttle(100);
$throttledIncr(); $throttledIncr(); $throttledIncr();
usleep(120 * 1000); $throttledIncr();
usleep(140 * 1000); $throttledIncr();
usleep(220 * 1000); $throttledIncr();
usleep(240 * 1000); $throttledIncr();
$this->assertEquals(5, $counter, 'incr was throttled with OO-style call');
}
public function testOnce() {
// from js + docs
$num = 0;
$increment = __::once(function() use (&$num) { return $num++; });
$increment();
$increment();
$this->assertEquals(1, $num);
// extra
$num = 0;
$increment = __(function() use (&$num) { return $num++; })->once();
$increment();
$increment();
$this->assertEquals(1, $num);
}
public function testWrap() {
// from js
$greet = function($name) { return 'hi: ' . $name; };
$backwards = __::wrap($greet, function($func, $name) { return $func($name) . ' ' . strrev($name); });
$this->assertEquals('hi: moe eom', $backwards('moe'), 'wrapped the salutation function');
$inner = function() { return 'Hello '; };
$arr = array('name'=>'Moe');
$arr['hi'] = __::wrap($inner, function($fn) use ($arr) { return $fn() . $arr['name']; });
$this->assertEquals('Hello Moe', $arr['hi']());
// extra
$inner = function() { return 'Hello '; };
$arr = array('name'=>'Curly');
$arr['hi'] = __($inner)->wrap(function($fn) use ($arr) { return $fn() . $arr['name']; });
$this->assertEquals('Hello Curly', $arr['hi']());
// docs
$hello = function($name) { return 'hello: ' . $name; };
$hi = __::wrap($hello, function($func) {
return 'before, ' . $func('moe') . ', after';
});
$this->assertEquals('before, hello: moe, after', $hi());
}
public function testCompose() {
// from js
$greet = function($name) { return 'hi: ' . $name; };
$exclaim = function($sentence) { return $sentence . '!'; };
$composed = __::compose($exclaim, $greet);
$this->assertEquals('hi: moe!', $composed('moe'), 'can compose a function that takes another');
$composed = __::compose($greet, $exclaim);
$this->assertEquals('hi: moe!', $composed('moe'), 'in this case, the functions are also commutative');
// extra
$composed = __($greet)->compose($exclaim);
$this->assertEquals('hi: moe!', $composed('moe'), 'in this case, the functions are also commutative');
// docs
$greet = function($name) { return 'hi: ' . $name; };
$exclaim = function($statement) { return $statement . '!'; };
$welcome = __::compose($exclaim, $greet);
$this->assertEquals('hi: moe!', $welcome('moe'));
}
public function testAfter() {
// from js
$testAfter = function($afterAmount, $timesCalled) {
$afterCalled = 0;
$after = __::after($afterAmount, function() use (&$afterCalled) {
$afterCalled++;
});
while($timesCalled--) $after();
return $afterCalled;
};
$this->assertEquals(1, $testAfter(5, 5), 'after(N) should fire after being called N times');
$this->assertEquals(0, $testAfter(5, 4), 'after(N) should not fire unless called N times');
// extra
$testAfterAgain = function($afterAmount, $timesCalled) {
$afterCalled = 0;
$after = __($afterAmount)->after(function() use (&$afterCalled) {
$afterCalled++;
});
while($timesCalled--) $after();
return $afterCalled;
};
$this->assertEquals(1, $testAfterAgain(5, 5), 'after(N) should fire after being called N times in OO-style call');
$this->assertEquals(0, $testAfterAgain(5, 4), 'after(N) should not fire unless called N times in OO-style call');
// docs
$str = '';
$func = __::after(3, function() use(&$str) { $str = 'x'; });
$func();
$func();
$func();
$this->assertEquals('x', $str);
}
}