Skip to content

Commit c9aa358

Browse files
committed
[TASK] Add tests for the exceptions
Part of #757
1 parent a5d22be commit c9aa358

File tree

4 files changed

+470
-0
lines changed

4 files changed

+470
-0
lines changed
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\OutputException;
9+
10+
/**
11+
* @covers \Sabberworm\CSS\Parsing\OutputException
12+
*/
13+
final class OutputExceptionTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function getMessageReturnsMessageProvidedToConstructor(): void
19+
{
20+
$message = 'The cake is a lie.';
21+
$exception = new OutputException($message);
22+
23+
self::assertStringContainsString($message, $exception->getMessage());
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function getLineNoByDefaultReturnsZero(): void
30+
{
31+
$exception = new OutputException('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 OutputException('foo', $lineNumber);
43+
44+
self::assertSame($lineNumber, $exception->getLineNo());
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function getMessageForLineNumberProvidedIncludesMessage(): void
51+
{
52+
$lineNumber = 17;
53+
$exception = new OutputException('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(OutputException::class);
64+
65+
throw new OutputException('foo');
66+
}
67+
}
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 getMessageForLineNumberProvidedIncludesMessage(): 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: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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

Comments
 (0)