|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Reactor; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Reactor\Exception\MultilineException; |
|
16
|
|
|
use Spiral\Reactor\Partial\Source; |
|
17
|
|
|
|
|
18
|
|
|
class SourceTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testSource(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$s = Source::fromString(" \$name='antony';\r\n return \$name;"); |
|
23
|
|
|
$this->assertSame(" \$name='antony';", $s->getLines()[0]); |
|
24
|
|
|
$this->assertSame(' return $name;', $s->getLines()[1]); |
|
25
|
|
|
|
|
26
|
|
|
$s = Source::fromString(" \$name='antony';\r\n return \$name;", true); |
|
27
|
|
|
$this->assertSame("\$name='antony';", $s->getLines()[0]); |
|
28
|
|
|
$this->assertSame(' return $name;', $s->getLines()[1]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testNormalizeEndings(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$string = "line\n\rline2"; |
|
34
|
|
|
$this->assertSame("line\nline2", Source::normalizeEndings($string)); |
|
35
|
|
|
$string = "line\n\r\nline2"; |
|
36
|
|
|
$this->assertSame("line\n\nline2", Source::normalizeEndings($string, false)); |
|
37
|
|
|
$this->assertSame("line\nline2", Source::normalizeEndings($string, true)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testNormalizeEndingsEmptyReference(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$input = ['', ' b', ' c']; |
|
43
|
|
|
$output = ['', 'b', 'c']; |
|
44
|
|
|
$this->assertSame( |
|
45
|
|
|
implode("\n", $output), |
|
46
|
|
|
Source::normalizeIndents(implode("\n", $input)) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testNormalizeEndingsEmptySpaceReference(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$input = [' ', ' b', ' c']; |
|
53
|
|
|
$output = ['', 'b', 'c']; |
|
54
|
|
|
$this->assertSame( |
|
55
|
|
|
implode("\n", $output), |
|
56
|
|
|
Source::normalizeIndents(implode("\n", $input)) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testNormalizeEndingsNonEmptyReference(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$input = ['a', ' b', ' c']; |
|
63
|
|
|
$output = ['a', ' b', ' c']; |
|
64
|
|
|
$this->assertSame( |
|
65
|
|
|
implode("\n", $output), |
|
66
|
|
|
Source::normalizeIndents(implode("\n", $input)) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testAddLine(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(MultilineException::class); |
|
73
|
|
|
|
|
74
|
|
|
$s = new Source(['line a', 'line b']); |
|
75
|
|
|
$s->addLine('line c'); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame(['line a', 'line b', 'line c'], $s->getLines()); |
|
78
|
|
|
|
|
79
|
|
|
$s->addLine("line d\nline e"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testStringify(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$s = new Source(['line a', 'line b']); |
|
85
|
|
|
$s->addLine('line c'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertSame("line a\nline b\nline c", (string)$s); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|