1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT license. See License.txt in the project root for license information.
3
3
4
- #nullable disable
5
-
4
+ using System ;
6
5
using System . Threading . Tasks ;
6
+ using Microsoft . AspNetCore . Razor ;
7
7
using Microsoft . AspNetCore . Razor . Language . Components ;
8
8
using Microsoft . AspNetCore . Razor . Test . Common . VisualStudio ;
9
9
using Microsoft . AspNetCore . Razor . Test . Common . Workspaces ;
10
10
using Microsoft . CodeAnalysis ;
11
11
using Microsoft . CodeAnalysis . CSharp ;
12
12
using Microsoft . CodeAnalysis . Razor . ProjectSystem ;
13
13
using Microsoft . CodeAnalysis . Text ;
14
- using Microsoft . VisualStudio . LanguageServices . Razor ;
15
14
using Microsoft . VisualStudio . Shell ;
16
15
using Microsoft . VisualStudio . Threading ;
17
16
using Xunit ;
@@ -110,9 +109,9 @@ public WorkspaceProjectStateChangeDetectorTest(ITestOutputHelper testOutput)
110
109
filePath : "Three.csproj" ,
111
110
documents : [ razorDocumentInfo ] ) . WithCompilationOutputInfo ( new CompilationOutputInfo ( ) . WithAssemblyPath ( "obj3\\ Three.dll" ) ) ) ;
112
111
113
- _projectNumberOne = _solutionWithTwoProjects . GetProject ( projectId1 ) ;
114
- _projectNumberTwo = _solutionWithTwoProjects . GetProject ( projectId2 ) ;
115
- _projectNumberThree = _solutionWithOneProject . GetProject ( projectId3 ) ;
112
+ _projectNumberOne = _solutionWithTwoProjects . GetProject ( projectId1 ) . AssumeNotNull ( ) ;
113
+ _projectNumberTwo = _solutionWithTwoProjects . GetProject ( projectId2 ) . AssumeNotNull ( ) ;
114
+ _projectNumberThree = _solutionWithOneProject . GetProject ( projectId3 ) . AssumeNotNull ( ) ;
116
115
117
116
_hostProjectOne = new HostProject ( "One.csproj" , "obj1" , FallbackRazorConfiguration . MVC_1_1 , "One" ) ;
118
117
_hostProjectTwo = new HostProject ( "Two.csproj" , "obj2" , FallbackRazorConfiguration . MVC_1_1 , "Two" ) ;
@@ -125,7 +124,7 @@ public async Task SolutionClosing_StopsActiveWork()
125
124
// Arrange
126
125
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
127
126
var projectManager = CreateProjectSnapshotManager ( ) ;
128
- using var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
127
+ using var detector = CreateDetector ( generator , projectManager ) ;
129
128
var detectorAccessor = detector . GetTestAccessor ( ) ;
130
129
131
130
var workspaceChangedTask = detectorAccessor . ListenForWorkspaceChangesAsync (
@@ -167,7 +166,7 @@ public async Task WorkspaceChanged_DocumentEvents_EnqueuesUpdatesForDependentPro
167
166
// Arrange
168
167
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
169
168
var projectManager = CreateProjectSnapshotManager ( ) ;
170
- using var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
169
+ using var detector = CreateDetector ( generator , projectManager ) ;
171
170
var detectorAccessor = detector . GetTestAccessor ( ) ;
172
171
173
172
await projectManager . UpdateAsync ( updater =>
@@ -208,7 +207,7 @@ public async Task WorkspaceChanged_ProjectEvents_EnqueuesUpdatesForDependentProj
208
207
// Arrange
209
208
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
210
209
var projectManager = CreateProjectSnapshotManager ( ) ;
211
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
210
+ using var detector = CreateDetector ( generator , projectManager ) ;
212
211
var detectorAccessor = detector . GetTestAccessor ( ) ;
213
212
214
213
await projectManager . UpdateAsync ( updater =>
@@ -251,7 +250,7 @@ public async Task WorkspaceChanged_SolutionEvents_EnqueuesUpdatesForProjectsInSo
251
250
// Arrange
252
251
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
253
252
var projectManager = CreateProjectSnapshotManager ( ) ;
254
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
253
+ using var detector = CreateDetector ( generator , projectManager ) ;
255
254
var detectorAccessor = detector . GetTestAccessor ( ) ;
256
255
257
256
await projectManager . UpdateAsync ( updater =>
@@ -270,8 +269,8 @@ await projectManager.UpdateAsync(updater =>
270
269
// Assert
271
270
Assert . Collection (
272
271
generator . Updates ,
273
- p => Assert . Equal ( _projectNumberOne . Id , p . WorkspaceProject . Id ) ,
274
- p => Assert . Equal ( _projectNumberTwo . Id , p . WorkspaceProject . Id ) ) ;
272
+ p => Assert . Equal ( _projectNumberOne . Id , p . WorkspaceProject ? . Id ) ,
273
+ p => Assert . Equal ( _projectNumberTwo . Id , p . WorkspaceProject ? . Id ) ) ;
275
274
}
276
275
277
276
[ UITheory ]
@@ -285,7 +284,7 @@ public async Task WorkspaceChanged_SolutionEvents_EnqueuesStateClear_EnqueuesSol
285
284
// Arrange
286
285
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
287
286
var projectManager = CreateProjectSnapshotManager ( ) ;
288
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
287
+ using var detector = CreateDetector ( generator , projectManager ) ;
289
288
var detectorAccessor = detector . GetTestAccessor ( ) ;
290
289
291
290
await projectManager . UpdateAsync ( updater =>
@@ -311,10 +310,10 @@ await projectManager.UpdateAsync(updater =>
311
310
// Assert
312
311
Assert . Collection (
313
312
generator . Updates ,
314
- p => Assert . Equal ( _projectNumberThree . Id , p . WorkspaceProject . Id ) ,
313
+ p => Assert . Equal ( _projectNumberThree . Id , p . WorkspaceProject ? . Id ) ,
315
314
p => Assert . Null ( p . WorkspaceProject ) ,
316
- p => Assert . Equal ( _projectNumberOne . Id , p . WorkspaceProject . Id ) ,
317
- p => Assert . Equal ( _projectNumberTwo . Id , p . WorkspaceProject . Id ) ) ;
315
+ p => Assert . Equal ( _projectNumberOne . Id , p . WorkspaceProject ? . Id ) ,
316
+ p => Assert . Equal ( _projectNumberTwo . Id , p . WorkspaceProject ? . Id ) ) ;
318
317
}
319
318
320
319
[ UITheory ]
@@ -325,7 +324,7 @@ public async Task WorkspaceChanged_ProjectChangeEvents_UpdatesProjectState_After
325
324
// Arrange
326
325
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
327
326
var projectManager = CreateProjectSnapshotManager ( ) ;
328
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
327
+ using var detector = CreateDetector ( generator , projectManager ) ;
329
328
var detectorAccessor = detector . GetTestAccessor ( ) ;
330
329
331
330
await projectManager . UpdateAsync ( updater =>
@@ -351,8 +350,8 @@ await projectManager.UpdateAsync(updater =>
351
350
352
351
// Assert
353
352
var update = Assert . Single ( generator . Updates ) ;
354
- Assert . Equal ( update . WorkspaceProject . Id , _projectNumberOne . Id ) ;
355
- Assert . Equal ( update . ProjectSnapshot . FilePath , _hostProjectOne . FilePath ) ;
353
+ Assert . Equal ( _projectNumberOne . Id , update . WorkspaceProject ? . Id ) ;
354
+ Assert . Equal ( _hostProjectOne . FilePath , update . ProjectSnapshot . FilePath ) ;
356
355
}
357
356
358
357
[ UIFact ]
@@ -361,7 +360,7 @@ public async Task WorkspaceChanged_DocumentChanged_BackgroundVirtualCS_UpdatesPr
361
360
// Arrange
362
361
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
363
362
var projectManager = CreateProjectSnapshotManager ( ) ;
364
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
363
+ using var detector = CreateDetector ( generator , projectManager ) ;
365
364
var detectorAccessor = detector . GetTestAccessor ( ) ;
366
365
367
366
Workspace . TryApplyChanges ( _solutionWithTwoProjects ) ;
@@ -383,8 +382,8 @@ await projectManager.UpdateAsync(updater =>
383
382
384
383
// Assert
385
384
var update = Assert . Single ( generator . Updates ) ;
386
- Assert . Equal ( update . WorkspaceProject . Id , _projectNumberOne . Id ) ;
387
- Assert . Equal ( update . ProjectSnapshot . FilePath , _hostProjectOne . FilePath ) ;
385
+ Assert . Equal ( _projectNumberOne . Id , update . WorkspaceProject ? . Id ) ;
386
+ Assert . Equal ( _hostProjectOne . FilePath , update . ProjectSnapshot . FilePath ) ;
388
387
}
389
388
390
389
[ UIFact ]
@@ -393,7 +392,7 @@ public async Task WorkspaceChanged_DocumentChanged_CSHTML_UpdatesProjectState_Af
393
392
// Arrange
394
393
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
395
394
var projectManager = CreateProjectSnapshotManager ( ) ;
396
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
395
+ using var detector = CreateDetector ( generator , projectManager ) ;
397
396
var detectorAccessor = detector . GetTestAccessor ( ) ;
398
397
399
398
Workspace . TryApplyChanges ( _solutionWithTwoProjects ) ;
@@ -415,8 +414,8 @@ await projectManager.UpdateAsync(updater =>
415
414
416
415
// Assert
417
416
var update = Assert . Single ( generator . Updates ) ;
418
- Assert . Equal ( update . WorkspaceProject . Id , _projectNumberOne . Id ) ;
419
- Assert . Equal ( update . ProjectSnapshot . FilePath , _hostProjectOne . FilePath ) ;
417
+ Assert . Equal ( _projectNumberOne . Id , update . WorkspaceProject ? . Id ) ;
418
+ Assert . Equal ( _hostProjectOne . FilePath , update . ProjectSnapshot . FilePath ) ;
420
419
}
421
420
422
421
[ UIFact ]
@@ -425,7 +424,7 @@ public async Task WorkspaceChanged_DocumentChanged_Razor_UpdatesProjectState_Aft
425
424
// Arrange
426
425
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
427
426
var projectManager = CreateProjectSnapshotManager ( ) ;
428
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
427
+ using var detector = CreateDetector ( generator , projectManager ) ;
429
428
var detectorAccessor = detector . GetTestAccessor ( ) ;
430
429
431
430
Workspace . TryApplyChanges ( _solutionWithTwoProjects ) ;
@@ -447,8 +446,8 @@ await projectManager.UpdateAsync(updater =>
447
446
448
447
// Assert
449
448
var update = Assert . Single ( generator . Updates ) ;
450
- Assert . Equal ( update . WorkspaceProject . Id , _projectNumberOne . Id ) ;
451
- Assert . Equal ( update . ProjectSnapshot . FilePath , _hostProjectOne . FilePath ) ;
449
+ Assert . Equal ( _projectNumberOne . Id , update . WorkspaceProject ? . Id ) ;
450
+ Assert . Equal ( _hostProjectOne . FilePath , update . ProjectSnapshot . FilePath ) ;
452
451
}
453
452
454
453
[ UIFact ]
@@ -457,7 +456,7 @@ public async Task WorkspaceChanged_DocumentChanged_PartialComponent_UpdatesProje
457
456
// Arrange
458
457
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
459
458
var projectManager = CreateProjectSnapshotManager ( ) ;
460
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
459
+ using var detector = CreateDetector ( generator , projectManager ) ;
461
460
var detectorAccessor = detector . GetTestAccessor ( ) ;
462
461
463
462
Workspace . TryApplyChanges ( _solutionWithTwoProjects ) ;
@@ -482,6 +481,7 @@ public interface IComponent {}
482
481
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
483
482
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
484
483
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
484
+ Assert . NotNull ( document ) ;
485
485
486
486
// The change detector only operates when a semantic model / syntax tree is available.
487
487
await document . GetSyntaxRootAsync ( ) ;
@@ -496,8 +496,8 @@ public interface IComponent {}
496
496
497
497
// Assert
498
498
var update = Assert . Single ( generator . Updates ) ;
499
- Assert . Equal ( update . WorkspaceProject . Id , _projectNumberOne . Id ) ;
500
- Assert . Equal ( update . ProjectSnapshot . FilePath , _hostProjectOne . FilePath ) ;
499
+ Assert . Equal ( _projectNumberOne . Id , update . WorkspaceProject ? . Id ) ;
500
+ Assert . Equal ( _hostProjectOne . FilePath , update . ProjectSnapshot . FilePath ) ;
501
501
}
502
502
503
503
[ UIFact ]
@@ -506,7 +506,7 @@ public async Task WorkspaceChanged_ProjectRemovedEvent_QueuesProjectStateRemoval
506
506
// Arrange
507
507
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
508
508
var projectManager = CreateProjectSnapshotManager ( ) ;
509
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
509
+ using var detector = CreateDetector ( generator , projectManager ) ;
510
510
var detectorAccessor = detector . GetTestAccessor ( ) ;
511
511
512
512
await projectManager . UpdateAsync ( updater =>
@@ -534,7 +534,7 @@ public async Task WorkspaceChanged_ProjectAddedEvent_AddsProject()
534
534
// Arrange
535
535
var generator = new TestProjectWorkspaceStateGenerator ( ) ;
536
536
var projectManager = CreateProjectSnapshotManager ( ) ;
537
- var detector = new WorkspaceProjectStateChangeDetector ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider ) ;
537
+ using var detector = CreateDetector ( generator , projectManager ) ;
538
538
var detectorAccessor = detector . GetTestAccessor ( ) ;
539
539
540
540
await projectManager . UpdateAsync ( updater =>
@@ -554,7 +554,7 @@ await projectManager.UpdateAsync(updater =>
554
554
// Assert
555
555
Assert . Single (
556
556
generator . Updates ,
557
- p => p . WorkspaceProject . Id == _projectNumberThree . Id ) ;
557
+ p => p . WorkspaceProject ? . Id == _projectNumberThree . Id ) ;
558
558
}
559
559
560
560
[ Fact ]
@@ -569,6 +569,7 @@ public partial class TestComponent{}
569
569
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
570
570
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
571
571
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
572
+ Assert . NotNull ( document ) ;
572
573
573
574
// Initialize document
574
575
await document . GetSyntaxRootAsync ( ) ;
@@ -597,6 +598,7 @@ public interface IComponent {}
597
598
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
598
599
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
599
600
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
601
+ Assert . NotNull ( document ) ;
600
602
601
603
// Initialize document
602
604
await document . GetSyntaxRootAsync ( ) ;
@@ -625,6 +627,7 @@ public interface IComponent {}
625
627
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
626
628
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
627
629
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
630
+ Assert . NotNull ( document ) ;
628
631
629
632
// Act
630
633
var result = WorkspaceProjectStateChangeDetector . IsPartialComponentClass ( document ) ;
@@ -649,6 +652,7 @@ public interface IComponent {}
649
652
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
650
653
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
651
654
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
655
+ Assert . NotNull ( document ) ;
652
656
653
657
await document . GetSyntaxRootAsync ( ) ;
654
658
@@ -669,6 +673,7 @@ public async Task IsPartialComponentClass_NonClass_ReturnsFalse()
669
673
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
670
674
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
671
675
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
676
+ Assert . NotNull ( document ) ;
672
677
673
678
// Initialize document
674
679
await document . GetSyntaxRootAsync ( ) ;
@@ -684,7 +689,6 @@ public async Task IsPartialComponentClass_NonClass_ReturnsFalse()
684
689
[ Fact ]
685
690
public async Task IsPartialComponentClass_MultipleClassesOneComponentPartial_ReturnsTrue ( )
686
691
{
687
-
688
692
// Arrange
689
693
var sourceText = SourceText . From ( $$ """
690
694
public partial class NonComponent1 {}
@@ -702,6 +706,7 @@ public interface IComponent {}
702
706
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
703
707
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
704
708
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
709
+ Assert . NotNull ( document ) ;
705
710
706
711
// Initialize document
707
712
await document . GetSyntaxRootAsync ( ) ;
@@ -717,7 +722,6 @@ public interface IComponent {}
717
722
[ Fact ]
718
723
public async Task IsPartialComponentClass_NonComponents_ReturnsFalse ( )
719
724
{
720
-
721
725
// Arrange
722
726
var sourceText = SourceText . From ( """
723
727
public partial class NonComponent1 {}
@@ -734,6 +738,7 @@ public interface IComponent {}
734
738
. WithDocumentText ( _partialComponentClassDocumentId , sourceText )
735
739
. WithDocumentSyntaxRoot ( _partialComponentClassDocumentId , syntaxTreeRoot , PreservationMode . PreserveIdentity ) ;
736
740
var document = solution . GetDocument ( _partialComponentClassDocumentId ) ;
741
+ Assert . NotNull ( document ) ;
737
742
738
743
// Initialize document
739
744
await document . GetSyntaxRootAsync ( ) ;
@@ -745,4 +750,7 @@ public interface IComponent {}
745
750
// Assert
746
751
Assert . False ( result ) ;
747
752
}
753
+
754
+ private WorkspaceProjectStateChangeDetector CreateDetector ( IProjectWorkspaceStateGenerator generator , IProjectSnapshotManager projectManager )
755
+ => new ( generator , projectManager , TestLanguageServerFeatureOptions . Instance , WorkspaceProvider , TimeSpan . FromMilliseconds ( 10 ) ) ;
748
756
}
0 commit comments