Skip to content

Commit 8df5e1f

Browse files
committed
test: collection test
1 parent 4a8f337 commit 8df5e1f

File tree

1 file changed

+138
-2
lines changed

1 file changed

+138
-2
lines changed

tests/CollectionTest.php

+138-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Ahc\Underscore\Tests;
44

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

77
class CollectionTest extends \PHPUnit_Framework_TestCase
88
{
@@ -14,7 +14,7 @@ public function test_array_json_props()
1414
$this->assertSame(8, $_[2]);
1515
$this->assertTrue(isset($_['c']));
1616
$this->assertFalse(isset($_['D']));
17-
$this->assertCount(5, $_);
17+
$this->assertSame(5, $_->size());
1818

1919
unset($_['c']);
2020

@@ -243,4 +243,140 @@ public function test_findWhere()
243243
$this->assertSame(['a' => 1, 'b' => 3], $b3, 'findwhere b = 3');
244244
$this->assertNull($a2b1, 'where a = 2 and b = 1');
245245
}
246+
247+
public function test_max_min()
248+
{
249+
$list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 3], ['a' => 0, 'b' => 1]]);
250+
251+
$this->assertSame(2, $list->max('a'), 'max a = 2');
252+
$this->assertSame(3, $list->max('b'), 'max a = 3');
253+
$this->assertSame(0, $list->min('a'), 'min a = 0');
254+
$this->assertSame(1, $list->min('b'), 'min b = 1');
255+
256+
$this->assertSame(5, $list->max(function ($i) {
257+
return $i['a'] + $i['b'];
258+
}), 'max sum of a and b');
259+
260+
$this->assertSame(1, $list->min(function ($i) {
261+
return $i['b'] - $i['a'];
262+
}), 'max diff of b and a');
263+
264+
$list = _::_([1, 99, 9, -10, 1000, false, 0, 'string', -99, 10000, 87, null]);
265+
266+
$this->assertSame(10000, $list->max(), 'max = 10000');
267+
$this->assertSame(-99, $list->min(), 'min = -99');
268+
}
269+
270+
public function test_shuffle()
271+
{
272+
$pool = range(1, 5);
273+
$shuf = _::_($pool)->shuffle()->get();
274+
275+
foreach ($shuf as $key => $value) {
276+
$this->assertArrayHasKey($key, $pool, 'shuffled item is one from pool');
277+
$this->assertSame($pool[$key], $value, 'The values are the same as in pool');
278+
}
279+
280+
$this->assertSame(count($pool), count($shuf), 'Should have exact counts');
281+
}
282+
283+
public function test_sample()
284+
{
285+
$pool = range(10, 5, -1);
286+
287+
foreach ([1, 2, 3] as $n) {
288+
$samp = _::_($pool)->sample($n)->get();
289+
290+
foreach ($samp as $key => $value) {
291+
$this->assertArrayHasKey($key, $pool, 'sampled item is one from pool');
292+
$this->assertSame($pool[$key], $value, 'The values are the same as in pool');
293+
}
294+
295+
$this->assertCount($n, $samp, 'The count should be the one specified');
296+
}
297+
}
298+
299+
public function test_sortBy()
300+
{
301+
$sort = $init = range(1, 15);
302+
$sort = _::_($sort)->shuffle()->get();
303+
304+
$this->assertNotSame($init, $sort, 'Should be random');
305+
306+
$sort = _::_($sort)->sortBy(null)->get();
307+
308+
$this->assertSame($init, $sort, 'Should be sorted');
309+
310+
$list = _::_([['a' => 1, 'b' => 2], ['a' => 2, 'b' => 3], ['a' => 0, 'b' => 1]]);
311+
312+
$byA = $list->sortBy('a')->get();
313+
$this->assertSame(
314+
[2 => ['a' => 0, 'b' => 1], 0 => ['a' => 1, 'b' => 2], 1 => ['a' => 2, 'b' => 3]],
315+
$byA, 'sort by a'
316+
);
317+
318+
$byAB = $list->sortBy(function ($i) {
319+
return $i['a'] + $i['b'];
320+
})->get();
321+
322+
$this->assertSame(
323+
[2 => ['a' => 0, 'b' => 1], 0 => ['a' => 1, 'b' => 2], 1 => ['a' => 2, 'b' => 3]],
324+
$byAB, 'sort by a+b'
325+
);
326+
}
327+
328+
public function test_groupBy_indexBy_countBy()
329+
{
330+
$list = _::_([
331+
['a' => 0, 'b' => 1, 'c' => 1],
332+
['a' => true, 'b' => false, 'c' => 'c'],
333+
['a' => 2, 'b' => 1, 'c' => 2],
334+
['a' => 1, 'b' => null, 'c' => 0],
335+
]);
336+
337+
$grpByA = $list->groupBy('a')->get();
338+
$idxByA = $list->indexBy('a')->get();
339+
$cntByA = $list->countBy('a')->get();
340+
341+
$this->assertSame([
342+
0 => [0 => ['a' => 0, 'b' => 1, 'c' => 1]],
343+
1 => [1 => ['a' => true, 'b' => false, 'c' => 'c'], 3 => ['a' => 1, 'b' => null, 'c' => 0]],
344+
2 => [2 => ['a' => 2, 'b' => 1, 'c' => 2]],
345+
], $grpByA, 'groupBy a');
346+
347+
$this->assertSame([
348+
0 => ['a' => 0, 'b' => 1, 'c' => 1],
349+
1 => ['a' => 1, 'b' => null, 'c' => 0],
350+
2 => ['a' => 2, 'b' => 1, 'c' => 2],
351+
], $idxByA, 'indexBy a');
352+
353+
$this->assertSame([
354+
0 => 1,
355+
1 => 2,
356+
2 => 1,
357+
], $cntByA, 'countBy a');
358+
}
359+
360+
public function test_toArray()
361+
{
362+
$array = [['deep'=> 1, 'ok'], 'shallow', 0, false];
363+
364+
$this->assertSame($array, _::_($array)->toArray());
365+
}
366+
367+
public function test_partition()
368+
{
369+
$nums = _::_(range(1, 10));
370+
$oddEvn = $nums->partition(function ($i) { return $i % 2; })->get();
371+
$evnOdd = $nums->partition(function ($i) { return $i % 2 == 0; })->get();
372+
373+
$this->assertCount(2, $oddEvn, '2 partitions');
374+
$this->assertArrayHasKey(0, $oddEvn, 'odd partition');
375+
$this->assertArrayHasKey(1, $oddEvn, 'even partition');
376+
377+
$this->assertSame([1, 3, 5, 7, 9], $oddEvn[0], 'odds');
378+
$this->assertSame([1, 3, 5, 7, 9], $oddEvn[0], 'odds');
379+
$this->assertSame($evnOdd[1], $oddEvn[0], 'odds crosscheck');
380+
$this->assertSame($evnOdd[0], $oddEvn[1], 'evens crosscheck');
381+
}
246382
}

0 commit comments

Comments
 (0)