Skip to content

Commit

Permalink
better test coverage and bug fixes on flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
camille-hdl committed Jun 3, 2020
1 parent 43afc4b commit fbdb856
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Transducer/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ public function __construct(int $levels)
$this->levels = $levels;
}

public static function flattenItem($levels, $item)
/**
* @param mixed $subject
* @return boolean
*/
protected static function isPlainArray($subject): bool
{
return \is_array($subject) && !isAssociativeArray($subject);
}

/**
* @param integer $levels
* @param array|\Traversable $item
* @return array
*/
public static function flattenItem(int $levels, $item): array
{
$output = [];
foreach ($item as $child) {
if (\is_array($child) && $levels > 1) {
if (self::isPlainArray($child) && $levels > 1) {
$output = array_merge($output, self::flattenItem($levels - 1, $child));
} else {
$output[] = $child;
Expand Down
20 changes: 20 additions & 0 deletions tests/ArrayIteratorSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LazyLists\Test;

use ArrayIterator;

/**
* For testing purposes only
*/
class ArrayIteratorSpy extends ArrayIterator
{
public $howManyNexts = 0;
public function next()
{
$this->howManyNexts++;
parent::next();
}
}
3 changes: 3 additions & 0 deletions tests/IsAssociativeArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ public function testNegatives()
$this->assertFalse(isAssociativeArray([]));
$this->assertFalse(isAssociativeArray([1, 2]));
$this->assertFalse(isAssociativeArray([["" => ""]]));
$this->assertFalse(isAssociativeArray(true));
$this->assertFalse(isAssociativeArray(false));
$this->assertFalse(isAssociativeArray(new \stdClass()));
}
}
12 changes: 12 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public function test()
$this->assertSame(['k1val1', 'k2val2'], map($fn, $hashIterator));
}

public function testNullValue()
{
$list = ['value', null, 'value'];
$identity = static function ($v) {
return $v;
};
$this->assertSame(
['value', null, 'value'],
map($identity, $list)
);
}

public function testPassNoCollection()
{
$this->expectException(\Exception::class);
Expand Down
64 changes: 64 additions & 0 deletions tests/PipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace LazyLists\Test;

use LazyLists\Exception\InvalidArgumentException;

use function LazyLists\map;
use function LazyLists\filter;
use function LazyLists\pipe;
Expand Down Expand Up @@ -131,4 +133,66 @@ public function testDirectoryIterator()
sort($output);
$this->assertSame([1, 2, 3], $output);
}

public function testInvalidTransducers()
{
$this->expectException(InvalidArgumentException::class);
$pipe = pipe("notATransducer");
}

public function testRealUseCase()
{
$data1 = [
["a" => 1],
["a" => 11],
["a" => 2],
];
$data2 = [
["a" => 22],
["a" => 15],
["a" => 3],
];
$data3 = [
["a" => 3],
];
$input = new ArrayIteratorSpy([
\json_encode($data1),
\json_encode($data2),
\json_encode($data3),
]);
$decode_count = 0;
$decode = function ($i) use (&$decode_count) {
$decode_count++;
return \json_decode($i, true);
};
$getA_count = 0;
$getA = function ($data) use (&$getA_count) {
$getA_count++;
return $data["a"];
};
$sup_count = 0;
$supOrEq10 = function ($a) use (&$sup_count) {
$sup_count++;
return $a >= 10;
};
$sum_count = 0;
$sum = function ($acc, $a) use (&$sum_count) {
$sum_count++;
return $acc + $a;
};
$expected = 48;
$pipe = pipe(
map($decode),
flatten(1),
map($getA),
filter($supOrEq10),
reduce($sum, 0)
);
$this->assertEquals($expected, $pipe($input));
$this->assertEquals(4, $input->howManyNexts, "We should iterate over the input only once");
$this->assertEquals(3, $decode_count);
$this->assertEquals(7, $getA_count);
$this->assertEquals(7, $sup_count);
$this->assertEquals(3, $sum_count);
}
}
55 changes: 55 additions & 0 deletions tests/Transducer/FlattenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace LazyLists\Test\Transducer;

use LazyLists\Test\TestCase;

use function LazyLists\map;
use function LazyLists\flatten;
use function LazyLists\pipe;
use function LazyLists\take;

class FlattenTest extends TestCase
{
public function test()
{
$input = [
[1, 2],
[3, 4]
];
$times2 = map(static function ($v) {
return $v * 2;
});
$pipe = pipe(flatten(1), $times2);
$this->assertSame([2, 4, 6, 8], $pipe($input));
}
public function testNull()
{
$input = [
[1, null, 2]
];
$pipe = pipe(flatten(1));
$this->assertCount(3, $pipe($input));
$this->assertSame([1, null, 2], $pipe($input));
}
public function testTake()
{
$input = [
[1, null, 2]
];
$pipe = pipe(flatten(1), take(2));
$this->assertCount(2, $pipe($input));
$this->assertSame([1, null], $pipe($input));
}
public function testAssociativeArrays()
{
$input = [
[[1], ["a" => 1], 2]
];
$pipe = pipe(flatten(2));
$this->assertCount(3, $pipe($input));
$this->assertSame([1, ["a" => 1], 2], $pipe($input));
}
}
24 changes: 24 additions & 0 deletions tests/Transducer/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,28 @@ public function test()
$pipe2 = pipe($times2);
$this->assertSame([2, 4, 6], $pipe2([1, 2, 3]));
}
public function testNull()
{
$identity = map(static function ($v) {
return $v;
});
$pipe2 = pipe($identity);
$this->assertSame([1, null, 2], $pipe2([1, null, 2]));
}
public function testEmpty()
{
$identity = map(static function ($v) {
return $v;
});
$pipe2 = pipe($identity);
$this->assertNull($pipe2([]));
}
public function testOneValue()
{
$identity = map(static function ($v) {
return $v;
});
$pipe2 = pipe($identity);
$this->assertSame([null, 2], $pipe2([null, 2]));
}
}

0 comments on commit fbdb856

Please sign in to comment.