1
1
using Microsoft . CodeAnalysis . CSharp . Testing ;
2
2
using Microsoft . CodeAnalysis . Testing . Verifiers ;
3
3
using SourceKit . Analyzers . MustBePartial . Analyzers ;
4
+ using SourceKit . Analyzers . MustBePartial . Annotations ;
5
+ using SourceKit . Sample . Analyzers . MustBePartial ;
6
+ using SourceKit . Tests . Tools ;
4
7
using Xunit ;
5
- using Verify = Microsoft . CodeAnalysis . CSharp . Testing . XUnit . AnalyzerVerifier <
8
+ using AnalyzerVerifier = Microsoft . CodeAnalysis . CSharp . Testing . XUnit . AnalyzerVerifier <
6
9
SourceKit . Analyzers . MustBePartial . Analyzers . DerivativesMustBePartialAnalyzer > ;
10
+ using CodeFixTest = Microsoft . CodeAnalysis . CSharp . Testing . CSharpCodeFixTest <
11
+ SourceKit . Analyzers . MustBePartial . Analyzers . DerivativesMustBePartialAnalyzer ,
12
+ SourceKit . Analyzers . MustBePartial . CodeFixes . MakeTypePartialCodeFixProvider ,
13
+ Microsoft . CodeAnalysis . Testing . Verifiers . XUnitVerifier > ;
7
14
8
15
namespace SourceKit . Tests . Analyzers ;
9
16
10
17
public class MustBePartialTests
11
18
{
12
19
[ Fact ]
13
- public async Task A ( )
20
+ public async Task DerivativesMustBePartial_ShouldReportDiagnostic_WhenTypeIsNotPartial ( )
14
21
{
15
- var subject = File . ReadAllText ( "MustBePartial/NotPartialDerivative.cs" ) ;
22
+ var sourceFile = await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/NonPartialDerivative.cs" ) ;
23
+
24
+ var diagnostic = AnalyzerVerifier . Diagnostic ( DerivativesMustBePartialAnalyzer . Descriptor )
25
+ . WithLocation ( sourceFile . Name , 3 , 14 )
26
+ . WithArguments ( nameof ( NonPartialDerivative ) ) ;
16
27
17
28
var test = new CSharpAnalyzerTest < DerivativesMustBePartialAnalyzer , XUnitVerifier >
18
29
{
19
30
TestState =
20
31
{
21
- Sources = { subject } ,
22
- AdditionalFiles = { await LoadFileAsync ( "MustBePartial/IPartialBase.cs" ) } ,
23
- ExpectedDiagnostics = { Verify . Diagnostic ( DerivativesMustBePartialAnalyzer . Descriptor ) } ,
32
+ Sources =
33
+ {
34
+ sourceFile ,
35
+ await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/IPartialBase.cs" ) ,
36
+ } ,
37
+ AdditionalReferences = { typeof ( DerivativesMustBePartialAttribute ) . Assembly } ,
24
38
} ,
39
+ ExpectedDiagnostics = { diagnostic } ,
25
40
} ;
26
41
27
42
await test . RunAsync ( ) ;
28
43
}
29
44
30
- private static Task < ( string name , string content ) > LoadFileAsync ( string path )
45
+ [ Fact ]
46
+ public async Task DerivativesMustBePartial_ShouldReportNoDiagnostic_WhenTypeIsPartial ( )
31
47
{
32
- var name = Path . ChangeExtension ( Path . GetFileName ( path ) , null ) ;
33
- var content = File . ReadAllText ( path ) ;
48
+ var test = new CSharpAnalyzerTest < DerivativesMustBePartialAnalyzer , XUnitVerifier >
49
+ {
50
+ TestState =
51
+ {
52
+ Sources =
53
+ {
54
+ await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/PartialDerivative.cs" ) ,
55
+ await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/IPartialBase.cs" ) ,
56
+ } ,
57
+ AdditionalReferences = { typeof ( DerivativesMustBePartialAttribute ) . Assembly } ,
58
+ } ,
59
+ } ;
60
+
61
+ await test . RunAsync ( ) ;
62
+ }
34
63
35
- return Task . FromResult ( ( name , content ) ) ;
64
+ [ Fact ]
65
+ public async Task MakeTypePartial_ShouldMakeTypePartial_WhenDiagnosticReported ( )
66
+ {
67
+ var interfaceSource = await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/IPartialBase.cs" ) ;
68
+ var sourceFile = await SourceFile . LoadAsync ( "SourceKit.Sample/Analyzers/MustBePartial/NonPartialDerivative.cs" ) ;
69
+ var fixedContent = sourceFile . Content . Replace ( "public class" , "public partial class" ) ;
70
+
71
+ var fixedSource = sourceFile with { Content = fixedContent } ;
72
+
73
+ var diagnostic = AnalyzerVerifier . Diagnostic ( DerivativesMustBePartialAnalyzer . Descriptor )
74
+ . WithLocation ( sourceFile . Name , 3 , 14 )
75
+ . WithArguments ( nameof ( NonPartialDerivative ) ) ;
76
+
77
+ var test = new CodeFixTest
78
+ {
79
+ TestState =
80
+ {
81
+ Sources = { sourceFile , interfaceSource , } ,
82
+ AdditionalReferences = { typeof ( DerivativesMustBePartialAttribute ) . Assembly } ,
83
+ } ,
84
+ FixedState =
85
+ {
86
+ Sources = { fixedSource , interfaceSource } ,
87
+ } ,
88
+ ExpectedDiagnostics = { diagnostic } ,
89
+ } ;
90
+
91
+ await test . RunAsync ( ) ;
36
92
}
37
93
}
0 commit comments