endtoend-test-psl/tests/Psl/Math/MaxByTest.php

54 lines
1.0 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Math;
use Generator;
2019-12-24 01:52:07 +01:00
use PHPUnit\Framework\TestCase;
2020-08-27 16:44:22 +02:00
use Psl\Arr;
2019-12-26 21:03:29 +01:00
use Psl\Math;
use Psl\Str;
2019-12-24 01:52:07 +01:00
final class MaxByTest extends TestCase
2019-12-24 01:52:07 +01:00
{
2019-12-26 21:03:29 +01:00
/**
* @dataProvider provideData
*/
2020-08-27 16:44:22 +02:00
public function testMaxBy($expected, array $values, callable $fun): void
2019-12-26 21:03:29 +01:00
{
static::assertSame($expected, Math\max_by($values, $fun));
2019-12-26 21:03:29 +01:00
}
public function provideData(): Generator
2019-12-26 21:03:29 +01:00
{
yield [
'bazqux',
['foo', 'bar', 'baz', 'qux', 'foobar', 'bazqux'],
static fn ($value) => Str\length($value),
];
2019-12-26 21:03:29 +01:00
yield [
['foo', 'bar', 'baz'],
2019-12-26 21:03:29 +01:00
[
['foo'],
['foo', 'bar'],
['foo', 'bar', 'baz']
2019-12-26 21:03:29 +01:00
],
static fn ($arr) => Arr\count($arr),
];
2019-12-26 21:03:29 +01:00
yield [
9,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
static fn ($i) => $i,
];
2019-12-26 21:03:29 +01:00
yield [
null,
[],
static fn ($i) => $i,
2019-12-26 21:03:29 +01:00
];
}
2019-12-24 01:52:07 +01:00
}