Skip to content

Commit 7847f1f

Browse files
committed
Tests for ParsingInput
1 parent 0cde52f commit 7847f1f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/ParsingInput.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function getChar(): string
5858
public function consume(int $length, string $expected = null): string
5959
{
6060
assert($length > 0);
61+
assert($expected === null || strlen($expected) === $length);
6162

6263
if ($length > strlen($this->value) - $this->position) {
6364
throw new \RuntimeException('Reached end of value');

tests/ParsingInputTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace gapple\Tests\StructuredFields;
4+
5+
use gapple\StructuredFields\ParsingInput;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ParsingInputTest extends TestCase
9+
{
10+
public function testGetEmptyChar(): void
11+
{
12+
$input = new ParsingInput('');
13+
$this->expectException(\RuntimeException::class);
14+
$input->getChar();
15+
}
16+
17+
/**
18+
* @return array<string, array{string, bool, string}>
19+
*/
20+
public static function trimProvider(): array
21+
{
22+
return [
23+
'space' => [' test ', false, 'test '],
24+
'ows' => [" \t test ", true, 'test '],
25+
'non-ows' => [" \t test ", false, "\t test "],
26+
];
27+
}
28+
29+
/**
30+
* @dataProvider trimProvider
31+
*/
32+
public function testTrim(string $value, bool $ows, string $expected): void
33+
{
34+
$input = new ParsingInput($value);
35+
$input->trim($ows);
36+
37+
$this->assertEquals($expected, $input->remaining());
38+
}
39+
40+
public function testConsumeExcess(): void
41+
{
42+
$input = new ParsingInput('test');
43+
$this->expectException(\RuntimeException::class);
44+
$this->expectExceptionMessage('Reached end of value');
45+
$input->consume(5);
46+
}
47+
48+
public function testConsumeNotMatched(): void
49+
{
50+
$input = new ParsingInput('test');
51+
$this->expectException(\RuntimeException::class);
52+
$this->expectExceptionMessage('Unexpected character');
53+
$input->consumeString('foo');
54+
}
55+
}

0 commit comments

Comments
 (0)