-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement values as complement to annotations
See xp-framework/rfc#336, values are key/value pairs that can be attached to types and type members. They serve as a complement to PHP 8 attributes which do not support arbitrary expressions in their argument lists.
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php namespace lang\ast\unittest\parse; | ||
|
||
use lang\ast\nodes\{Annotated, Literal, LambdaExpression}; | ||
use unittest\Assert; | ||
|
||
/** @see https://github.com/xp-framework/rfc/issues/336 */ | ||
class ValuesTest extends ParseTest { | ||
|
||
/** | ||
* Parses source into type | ||
* | ||
* @param string $source | ||
* @return lang.ast.TypeDeclaration | ||
*/ | ||
private function type($source) { | ||
return $this->parse($source)->tree()->type('T'); | ||
} | ||
|
||
/** | ||
* Assertion helper | ||
* | ||
* @param var $expected | ||
* @param lang.ast.Node $node | ||
* @throws unittest.AssertionFailedError | ||
* @return void | ||
*/ | ||
private function assertValues($expected, $node) { | ||
Assert::equals($expected, cast($node, Annotated::class)->values); | ||
} | ||
|
||
#[@test] | ||
public function on_class() { | ||
$this->assertValues( | ||
['using' => new Literal('"value"', self::LINE)], | ||
$this->type('#$using: "value" class T { }') | ||
); | ||
} | ||
|
||
#[@test] | ||
public function on_constant() { | ||
$this->assertValues( | ||
['using' => new Literal('"value"', self::LINE)], | ||
$this->type('class T { #$using: "value" const FIXTURE = "test"; }')->constant('FIXTURE') | ||
); | ||
} | ||
|
||
#[@test] | ||
public function on_property() { | ||
$this->assertValues( | ||
['using' => new Literal('"value"', self::LINE)], | ||
$this->type('class T { #$using: "value" public $fixture; }')->property('fixture') | ||
); | ||
} | ||
|
||
#[@test] | ||
public function on_method() { | ||
$this->assertValues( | ||
['using' => new Literal('"value"', self::LINE)], | ||
$this->type('class T { #$using: "value" public function fixture() { } }')->method('fixture') | ||
); | ||
} | ||
|
||
#[@test] | ||
public function values_on_multiple_lines() { | ||
$this->assertValues( | ||
['author' => new Literal('"test"', self::LINE + 1), 'version' => new Literal('1', self::LINE + 2)], | ||
$this->type(' | ||
#$author: "test" | ||
#$version: 1 | ||
class T { } | ||
') | ||
); | ||
} | ||
|
||
#[@test] | ||
public function with_function() { | ||
$type= $this->type(' | ||
#$using: fn() => version_compare(PHP_VERSION, "7.0.0", ">=") | ||
class T { } | ||
'); | ||
Assert::instance(LambdaExpression::class, cast($type, Annotated::class)->values['using']); | ||
} | ||
} |