annotate vendor/nikic/php-parser/test/PhpParser/ErrorTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@17 5 class ErrorTest extends \PHPUnit\Framework\TestCase
Chris@0 6 {
Chris@0 7 public function testConstruct() {
Chris@13 8 $attributes = [
Chris@0 9 'startLine' => 10,
Chris@0 10 'endLine' => 11,
Chris@13 11 ];
Chris@0 12 $error = new Error('Some error', $attributes);
Chris@0 13
Chris@0 14 $this->assertSame('Some error', $error->getRawMessage());
Chris@0 15 $this->assertSame($attributes, $error->getAttributes());
Chris@0 16 $this->assertSame(10, $error->getStartLine());
Chris@0 17 $this->assertSame(11, $error->getEndLine());
Chris@0 18 $this->assertSame('Some error on line 10', $error->getMessage());
Chris@0 19
Chris@0 20 return $error;
Chris@0 21 }
Chris@0 22
Chris@0 23 /**
Chris@0 24 * @depends testConstruct
Chris@0 25 */
Chris@0 26 public function testSetMessageAndLine(Error $error) {
Chris@0 27 $error->setRawMessage('Some other error');
Chris@0 28 $this->assertSame('Some other error', $error->getRawMessage());
Chris@0 29
Chris@0 30 $error->setStartLine(15);
Chris@0 31 $this->assertSame(15, $error->getStartLine());
Chris@0 32 $this->assertSame('Some other error on line 15', $error->getMessage());
Chris@0 33 }
Chris@0 34
Chris@0 35 public function testUnknownLine() {
Chris@0 36 $error = new Error('Some error');
Chris@0 37
Chris@0 38 $this->assertSame(-1, $error->getStartLine());
Chris@0 39 $this->assertSame(-1, $error->getEndLine());
Chris@0 40 $this->assertSame('Some error on unknown line', $error->getMessage());
Chris@0 41 }
Chris@0 42
Chris@0 43 /** @dataProvider provideTestColumnInfo */
Chris@0 44 public function testColumnInfo($code, $startPos, $endPos, $startColumn, $endColumn) {
Chris@13 45 $error = new Error('Some error', [
Chris@0 46 'startFilePos' => $startPos,
Chris@0 47 'endFilePos' => $endPos,
Chris@13 48 ]);
Chris@0 49
Chris@13 50 $this->assertTrue($error->hasColumnInfo());
Chris@0 51 $this->assertSame($startColumn, $error->getStartColumn($code));
Chris@0 52 $this->assertSame($endColumn, $error->getEndColumn($code));
Chris@0 53
Chris@0 54 }
Chris@0 55
Chris@0 56 public function provideTestColumnInfo() {
Chris@13 57 return [
Chris@0 58 // Error at "bar"
Chris@13 59 ["<?php foo bar baz", 10, 12, 11, 13],
Chris@13 60 ["<?php\nfoo bar baz", 10, 12, 5, 7],
Chris@13 61 ["<?php foo\nbar baz", 10, 12, 1, 3],
Chris@13 62 ["<?php foo bar\nbaz", 10, 12, 11, 13],
Chris@13 63 ["<?php\r\nfoo bar baz", 11, 13, 5, 7],
Chris@0 64 // Error at "baz"
Chris@13 65 ["<?php foo bar baz", 14, 16, 15, 17],
Chris@13 66 ["<?php foo bar\nbaz", 14, 16, 1, 3],
Chris@0 67 // Error at string literal
Chris@13 68 ["<?php foo 'bar\nbaz' xyz", 10, 18, 11, 4],
Chris@13 69 ["<?php\nfoo 'bar\nbaz' xyz", 10, 18, 5, 4],
Chris@13 70 ["<?php foo\n'\nbarbaz\n'\nxyz", 10, 19, 1, 1],
Chris@0 71 // Error over full string
Chris@13 72 ["<?php", 0, 4, 1, 5],
Chris@13 73 ["<?\nphp", 0, 5, 1, 3],
Chris@13 74 ];
Chris@0 75 }
Chris@0 76
Chris@0 77 public function testNoColumnInfo() {
Chris@0 78 $error = new Error('Some error', 3);
Chris@0 79
Chris@13 80 $this->assertFalse($error->hasColumnInfo());
Chris@0 81 try {
Chris@0 82 $error->getStartColumn('');
Chris@0 83 $this->fail('Expected RuntimeException');
Chris@0 84 } catch (\RuntimeException $e) {
Chris@0 85 $this->assertSame('Error does not have column information', $e->getMessage());
Chris@0 86 }
Chris@0 87 try {
Chris@0 88 $error->getEndColumn('');
Chris@0 89 $this->fail('Expected RuntimeException');
Chris@0 90 } catch (\RuntimeException $e) {
Chris@0 91 $this->assertSame('Error does not have column information', $e->getMessage());
Chris@0 92 }
Chris@0 93 }
Chris@0 94
Chris@0 95 public function testInvalidPosInfo() {
Chris@17 96 $this->expectException(\RuntimeException::class);
Chris@17 97 $this->expectExceptionMessage('Invalid position information');
Chris@13 98 $error = new Error('Some error', [
Chris@0 99 'startFilePos' => 10,
Chris@0 100 'endFilePos' => 11,
Chris@13 101 ]);
Chris@0 102 $error->getStartColumn('code');
Chris@0 103 }
Chris@0 104 }