@@ -394,32 +394,66 @@ func NoLogMatching(typ protocol.MessageType, re string) LogExpectation {
394
394
}
395
395
}
396
396
397
- // RegistrationExpectation is an expectation on the capability registrations
398
- // received by the editor from gopls.
399
- type RegistrationExpectation struct {
400
- check func ([]* protocol.RegistrationParams ) Verdict
401
- description string
397
+ // FileWatchMatching expects that a file registration matches re.
398
+ func FileWatchMatching (re string ) SimpleExpectation {
399
+ return SimpleExpectation {
400
+ check : checkFileWatch (re , Met , Unmet ),
401
+ description : fmt .Sprintf ("file watch matching %q" , re ),
402
+ }
402
403
}
403
404
404
- // Check implements the Expectation interface.
405
- func (e RegistrationExpectation ) Check (s State ) Verdict {
406
- return e .check (s .registrations )
405
+ // NoFileWatchMatching expects that no file registration matches re.
406
+ func NoFileWatchMatching (re string ) SimpleExpectation {
407
+ return SimpleExpectation {
408
+ check : checkFileWatch (re , Unmet , Met ),
409
+ description : fmt .Sprintf ("no file watch matching %q" , re ),
410
+ }
407
411
}
408
412
409
- // Description implements the Expectation interface.
410
- func (e RegistrationExpectation ) Description () string {
411
- return e .description
413
+ func checkFileWatch (re string , onMatch , onNoMatch Verdict ) func (State ) Verdict {
414
+ rec := regexp .MustCompile (re )
415
+ return func (s State ) Verdict {
416
+ r := s .registeredCapabilities ["workspace/didChangeWatchedFiles" ]
417
+ watchers := jsonProperty (r .RegisterOptions , "watchers" ).([]interface {})
418
+ for _ , watcher := range watchers {
419
+ pattern := jsonProperty (watcher , "globPattern" ).(string )
420
+ if rec .MatchString (pattern ) {
421
+ return onMatch
422
+ }
423
+ }
424
+ return onNoMatch
425
+ }
426
+ }
427
+
428
+ // jsonProperty extracts a value from a path of JSON property names, assuming
429
+ // the default encoding/json unmarshaling to the empty interface (i.e.: that
430
+ // JSON objects are unmarshalled as map[string]interface{})
431
+ //
432
+ // For example, if obj is unmarshalled from the following json:
433
+ //
434
+ // {
435
+ // "foo": { "bar": 3 }
436
+ // }
437
+ //
438
+ // Then jsonProperty(obj, "foo", "bar") will be 3.
439
+ func jsonProperty (obj interface {}, path ... string ) interface {} {
440
+ if len (path ) == 0 || obj == nil {
441
+ return obj
442
+ }
443
+ m := obj .(map [string ]interface {})
444
+ return jsonProperty (m [path [0 ]], path [1 :]... )
412
445
}
413
446
414
447
// RegistrationMatching asserts that the client has received a capability
415
448
// registration matching the given regexp.
416
- func RegistrationMatching (re string ) RegistrationExpectation {
417
- rec , err := regexp .Compile (re )
418
- if err != nil {
419
- panic (err )
420
- }
421
- check := func (params []* protocol.RegistrationParams ) Verdict {
422
- for _ , p := range params {
449
+ //
450
+ // TODO(rfindley): remove this once TestWatchReplaceTargets has been revisited.
451
+ //
452
+ // Deprecated: use (No)FileWatchMatching
453
+ func RegistrationMatching (re string ) SimpleExpectation {
454
+ rec := regexp .MustCompile (re )
455
+ check := func (s State ) Verdict {
456
+ for _ , p := range s .registrations {
423
457
for _ , r := range p .Registrations {
424
458
if rec .Match ([]byte (r .Method )) {
425
459
return Met
@@ -428,38 +462,18 @@ func RegistrationMatching(re string) RegistrationExpectation {
428
462
}
429
463
return Unmet
430
464
}
431
- return RegistrationExpectation {
465
+ return SimpleExpectation {
432
466
check : check ,
433
467
description : fmt .Sprintf ("registration matching %q" , re ),
434
468
}
435
469
}
436
470
437
- // UnregistrationExpectation is an expectation on the capability
438
- // unregistrations received by the editor from gopls.
439
- type UnregistrationExpectation struct {
440
- check func ([]* protocol.UnregistrationParams ) Verdict
441
- description string
442
- }
443
-
444
- // Check implements the Expectation interface.
445
- func (e UnregistrationExpectation ) Check (s State ) Verdict {
446
- return e .check (s .unregistrations )
447
- }
448
-
449
- // Description implements the Expectation interface.
450
- func (e UnregistrationExpectation ) Description () string {
451
- return e .description
452
- }
453
-
454
471
// UnregistrationMatching asserts that the client has received an
455
472
// unregistration whose ID matches the given regexp.
456
- func UnregistrationMatching (re string ) UnregistrationExpectation {
457
- rec , err := regexp .Compile (re )
458
- if err != nil {
459
- panic (err )
460
- }
461
- check := func (params []* protocol.UnregistrationParams ) Verdict {
462
- for _ , p := range params {
473
+ func UnregistrationMatching (re string ) SimpleExpectation {
474
+ rec := regexp .MustCompile (re )
475
+ check := func (s State ) Verdict {
476
+ for _ , p := range s .unregistrations {
463
477
for _ , r := range p .Unregisterations {
464
478
if rec .Match ([]byte (r .Method )) {
465
479
return Met
@@ -468,7 +482,7 @@ func UnregistrationMatching(re string) UnregistrationExpectation {
468
482
}
469
483
return Unmet
470
484
}
471
- return UnregistrationExpectation {
485
+ return SimpleExpectation {
472
486
check : check ,
473
487
description : fmt .Sprintf ("unregistration matching %q" , re ),
474
488
}
0 commit comments