菜谱项目

ErrorTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace PhpParser;
  3. class ErrorTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testConstruct() {
  6. $attributes = array(
  7. 'startLine' => 10,
  8. 'endLine' => 11,
  9. );
  10. $error = new Error('Some error', $attributes);
  11. $this->assertSame('Some error', $error->getRawMessage());
  12. $this->assertSame($attributes, $error->getAttributes());
  13. $this->assertSame(10, $error->getStartLine());
  14. $this->assertSame(11, $error->getEndLine());
  15. $this->assertSame('Some error on line 10', $error->getMessage());
  16. return $error;
  17. }
  18. /**
  19. * @depends testConstruct
  20. */
  21. public function testSetMessageAndLine(Error $error) {
  22. $error->setRawMessage('Some other error');
  23. $this->assertSame('Some other error', $error->getRawMessage());
  24. $error->setStartLine(15);
  25. $this->assertSame(15, $error->getStartLine());
  26. $this->assertSame('Some other error on line 15', $error->getMessage());
  27. }
  28. public function testUnknownLine() {
  29. $error = new Error('Some error');
  30. $this->assertSame(-1, $error->getStartLine());
  31. $this->assertSame(-1, $error->getEndLine());
  32. $this->assertSame('Some error on unknown line', $error->getMessage());
  33. }
  34. /** @dataProvider provideTestColumnInfo */
  35. public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn) {
  36. $error = new Error('Some error', array(
  37. 'startFilePos' => $startPos,
  38. 'endFilePos' => $endPos,
  39. ));
  40. $this->assertSame(true, $error->hasColumnInfo());
  41. $this->assertSame($startColumn, $error->getStartColumn($code));
  42. $this->assertSame($endColumn, $error->getEndColumn($code));
  43. }
  44. public function provideTestColumnInfo() {
  45. return array(
  46. // Error at "bar"
  47. array("<?php foo bar baz", 10, 12, 11, 13),
  48. array("<?php\nfoo bar baz", 10, 12, 5, 7),
  49. array("<?php foo\nbar baz", 10, 12, 1, 3),
  50. array("<?php foo bar\nbaz", 10, 12, 11, 13),
  51. array("<?php\r\nfoo bar baz", 11, 13, 5, 7),
  52. // Error at "baz"
  53. array("<?php foo bar baz", 14, 16, 15, 17),
  54. array("<?php foo bar\nbaz", 14, 16, 1, 3),
  55. // Error at string literal
  56. array("<?php foo 'bar\nbaz' xyz", 10, 18, 11, 4),
  57. array("<?php\nfoo 'bar\nbaz' xyz", 10, 18, 5, 4),
  58. array("<?php foo\n'\nbarbaz\n'\nxyz", 10, 19, 1, 1),
  59. // Error over full string
  60. array("<?php", 0, 4, 1, 5),
  61. array("<?\nphp", 0, 5, 1, 3),
  62. );
  63. }
  64. public function testNoColumnInfo() {
  65. $error = new Error('Some error', 3);
  66. $this->assertSame(false, $error->hasColumnInfo());
  67. try {
  68. $error->getStartColumn('');
  69. $this->fail('Expected RuntimeException');
  70. } catch (\RuntimeException $e) {
  71. $this->assertSame('Error does not have column information', $e->getMessage());
  72. }
  73. try {
  74. $error->getEndColumn('');
  75. $this->fail('Expected RuntimeException');
  76. } catch (\RuntimeException $e) {
  77. $this->assertSame('Error does not have column information', $e->getMessage());
  78. }
  79. }
  80. /**
  81. * @expectedException \RuntimeException
  82. * @expectedExceptionMessage Invalid position information
  83. */
  84. public function testInvalidPosInfo() {
  85. $error = new Error('Some error', array(
  86. 'startFilePos' => 10,
  87. 'endFilePos' => 11,
  88. ));
  89. $error->getStartColumn('code');
  90. }
  91. }