|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sabberworm\CSS\Tests\Unit\Parsing; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers \Sabberworm\CSS\Parsing\UnexpectedEOFException |
| 12 | + */ |
| 13 | +final class UnexpectedEOFExceptionTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @test |
| 17 | + */ |
| 18 | + public function getLineNoByDefaultReturnsZero(): void |
| 19 | + { |
| 20 | + $exception = new UnexpectedEOFException('expected', 'found'); |
| 21 | + |
| 22 | + self::assertSame(0, $exception->getLineNo()); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @test |
| 27 | + */ |
| 28 | + public function getLineNoReturnsLineNumberProvidedToConstructor(): void |
| 29 | + { |
| 30 | + $lineNumber = 17; |
| 31 | + $exception = new UnexpectedEOFException('expected', 'found', 'literal', $lineNumber); |
| 32 | + |
| 33 | + self::assertSame($lineNumber, $exception->getLineNo()); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @test |
| 38 | + */ |
| 39 | + public function getMessageForLineNumberProvidedIncludesMessage(): void |
| 40 | + { |
| 41 | + $lineNumber = 17; |
| 42 | + $exception = new UnexpectedEOFException('expected', 'found', 'literal', $lineNumber); |
| 43 | + |
| 44 | + self::assertStringContainsString(' [line no: ' . $lineNumber . ']', $exception->getMessage()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + */ |
| 50 | + public function canBeThrown(): void |
| 51 | + { |
| 52 | + $this->expectException(UnexpectedEOFException::class); |
| 53 | + |
| 54 | + throw new UnexpectedEOFException('expected', 'found'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + */ |
| 60 | + public function messageByDefaultRefersToTokenNotFound(): void |
| 61 | + { |
| 62 | + $expected = 'tea'; |
| 63 | + $found = 'coffee'; |
| 64 | + |
| 65 | + $exception = new UnexpectedEOFException($expected, $found); |
| 66 | + |
| 67 | + $expectedMessage = 'Token “' . $expected . '” (literal) not found. Got “' . $found . '”.'; |
| 68 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @test |
| 73 | + */ |
| 74 | + public function messageForInvalidMatchTypeRefersToTokenNotFound(): void |
| 75 | + { |
| 76 | + $expected = 'tea'; |
| 77 | + $found = 'coffee'; |
| 78 | + |
| 79 | + $exception = new UnexpectedEOFException($expected, $found, 'coding'); |
| 80 | + |
| 81 | + $expectedMessage = 'Token “' . $expected . '” (coding) not found. Got “' . $found . '”.'; |
| 82 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @test |
| 87 | + */ |
| 88 | + public function messageForLiteralMatchTypeRefersToTokenNotFound(): void |
| 89 | + { |
| 90 | + $expected = 'tea'; |
| 91 | + $found = 'coffee'; |
| 92 | + |
| 93 | + $exception = new UnexpectedEOFException($expected, $found, 'literal'); |
| 94 | + |
| 95 | + $expectedMessage = 'Token “' . $expected . '” (literal) not found. Got “' . $found . '”.'; |
| 96 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @test |
| 101 | + */ |
| 102 | + public function messageForSearchMatchTypeRefersToNoResults(): void |
| 103 | + { |
| 104 | + $expected = 'tea'; |
| 105 | + $found = 'coffee'; |
| 106 | + |
| 107 | + $exception = new UnexpectedEOFException($expected, $found, 'search'); |
| 108 | + |
| 109 | + $expectedMessage = 'Search for “' . $expected . '” returned no results. Context: “' . $found . '”.'; |
| 110 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @test |
| 115 | + */ |
| 116 | + public function messageForCountMatchTypeRefersToNumberOfCharacters(): void |
| 117 | + { |
| 118 | + $expected = 'tea'; |
| 119 | + $found = 'coffee'; |
| 120 | + |
| 121 | + $exception = new UnexpectedEOFException($expected, $found, 'count'); |
| 122 | + |
| 123 | + $expectedMessage = 'Next token was expected to have ' . $expected . ' chars. Context: “' . $found . '”.'; |
| 124 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @test |
| 129 | + */ |
| 130 | + public function messageForIdentifierMatchTypeRefersToIdentifier(): void |
| 131 | + { |
| 132 | + $expected = 'tea'; |
| 133 | + $found = 'coffee'; |
| 134 | + |
| 135 | + $exception = new UnexpectedEOFException($expected, $found, 'identifier'); |
| 136 | + |
| 137 | + $expectedMessage = 'Identifier expected. Got “' . $found . '”'; |
| 138 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @test |
| 143 | + */ |
| 144 | + public function messageForCustomMatchTypeMentionsExpectedAndFound(): void |
| 145 | + { |
| 146 | + $expected = 'tea'; |
| 147 | + $found = 'coffee'; |
| 148 | + |
| 149 | + $exception = new UnexpectedEOFException($expected, $found, 'custom'); |
| 150 | + |
| 151 | + $expectedMessage = $expected . ' ' . $found; |
| 152 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @test |
| 157 | + */ |
| 158 | + public function messageForCustomMatchTypeTrimsMessage(): void |
| 159 | + { |
| 160 | + $expected = 'tea'; |
| 161 | + $found = 'coffee'; |
| 162 | + |
| 163 | + $exception = new UnexpectedEOFException(' ' . $expected, $found . ' ', 'custom'); |
| 164 | + |
| 165 | + $expectedMessage = $expected . ' ' . $found; |
| 166 | + self::assertStringContainsString($expectedMessage, $exception->getMessage()); |
| 167 | + } |
| 168 | +} |
0 commit comments