53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* Copyright 2026 Kacper Donat
|
|
*
|
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
|
*
|
|
* Full license available in separate LICENSE file
|
|
*/
|
|
|
|
use Kadet\Functional as f;
|
|
use function Kadet\Functional\Collections\Internal\callback;
|
|
use const Kadet\Functional\_;
|
|
|
|
class CallbackTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
public function testInternalFunctionGetsValueOnly()
|
|
{
|
|
$this->assertSame('a', callback(trim(...))(' a ', 'xyz'));
|
|
}
|
|
|
|
public function testInternalStringCallableGetsValueOnly()
|
|
{
|
|
$this->assertSame('A', callback('strtoupper')('a', 'key'));
|
|
}
|
|
|
|
public function testSingleParamClosureGetsValueOnly()
|
|
{
|
|
$this->assertSame([1], callback(function ($v) { return func_get_args(); })(1, 'k'));
|
|
}
|
|
|
|
public function testTwoParamClosureGetsKey()
|
|
{
|
|
$this->assertSame('k=v', callback(fn ($v, $k) => "$k=$v")('v', 'k'));
|
|
}
|
|
|
|
public function testVariadicClosureGetsKey()
|
|
{
|
|
$this->assertSame(['v', 'k'], callback(fn (...$args) => $args)('v', 'k'));
|
|
}
|
|
|
|
public function testPartialOfInternalFunctionDoesNotGetKey()
|
|
{
|
|
$this->assertSame('a', callback(f\partial(trim(...), _, 'x'))('xax', 'key'));
|
|
}
|
|
|
|
public function testDecoratorIsCompiled()
|
|
{
|
|
$callback = callback(f\partial(fn ($prefix, $v, $k) => "$prefix$k=$v", '>'));
|
|
|
|
$this->assertSame('>k=v', $callback('v', 'k'));
|
|
}
|
|
}
|