This repository was archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHighlighterTest.php
232 lines (210 loc) · 8.02 KB
/
HighlighterTest.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Phpactor\Extension\LanguageServerReferenceFinder\Tests\Integration\Model;
use Closure;
use Generator;
use Microsoft\PhpParser\Parser;
use PHPUnit\Framework\TestCase;
use Phpactor\Extension\LanguageServerReferenceFinder\Model\Highlighter;
use Phpactor\Extension\LanguageServerReferenceFinder\Model\Highlights;
use Phpactor\LanguageServerProtocol\DocumentHighlightKind;
use Phpactor\TestUtils\ExtractOffset;
use Phpactor\TextDocument\ByteOffset;
class HighlighterTest extends TestCase
{
/**
* @dataProvider provideVariables
* @dataProvider provideProperties
* @dataProvider provideMethods
* @dataProvider provideNames
* @dataProvider provideConstants
*/
public function testHighlight(string $source, Closure $assertion): void
{
[$source, $offset] = ExtractOffset::fromSource($source);
$assertion(
(new Highlighter(new Parser()))->highlightsFor(
$source,
ByteOffset::fromInt((int)$offset)
)
);
}
/**
* @return Generator<mixed>
*/
public function provideVariables(): Generator
{
yield 'none' => [
'<?php',
function (Highlights $highlights) {
self::assertCount(0, $highlights);
}
];
yield 'one' => [
'<?php $v<>ar;',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::READ, $highlights->first()->kind);
}
];
yield 'two vars including method var' => [
'<?php function foobar ($var) { $v<>ar; }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
}
];
yield 'only method var' => [
'<?php function foobar ($v<>ar) {}',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
}
];
yield 'write var including method var' => [
'<?php $var = "foo"; $v<>ar;}',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::WRITE, $highlights->first()->kind);
}
];
}
/**
* @return Generator<mixed>
*/
public function provideProperties(): Generator
{
yield 'property declaration' => [
'<?php class Foobar { private $f<>oobar; }',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'property declaration 2' => [
'<?php class Foobar { private $f<>oobar; private $barfoo;}',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'property read' => [
'<?php class Foobar { private $f<>oobar; function bar() { return $this->foobar; }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
self::assertEquals(DocumentHighlightKind::READ, $highlights->at(1)->kind);
}
];
yield 'property access' => [
'<?php class Foobar { private $foobar; function bar() { return $this->foo<>bar->barfoo; }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
self::assertEquals(DocumentHighlightKind::READ, $highlights->at(1)->kind);
}
];
yield 'property write' => [
'<?php class Foobar { private $f<>oobar; function bar() { return $this->foobar = "barfoo"; }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
self::assertEquals(DocumentHighlightKind::WRITE, $highlights->at(1)->kind);
}
];
}
/**
* @return Generator<mixed>
*/
public function provideMethods(): Generator
{
yield 'method declaration' => [
'<?php class Foobar { public function f<>oobar() {} }',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'method read' => [
'<?php class Foobar { function bar() { return $this->b<>ar(); }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
self::assertEquals(DocumentHighlightKind::READ, $highlights->at(1)->kind);
}
];
yield 'static method read' => [
'<?php class Foobar { static function bar() { return self::b<>ar(); }',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
self::assertEquals(DocumentHighlightKind::READ, $highlights->at(1)->kind);
}
];
}
/**
* @return Generator<mixed>
*/
public function provideNames(): Generator
{
yield 'class name' => [
'<?php class Foo<>bar {}',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'class name with fqn' => [
'<?php class Foo<>bar {const BAR=1;} Foobar::BAR;',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'class name and use statement' => [
'<?php use Barfoo\Foobar; new Foo<>bar()',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'class name and multiple use statements' => [
<<<'EOT'
<?php
use Bartoo\Barf;
use Barfoo\Foobar;
use BazBar;
new Foo<>bar()
EOT
,
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
}
/**
* @return Generator<mixed>
*/
public function provideConstants(): Generator
{
yield 'class constant' => [
'<?php class Foo { const B<>AR = "";}',
function (Highlights $highlights) {
self::assertCount(1, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'class constants' => [
'<?php class Foo<>bar {const BAR=1;} Foobar::BAR;',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
yield 'class constants on reference' => [
'<?php class Foobar {const BAR=1;} Foobar::B<>AR;',
function (Highlights $highlights) {
self::assertCount(2, $highlights);
self::assertEquals(DocumentHighlightKind::TEXT, $highlights->at(0)->kind);
}
];
}
}