Skip to content

Commit 7121bc3

Browse files
authored
[TASK] Add tests for the exceptions (#911)
1 parent 6057847 commit 7121bc3

File tree

4 files changed

+497
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)