|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpellcheck\Exception\InvalidArgumentException; |
|
6
|
|
|
use PhpSpellcheck\Utils\LineAndOffset; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class LineAndOffsetTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testFindFromFirstCharacterOffset(): void |
|
12
|
|
|
{ |
|
13
|
|
|
$text = <<<TEXT |
|
14
|
|
|
First line |
|
15
|
|
|
Second line |
|
16
|
|
|
Third line |
|
17
|
|
|
Last line |
|
18
|
|
|
TEXT; |
|
19
|
|
|
|
|
20
|
|
|
// offset 0 = before first character of 1st line |
|
21
|
|
|
$this->assertSame( |
|
22
|
|
|
[1, 0], |
|
23
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 0) |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
// offset 10 = after last character of 1st line |
|
27
|
|
|
$this->assertSame( |
|
28
|
|
|
[1, 10], |
|
29
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 10) |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
// offset 11 = before first character of 2nd line |
|
33
|
|
|
$this->assertSame( |
|
34
|
|
|
[2, 0], |
|
35
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 11) |
|
36
|
|
|
); |
|
37
|
|
|
// offset 23 = before first character of 3rd line |
|
38
|
|
|
$this->assertSame( |
|
39
|
|
|
[3, 0], |
|
40
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 23) |
|
41
|
|
|
); |
|
42
|
|
|
// offset 24 = after first character of 3rd line |
|
43
|
|
|
$this->assertSame( |
|
44
|
|
|
[3, 1], |
|
45
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 24) |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
// offset 34 = after first character of 4th line |
|
49
|
|
|
$this->assertSame( |
|
50
|
|
|
[4, 0], |
|
51
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 34) |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
// offset 34 = after last character of 4th line |
|
55
|
|
|
$this->assertSame( |
|
56
|
|
|
[4, 9], |
|
57
|
|
|
LineAndOffset::findFromFirstCharacterOffset($text, 43) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testThrowExceptionIfOffsetGivenIsHigherThanStringLength(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
64
|
|
|
$this->assertSame( |
|
65
|
|
|
[4, 0], |
|
66
|
|
|
LineAndOffset::findFromFirstCharacterOffset('test', 50) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testThrowExceptionIfOffsetisNegative(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
73
|
|
|
$this->assertSame( |
|
74
|
|
|
[4, 0], |
|
75
|
|
|
LineAndOffset::findFromFirstCharacterOffset('test', -50) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|