-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFileReaderTest.php
More file actions
99 lines (78 loc) · 2.81 KB
/
FileReaderTest.php
File metadata and controls
99 lines (78 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace PHPCR\Tests\Util\CND\Reader;
use PHPCR\Util\CND\Reader\FileReader;
use PHPUnit\Framework\TestCase;
class FileReaderTest extends TestCase
{
private string $filepath;
private FileReader $reader;
/**
* @var string[]
*/
private array $chars;
public function setUp(): void
{
$this->filepath = __DIR__.'/../Fixtures/files/TestFile.txt';
$this->reader = new FileReader($this->filepath);
$lines = [
'This is a test file...',
'',
'...containing dummy content.',
'',
];
$this->chars = array_merge(
/* @phpstan-ignore argument.type */ // our fixtures are expected to be without error, no need to check if split worked
preg_split('//', $lines[0], -1, PREG_SPLIT_NO_EMPTY),
["\n", "\n"],
/* @phpstan-ignore argument.type */
preg_split('//', $lines[2], -1, PREG_SPLIT_NO_EMPTY),
["\n", "\n"]
);
}
public function testConstructFileNotFound(): void
{
$this->expectException(\InvalidArgumentException::class);
new FileReader('unexisting_file');
}
public function testGetPath(): void
{
$this->assertEquals($this->filepath, $this->reader->getPath());
}
public function testGetNextChar(): void
{
$curLine = 1;
$curCol = 1;
for ($i = 0; $i < count($this->chars); ++$i) {
$peek = $this->reader->currentChar();
if ($peek === $this->reader->getEofMarker()) {
$this->assertEquals(count($this->chars) - 1, $i);
break;
}
$this->assertEquals($curLine, $this->reader->getCurrentLine());
$this->assertEquals($curCol, $this->reader->getCurrentColumn());
// Assert isEof is false before the end of the file
$this->assertFalse($this->reader->isEof());
// Assert isEol is true at end of the lines
if ("\n" === $peek) {
++$curLine;
$curCol = 1;
} else {
++$curCol;
}
// Assert the next character is the expected one
$this->assertEquals($peek, $this->chars[$i]);
$this->assertEquals(
$this->chars[$i],
$peek,
sprintf("Character mismatch at position %s, expected '%s', found '%s'", $i, $this->chars[$i], $peek)
);
$this->reader->forward();
$this->reader->consume();
}
// Check it's the end of the file
$this->assertEquals($this->reader->getEofMarker(), $this->reader->currentChar());
$this->assertTrue($this->reader->isEof());
$this->assertEquals(false, $this->reader->forwardChar());
}
}