nur-sery/tests/sys/funcTest.php

336 lines
13 KiB
PHP

<?php
namespace {
function func36(): int { return 36; }
function func_m1($a): array { return [$a]; }
function func_o1($b=9): array { return [$b]; }
function func_v(...$c): array { return [...$c]; }
function func_m1o1($a, $b=9): array { return [$a, $b]; }
function func_m1v($a, ...$c): array { return [$a, ...$c]; }
function func_m1o1v($a, $b=9, ...$c): array { return [$a, $b, ...$c]; }
function func_o1v($b=9, ...$c): array { return [$b, ...$c]; }
}
namespace nur\sery\sys {
use nulib\tests\TestCase;
class funcTest extends TestCase {
function testIs_static() {
self::assertFalse(func::is_static(null));
self::assertFalse(func::is_static(""));
self::assertFalse(func::is_static("::"));
self::assertFalse(func::is_static("xxx::"));
self::assertTrue(func::is_static("::xxx"));
self::assertFalse(func::is_static([]));
self::assertFalse(func::is_static([""]));
self::assertTrue(func::is_static(["xxx"]));
self::assertFalse(func::is_static([null, ""]));
self::assertTrue(func::is_static([null, "yyy"]));
self::assertFalse(func::is_static(["xxx", ""]));
self::assertTrue(func::is_static(["xxx", "yyy"]));
self::assertTrue(func::is_static([null, "yyy", "aaa"]));
self::assertTrue(func::is_static(["xxx", "yyy", "aaa"]));
}
function testFix_static() {
$class = "class";
$func = null;
func::fix_static($func, $class);
self::assertSame(null, $func);
$func = "";
func::fix_static($func, $class);
self::assertSame("", $func);
$func = "::";
func::fix_static($func, $class);
self::assertSame("class::", $func);
$func = "xxx::";
func::fix_static($func, $class);
self::assertSame("xxx::", $func);
$func = "::xxx";
func::fix_static($func, $class);
self::assertSame("class::xxx", $func);
$func = [];
func::fix_static($func, $class);
self::assertSame([], $func);
$func = [""];
func::fix_static($func, $class);
self::assertSame(["class", ""], $func);
$func = ["xxx"];
func::fix_static($func, $class);
self::assertSame(["class", "xxx"], $func);
$func = ["xxx", ""];
func::fix_static($func, $class);
self::assertSame(["xxx", ""], $func);
$func = [null, "yyy"];
func::fix_static($func, $class);
self::assertSame(["class", "yyy"], $func);
$func = ["xxx", "yyy"];
func::fix_static($func, $class);
self::assertSame(["xxx", "yyy"], $func);
$func = [null, "yyy", "aaa"];
func::fix_static($func, $class);
self::assertSame(["class", "yyy", "aaa"], $func);
$func = ["xxx", "yyy", "aaa"];
func::fix_static($func, $class);
self::assertSame(["xxx", "yyy", "aaa"], $func);
}
function testIs_method() {
self::assertFalse(func::is_method(null));
self::assertFalse(func::is_method(""));
self::assertFalse(func::is_method("->"));
self::assertTrue(func::is_method("->xxx"));
self::assertFalse(func::is_method([]));
self::assertFalse(func::is_method([""]));
self::assertTrue(func::is_method(["->xxx"]));
self::assertTrue(func::is_method(["->xxx", "aaa"]));
self::assertFalse(func::is_method([null, "->"]));
self::assertTrue(func::is_method([null, "->yyy"]));
self::assertFalse(func::is_method(["xxx", "->"]));
self::assertTrue(func::is_method(["xxx", "->yyy"]));
self::assertTrue(func::is_method([null, "->yyy", "aaa"]));
self::assertTrue(func::is_method(["xxx", "->yyy", "aaa"]));
}
function testFix_method() {
$object = new \stdClass();
$func= null;
func::fix_method($func, $object);
self::assertSame(null, $func);
$func= "";
func::fix_method($func, $object);
self::assertSame("", $func);
$func= "->";
func::fix_method($func, $object);
self::assertSame("->", $func);
$func= "->xxx";
func::fix_method($func, $object);
self::assertSame([$object, "xxx"], $func);
$func= [];
func::fix_method($func, $object);
self::assertSame([], $func);
$func= [""];
func::fix_method($func, $object);
self::assertSame([""], $func);
$func= ["->xxx"];
func::fix_method($func, $object);
self::assertSame([$object, "xxx"], $func);
$func= ["->xxx", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "xxx", "aaa"], $func);
$func= [null, "->"];
func::fix_method($func, $object);
self::assertSame([null, "->"], $func);
$func= [null, "->yyy"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy"], $func);
$func= ["xxx", "->"];
func::fix_method($func, $object);
self::assertSame(["xxx", "->"], $func);
$func= ["xxx", "->yyy"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy"], $func);
$func= [null, "->yyy", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy", "aaa"], $func);
$func= ["xxx", "->yyy", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy", "aaa"], $func);
}
function testCall() {
self::assertSame(36, func::call("func36"));
self::assertSame(12, func::call(TC::class."::method"));
self::assertSame(12, func::call([TC::class, "method"]));
$closure = function() {
return 21;
};
self::assertSame(21, func::call($closure));
}
function test_prepare_fill() {
# vérifier que les arguments sont bien remplis, en fonction du fait qu'ils
# soient obligatoires, facultatifs ou variadiques
# m1
self::assertSame([null], func::call("func_m1"));
self::assertSame([null], func::call("func_m1", null));
self::assertSame([null], func::call("func_m1", null, null));
self::assertSame([null], func::call("func_m1", null, null, null));
self::assertSame([null], func::call("func_m1", null, null, null, null));
self::assertSame([1], func::call("func_m1", 1));
self::assertSame([1], func::call("func_m1", 1, 2));
self::assertSame([1], func::call("func_m1", 1, 2, 3));
self::assertSame([1], func::call("func_m1", 1, 2, 3, 4));
# o1
self::assertSame([9], func::call("func_o1"));
self::assertSame([null], func::call("func_o1", null));
self::assertSame([null], func::call("func_o1", null, null));
self::assertSame([null], func::call("func_o1", null, null, null));
self::assertSame([null], func::call("func_o1", null, null, null, null));
self::assertSame([1], func::call("func_o1", 1));
self::assertSame([1], func::call("func_o1", 1, 2));
self::assertSame([1], func::call("func_o1", 1, 2, 3));
self::assertSame([1], func::call("func_o1", 1, 2, 3, 4));
# v
self::assertSame([], func::call("func_v"));
self::assertSame([null], func::call("func_v", null));
self::assertSame([null, null], func::call("func_v", null, null));
self::assertSame([null, null, null], func::call("func_v", null, null, null));
self::assertSame([null, null, null, null], func::call("func_v", null, null, null, null));
self::assertSame([1], func::call("func_v", 1));
self::assertSame([1, 2], func::call("func_v", 1, 2));
self::assertSame([1, 2, 3], func::call("func_v", 1, 2, 3));
self::assertSame([1, 2, 3, 4], func::call("func_v", 1, 2, 3, 4));
# m1o1
self::assertSame([null, 9], func::call("func_m1o1"));
self::assertSame([null, 9], func::call("func_m1o1", null));
self::assertSame([null, null], func::call("func_m1o1", null, null));
self::assertSame([null, null], func::call("func_m1o1", null, null, null));
self::assertSame([null, null], func::call("func_m1o1", null, null, null, null));
self::assertSame([1, 9], func::call("func_m1o1", 1));
self::assertSame([1, 2], func::call("func_m1o1", 1, 2));
self::assertSame([1, 2], func::call("func_m1o1", 1, 2, 3));
self::assertSame([1, 2], func::call("func_m1o1", 1, 2, 3, 4));
# m1v
self::assertSame([null], func::call("func_m1v"));
self::assertSame([null], func::call("func_m1v", null));
self::assertSame([null, null], func::call("func_m1v", null, null));
self::assertSame([null, null, null], func::call("func_m1v", null, null, null));
self::assertSame([null, null, null, null], func::call("func_m1v", null, null, null, null));
self::assertSame([1], func::call("func_m1v", 1));
self::assertSame([1, 2], func::call("func_m1v", 1, 2));
self::assertSame([1, 2, 3], func::call("func_m1v", 1, 2, 3));
self::assertSame([1, 2, 3, 4], func::call("func_m1v", 1, 2, 3, 4));
# m1o1v
self::assertSame([null, 9], func::call("func_m1o1v"));
self::assertSame([null, 9], func::call("func_m1o1v", null));
self::assertSame([null, null], func::call("func_m1o1v", null, null));
self::assertSame([null, null, null], func::call("func_m1o1v", null, null, null));
self::assertSame([null, null, null, null], func::call("func_m1o1v", null, null, null, null));
self::assertSame([1, 9], func::call("func_m1o1v", 1));
self::assertSame([1, 2], func::call("func_m1o1v", 1, 2));
self::assertSame([1, 2, 3], func::call("func_m1o1v", 1, 2, 3));
self::assertSame([1, 2, 3, 4], func::call("func_m1o1v", 1, 2, 3, 4));
# o1v
self::assertSame([9], func::call("func_o1v"));
self::assertSame([null], func::call("func_o1v", null));
self::assertSame([null, null], func::call("func_o1v", null, null));
self::assertSame([null, null, null], func::call("func_o1v", null, null, null));
self::assertSame([null, null, null, null], func::call("func_o1v", null, null, null, null));
self::assertSame([1], func::call("func_o1v", 1));
self::assertSame([1, 2], func::call("func_o1v", 1, 2));
self::assertSame([1, 2, 3], func::call("func_o1v", 1, 2, 3));
self::assertSame([1, 2, 3, 4], func::call("func_o1v", 1, 2, 3, 4));
}
function testCall_all() {
$c1 = new C1();
$c2 = new C2();
$c3 = new C3();
self::assertSameValues([11, 12], func::call_all(C1::class));
self::assertSameValues([11, 12, 21, 22], func::call_all($c1));
self::assertSameValues([13, 11, 12], func::call_all(C2::class));
self::assertSameValues([13, 23, 11, 12, 21, 22], func::call_all($c2));
self::assertSameValues([111, 13, 12], func::call_all(C3::class));
self::assertSameValues([111, 121, 13, 23, 12, 22], func::call_all($c3));
$options = "conf";
self::assertSameValues([11], func::call_all(C1::class, $options));
self::assertSameValues([11, 21], func::call_all($c1, $options));
self::assertSameValues([11], func::call_all(C2::class, $options));
self::assertSameValues([11, 21], func::call_all($c2, $options));
self::assertSameValues([111], func::call_all(C3::class, $options));
self::assertSameValues([111, 121], func::call_all($c3, $options));
$options = ["prefix" => "conf"];
self::assertSameValues([11], func::call_all(C1::class, $options));
self::assertSameValues([11, 21], func::call_all($c1, $options));
self::assertSameValues([11], func::call_all(C2::class, $options));
self::assertSameValues([11, 21], func::call_all($c2, $options));
self::assertSameValues([111], func::call_all(C3::class, $options));
self::assertSameValues([111, 121], func::call_all($c3, $options));
self::assertSameValues([11, 12], func::call_all($c1, ["include" => "x"]));
self::assertSameValues([11, 21], func::call_all($c1, ["include" => "y"]));
self::assertSameValues([11, 12, 21], func::call_all($c1, ["include" => ["x", "y"]]));
self::assertSameValues([21, 22], func::call_all($c1, ["exclude" => "x"]));
self::assertSameValues([12, 22], func::call_all($c1, ["exclude" => "y"]));
self::assertSameValues([22], func::call_all($c1, ["exclude" => ["x", "y"]]));
self::assertSameValues([12], func::call_all($c1, ["include" => "x", "exclude" => "y"]));
}
function testCons() {
$obj1 = func::cons(WoCons::class, 1, 2, 3);
self::assertInstanceOf(WoCons::class, $obj1);
$obj2 = func::cons(WithEmptyCons::class, 1, 2, 3);
self::assertInstanceOf(WithEmptyCons::class, $obj2);
$obj3 = func::cons(WithCons::class, 1, 2, 3);
self::assertInstanceOf(WithCons::class, $obj3);
self::assertSame(1, $obj3->first);
}
}
class WoCons {
}
class WithEmptyCons {
function __construct() {
}
}
class WithCons {
public $first;
function __construct($first) {
$this->first = $first;
}
}
class TC {
static function method() {
return 12;
}
}
class C1 {
static function confps1_xy() {
return 11;
}
static function ps2_x() {
return 12;
}
function confp1_y() {
return 21;
}
function p2() {
return 22;
}
}
class C2 extends C1 {
static function ps3() {
return 13;
}
function p3() {
return 23;
}
}
class C3 extends C2 {
static function confps1_xy() {
return 111;
}
function confp1_y() {
return 121;
}
}
}