Skip to content

Commit c4b14a4

Browse files
committed
Add tests for more expressions used as property initializer
1 parent 8f2c8fc commit c4b14a4

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
/** Used by `UsingTest` */
66
class Handle implements \IDisposable {
7+
public static $DEFAULT;
78
public static $called= [];
89
private $id;
910

11+
static function __static() {
12+
self::$DEFAULT= new Handle(0);
13+
}
14+
1015
public function __construct($id) { $this->id= $id; }
1116

1217
public function redirect($id) {

src/test/php/lang/ast/unittest/emit/InitializeWithNewTest.class.php renamed to src/test/php/lang/ast/unittest/emit/InitializeWithExpressionsTest.class.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,34 @@
44
use unittest\{Assert, Expect, Test, Values};
55

66
/**
7-
* New in initializers
7+
* Initialize parameters and properties with arbitrary expressions
88
*
9+
* @see https://github.com/xp-framework/compiler/pull/104
910
* @see https://wiki.php.net/rfc/new_in_initializers
1011
*/
11-
class InitializeWithNewTest extends EmittingTest {
12+
class InitializeWithExpressionsTest extends EmittingTest {
13+
14+
/** @return iterable */
15+
private function expressions() {
16+
yield ['"test"', 'test'];
17+
yield ['[1, 2, 3]', [1, 2, 3]];
18+
yield ['MODIFIER_PUBLIC', MODIFIER_PUBLIC];
19+
yield ['self::INITIAL', 'initial'];
20+
yield ['Handle::$DEFAULT', Handle::$DEFAULT];
21+
yield ['new Handle(0)', new Handle(0)];
22+
yield ['[new Handle(0)]', [new Handle(0)]];
23+
}
1224

13-
#[Test]
14-
public function property() {
15-
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {
16-
private $h= new Handle(0);
25+
#[Test, Values('expressions')]
26+
public function property($code, $expected) {
27+
Assert::equals($expected, $this->run(sprintf('use lang\ast\unittest\emit\Handle; class <T> {
28+
const INITIAL= "initial";
29+
private $h= %s;
1730
1831
public function run() {
1932
return $this->h;
2033
}
21-
}');
22-
Assert::equals(new Handle(0), $r);
34+
}', $code)));
2335
}
2436

2537
#[Test]

0 commit comments

Comments
 (0)