Skip to content

Commit 8f9a9dc

Browse files
committed
Enable nullable reference types in test project
1 parent 63d9178 commit 8f9a9dc

18 files changed

+97
-218
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/CSharpGuidelinesAnalysisTestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace CSharpGuidelinesAnalyzer.Test
66
{
77
public abstract class CSharpGuidelinesAnalysisTestFixture : AnalysisTestFixture
88
{
9-
private protected async Task VerifyGuidelineDiagnosticAsync([NotNull] ParsedSourceCode source, [NotNull] [ItemNotNull] params string[] messages)
9+
private protected async Task VerifyGuidelineDiagnosticAsync(ParsedSourceCode source, params string[] messages)
1010
{
1111
Guard.NotNull(source, nameof(source));
1212
Guard.NotNull(messages, nameof(messages));

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/CSharpGuidelinesAnalyzer.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>net60</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
57
</PropertyGroup>
68

79
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -25,7 +27,6 @@
2527
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
2628
<PackageReference Include="OpenCover" Version="4.7.1221" />
2729
<PackageReference Include="ReportGenerator" Version="5.0.4" />
28-
<PackageReference Include="ResharperCodeContractNullability" Version="2.0.2" PrivateAssets="all" />
2930
<PackageReference Include="xunit" Version="2.4.1" />
3031
<PackageReference Include="xunit.runner.console" Version="2.4.1">
3132
<PrivateAssets>all</PrivateAssets>

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/ParsedSourceCode.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ namespace CSharpGuidelinesAnalyzer.Test
55
{
66
internal sealed class ParsedSourceCode
77
{
8-
[NotNull]
98
public AnalyzerTestContext TestContext { get; }
109

11-
public ParsedSourceCode([NotNull] string sourceText, [NotNull] AnalyzerTestContext testContext)
10+
public ParsedSourceCode(string sourceText, AnalyzerTestContext testContext)
1211
{
1312
Guard.NotNull(sourceText, nameof(sourceText));
1413
Guard.NotNull(testContext, nameof(testContext));

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/RoslynTestFramework/AnalysisTestFixture.cs

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,35 @@ namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework
1414
{
1515
public abstract class AnalysisTestFixture
1616
{
17-
[NotNull]
1817
protected abstract string DiagnosticId { get; }
1918

20-
[NotNull]
2119
protected abstract DiagnosticAnalyzer CreateAnalyzer();
2220

23-
protected async Task AssertDiagnosticsAsync([NotNull] AnalyzerTestContext context, [NotNull] [ItemNotNull] params string[] messages)
21+
protected async Task AssertDiagnosticsAsync(AnalyzerTestContext context, params string[] messages)
2422
{
2523
FrameworkGuard.NotNull(context, nameof(context));
2624
FrameworkGuard.NotNull(messages, nameof(messages));
2725

2826
await RunDiagnosticsAsync(context, messages);
2927
}
3028

31-
private async Task RunDiagnosticsAsync([NotNull] AnalyzerTestContext context,
32-
[NotNull] [ItemNotNull] params string[] messages)
29+
private async Task RunDiagnosticsAsync(AnalyzerTestContext context, params string[] messages)
3330
{
3431
AnalysisResult result = await GetAnalysisResultAsync(context, messages);
3532

3633
VerifyDiagnosticCount(result);
3734
VerifyDiagnostics(result);
3835
}
3936

40-
[NotNull]
41-
[ItemNotNull]
42-
private async Task<AnalysisResult> GetAnalysisResultAsync([NotNull] AnalyzerTestContext context, [NotNull] [ItemNotNull] string[] messages)
37+
private async Task<AnalysisResult> GetAnalysisResultAsync(AnalyzerTestContext context, string[] messages)
4338
{
4439
var document = DocumentFactory.ToDocument(context.SourceCode, context);
4540

4641
IList<Diagnostic> diagnostics = await GetSortedAnalyzerDiagnosticsAsync(document, context);
4742
return new AnalysisResult(diagnostics, context.SourceSpans, messages);
4843
}
4944

50-
[NotNull]
51-
[ItemNotNull]
52-
private async Task<IList<Diagnostic>> GetSortedAnalyzerDiagnosticsAsync([NotNull] Document document, [NotNull] AnalyzerTestContext context)
45+
private async Task<IList<Diagnostic>> GetSortedAnalyzerDiagnosticsAsync(Document document, AnalyzerTestContext context)
5346
{
5447
var diagnostics = new List<Diagnostic>();
5548

@@ -64,30 +57,24 @@ private async Task<IList<Diagnostic>> GetSortedAnalyzerDiagnosticsAsync([NotNull
6457
return diagnostics.OrderBy(diagnostic => diagnostic.Location.SourceSpan).ToImmutableArray();
6558
}
6659

67-
[NotNull]
68-
[ItemNotNull]
69-
private async IAsyncEnumerable<Diagnostic> EnumerateDiagnosticsForDocumentAsync([NotNull] Document document,
70-
[NotNull] AnalyzerTestContext context)
60+
private async IAsyncEnumerable<Diagnostic> EnumerateDiagnosticsForDocumentAsync(Document document, AnalyzerTestContext context)
7161
{
7262
CompilationWithAnalyzers compilationWithAnalyzers =
7363
await GetCompilationWithAnalyzersAsync(document, context.ValidationMode, context.Options);
7464

75-
SyntaxTree tree = await document.GetSyntaxTreeAsync();
65+
SyntaxTree? tree = await document.GetSyntaxTreeAsync();
7666

7767
await foreach (Diagnostic diagnostic in EnumerateAnalyzerDiagnosticsAsync(compilationWithAnalyzers, tree!))
7868
{
7969
yield return diagnostic;
8070
}
8171
}
8272

83-
[NotNull]
84-
[ItemNotNull]
85-
private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync([NotNull] Document document,
86-
TestValidationMode validationMode, [NotNull] AnalyzerOptions options)
73+
private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync(Document document, TestValidationMode validationMode, AnalyzerOptions options)
8774
{
8875
DiagnosticAnalyzer analyzer = CreateAnalyzer();
8976

90-
Compilation compilation = await document.Project.GetCompilationAsync();
77+
Compilation? compilation = await document.Project.GetCompilationAsync();
9178
compilation = EnsureAnalyzerIsEnabled(analyzer, compilation!);
9279

9380
ImmutableArray<Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None);
@@ -101,9 +88,7 @@ private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync([N
10188
return compilation.WithAnalyzers(analyzers, options);
10289
}
10390

104-
[NotNull]
105-
private static Compilation EnsureAnalyzerIsEnabled([NotNull] DiagnosticAnalyzer analyzer,
106-
[NotNull] Compilation compilation)
91+
private static Compilation EnsureAnalyzerIsEnabled(DiagnosticAnalyzer analyzer, Compilation compilation)
10792
{
10893
ImmutableDictionary<string, ReportDiagnostic> diagnosticOptions = compilation.Options.SpecificDiagnosticOptions;
10994

@@ -119,18 +104,15 @@ private static Compilation EnsureAnalyzerIsEnabled([NotNull] DiagnosticAnalyzer
119104
return compilation.WithOptions(compilationWithSpecificOptions);
120105
}
121106

122-
private void ValidateCompileErrors([ItemNotNull] ImmutableArray<Diagnostic> compilerDiagnostics)
107+
private void ValidateCompileErrors(ImmutableArray<Diagnostic> compilerDiagnostics)
123108
{
124109
Diagnostic[] compilerErrors = compilerDiagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error)
125110
.ToArray();
126111

127112
compilerErrors.Should().BeEmpty("test should not have compile errors");
128113
}
129114

130-
[NotNull]
131-
[ItemNotNull]
132-
private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAsync([NotNull] CompilationWithAnalyzers compilationWithAnalyzers,
133-
[NotNull] SyntaxTree tree)
115+
private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAsync(CompilationWithAnalyzers compilationWithAnalyzers, SyntaxTree tree)
134116
{
135117
foreach (Diagnostic diagnostic in await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync())
136118
{
@@ -145,7 +127,7 @@ private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAs
145127
}
146128
}
147129

148-
private static void ThrowForCrashingAnalyzer([NotNull] Diagnostic diagnostic)
130+
private static void ThrowForCrashingAnalyzer(Diagnostic diagnostic)
149131
{
150132
if (diagnostic.Id == "AD0001")
151133
{
@@ -154,19 +136,19 @@ private static void ThrowForCrashingAnalyzer([NotNull] Diagnostic diagnostic)
154136
}
155137
}
156138

157-
private static void VerifyDiagnosticCount([NotNull] AnalysisResult result)
139+
private static void VerifyDiagnosticCount(AnalysisResult result)
158140
{
159141
result.DiagnosticsWithLocation.Should().HaveSameCount(result.SpansExpected);
160142
result.Diagnostics.Should().HaveSameCount(result.MessagesExpected);
161143
}
162144

163-
private static void VerifyDiagnostics([NotNull] AnalysisResult result)
145+
private static void VerifyDiagnostics(AnalysisResult result)
164146
{
165147
VerifyDiagnosticMessages(result);
166148
VerifyDiagnosticLocations(result);
167149
}
168150

169-
private static void VerifyDiagnosticMessages([NotNull] AnalysisResult result)
151+
private static void VerifyDiagnosticMessages(AnalysisResult result)
170152
{
171153
int messageIndex = 0;
172154

@@ -180,7 +162,7 @@ private static void VerifyDiagnosticMessages([NotNull] AnalysisResult result)
180162
}
181163
}
182164

183-
private static void VerifyDiagnosticLocations([NotNull] AnalysisResult result)
165+
private static void VerifyDiagnosticLocations(AnalysisResult result)
184166
{
185167
int spanIndex = 0;
186168

@@ -195,23 +177,16 @@ private static void VerifyDiagnosticLocations([NotNull] AnalysisResult result)
195177

196178
private sealed class AnalysisResult
197179
{
198-
[NotNull]
199-
[ItemNotNull]
200180
public IList<Diagnostic> Diagnostics { get; }
201181

202-
[NotNull]
203-
[ItemNotNull]
204182
public IList<Diagnostic> DiagnosticsWithLocation => Diagnostics.Where(diagnostic => diagnostic.Location.IsInSource).ToArray();
205183

206-
[NotNull]
207184
public IList<TextSpan> SpansExpected { get; }
208185

209-
[NotNull]
210-
[ItemNotNull]
211186
public IList<string> MessagesExpected { get; }
212187

213-
public AnalysisResult([NotNull] [ItemNotNull] IList<Diagnostic> diagnostics, [NotNull] IList<TextSpan> spansExpected,
214-
[NotNull] [ItemNotNull] IList<string> messagesExpected)
188+
public AnalysisResult(IList<Diagnostic> diagnostics, IList<TextSpan> spansExpected,
189+
IList<string> messagesExpected)
215190
{
216191
FrameworkGuard.NotNull(diagnostics, nameof(diagnostics));
217192
FrameworkGuard.NotNull(spansExpected, nameof(spansExpected));

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/RoslynTestFramework/AnalyzerTestContext.cs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,18 @@ public sealed class AnalyzerTestContext
1818
private const OutputKind DefaultOutputKind = OutputKind.DynamicallyLinkedLibrary;
1919
private const TestValidationMode DefaultTestValidationMode = TestValidationMode.AllowCompileWarnings;
2020

21-
[NotNull]
22-
[ItemNotNull]
2321
private static readonly Lazy<ImmutableHashSet<MetadataReference>> DefaultReferencesLazy = new Lazy<ImmutableHashSet<MetadataReference>>(
2422
() => ReferenceAssemblies.Net.Net60.ResolveAsync(null, CancellationToken.None).Result.ToImmutableHashSet(),
2523
LazyThreadSafetyMode.ExecutionAndPublication);
2624

27-
[NotNull]
2825
public string SourceCode { get; }
2926

30-
[NotNull]
3127
public IList<TextSpan> SourceSpans { get; }
3228

33-
[NotNull]
3429
public string FileName { get; }
3530

36-
[NotNull]
3731
public string AssemblyName { get; }
3832

39-
[NotNull]
40-
[ItemNotNull]
4133
public ImmutableHashSet<MetadataReference> References { get; }
4234

4335
public DocumentationMode DocumentationMode { get; }
@@ -48,10 +40,9 @@ public sealed class AnalyzerTestContext
4840

4941
public TestValidationMode ValidationMode { get; }
5042

51-
[NotNull]
5243
public AnalyzerOptions Options { get; }
5344

54-
public AnalyzerTestContext([NotNull] string sourceCode, [NotNull] IList<TextSpan> sourceSpans, [NotNull] AnalyzerOptions options)
45+
public AnalyzerTestContext(string sourceCode, IList<TextSpan> sourceSpans, AnalyzerOptions options)
5546
: this(sourceCode, sourceSpans, DefaultFileName, DefaultAssemblyName, DefaultReferencesLazy.Value, DefaultDocumentationMode, DefaultOutputKind,
5647
TreatWarningsAsErrors.None, DefaultTestValidationMode, options)
5748
{
@@ -61,9 +52,9 @@ public AnalyzerTestContext([NotNull] string sourceCode, [NotNull] IList<TextSpan
6152
}
6253

6354
#pragma warning disable AV1561 // Signature contains too many parameters
64-
private AnalyzerTestContext([NotNull] string sourceCode, [NotNull] IList<TextSpan> sourceSpans, [NotNull] string fileName,
65-
[NotNull] string assemblyName, [NotNull] [ItemNotNull] ImmutableHashSet<MetadataReference> references, DocumentationMode documentationMode,
66-
OutputKind outputKind, TreatWarningsAsErrors warningsAsErrors, TestValidationMode validationMode, [NotNull] AnalyzerOptions options)
55+
private AnalyzerTestContext(string sourceCode, IList<TextSpan> sourceSpans, string fileName, string assemblyName,
56+
ImmutableHashSet<MetadataReference> references, DocumentationMode documentationMode, OutputKind outputKind, TreatWarningsAsErrors warningsAsErrors,
57+
TestValidationMode validationMode, AnalyzerOptions options)
6758
{
6859
SourceCode = sourceCode;
6960
SourceSpans = sourceSpans;
@@ -78,8 +69,7 @@ private AnalyzerTestContext([NotNull] string sourceCode, [NotNull] IList<TextSpa
7869
}
7970
#pragma warning restore AV1561 // Signature contains too many parameters
8071

81-
[NotNull]
82-
public AnalyzerTestContext WithCode([NotNull] string sourceCode, [NotNull] IList<TextSpan> sourceSpans)
72+
public AnalyzerTestContext WithCode(string sourceCode, IList<TextSpan> sourceSpans)
8373
{
8474
FrameworkGuard.NotNull(sourceCode, nameof(sourceCode));
8575
FrameworkGuard.NotNull(sourceSpans, nameof(sourceSpans));
@@ -88,24 +78,21 @@ public AnalyzerTestContext WithCode([NotNull] string sourceCode, [NotNull] IList
8878
DocumentationMode, OutputKind, WarningsAsErrors, ValidationMode, Options);
8979
}
9080

91-
[NotNull]
92-
public AnalyzerTestContext InFileNamed([NotNull] string fileName)
81+
public AnalyzerTestContext InFileNamed(string fileName)
9382
{
9483
FrameworkGuard.NotNullNorWhiteSpace(fileName, nameof(fileName));
9584

9685
return new AnalyzerTestContext(SourceCode, SourceSpans, fileName, AssemblyName, References,
9786
DocumentationMode, OutputKind, WarningsAsErrors, ValidationMode, Options);
9887
}
9988

100-
[NotNull]
101-
public AnalyzerTestContext InAssemblyNamed([NotNull] string assemblyName)
89+
public AnalyzerTestContext InAssemblyNamed(string assemblyName)
10290
{
10391
return new AnalyzerTestContext(SourceCode, SourceSpans, FileName, assemblyName, References,
10492
DocumentationMode, OutputKind, WarningsAsErrors, ValidationMode, Options);
10593
}
10694

107-
[NotNull]
108-
public AnalyzerTestContext WithReferences([NotNull] [ItemNotNull] IEnumerable<MetadataReference> references)
95+
public AnalyzerTestContext WithReferences(IEnumerable<MetadataReference> references)
10996
{
11097
FrameworkGuard.NotNull(references, nameof(references));
11198

@@ -116,36 +103,31 @@ public AnalyzerTestContext WithReferences([NotNull] [ItemNotNull] IEnumerable<Me
116103
ValidationMode, Options);
117104
}
118105

119-
[NotNull]
120106
public AnalyzerTestContext WithDocumentationMode(DocumentationMode mode)
121107
{
122108
return new AnalyzerTestContext(SourceCode, SourceSpans, FileName, AssemblyName, References, mode,
123109
OutputKind, WarningsAsErrors, ValidationMode, Options);
124110
}
125111

126-
[NotNull]
127112
public AnalyzerTestContext WithOutputKind(OutputKind outputKind)
128113
{
129114
return new AnalyzerTestContext(SourceCode, SourceSpans, FileName, AssemblyName, References,
130115
DocumentationMode, outputKind, WarningsAsErrors, ValidationMode, Options);
131116
}
132117

133-
[NotNull]
134118
public AnalyzerTestContext CompileWithWarningsAsErrors(TreatWarningsAsErrors warningsAsErrors)
135119
{
136120
return new AnalyzerTestContext(SourceCode, SourceSpans, FileName, AssemblyName, References,
137121
DocumentationMode, OutputKind, warningsAsErrors, ValidationMode, Options);
138122
}
139123

140-
[NotNull]
141124
public AnalyzerTestContext InValidationMode(TestValidationMode validationMode)
142125
{
143126
return new AnalyzerTestContext(SourceCode, SourceSpans, FileName, AssemblyName, References,
144127
DocumentationMode, OutputKind, WarningsAsErrors, validationMode, Options);
145128
}
146129

147-
[NotNull]
148-
public AnalyzerTestContext WithOptions([NotNull] AnalyzerOptions options)
130+
public AnalyzerTestContext WithOptions(AnalyzerOptions options)
149131
{
150132
FrameworkGuard.NotNull(options, nameof(options));
151133

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/RoslynTestFramework/DocumentFactory.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework
77
/// <summary />
88
internal static class DocumentFactory
99
{
10-
[NotNull]
1110
private static readonly CSharpCompilationOptions DefaultCompilationOptions =
1211
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
1312

14-
[NotNull]
1513
private static readonly CSharpParseOptions DefaultParseOptions = new CSharpParseOptions();
1614

17-
[NotNull]
18-
public static Document ToDocument([NotNull] string code, [NotNull] AnalyzerTestContext context)
15+
public static Document ToDocument(string code, AnalyzerTestContext context)
1916
{
2017
ParseOptions parseOptions = GetParseOptions(context.DocumentationMode);
2118
CompilationOptions compilationOptions = GetCompilationOptions(context);
@@ -30,14 +27,12 @@ public static Document ToDocument([NotNull] string code, [NotNull] AnalyzerTestC
3027
return document;
3128
}
3229

33-
[NotNull]
3430
private static ParseOptions GetParseOptions(DocumentationMode documentationMode)
3531
{
3632
return DefaultParseOptions.WithDocumentationMode(documentationMode);
3733
}
3834

39-
[NotNull]
40-
private static CompilationOptions GetCompilationOptions([NotNull] AnalyzerTestContext context)
35+
private static CompilationOptions GetCompilationOptions(AnalyzerTestContext context)
4136
{
4237
CompilationOptions options = DefaultCompilationOptions;
4338

0 commit comments

Comments
 (0)