From 5bf80569ec2a8f756d168ef10eeb4a3c8f1efeb3 Mon Sep 17 00:00:00 2001 From: azjezz Date: Thu, 26 Dec 2019 15:45:26 +0100 Subject: [PATCH] [Arr] increase code coverage --- src/Psl/Arr/sort_with_keys_by.php | 26 ++++++++++++++------------ tests/Psl/Arr/RandomTest.php | 8 ++++++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Psl/Arr/sort_with_keys_by.php b/src/Psl/Arr/sort_with_keys_by.php index a1d59ec..91800b0 100644 --- a/src/Psl/Arr/sort_with_keys_by.php +++ b/src/Psl/Arr/sort_with_keys_by.php @@ -40,19 +40,21 @@ function sort_with_keys_by(iterable $iterable, callable $scalar_func, ?callable fn ($a, $b): int => $a[0] <=> $b[0]; } - $result = Iter\map_with_key( - sort_with_keys( - Iter\map( - $iterable, - /** - * @psalm-param Tv $value - * - * @psalm-return array{0: Ts, 1: Tv} - */ - fn ($value) => [$scalar_func($value), $value], - ), - $tuple_comparator + $sorted = sort_with_keys( + Iter\map( + $iterable, + /** + * @psalm-param Tv $value + * + * @psalm-return array{0: Ts, 1: Tv} + */ + fn ($value) => [$scalar_func($value), $value], ), + $tuple_comparator + ); + + $result = Iter\map_with_key( + $sorted, /** * @psalm-param Tk $k * @psalm-param array{0: Ts, 1: Tv} $v diff --git a/tests/Psl/Arr/RandomTest.php b/tests/Psl/Arr/RandomTest.php index 158a73e..0be13c1 100644 --- a/tests/Psl/Arr/RandomTest.php +++ b/tests/Psl/Arr/RandomTest.php @@ -19,6 +19,14 @@ class RandomTest extends TestCase static::assertContains($value, $values); } + public function testRandomWithOneItem(): void + { + $values = ['a']; + $value = Arr\random($values); + + static::assertSame('a', $value); + } + public function testRandomThrowsWhenTheGivenArrayIsEmpty(): void { $this->expectException(Exception\InvariantViolationException::class);