|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp7.ReadabilityRules; |
| 10 | + using Xunit; |
| 11 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<StyleCop.Analyzers.ReadabilityRules.SA1125UseShorthandForNullableTypes>; |
7 | 12 |
|
8 | 13 | public partial class SA1125CSharp8UnitTests : SA1125CSharp7UnitTests |
9 | 14 | { |
| 15 | + /// <summary> |
| 16 | + /// Verifies that the rule continues to report diagnostics for value type nullable long forms when nullable annotations are enabled. |
| 17 | + /// </summary> |
| 18 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 19 | + [Fact] |
| 20 | + [WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")] |
| 21 | + public async Task TestNullableEnableWithLongFormValueTypesAsync() |
| 22 | + { |
| 23 | + const string testCode = @"#nullable enable |
| 24 | +
|
| 25 | +using System; |
| 26 | +
|
| 27 | +class TestClass<T> |
| 28 | + where T : struct |
| 29 | +{ |
| 30 | + private Nullable<int> field1; |
| 31 | + private System.Nullable<int> field2; |
| 32 | + private global::System.Nullable<T> field3; |
| 33 | +
|
| 34 | + private string? referenceField; |
| 35 | +}"; |
| 36 | + |
| 37 | + var expectedDiagnostics = new[] |
| 38 | + { |
| 39 | + Diagnostic().WithLocation(8, 13), |
| 40 | + Diagnostic().WithLocation(9, 13), |
| 41 | + Diagnostic().WithLocation(10, 13), |
| 42 | + }; |
| 43 | + |
| 44 | + await VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Verifies that nullable reference type annotations are not reported as violations of SA1125. |
| 49 | + /// </summary> |
| 50 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 51 | + [Fact] |
| 52 | + [WorkItem(3006, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3006")] |
| 53 | + public async Task TestNullableReferenceAnnotationsNoDiagnosticsAsync() |
| 54 | + { |
| 55 | + const string testCode = @"#nullable enable |
| 56 | +
|
| 57 | +using System.Collections.Generic; |
| 58 | +
|
| 59 | +class TestClass<T> |
| 60 | + where T : class |
| 61 | +{ |
| 62 | + private string? field; |
| 63 | + private List<string?>? list; |
| 64 | +
|
| 65 | + public T? Property { get; set; } |
| 66 | +
|
| 67 | + public T? Method(T? parameter) |
| 68 | + { |
| 69 | + return parameter; |
| 70 | + } |
| 71 | +}"; |
| 72 | + |
| 73 | + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 74 | + } |
10 | 75 | } |
11 | 76 | } |
0 commit comments