Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Annotation out of quarantine, and key methods tested.
- Loading branch information
1 parent
8139d2d
commit 08b8530
Showing
3 changed files
with
152 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
use made\up\space; | ||
use recess\lang\Annotation; | ||
|
||
require_once 'DummyAnnotation.class.php'; | ||
use made\up\space\DummyAnnotation; | ||
|
||
class AnnotationTest extends PHPUnit_Framework_TestCase { | ||
|
||
function testSimpleParse() { | ||
DummyAnnotation::load(); | ||
$docstring = "/** !Dummy */"; | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertEquals(1,count($annotations)); | ||
$this->assertEquals(array(new DummyAnnotation), $annotations); | ||
} | ||
|
||
function testParamsParse() { | ||
DummyAnnotation::load(); | ||
$docstring = "/** !Dummy a, b, c */"; | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertEquals(1, count($annotations)); | ||
$expected = new DummyAnnotation; | ||
$expected->parameters = array('a','b','c'); | ||
$this->assertEquals(array($expected), $annotations); | ||
} | ||
|
||
function testKeyValParamsParse() { | ||
DummyAnnotation::load(); | ||
$docstring = "/** !Dummy a: b, c: d */"; | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertEquals(1, count($annotations)); | ||
$expected = new DummyAnnotation; | ||
$expected->parameters = array('a'=>'b','c'=>'d'); | ||
$this->assertEquals(array($expected), $annotations); | ||
} | ||
|
||
function testSubArrayParse() { | ||
DummyAnnotation::load(); | ||
$docstring = "/** !Dummy a: (b: c, d, e, f: g), h */"; | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertEquals(1, count($annotations)); | ||
$expected = new DummyAnnotation; | ||
$expected->parameters = array('a'=>array('b'=>'c','d','e','f'=>'g'),'h'); | ||
$this->assertEquals(array($expected), $annotations); | ||
} | ||
|
||
function testMultiParse() { | ||
DummyAnnotation::load(); | ||
$docstring = "/** | ||
* !Dummy a, b, c | ||
* !Dummy d, e, f | ||
*/"; | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertEquals(2, count($annotations)); | ||
$expectedA = new DummyAnnotation; | ||
$expectedA->parameters = array('a','b','c'); | ||
$expectedB = new DummyAnnotation; | ||
$expectedB->parameters = array('d','e','f'); | ||
$this->assertEquals(array($expectedA,$expectedB), $annotations); | ||
} | ||
|
||
function testUnknownAnnotation() { | ||
$docstring = "/** !Foo */"; | ||
try { | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertTrue(false); | ||
} catch (Exception $e) { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
function testInvalidAnnotation() { | ||
$docstring = "/** !Foo 'bar */"; | ||
try { | ||
$annotations = Annotation::parse($docstring); | ||
$this->assertTrue(false); | ||
} catch (Exception $e) { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
} |
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,58 @@ | ||
<?php | ||
namespace made\up\space; | ||
use recess\lang\Annotation; | ||
class DummyAnnotation extends Annotation { | ||
/* Begin abstract methods */ | ||
|
||
/** | ||
* Returns a string representation of the intended usage of an annotation. | ||
* | ||
* @return string | ||
*/ | ||
public function usage() { | ||
return ""; | ||
} | ||
|
||
/** | ||
* Returns an integer representation of the type(s) of PHP language constructs | ||
* the annotation is applicable to. Use the Annotation::FOR_* consts to return | ||
* the desired result. | ||
* | ||
* Examples: | ||
* // Only valid on classes | ||
* function isFor() { return Annotation::FOR_CLASS; } | ||
* | ||
* // Valid on methods or properties | ||
* function isFor() { return Annotation::FOR_METHOD | Annotation::FOR_PROPERTY; } | ||
* | ||
* @return integer | ||
*/ | ||
public function isFor() { | ||
return Annotation::FOR_CLASS; | ||
} | ||
|
||
/** | ||
* Validate is called just before expansion. Because there may be multiple | ||
* constraints of an annotation the implementation of validate should append | ||
* any error messages to the protected $errors property. Commonly used validations | ||
* helper methods are provided as protected methods on the Annotation class. | ||
* | ||
* @param $class The classname the annotation is on. | ||
*/ | ||
protected function validate($class) { | ||
return true; | ||
} | ||
|
||
/** | ||
* The expansion step of an annotation gives it an opportunity to manipulate | ||
* a class' descriptor by introducing additional metadata, attach methods, and | ||
* wrap methods. | ||
* | ||
* @param string $class Classname the annotation is applied to. | ||
* @param mixed $reflection The Reflection(Class|Method|Property) object the annotation is applied to. | ||
* @param ClassDescriptor $descriptor The ClassDescriptor being manipulated. | ||
*/ | ||
protected function expand($class, $reflection, $descriptor) { | ||
|
||
} | ||
} |