diff --git a/tests/GetCodeSnippetTest.php b/tests/GetCodeSnippetTest.php new file mode 100644 index 0000000..989a6ed --- /dev/null +++ b/tests/GetCodeSnippetTest.php @@ -0,0 +1,138 @@ + +EOL; + + /** + * Test retrieving a code snippet from a larger context. + * + * @dataProvider dataGetCodeSnippet + * + * @param string $expected Expected function output. + * @param int $lineNo Line number to get the code snippet for. + * @param int $linesBeforeAfter Number of lines of code context to retrieve. + */ + public function testGetCodeSnippet($expected, $lineNo, $linesBeforeAfter = 2) + { + $highlighter = new Highlighter($this->getConsoleColorMock()); + $output = $highlighter->getCodeSnippet($this->input, $lineNo, $linesBeforeAfter, $linesBeforeAfter); + + // Allow unit tests to succeed on non-*nix systems. + $output = str_replace(array("\r\n", "\r"), "\n", $output); + + $this->assertSame($expected, $output); + } + + /** + * Data provider. + * + * Includes test cases to verify that the line number padding is handled correctly + * depending on the "widest" line number. + * + * @return array + */ + public function dataGetCodeSnippet() + { + return array( + 'Snippet at start of code - line 1' => array( + 'expected' => <<<'EOL' + > 1| + 2| + 3| namespace FooBar; +EOL + , + 'lineNo' => 1, + ), + 'Snippet at start of code - line 2' => array( + 'expected' => <<<'EOL' + 1| + > 2| + 3| namespace FooBar; + 4| +EOL + , + 'lineNo' => 2, + ), + 'Snippet middle of code' => array( + 'expected' => <<<'EOL' + 7| * @param type $param Description. + 8| */ + > 9| public function bar($param) { + 10| // Do something. + 11| } +EOL + , + 'lineNo' => 9, + ), + 'Snippet at end of code - line before last' => array( + 'expected' => <<<'EOL' + 10| // Do something. + 11| } + > 12| } + 13| ?> +EOL + , + 'lineNo' => 12, + ), + 'Snippet at end of code - last line' => array( + 'expected' => <<<'EOL' + 11| } + 12| } + > 13| ?> +EOL + , + 'lineNo' => 13, + ), + 'Snippet middle of code, 1 line context' => array( + 'expected' => <<<'EOL' + 8| */ + > 9| public function bar($param) { + 10| // Do something. +EOL + , + 'lineNo' => 9, + 'linesBeforeAfter' => 1, + ), + 'Snippet middle of code, 3 line context' => array( + 'expected' => <<<'EOL' + 6| /** + 7| * @param type $param Description. + 8| */ + > 9| public function bar($param) { + 10| // Do something. + 11| } + 12| } +EOL + , + 'lineNo' => 9, + 'linesBeforeAfter' => 3, + ), + ); + } +}