CommentTest.php 2.5 KB

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