Skip to content

Commit

Permalink
test: Add more tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol committed Oct 12, 2020
1 parent c3c1dd0 commit 0529610
Showing 1 changed file with 134 additions and 4 deletions.
138 changes: 134 additions & 4 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ static function ($item) {
->apply($apply1)
->apply($apply2)
->shouldIterateAs([1, 2, 3, 4, 5, 6]);

$a = (new stdClass());
$b = (new stdClass());
$c = (new stdClass());

$a->prop = 'a';
$b->prop = 'b';
$c->prop = 'c';

$input = [
$a, $b, $c,
];

$this::fromIterable($input)
->apply(
static function ($value, $key): bool {
$value->prop = $key;

return true;
}
)
->map(
static function ($value): int {
return $value->prop;
}
)
->shouldIterateAs([
0,
1,
2,
]);
}

public function it_can_associate(): void
Expand Down Expand Up @@ -176,7 +207,20 @@ public function it_can_be_constructed_from_a_stream(): void
->shouldReturn(1);
}

public function it_can_be_constructed_from_array(): void
public function it_can_be_constructed_from_a_string(): void
{
$this::fromString('izumi')
->getIterator()
->shouldIterateAs([
'i',
'z',
'u',
'm',
'i',
]);
}

public function it_can_be_constructed_from_an_iterable(): void
{
$this
->beConstructedThrough('fromIterable', [range('A', 'E')]);
Expand All @@ -185,6 +229,39 @@ public function it_can_be_constructed_from_array(): void

$this
->shouldIterateAs(['A', 'B', 'C', 'D', 'E']);

$iterable = [
'a',
'b',
'c',
];

$this::fromIterable($iterable)
->getIterator()
->shouldIterateAs([
'a',
'b',
'c',
]);
}

public function it_can_be_constructed_from_an_object(): void
{
$foo = new class() {
private $a = 'a';

private $b = 'b';

private $c = 'c';
};

$this::with($foo)
->normalize()
->shouldIterateAs([
'a',
'b',
'c',
]);
}

public function it_can_be_constructed_from_empty(): void
Expand All @@ -208,11 +285,27 @@ public function it_can_be_constructed_from_nothing(): void
public function it_can_be_constructed_with_a_closure(): void
{
$this
->beConstructedThrough('fromCallable', [static function () {
yield from range(1, 3);
}]);
->beConstructedThrough('fromCallable', [
static function (int $a, int $b): Generator {
return yield from range($a, $b);
},
1,
5,
]);

$this->shouldImplement(Collection::class);

$this
->getIterator()
->shouldIterateAs(
[
1,
2,
3,
4,
5,
]
);
}

public function it_can_be_constructed_with_an_arrayObject(): void
Expand Down Expand Up @@ -507,6 +600,10 @@ public function it_can_current(): void
$this::fromIterable($input)
->current(4)
->shouldReturn(5);

$this::fromIterable(['a'])
->current(0)
->shouldReturn('a');
}

public function it_can_cycle(): void
Expand Down Expand Up @@ -1789,6 +1886,21 @@ public function it_can_random(): void
$this::fromIterable($input)
->random(26)
->shouldNotIterateAs($generator($input));

$input = range('a', 'z');

$generator = static function (array $input): Generator {
yield from $input;
};

$this::fromIterable(['a'])
->random()
->shouldIterateAs(['a']);

$this::fromIterable($input)
->random(0)
->shouldThrow(OutOfBoundsException::class)
->during('all');
}

public function it_can_reduction(): void
Expand Down Expand Up @@ -2612,6 +2724,24 @@ public function it_can_use_range(): void
(float) 0,
(float) 0,
]);

$this::range(1, 5)
->shouldIterateAs([
0 => (float) 1,
1 => (float) 2,
2 => (float) 3,
3 => (float) 4,
]);

$this::range(1, 5, 0)
->limit(5)
->shouldIterateAs([
0 => (float) 1,
1 => (float) 1,
2 => (float) 1,
3 => (float) 1,
4 => (float) 1,
]);
}

public function it_can_use_range_with_value_1(): void
Expand Down

0 comments on commit 0529610

Please sign in to comment.