Skip to content

Commit

Permalink
Added unit tests and made sure the annotation scanner works with Wind…
Browse files Browse the repository at this point in the history
…ows, Linux and Mac line endings.
  • Loading branch information
waltertamboer authored and Ocramius committed Feb 2, 2015
1 parent bf793e7 commit 7d424f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
9 changes: 6 additions & 3 deletions library/Zend/Code/Scanner/AnnotationScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function tokenize()
}

if ($MACRO_HAS_CONTEXT($CONTEXT_CLASS)) {
if (in_array($currentChar, array(' ', '(', "\n"))) {
if (in_array($currentChar, array(' ', '(', "\n", "\r"))) {
$context &= ~$CONTEXT_CLASS;
$MACRO_TOKEN_ADVANCE();
} else {
Expand All @@ -225,8 +225,9 @@ protected function tokenize()
}
}

// Since we don't know what line endings are used in the file, we check for all scenarios. If we find
// a cariage return (\r), we check the next character for a line feed (\n). If so we consume it.
// Since we don't know what line endings are used in the file, we check for all scenarios. If we find a
// cariage return (\r), we check the next character for a line feed (\n). If so we consume it and act as
// if the cariage return was a line feed.
$lineEnded = $currentChar === "\n";
if ($currentChar === "\r") {
$lineEnded = true;
Expand All @@ -235,6 +236,8 @@ protected function tokenize()
if ($nextChar !== "\n") {
$streamIndex--;
}

$currentChar = "\n";
}

if ($lineEnded) {
Expand Down
21 changes: 17 additions & 4 deletions tests/ZendTest/Code/Scanner/AnnotationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

class AnnotationScannerTest extends \PHPUnit_Framework_TestCase
{
public function testScannerWorks()
/**
* @dataProvider scannerWorksDataProvider
*/
public function testScannerWorks($newLine)
{
$annotationManager = new AnnotationManager();
$parser = new GenericAnnotationParser();
Expand All @@ -26,9 +29,10 @@ public function testScannerWorks()
));
$annotationManager->attach($parser);

$docComment = '/**' . "\n"
. ' * @Test\Foo(\'anything I want()' . "\n" . ' * to be\')' . "\n"
. ' * @Test\Bar' . "\n */";
$docComment = '/**' . $newLine
. ' * @Test\Foo(\'anything I want()' . $newLine
. ' * to be\')' . $newLine
. ' * @Test\Bar' . $newLine . " */";

$nameInfo = new NameInformation();
$nameInfo->addUse('ZendTest\Code\Scanner\TestAsset\Annotation', 'Test');
Expand All @@ -38,4 +42,13 @@ public function testScannerWorks()
$this->assertEquals("'anything I want()\n to be'", $annotationScanner[0]->getContent());
$this->assertEquals(get_class($bar), get_class($annotationScanner[1]));
}

public function scannerWorksDataProvider()
{
return array(
array("\n"),
array("\r"),
array("\r\n"),
);
}
}

0 comments on commit 7d424f6

Please sign in to comment.