|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ColinODell\Indentation\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use ColinODell\Indentation\Indentation; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
final class IndentationTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @dataProvider provideCasesForTestDetect |
|
14
|
|
|
*/ |
|
15
|
|
|
public function testDetect(string $filename, Indentation $expected, string $asString): void |
|
16
|
|
|
{ |
|
17
|
|
|
$actual = Indentation::detect($filename); |
|
18
|
|
|
self::assertEquals($expected, $actual); |
|
19
|
|
|
self::assertSame($asString, (string) $actual); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return iterable<array<mixed>> |
|
24
|
|
|
*/ |
|
25
|
|
|
public function provideCasesForTestDetect(): iterable |
|
26
|
|
|
{ |
|
27
|
|
|
yield 'Detect space indentation' => [ |
|
28
|
|
|
$this->loadFixture('space.js'), |
|
29
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
30
|
|
|
' ', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
yield 'Detect tab indentation' => [ |
|
34
|
|
|
$this->loadFixture('tab.js'), |
|
35
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
36
|
|
|
"\t", |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
yield 'Detect multiple tabs' => [ |
|
40
|
|
|
$this->loadFixture('tab-four.js'), |
|
41
|
|
|
new Indentation(4, Indentation::TYPE_TAB), |
|
42
|
|
|
"\t\t\t\t", |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
yield 'Detect equal tabs and spaces' => [ |
|
46
|
|
|
$this->loadFixture('mixed-tab.js'), |
|
47
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
48
|
|
|
"\t", |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
yield 'Detect indent of a file with mostly spaces' => [ |
|
52
|
|
|
$this->loadFixture('mixed-space.js'), |
|
53
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
54
|
|
|
' ', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
yield 'Detect indent of a weirdly indented vendor prefixed CSS' => [ |
|
58
|
|
|
$this->loadFixture('vendor-prefixed-css.css'), |
|
59
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
60
|
|
|
' ', |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
yield 'Return 0 when these is no indentation' => [ |
|
64
|
|
|
'<ul></ul>', |
|
65
|
|
|
new Indentation(0, Indentation::TYPE_UNKNOWN), |
|
66
|
|
|
'', |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
yield 'Indentation for fifty-fifty indented files with spaces first' => [ |
|
70
|
|
|
$this->loadFixture('fifty-fifty-space-first.js'), |
|
71
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
72
|
|
|
' ', |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
yield 'Indentation for fifty-fifty indented files with tabs first' => [ |
|
76
|
|
|
$this->loadFixture('fifty-fifty-tab-first.js'), |
|
77
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
78
|
|
|
"\t", |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
yield 'Indentation for files with spaces and tabs last' => [ |
|
82
|
|
|
$this->loadFixture('space-tab-last.js'), |
|
83
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
84
|
|
|
"\t", |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
yield 'Indentation of a file with single line comments' => [ |
|
88
|
|
|
$this->loadFixture('single-space-ignore.js'), |
|
89
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
90
|
|
|
' ', |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
yield 'Indentation for files with single spaces only' => [ |
|
94
|
|
|
$this->loadFixture('single-space-only.js'), |
|
95
|
|
|
new Indentation(1, Indentation::TYPE_SPACE), |
|
96
|
|
|
' ', |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @dataProvider provideCasesForTestChange |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testChange(string $contents, Indentation $indentation, string $expected): void |
|
104
|
|
|
{ |
|
105
|
|
|
$actual = Indentation::change($contents, $indentation); |
|
106
|
|
|
self::assertSame($expected, $actual); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return iterable<array<mixed>> |
|
111
|
|
|
*/ |
|
112
|
|
|
public function provideCasesForTestChange(): iterable |
|
113
|
|
|
{ |
|
114
|
|
|
yield 'Empty string' => [ |
|
115
|
|
|
'', |
|
116
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
117
|
|
|
'', |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
yield 'No indentation' => [ |
|
121
|
|
|
'<ul></ul>', |
|
122
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
123
|
|
|
'<ul></ul>', |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
yield 'Two spaces to four spaces' => [ |
|
127
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
128
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
129
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
yield 'Four spaces to two spaces' => [ |
|
133
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
134
|
|
|
new Indentation(2, Indentation::TYPE_SPACE), |
|
135
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
yield 'Two spaces to tabs' => [ |
|
139
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
140
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
141
|
|
|
"<div>\n\t<ul>\n\t\t<li>yay</li>\n\t</ul>\n</div>", |
|
142
|
|
|
]; |
|
143
|
|
|
|
|
144
|
|
|
yield 'Four spaces to tabs' => [ |
|
145
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
146
|
|
|
new Indentation(1, Indentation::TYPE_TAB), |
|
147
|
|
|
"<div>\n\t<ul>\n\t\t<li>yay</li>\n\t</ul>\n</div>", |
|
148
|
|
|
]; |
|
149
|
|
|
|
|
150
|
|
|
yield 'Tabs to four spaces' => [ |
|
151
|
|
|
"<div>\n\t<ul>\n\t\t<li>yay</li>\n\t</ul>\n</div>", |
|
152
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
153
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
154
|
|
|
]; |
|
155
|
|
|
|
|
156
|
|
|
yield 'Two tabs to two spaces' => [ |
|
157
|
|
|
"<div>\n\t\t<ul>\n\t\t\t\t<li>yay</li>\n\t\t</ul>\n</div>", |
|
158
|
|
|
new Indentation(2, Indentation::TYPE_SPACE), |
|
159
|
|
|
"<div>\n <ul>\n <li>yay</li>\n </ul>\n</div>", |
|
160
|
|
|
]; |
|
161
|
|
|
|
|
162
|
|
|
yield 'Newlines are preserved' => [ |
|
163
|
|
|
"\n<div>\n <ul>\r\n <li>yay</li>\r </ul>\n</div>\n\n", |
|
164
|
|
|
new Indentation(4, Indentation::TYPE_SPACE), |
|
165
|
|
|
"\n<div>\n <ul>\r\n <li>yay</li>\r </ul>\n</div>\n\n", |
|
166
|
|
|
]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
private function loadFixture(string $filename): string |
|
170
|
|
|
{ |
|
171
|
|
|
$fixture = \file_get_contents(__DIR__ . '/fixtures/' . $filename); |
|
172
|
|
|
if ($fixture === false) { |
|
173
|
|
|
$this->fail('Fixture file not found'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $fixture; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|