11<?php namespace lang \ast \unittest \parse ;
22
3- use lang \ast \nodes \{BinaryExpression , ClosureExpression , Literal , Parameter , ReturnStatement , Signature , Variable };
3+ use lang \ast \nodes \{
4+ BinaryExpression ,
5+ Braced ,
6+ ClosureExpression ,
7+ InvokeExpression ,
8+ Literal ,
9+ Parameter ,
10+ ReturnStatement ,
11+ Signature ,
12+ Variable
13+ };
414use lang \ast \{FunctionType , Type };
515use test \{Assert , Before , Test };
616
717class ClosuresTest extends ParseTest {
8- private $ returns ;
18+ private $ returns, $ invoke ;
919
1020 #[Before]
1121 public function returns () {
@@ -20,6 +30,15 @@ public function returns() {
2030 );
2131 }
2232
33+ #[Before]
34+ public function invoke () {
35+ $ this ->invoke = new InvokeExpression (
36+ new Literal ('var_dump ' , self ::LINE ),
37+ [new Literal ('true ' , self ::LINE )],
38+ self ::LINE
39+ );
40+ }
41+
2342 #[Test]
2443 public function with_body () {
2544 $ this ->assertParsed (
@@ -64,24 +83,40 @@ public function with_use_by_reference() {
6483 #[Test]
6584 public function with_return_type () {
6685 $ this ->assertParsed (
67- [new ClosureExpression (new Signature ([], new Type ('int ' ), false , self ::LINE ), null , [$ this ->returns ], false , self ::LINE )],
68- 'function(): int { return $a + 1 ; }; '
86+ [new ClosureExpression (new Signature ([], new Type ('int ' ), false , self ::LINE ), null , [$ this ->invoke ], false , self ::LINE )],
87+ 'function(): int { var_dump(true) ; }; '
6988 );
7089 }
7190
7291 #[Test]
7392 public function with_nullable_return_type () {
7493 $ this ->assertParsed (
75- [new ClosureExpression (new Signature ([], new Type ('?int ' ), false , self ::LINE ), null , [$ this ->returns ], false , self ::LINE )],
76- 'function(): ?int { return $a + 1 ; }; '
94+ [new ClosureExpression (new Signature ([], new Type ('?int ' ), false , self ::LINE ), null , [$ this ->invoke ], false , self ::LINE )],
95+ 'function(): ?int { var_dump(true) ; }; '
7796 );
7897 }
7998
8099 #[Test]
81100 public function static_function () {
82101 $ this ->assertParsed (
83- [new ClosureExpression (new Signature ([], null , false , self ::LINE ), null , [$ this ->returns ], true , self ::LINE )],
84- 'static function() { return $a + 1; }; '
102+ [new ClosureExpression (new Signature ([], null , false , self ::LINE ), null , [$ this ->invoke ], true , self ::LINE )],
103+ 'static function() { var_dump(true); }; '
104+ );
105+ }
106+
107+ /** @see https://github.com/xp-framework/ast/issues/60 */
108+ #[Test]
109+ public function iife_with_statement () {
110+ $ this ->assertParsed (
111+ [new InvokeExpression (
112+ new Braced (
113+ new ClosureExpression (new Signature ([], null , false , self ::LINE ), null , [$ this ->invoke ], false , self ::LINE ),
114+ self ::LINE
115+ ),
116+ [],
117+ self ::LINE
118+ )],
119+ '(function() { var_dump(true); })(); '
85120 );
86121 }
87122}
0 commit comments