CommentTest.php 2.1 KB

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