xref: /PHP-Parser/test/PhpParser/CommentTest.php (revision d57da64d)
1<?php declare(strict_types=1);
2
3namespace PhpParser;
4
5class CommentTest extends \PHPUnit\Framework\TestCase {
6    public function testGetters(): void {
7        $comment = new Comment('/* Some comment */',
8            1, 10, 2, 1, 27, 2);
9
10        $this->assertSame('/* Some comment */', $comment->getText());
11        $this->assertSame('/* Some comment */', (string) $comment);
12        $this->assertSame(1, $comment->getStartLine());
13        $this->assertSame(10, $comment->getStartFilePos());
14        $this->assertSame(2, $comment->getStartTokenPos());
15        $this->assertSame(1, $comment->getEndLine());
16        $this->assertSame(27, $comment->getEndFilePos());
17        $this->assertSame(2, $comment->getEndTokenPos());
18    }
19
20    /**
21     * @dataProvider provideTestReformatting
22     */
23    public function testReformatting($commentText, $reformattedText): void {
24        $comment = new Comment($commentText);
25        $this->assertSame($reformattedText, $comment->getReformattedText());
26    }
27
28    public function provideTestReformatting() {
29        return [
30            ['// Some text', '// Some text'],
31            ['/* Some text */', '/* Some text */'],
32            [
33                "/**\n     * Some text.\n     * Some more text.\n     */",
34                "/**\n * Some text.\n * Some more text.\n */"
35            ],
36            [
37                "/**\r\n     * Some text.\r\n     * Some more text.\r\n     */",
38                "/**\n * Some text.\n * Some more text.\n */"
39            ],
40            [
41                "/*\n        Some text.\n        Some more text.\n    */",
42                "/*\n    Some text.\n    Some more text.\n*/"
43            ],
44            [
45                "/*\r\n        Some text.\r\n        Some more text.\r\n    */",
46                "/*\n    Some text.\n    Some more text.\n*/"
47            ],
48            [
49                "/* Some text.\n       More text.\n       Even more text. */",
50                "/* Some text.\n   More text.\n   Even more text. */"
51            ],
52            [
53                "/* Some text.\r\n       More text.\r\n       Even more text. */",
54                "/* Some text.\n   More text.\n   Even more text. */"
55            ],
56            [
57                "/* Some text.\n       More text.\n         Indented text. */",
58                "/* Some text.\n   More text.\n     Indented text. */",
59            ],
60            // invalid comment -> no reformatting
61            [
62                "hello\n    world",
63                "hello\n    world",
64            ],
65        ];
66    }
67}
68