-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.lambda.php
108 lines (97 loc) · 3.54 KB
/
test.lambda.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* This file is part of the Ariadne Component Library.
*
* (c) Muze <info@muze.nl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once( __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php' );
class TestLambda extends UnitTestCase {
function testPrototype() {
$view = \arc\lambda::prototype( [
'foo' => 'bar',
'bar' => function() {
return $this->foo;
}
] );
$this->assertTrue( $view->foo === 'bar' );
$this->assertTrue( $view->bar() === 'bar' );
}
function testPrototypeInheritance() {
$foo = \arc\lambda::prototype( [
'foo' => 'bar',
'bar' => function() {
return $this->foo;
}
]);
$bar = $foo->extend( [
'foo' => 'rab'
]);
$this->assertTrue( $foo->foo === 'bar' );
$this->assertTrue( $bar->foo === 'rab' );
$this->assertTrue( $foo->bar() === 'bar' );
$this->assertTrue( $bar->bar() === 'rab' );
$this->assertTrue( $bar->hasOwnProperty('foo') );
$this->assertFalse( $bar->hasOwnProperty('bar') );
}
function testPrototypeInheritance2() {
$foo = \arc\lambda::prototype([
'bar' => function() {
return 'bar';
}
]);
$bar = $foo->extend([
'bar' => function() use ( $foo ) {
return 'foo'.$foo->bar();
}
]);
$this->assertTrue( $bar->bar() === 'foobar' );
}
function testPrototypeInheritance3() {
$foo = \arc\lambda::prototype([
'bar' => function() {
return 'bar';
},
'foo' => function() {
return '<b>'.$this->bar().'</b>';
}
]);
$bar = $foo->extend([
'bar' => function() use ( $foo ) {
return 'foo'.$foo->bar();
}
]);
$this->assertTrue( $bar->foo() === '<b>foobar</b>' );
}
function testSingleton() {
$bar = \arc\lambda::singleton( function() {
return 'bar' . time();
} );
$baz = \arc\lambda::singleton( function() {
return 'baz';
} );
$test1 = $bar();
sleep(1);
$test2 = $bar();
$this->assertTrue( $test1 == $test2 );
$this->assertTrue( $baz() == 'baz' );
}
function testPartial() {
$bar = function( $x, $y, $z, $q=1 ) {
return [ 'x' => $x, 'y' => $y, 'z' => $z, 'q' => $q];
};
$baz = \arc\lambda::partial( $bar, [ 0 => 'x', 2 => 'z' ] );
$result = $baz( 'y' );
$this->assertTrue( $result == [ 'x' => 'x', 'y' => 'y', 'z' => 'z', 'q' => 1 ] );
}
function testPartialPartial() {
$bar = function( $x, $y, $z='z', $q=1 ) {
return [ 'x' => $x, 'y' => $y, 'z' => $z, 'q' => $q];
};
$baz = \arc\lambda::partial( $bar, [ 0 => 'x', 3 => 'q' ], [ 2 => 'z' ] );
$result = $baz( 'y' );
$this->assertTrue( $result == [ 'x' => 'x', 'y' => 'y', 'z' => 'z', 'q' => 'q' ] );
}
}