Skip to content

Commit 822b215

Browse files
committed
test: more tests more coverage
1 parent f2d9dbb commit 822b215

File tree

4 files changed

+206
-61
lines changed

4 files changed

+206
-61
lines changed

tests/UnderscoreArrayTest.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,17 @@ public function test_object()
126126
}
127127
}
128128

129-
public function test_firstIndex_lastIndex()
129+
public function test_findIndex_findLastIndex()
130130
{
131131
$array = _::_([[1, 2], 'a' => 3, 'x' => 4, 'y' => 2, 'b' => 'B']);
132132

133-
$this->assertSame(0, $array->firstIndex());
134-
$this->assertSame('b', $array->lastIndex());
133+
$this->assertSame(0, $array->findIndex());
134+
$this->assertSame('b', $array->findLastIndex());
135135

136-
$this->assertSame('x', $array->firstIndex(function ($i) {
136+
$this->assertSame('x', $array->findIndex(function ($i) {
137137
return is_numeric($i) && $i % 2 === 0;
138138
}));
139-
$this->assertSame('y', $array->lastIndex(function ($i) {
139+
$this->assertSame('y', $array->findLastIndex(function ($i) {
140140
return is_numeric($i) && $i % 2 === 0;
141141
}));
142142
}
@@ -155,4 +155,29 @@ public function test_range()
155155
$this->assertSame([10, 12, 14, 16, 18], _::_()->range(10, 18, 2)->get());
156156
$this->assertSame([20, 19, 18, 17, 16], _::_()->range(20, 16, -1)->get());
157157
}
158+
159+
public function test_sortedIndex()
160+
{
161+
$nums = [1, 3, 5, 8, 11];
162+
$new = 9;
163+
164+
$newIdx = _::_($nums)->sortedIndex($new, null);
165+
166+
$this->assertSame(4, $newIdx);
167+
168+
$data = [
169+
'a' => ['x' => 1, 'y' => 2],
170+
'b' => ['x' => 2, 'y' => 2],
171+
'c' => ['x' => 3, 'y' => 3],
172+
'd' => ['x' => 4, 'y' => 3],
173+
'e' => ['x' => 5, 'y' => 4],
174+
];
175+
176+
$new = ['x' => 3, 'y' => 2];
177+
$newIdx = _::_($data)->sortedIndex($new, function ($row) {
178+
return $row['x'] + $row['y'];
179+
});
180+
181+
$this->assertSame('c', $newIdx);
182+
}
158183
}

tests/UnderscoreBaseTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Ahc\Underscore\Tests;
44

5-
use Ahc\Underscore\UnderscoreBase as _;
5+
use Ahc\Underscore\Underscore as _;
66

77
class Stub
88
{
@@ -81,4 +81,48 @@ public function test_pick_omit()
8181
$this->assertSame(['a' => 3, 7], $array->omit([1, 'b'])->get());
8282
$this->assertSame(['b' => 'B', 1 => ['c', 5]], $array->omit('a', 0)->get());
8383
}
84+
85+
public function test_clone_tap()
86+
{
87+
$main = _::_(['will', 'be', 'cloned']);
88+
$clon = $main->clon();
89+
90+
$this->assertNotSame($main, $clon, 'hard equal');
91+
$this->assertNotSame(spl_object_hash($main), spl_object_hash($clon));
92+
$this->assertEquals($main, $clon, 'soft equal');
93+
$this->assertSame($main->toArray(), $clon->toArray());
94+
95+
$tap = $main->tap(function ($und) {
96+
return $und->values();
97+
});
98+
99+
$this->assertSame($main, $tap, 'hard equal');
100+
}
101+
102+
/**
103+
* @expectedException \Ahc\Underscore\UnderscoreException
104+
* @expectedExceptionMessage The mixin with name 'notMixedIn' is not defined
105+
*/
106+
public function test_mixin()
107+
{
108+
_::mixin('double', function () {
109+
return $this->map(function ($v) {
110+
return $v * 2;
111+
});
112+
});
113+
114+
$und = _::_([10, 20, 30]);
115+
116+
$this->assertTrue(is_callable([$und, 'double']));
117+
$this->assertSame([20, 40, 60], $und->double()->toArray());
118+
119+
$und->notMixedIn();
120+
}
121+
122+
public function test_valueOf()
123+
{
124+
$this->assertSame('[]', _::_()->valueOf());
125+
$this->assertSame('[1,2]', _::_([1, 2])->valueOf());
126+
$this->assertSame('["a","b"]', _::_(['a', 'b'])->valueOf());
127+
}
84128
}

tests/UnderscoreFunctionTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Ahc\Underscore\Tests;
4+
5+
use Ahc\Underscore\UnderscoreFunction as _;
6+
7+
class UnderscoreFunctionTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function test_memoize()
10+
{
11+
$memoSum = _::_()->memoize(function ($a, $b) {
12+
echo "sum $a + $b";
13+
14+
return $a + $b;
15+
});
16+
17+
// Every time the sum callback is called it echoes something.
18+
// But since it is memorized, it should only echo in the first call.
19+
ob_start();
20+
21+
// Call 3 times!
22+
$this->assertSame(1 + 2, $memoSum(1, 2));
23+
$this->assertSame(1 + 2, $memoSum(1, 2));
24+
$this->assertSame(1 + 2, $memoSum(1, 2));
25+
26+
// Call twice for different args!
27+
$this->assertSame(3 + 2, $memoSum(3, 2));
28+
$this->assertSame(3 + 2, $memoSum(3, 2));
29+
30+
$buffer = ob_get_clean();
31+
32+
$this->assertSame(1, substr_count($buffer, 'sum 1 + 2'),
33+
'Should be called only once, subsequent calls uses memo'
34+
);
35+
$this->assertSame(1, substr_count($buffer, 'sum 3 + 2'),
36+
'Should be called only once, subsequent calls uses memo'
37+
);
38+
}
39+
40+
public function test_delay()
41+
{
42+
$callback = function () {
43+
// Do nothing!
44+
};
45+
46+
// Calibrate time taken by callback!
47+
$cTime = microtime(1);
48+
$callback();
49+
$cTime = microtime(1) - $cTime;
50+
51+
// Now delay this callback by 10millis (0.01sec).
52+
$delayCall = _::_()->delay($callback, 10);
53+
54+
$time = microtime(1);
55+
$delayCall();
56+
$time = microtime(1) - $time;
57+
58+
// The overall time must be >= (cTime + 1sec).
59+
$this->assertGreaterThanOrEqual(0.01 + $cTime, $time);
60+
}
61+
62+
public function test_throttle()
63+
{
64+
$callback = function () {
65+
echo 'throttle';
66+
};
67+
68+
// Throttle the call for once per 10millis (0.01 sec)
69+
// So that for a period of 300millis it should be actually called at most 3 times.
70+
$throtCall = _::_()->throttle($callback, 10);
71+
72+
ob_start();
73+
74+
$start = microtime(1);
75+
while (microtime(1) - $start <= 0.031) {
76+
$throtCall();
77+
}
78+
79+
$buffer = ob_get_clean();
80+
81+
$this->assertLessThanOrEqual(3, substr_count($buffer, 'throttle'),
82+
'Should be called only once, subsequent calls uses memo'
83+
);
84+
}
85+
86+
public function test_compose()
87+
{
88+
$c = _::_()->compose('strlen', 'strtolower', 'strtoupper');
89+
90+
$this->assertSame(7, $c('aBc.xYz'));
91+
}
92+
}

tests/UnderscoreTest.php

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,64 @@
66

77
class UnderscoreTest extends \PHPUnit_Framework_TestCase
88
{
9-
public function test_memoize()
9+
public function test_constant()
1010
{
11-
$memoSum = _::_()->memoize(function ($a, $b) {
12-
echo "sum $a + $b";
11+
foreach ([1, 'A', [], new \stdClass()] as $value) {
12+
$fn = _::_()->constant($value);
1313

14-
return $a + $b;
15-
});
16-
17-
// Every time the sum callback is called it echoes something.
18-
// But since it is memorized, it should only echo in the first call.
19-
ob_start();
20-
21-
// Call 3 times!
22-
$this->assertSame(1 + 2, $memoSum(1, 2));
23-
$this->assertSame(1 + 2, $memoSum(1, 2));
24-
$this->assertSame(1 + 2, $memoSum(1, 2));
14+
$this->assertSame($value, $fn());
15+
}
16+
}
2517

26-
// Call twice for different args!
27-
$this->assertSame(3 + 2, $memoSum(3, 2));
28-
$this->assertSame(3 + 2, $memoSum(3, 2));
18+
public function test_noop()
19+
{
20+
$epsilon = 0.0000000001;
2921

30-
$buffer = ob_get_clean();
22+
$t = microtime(1);
23+
$m = memory_get_usage();
24+
$x = _::_()->noop();
25+
$t = microtime(1) - $t;
26+
$m = memory_get_usage() - $m;
3127

32-
$this->assertSame(1, substr_count($buffer, 'sum 1 + 2'),
33-
'Should be called only once, subsequent calls uses memo'
34-
);
35-
$this->assertSame(1, substr_count($buffer, 'sum 3 + 2'),
36-
'Should be called only once, subsequent calls uses memo'
37-
);
28+
$this->assertLessThanOrEqual($t, $epsilon);
29+
$this->assertLessThanOrEqual($m, $epsilon);
3830
}
3931

40-
public function test_delay()
32+
public function test_times()
4133
{
42-
$callback = function () {
43-
// Do nothing!
34+
$fn = function ($i) {
35+
return $i * 2;
4436
};
4537

46-
// Calibrate time taken by callback!
47-
$cTime = microtime(1);
48-
$callback();
49-
$cTime = microtime(1) - $cTime;
50-
51-
// Now delay this callback by 10millis (0.01sec).
52-
$delayCall = _::_()->delay($callback, 10);
38+
$o = _::_()->times(5, $fn);
5339

54-
$time = microtime(1);
55-
$delayCall();
56-
$time = microtime(1) - $time;
57-
58-
// The overall time must be >= (cTime + 1sec).
59-
$this->assertGreaterThanOrEqual(0.01 + $cTime, $time);
40+
$this->assertSame([0, 2, 4, 6, 8], $o->toArray());
6041
}
6142

62-
public function test_throttle()
43+
public function test_random()
6344
{
64-
$callback = function () {
65-
echo 'throttle';
66-
};
45+
$i = 10;
6746

68-
// Throttle the call for once per 10millis (0.01 sec)
69-
// So that for a period of 300millis it should be actually called at most 3 times.
70-
$throtCall = _::_()->throttle($callback, 10);
47+
while ($i--) {
48+
$cases[rand(1, 10)] = rand(11, 20);
49+
}
7150

72-
ob_start();
51+
foreach ($cases as $l => $r) {
52+
$rand = _::_()->random($l, $r);
7353

74-
$start = microtime(1);
75-
while (microtime(1) - $start <= 0.031) {
76-
$throtCall();
54+
$this->assertGreaterThanOrEqual($l, $rand);
55+
$this->assertLessThanOrEqual($r, $rand);
7756
}
57+
}
7858

79-
$buffer = ob_get_clean();
59+
public function test_unique_id()
60+
{
61+
$u = _::_()->uniqueId();
62+
$u1 = _::_()->uniqueId();
63+
$u3 = _::_()->uniqueId('id:');
8064

81-
$this->assertLessThanOrEqual(3, substr_count($buffer, 'throttle'),
82-
'Should be called only once, subsequent calls uses memo'
83-
);
65+
$this->assertSame('1', $u);
66+
$this->assertSame('2', $u1);
67+
$this->assertSame('id:3', $u3);
8468
}
8569
}

0 commit comments

Comments
 (0)