@@ -5,6 +5,42 @@ import * as assert from "assert";
5
5
import { CodeLens , Position , workspace } from "vscode" ;
6
6
import { afterEach , beforeEach } from "mocha" ;
7
7
8
+ /**
9
+ * The content of a route file.
10
+ */
11
+ const routeContent = `
12
+ import 'package:dart_frog/dart_frog.dart';
13
+
14
+ Response onRequest(RequestContext context) {
15
+ return Response(body: 'Welcome to Dart Frog!');
16
+ }
17
+
18
+ ` ;
19
+
20
+ /**
21
+ * The content of a route file with a dynamic route.
22
+ */
23
+ const dynamicRouteContent = `
24
+ import 'package:dart_frog/dart_frog.dart';
25
+
26
+ Response onRequest(RequestContext context, String id) {
27
+ return Response(body: 'Welcome to Dart Frog!');
28
+ }
29
+
30
+ ` ;
31
+
32
+ /**
33
+ * The content of something that looks like a route file but isn't.
34
+ */
35
+ const invalidRouteContent = `
36
+ import 'package:dart_frog/dart_frog.dart';
37
+
38
+ Response notOnRequest(RequestContext context) {
39
+ return Response(body: 'Welcome to Dart Frog!');
40
+ }
41
+
42
+ ` ;
43
+
8
44
suite ( "RunOnRequestCodeLensProvider" , ( ) => {
9
45
let vscodeStub : any ;
10
46
let utilsStub : any ;
@@ -22,8 +58,7 @@ suite("RunOnRequestCodeLensProvider", () => {
22
58
} ;
23
59
workspaceConfiguration = sinon . stub ( ) ;
24
60
vscodeStub . workspace . getConfiguration . returns ( workspaceConfiguration ) ;
25
- const getConfiguration = sinon . stub ( ) ;
26
- workspaceConfiguration . get = getConfiguration ;
61
+ const getConfiguration = ( workspaceConfiguration . get = sinon . stub ( ) ) ;
27
62
getConfiguration . withArgs ( "enableCodeLens" , true ) . returns ( true ) ;
28
63
29
64
utilsStub = {
@@ -134,14 +169,7 @@ suite("RunOnRequestCodeLensProvider", () => {
134
169
} ) ;
135
170
136
171
test ( "returns the correct CodeLenses" , async ( ) => {
137
- const content = `
138
- import 'package:dart_frog/dart_frog.dart';
139
-
140
- Response onRequest(RequestContext context) {
141
- return Response(body: 'Welcome to Dart Frog!');
142
- }
143
-
144
- ` ;
172
+ const content = routeContent ;
145
173
const textDocument = await workspace . openTextDocument ( {
146
174
language : "text" ,
147
175
content,
@@ -168,14 +196,7 @@ Response onRequest(RequestContext context) {
168
196
} ) ;
169
197
170
198
test ( "returns the correct CodeLenses on a dynamic route" , async ( ) => {
171
- const content = `
172
- import 'package:dart_frog/dart_frog.dart';
173
-
174
- Response onRequest(RequestContext context, String id) {
175
- return Response(body: 'Welcome to Dart Frog!');
176
- }
177
-
178
- ` ;
199
+ const content = dynamicRouteContent ;
179
200
const textDocument = await workspace . openTextDocument ( {
180
201
language : "text" ,
181
202
content,
@@ -202,15 +223,154 @@ Response onRequest(RequestContext context, String id) {
202
223
} ) ;
203
224
204
225
test ( "returns no CodeLenses on a non route file" , async ( ) => {
205
- const content = `
206
- import 'package:dart_frog/dart_frog.dart';
226
+ const content = invalidRouteContent ;
227
+ const textDocument = await workspace . openTextDocument ( {
228
+ language : "text" ,
229
+ content,
230
+ } ) ;
231
+ document . getText = textDocument . getText . bind ( textDocument ) ;
232
+ document . positionAt = textDocument . positionAt . bind ( textDocument ) ;
233
+ document . lineAt = textDocument . lineAt . bind ( textDocument ) ;
234
+ document . getWordRangeAtPosition =
235
+ textDocument . getWordRangeAtPosition . bind ( textDocument ) ;
207
236
208
- Response notOnRequest(RequestContext context) {
209
- return Response(body: 'Welcome to Dart Frog!');
210
- }
237
+ const provider = new RunOnRequestCodeLensProvider ( ) ;
238
+ const result = await provider . provideCodeLenses ( document ) ;
211
239
212
- ` ;
240
+ assert . strictEqual ( result . length , 0 ) ;
241
+ } ) ;
242
+ } ) ;
243
+ } ) ;
244
+
245
+ suite ( "DebugOnRequestCodeLensProvider" , ( ) => {
246
+ let vscodeStub : any ;
247
+ let utilsStub : any ;
248
+ // eslint-disable-next-line @typescript-eslint/naming-convention
249
+ let DebugOnRequestCodeLensProvider : any ;
250
+ let document : any ;
251
+ let workspaceConfiguration : any ;
252
+
253
+ beforeEach ( ( ) => {
254
+ vscodeStub = {
255
+ workspace : {
256
+ onDidChangeConfiguration : sinon . stub ( ) ,
257
+ getConfiguration : sinon . stub ( ) ,
258
+ } ,
259
+ } ;
260
+ workspaceConfiguration = sinon . stub ( ) ;
261
+ vscodeStub . workspace . getConfiguration . returns ( workspaceConfiguration ) ;
262
+ const getConfiguration = ( workspaceConfiguration . get = sinon . stub ( ) ) ;
263
+ getConfiguration . withArgs ( "enableCodeLens" , true ) . returns ( true ) ;
264
+
265
+ utilsStub = {
266
+ nearestDartFrogProject : sinon . stub ( ) ,
267
+ } ;
268
+ utilsStub . nearestDartFrogProject . returns ( "/home/dart_frog" ) ;
269
+
270
+ DebugOnRequestCodeLensProvider = proxyquire (
271
+ "../../../code-lens/on-request-code-lens" ,
272
+ {
273
+ vscode : vscodeStub ,
274
+ // eslint-disable-next-line @typescript-eslint/naming-convention
275
+ "../utils" : utilsStub ,
276
+ }
277
+ ) . DebugOnRequestCodeLensProvider ;
278
+
279
+ document = sinon . stub ( ) ;
280
+ document . languageId = "dart" ;
281
+ document . uri = {
282
+ fsPath : "/home/dart_frog/routes/index.dart" ,
283
+ } ;
284
+ } ) ;
285
+
286
+ afterEach ( ( ) => {
287
+ sinon . restore ( ) ;
288
+ } ) ;
289
+
290
+ test ( "onDidChangeCodeLenses fires when configuration changes" , ( ) => {
291
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
292
+ const onDidChangeCodeLenses = sinon . stub ( ) ;
293
+ provider . onDidChangeCodeLenses ( onDidChangeCodeLenses ) ;
294
+
295
+ vscodeStub . workspace . onDidChangeConfiguration . callArg ( 0 ) ;
296
+
297
+ sinon . assert . calledOnce ( onDidChangeCodeLenses ) ;
298
+ } ) ;
299
+
300
+ suite ( "resolveCodeLens" , ( ) => {
301
+ test ( "returns the CodeLens when configuration is enabled" , async ( ) => {
302
+ workspaceConfiguration . get . withArgs ( "enableCodeLens" , true ) . returns ( true ) ;
213
303
304
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
305
+ const codeLens = new CodeLens ( sinon . stub ( ) ) ;
306
+ const result = await provider . resolveCodeLens ( codeLens , sinon . stub ( ) ) ;
307
+
308
+ assert . strictEqual ( result , codeLens ) ;
309
+ sinon . assert . match ( result . command , {
310
+ title : "Debug" ,
311
+ tooltip : "Starts and debugs a development server" ,
312
+ command : "dart-frog.start-debug-dev-server" ,
313
+ } ) ;
314
+ } ) ;
315
+
316
+ test ( "returns undefined when configuration is disabled" , async ( ) => {
317
+ workspaceConfiguration . get
318
+ . withArgs ( "enableCodeLens" , true )
319
+ . returns ( false ) ;
320
+
321
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
322
+ const codeLens = new CodeLens ( sinon . stub ( ) ) ;
323
+ const result = await provider . resolveCodeLens ( codeLens , sinon . stub ( ) ) ;
324
+
325
+ assert . strictEqual ( result , undefined ) ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ suite ( "providesCodeLenses" , ( ) => {
330
+ suite ( "returns undefined if the document is not" , ( ) => {
331
+ test ( "a Dart file" , ( ) => {
332
+ document . languageId = "not-dart" ;
333
+
334
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
335
+ const result = provider . provideCodeLenses ( document ) ;
336
+
337
+ assert . strictEqual ( result , undefined ) ;
338
+ } ) ;
339
+
340
+ test ( "in a Dart Frog project" , ( ) => {
341
+ utilsStub . nearestDartFrogProject . returns ( undefined ) ;
342
+
343
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
344
+ const result = provider . provideCodeLenses ( document ) ;
345
+
346
+ assert . strictEqual ( result , undefined ) ;
347
+ } ) ;
348
+
349
+ test ( "in the routes folder" , ( ) => {
350
+ document . uri = {
351
+ fsPath : "/home/dart_frog/not-routes/route.dart" ,
352
+ } ;
353
+
354
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
355
+ const result = provider . provideCodeLenses ( document ) ;
356
+
357
+ assert . strictEqual ( result , undefined ) ;
358
+ } ) ;
359
+
360
+ test ( "codeLens configuration is disabled" , ( ) => {
361
+ workspaceConfiguration . get
362
+ . withArgs ( "enableCodeLens" , true )
363
+ . returns ( false ) ;
364
+
365
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
366
+ const result = provider . provideCodeLenses ( document ) ;
367
+
368
+ assert . strictEqual ( result , undefined ) ;
369
+ } ) ;
370
+ } ) ;
371
+
372
+ test ( "returns the correct CodeLenses" , async ( ) => {
373
+ const content = routeContent ;
214
374
const textDocument = await workspace . openTextDocument ( {
215
375
language : "text" ,
216
376
content,
@@ -221,7 +381,61 @@ Response notOnRequest(RequestContext context) {
221
381
document . getWordRangeAtPosition =
222
382
textDocument . getWordRangeAtPosition . bind ( textDocument ) ;
223
383
224
- const provider = new RunOnRequestCodeLensProvider ( ) ;
384
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
385
+ const result = await provider . provideCodeLenses ( document ) ;
386
+
387
+ assert . strictEqual ( result . length , 1 ) ;
388
+
389
+ const codeLens = result [ 0 ] ;
390
+
391
+ const range = document . getWordRangeAtPosition (
392
+ new Position ( 3 , 0 ) ,
393
+ / R e s p o n s e o n R e q u e s t \( R e q u e s t C o n t e x t c o n t e x t \) { /
394
+ ) ! ;
395
+
396
+ sinon . assert . match ( codeLens , new CodeLens ( range ) ) ;
397
+ } ) ;
398
+
399
+ test ( "returns the correct CodeLenses on a dynamic route" , async ( ) => {
400
+ const content = dynamicRouteContent ;
401
+ const textDocument = await workspace . openTextDocument ( {
402
+ language : "text" ,
403
+ content,
404
+ } ) ;
405
+ document . getText = textDocument . getText . bind ( textDocument ) ;
406
+ document . positionAt = textDocument . positionAt . bind ( textDocument ) ;
407
+ document . lineAt = textDocument . lineAt . bind ( textDocument ) ;
408
+ document . getWordRangeAtPosition =
409
+ textDocument . getWordRangeAtPosition . bind ( textDocument ) ;
410
+
411
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
412
+ const result = await provider . provideCodeLenses ( document ) ;
413
+
414
+ assert . strictEqual ( result . length , 1 ) ;
415
+
416
+ const codeLens = result [ 0 ] ;
417
+
418
+ const range = document . getWordRangeAtPosition (
419
+ new Position ( 3 , 0 ) ,
420
+ / R e s p o n s e o n R e q u e s t \( R e q u e s t C o n t e x t c o n t e x t , S t r i n g i d \) { /
421
+ ) ! ;
422
+
423
+ sinon . assert . match ( codeLens , new CodeLens ( range ) ) ;
424
+ } ) ;
425
+
426
+ test ( "returns no CodeLenses on a non route file" , async ( ) => {
427
+ const content = invalidRouteContent ;
428
+ const textDocument = await workspace . openTextDocument ( {
429
+ language : "text" ,
430
+ content,
431
+ } ) ;
432
+ document . getText = textDocument . getText . bind ( textDocument ) ;
433
+ document . positionAt = textDocument . positionAt . bind ( textDocument ) ;
434
+ document . lineAt = textDocument . lineAt . bind ( textDocument ) ;
435
+ document . getWordRangeAtPosition =
436
+ textDocument . getWordRangeAtPosition . bind ( textDocument ) ;
437
+
438
+ const provider = new DebugOnRequestCodeLensProvider ( ) ;
225
439
const result = await provider . provideCodeLenses ( document ) ;
226
440
227
441
assert . strictEqual ( result . length , 0 ) ;
0 commit comments