From 08b853053017a3c1fbac8bbb56a1d4356c4d274a Mon Sep 17 00:00:00 2001 From: Kris Jordan Date: Sun, 30 Aug 2009 16:24:33 -0400 Subject: [PATCH] Annotation out of quarantine, and key methods tested. --- .../recess/lang/Annotation.class.php | 22 ++--- recess/test/recess/lang/AnnotationTest.php | 83 +++++++++++++++++++ .../recess/lang/DummyAnnotation.class.php | 58 +++++++++++++ 3 files changed, 152 insertions(+), 11 deletions(-) rename recess/{quarantine => }/recess/lang/Annotation.class.php (92%) create mode 100644 recess/test/recess/lang/AnnotationTest.php create mode 100644 recess/test/recess/lang/DummyAnnotation.class.php diff --git a/recess/quarantine/recess/lang/Annotation.class.php b/recess/recess/lang/Annotation.class.php similarity index 92% rename from recess/quarantine/recess/lang/Annotation.class.php rename to recess/recess/lang/Annotation.class.php index 16e1467..473ce61 100644 --- a/recess/quarantine/recess/lang/Annotation.class.php +++ b/recess/recess/lang/Annotation.class.php @@ -1,9 +1,6 @@ parameters = array_change_key_case($parameters, CASE_LOWER); - } - function isAValue($value) { return in_array($value, $this->values); } @@ -250,6 +244,13 @@ function expandAnnotation($class, $reflection, $descriptor) { $this->expand($class, $reflection, $descriptor); } + /** + * Initialize the parameters of the annotation by lowering key case + * @param $parameters + */ + function init($parameters) { + $this->parameters = array_change_key_case($parameters, CASE_LOWER); + } /** * Given a docstring, returns an array of Recess Annotations. @@ -285,7 +286,7 @@ static function parse($docstring) { @eval('$array = ' . $value); if(!isset($array)) { - throw new InvalidAnnotationValueException('There is an unparseable annotation value: "!' . $annotation . ': ' . $values[$key] . '"',0,0,'',0,array()); + throw new \Exception('There is an unparseable annotation value: "!' . $annotation . ': ' . $values[$key] . '"'); } $annotationClass = $annotation . 'Annotation'; @@ -293,8 +294,7 @@ static function parse($docstring) { $annotation = new self::$registeredAnnotations[$annotationClass]; $annotation->init($array); } else { - print_r(self::$registeredAnnotations); - throw new UnknownAnnotationException('Unknown annotation: "' . $annotation . '"',0,0,'',0,get_defined_vars()); + throw new \Exception('Unknown annotation: "' . $annotation . '" It must be loaded with: "' . $annotation .'"::load()'); } $returns[] = $annotation; diff --git a/recess/test/recess/lang/AnnotationTest.php b/recess/test/recess/lang/AnnotationTest.php new file mode 100644 index 0000000..5b7b2ef --- /dev/null +++ b/recess/test/recess/lang/AnnotationTest.php @@ -0,0 +1,83 @@ +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); + } + } + +} \ No newline at end of file diff --git a/recess/test/recess/lang/DummyAnnotation.class.php b/recess/test/recess/lang/DummyAnnotation.class.php new file mode 100644 index 0000000..73aaaf3 --- /dev/null +++ b/recess/test/recess/lang/DummyAnnotation.class.php @@ -0,0 +1,58 @@ +