Skip to content

Commit c5547f9

Browse files
committed
Verify support for initializing properties to closures
1 parent c4b14a4 commit c5547f9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/test/php/lang/ast/unittest/emit/Handle.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct($id) { $this->id= $id; }
1616

1717
public function redirect($id) {
1818
$this->id= $id;
19+
return $this;
1920
}
2021

2122
public function read($bytes= 8192) {

src/test/php/lang/ast/unittest/emit/InitializeWithExpressionsTest.class.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<?php namespace lang\ast\unittest\emit;
22

33
use lang\IllegalArgumentException;
4-
use unittest\{Assert, Expect, Test, Values};
4+
use unittest\{Assert, Test, Values};
55

66
/**
77
* Initialize parameters and properties with arbitrary expressions
88
*
99
* @see https://github.com/xp-framework/compiler/pull/104
1010
* @see https://wiki.php.net/rfc/new_in_initializers
11+
* @see https://wiki.php.net/rfc/calls_in_constant_expressions
1112
*/
1213
class InitializeWithExpressionsTest extends EmittingTest {
1314

1415
/** @return iterable */
1516
private function expressions() {
1617
yield ['"test"', 'test'];
1718
yield ['[1, 2, 3]', [1, 2, 3]];
19+
yield ['1 << 2', 1 << 2];
20+
yield ['strlen("Test")', strlen('Test')];
1821
yield ['MODIFIER_PUBLIC', MODIFIER_PUBLIC];
1922
yield ['self::INITIAL', 'initial'];
2023
yield ['Handle::$DEFAULT', Handle::$DEFAULT];
@@ -34,6 +37,18 @@ public function run() {
3437
}', $code)));
3538
}
3639

40+
#[Test]
41+
public function using_functions() {
42+
$r= $this->run('class <T> {
43+
private $h= fn($arg) => $arg->redirect(1);
44+
45+
public function run() {
46+
return $this->h;
47+
}
48+
}');
49+
Assert::equals(new Handle(1), $r(new Handle(0)));
50+
}
51+
3752
#[Test]
3853
public function property_initialization_accessible_inside_constructor() {
3954
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {

0 commit comments

Comments
 (0)