Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions tests/Unit/Parsing/OutputExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Parsing;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\OutputException;
use Sabberworm\CSS\Parsing\SourceException;

/**
* @covers \Sabberworm\CSS\Parsing\OutputException
*/
final class OutputExceptionTest extends TestCase
{
/**
* @test
*/
public function extendsSourceException(): void
{
self::assertInstanceOf(SourceException::class, new OutputException('foo'));
}

/**
* @test
*/
public function getMessageReturnsMessageProvidedToConstructor(): void
{
$message = 'The cake is a lie.';
$exception = new OutputException($message);

self::assertStringContainsString($message, $exception->getMessage());
}

/**
* @test
*/
public function getLineNoByDefaultReturnsZero(): void
{
$exception = new OutputException('foo');

self::assertSame(0, $exception->getLineNo());
}

/**
* @test
*/
public function getLineNoReturnsLineNumberProvidedToConstructor(): void
{
$lineNumber = 17;
$exception = new OutputException('foo', $lineNumber);

self::assertSame($lineNumber, $exception->getLineNo());
}

/**
* @test
*/
public function getMessageWithLineNumberProvidedIncludesLineNumber(): void
{
$lineNumber = 17;
$exception = new OutputException('foo', $lineNumber);

self::assertStringContainsString(' [line no: ' . $lineNumber . ']', $exception->getMessage());
}

/**
* @test
*/
public function canBeThrown(): void
{
$this->expectException(OutputException::class);

throw new OutputException('foo');
}
}
67 changes: 67 additions & 0 deletions tests/Unit/Parsing/SourceExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Parsing;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\SourceException;

/**
* @covers \Sabberworm\CSS\Parsing\SourceException
*/
final class SourceExceptionTest extends TestCase
{
/**
* @test
*/
public function getMessageReturnsMessageProvidedToConstructor(): void
{
$message = 'The cake is a lie.';
$exception = new SourceException($message);

self::assertStringContainsString($message, $exception->getMessage());
}

/**
* @test
*/
public function getLineNoByDefaultReturnsZero(): void
{
$exception = new SourceException('foo');

self::assertSame(0, $exception->getLineNo());
}

/**
* @test
*/
public function getLineNoReturnsLineNumberProvidedToConstructor(): void
{
$lineNumber = 17;
$exception = new SourceException('foo', $lineNumber);

self::assertSame($lineNumber, $exception->getLineNo());
}

/**
* @test
*/
public function getMessageWithLineNumberProvidedIncludesLineNumber(): void
{
$lineNumber = 17;
$exception = new SourceException('foo', $lineNumber);

self::assertStringContainsString(' [line no: ' . $lineNumber . ']', $exception->getMessage());
}

/**
* @test
*/
public function canBeThrown(): void
{
$this->expectException(SourceException::class);

throw new SourceException('foo');
}
}
177 changes: 177 additions & 0 deletions tests/Unit/Parsing/UnexpectedEOFExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Parsing;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

/**
* @covers \Sabberworm\CSS\Parsing\UnexpectedEOFException
*/
final class UnexpectedEOFExceptionTest extends TestCase
{
/**
* @test
*/
public function extendsUnexpectedTokenException(): void
{
self::assertInstanceOf(UnexpectedTokenException::class, new UnexpectedEOFException('expected', 'found'));
}

/**
* @test
*/
public function getLineNoByDefaultReturnsZero(): void
{
$exception = new UnexpectedEOFException('expected', 'found');

self::assertSame(0, $exception->getLineNo());
}

/**
* @test
*/
public function getLineNoReturnsLineNumberProvidedToConstructor(): void
{
$lineNumber = 17;
$exception = new UnexpectedEOFException('expected', 'found', 'literal', $lineNumber);

self::assertSame($lineNumber, $exception->getLineNo());
}

/**
* @test
*/
public function getMessageWithLineNumberProvidedIncludesLineNumber(): void
{
$lineNumber = 17;
$exception = new UnexpectedEOFException('expected', 'found', 'literal', $lineNumber);

self::assertStringContainsString(' [line no: ' . $lineNumber . ']', $exception->getMessage());
}

/**
* @test
*/
public function canBeThrown(): void
{
$this->expectException(UnexpectedEOFException::class);

throw new UnexpectedEOFException('expected', 'found');
}

/**
* @test
*/
public function messageByDefaultRefersToTokenNotFound(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found);

$expectedMessage = 'Token “' . $expected . '” (literal) not found. Got “' . $found . '”.';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForInvalidMatchTypeRefersToTokenNotFound(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'coding');

$expectedMessage = 'Token “' . $expected . '” (coding) not found. Got “' . $found . '”.';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForLiteralMatchTypeRefersToTokenNotFound(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'literal');

$expectedMessage = 'Token “' . $expected . '” (literal) not found. Got “' . $found . '”.';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForSearchMatchTypeRefersToNoResults(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'search');

$expectedMessage = 'Search for “' . $expected . '” returned no results. Context: “' . $found . '”.';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForCountMatchTypeRefersToNumberOfCharacters(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'count');

$expectedMessage = 'Next token was expected to have ' . $expected . ' chars. Context: “' . $found . '”.';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForIdentifierMatchTypeRefersToIdentifier(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'identifier');

$expectedMessage = 'Identifier expected. Got “' . $found . '”';
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForCustomMatchTypeMentionsExpectedAndFound(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException($expected, $found, 'custom');

$expectedMessage = $expected . ' ' . $found;
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}

/**
* @test
*/
public function messageForCustomMatchTypeTrimsMessage(): void
{
$expected = 'tea';
$found = 'coffee';

$exception = new UnexpectedEOFException(' ' . $expected, $found . ' ', 'custom');

$expectedMessage = $expected . ' ' . $found;
self::assertStringContainsString($expectedMessage, $exception->getMessage());
}
}
Loading