Skip to content

Commit 87555a0

Browse files
committed
Nullable in tests.
1 parent 6ef28c0 commit 87555a0

90 files changed

Lines changed: 472 additions & 242 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ReflectionAnalyzers.Tests/LibrarySettings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ internal static class LibrarySettings
2525
internal static void Initialize()
2626
{
2727
Settings.Default = Settings.Default
28-
.WithCompilationOptions(x => x.WithNullableContextOptions(NullableContextOptions.Disable))
2928
.WithMetadataReferences(MetadataReferences.Transitive(typeof(LibrarySettings), typeof(System.Windows.Controls.Control), typeof(System.Windows.Forms.Control)));
3029
}
3130
}

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/CodeFix.ConstructorInfoInvoke.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ public static class ConstructorInfoInvoke
1111
private static readonly CastReturnValueFix Fix = new();
1212
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.REFL001CastReturnValue);
1313

14-
[Test]
15-
public static void AssigningLocal()
14+
[TestCase("typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 })")]
15+
[TestCase("typeof(C).GetConstructor(new[] { typeof(int) })?.Invoke(new object[] { 1 })")]
16+
[TestCase("typeof(C).GetConstructor(new[] { typeof(int) })!.Invoke(new object[] { 1 })")]
17+
public static void AssigningLocal(string expression)
1618
{
1719
var before = @"
20+
#pragma warning disable CS8600, CS8602
1821
namespace N
1922
{
2023
public class C
@@ -24,9 +27,10 @@ public C(int i)
2427
var value = ↓typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 });
2528
}
2629
}
27-
}";
30+
}".AssertReplace("typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 })", expression);
2831

2932
var after = @"
33+
#pragma warning disable CS8600, CS8602
3034
namespace N
3135
{
3236
public class C
@@ -36,14 +40,17 @@ public C(int i)
3640
var value = (C)typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 });
3741
}
3842
}
39-
}";
43+
}".AssertReplace("typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 })", expression);
4044
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
4145
}
4246

43-
[Test]
44-
public static void Walk()
47+
[TestCase("info.Invoke(new object[] { 1 })")]
48+
//[TestCase("info?.Invoke(new object[] { 1 })")]
49+
//[TestCase("info!.Invoke(new object[] { 1 })")]
50+
public static void Walk(string expression)
4551
{
4652
var code = @"
53+
#pragma warning disable CS8600, CS8602
4754
namespace N
4855
{
4956
public class C
@@ -54,7 +61,7 @@ public C(int i)
5461
var value = ↓info.Invoke(new object[] { 1 });
5562
}
5663
}
57-
}";
64+
}".AssertReplace("info.Invoke(new object[] { 1 })", expression);
5865

5966
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
6067
}

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/CodeFix.DelegateCreateDelegate.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ public static class DelegateCreateDelegate
1111
private static readonly CastReturnValueFix Fix = new();
1212
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.REFL001CastReturnValue);
1313

14-
[Test]
15-
public static void AssigningLocal()
14+
[TestCase("typeof(C).GetMethod(nameof(M))")]
15+
[TestCase("typeof(C).GetMethod(nameof(M))!")]
16+
public static void AssigningLocal(string expression)
1617
{
1718
var before = @"
19+
#pragma warning disable CS8604
1820
namespace N
1921
{
2022
using System;
@@ -30,9 +32,10 @@ public C()
3032
3133
public static int M() => 0;
3234
}
33-
}";
35+
}".AssertReplace("typeof(C).GetMethod(nameof(M))", expression);
3436

3537
var after = @"
38+
#pragma warning disable CS8604
3639
namespace N
3740
{
3841
using System;
@@ -48,14 +51,15 @@ public C()
4851
4952
public static int M() => 0;
5053
}
51-
}";
54+
}".AssertReplace("typeof(C).GetMethod(nameof(M))", expression);
5255
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
5356
}
5457

5558
[Test]
5659
public static void Walk()
5760
{
5861
var before = @"
62+
#pragma warning disable CS8604
5963
namespace N
6064
{
6165
using System;
@@ -75,6 +79,7 @@ public C()
7579
}";
7680

7781
var after = @"
82+
#pragma warning disable CS8604
7883
namespace N
7984
{
8085
using System;

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/Diagnostics.ConstructorInfoInvoke.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ReflectionAnalyzers.Tests.REFL001CastReturnValueTests
1+
namespace ReflectionAnalyzers.Tests.REFL001CastReturnValueTests
22
{
33
using Gu.Roslyn.Asserts;
44
using NUnit.Framework;
@@ -20,7 +20,7 @@ public class C
2020
{
2121
public C(int i)
2222
{
23-
var value = ↓typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(new object[] { 1 });
23+
var value = ↓typeof(C).GetConstructor(new[] { typeof(int) })?.Invoke(new object[] { 1 });
2424
}
2525
}
2626
}";

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/Diagnostics.MethodInfoInvoke.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ public static class MethodInfoInvoke
1010
private static readonly InvokeAnalyzer Analyzer = new();
1111
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.REFL001CastReturnValue);
1212

13-
[Test]
14-
public static void Simple()
13+
[TestCase("typeof(C).GetMethod(nameof(M)).Invoke(null, null)")]
14+
[TestCase("typeof(C).GetMethod(nameof(M))!.Invoke(null, null)")]
15+
[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null)")]
16+
public static void Simple(string expression)
1517
{
1618
var code = @"
19+
#pragma warning disable CS8602
1720
namespace N
1821
{
1922
public class C
@@ -25,7 +28,7 @@ public C()
2528
2629
public static int M() => 0;
2730
}
28-
}";
31+
}".AssertReplace("typeof(C).GetMethod(nameof(M)).Invoke(null, null)", expression);
2932

3033
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
3134
}
@@ -34,6 +37,7 @@ public C()
3437
public static void Walk()
3538
{
3639
var code = @"
40+
#pragma warning disable CS8602
3741
namespace N
3842
{
3943
public class C

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/Valid.MethodInfoInvoke.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ReflectionAnalyzers.Tests.REFL001CastReturnValueTests
1+
namespace ReflectionAnalyzers.Tests.REFL001CastReturnValueTests
22
{
33
using Gu.Roslyn.Asserts;
44
using Microsoft.CodeAnalysis;
@@ -18,6 +18,7 @@ public static class MethodInfoInvoke
1818
public static void Discarding(string call)
1919
{
2020
var code = @"
21+
#pragma warning disable CS8602, CS8605
2122
namespace N
2223
{
2324
public class C
@@ -40,6 +41,7 @@ public static void M()
4041
public static void AssigningLocal()
4142
{
4243
var code = @"
44+
#pragma warning disable CS8602, CS8605
4345
namespace N
4446
{
4547
public class C
@@ -60,6 +62,7 @@ public C()
6062
public static void IsPattern()
6163
{
6264
var code = @"
65+
#pragma warning disable CS8602, CS8605
6366
namespace N
6467
{
6568
public class C
@@ -71,7 +74,7 @@ public C()
7174
}
7275
}
7376
74-
public static string M() => null;
77+
public static string? M() => null;
7578
}
7679
}";
7780

@@ -82,6 +85,7 @@ public C()
8285
public static void SwitchPattern()
8386
{
8487
var code = @"
88+
#pragma warning disable CS8602, CS8605
8589
namespace N
8690
{
8791
public class C
@@ -95,7 +99,7 @@ public C()
9599
}
96100
}
97101
98-
public static string M() => null;
102+
public static string? M() => null;
99103
}
100104
}";
101105

@@ -106,6 +110,7 @@ public C()
106110
public static void AssigningField()
107111
{
108112
var code = @"
113+
#pragma warning disable CS8602, CS8605
109114
namespace N
110115
{
111116
public class C
@@ -128,6 +133,7 @@ public C()
128133
public static void UsingInExpression()
129134
{
130135
var code = @"
136+
#pragma warning disable CS8602, CS8605
131137
namespace N
132138
{
133139
public class C
@@ -148,6 +154,7 @@ public C()
148154
public static void CallingToString()
149155
{
150156
var code = @"
157+
#pragma warning disable CS8602, CS8605
151158
namespace N
152159
{
153160
public class C
@@ -170,6 +177,7 @@ public C()
170177
public static void Discarded(string discard)
171178
{
172179
var code = @"
180+
#pragma warning disable CS8602, CS8605
173181
namespace N
174182
{
175183
public class C

ReflectionAnalyzers.Tests/REFL002DiscardReturnValueTests/Diagnostics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class Diagnostics
1212
public static void AssigningLocal()
1313
{
1414
var code = @"
15+
#pragma warning disable CS8602
1516
namespace N
1617
{
1718
public class C
@@ -34,6 +35,7 @@ public static void M()
3435
public static void AssigningField()
3536
{
3637
var code = @"
38+
#pragma warning disable CS8602, CS8605
3739
namespace N
3840
{
3941
public class C
@@ -58,6 +60,7 @@ public static void M()
5860
public static void UsingInExpression()
5961
{
6062
var code = @"
63+
#pragma warning disable CS8602, CS8605
6164
namespace N
6265
{
6366
public class C
@@ -81,6 +84,7 @@ public static void M()
8184
public static void InvokeWithGetUninitializedObjectAndArgument(string call)
8285
{
8386
var code = @"
87+
#pragma warning disable CS8602, CS8605
8488
namespace N
8589
{
8690
using System;

ReflectionAnalyzers.Tests/REFL002DiscardReturnValueTests/Valid.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ReflectionAnalyzers.Tests.REFL002DiscardReturnValueTests
1+
namespace ReflectionAnalyzers.Tests.REFL002DiscardReturnValueTests
22
{
33
using Gu.Roslyn.Asserts;
44
using Microsoft.CodeAnalysis;
@@ -16,6 +16,7 @@ public static class Valid
1616
public static void Discarding(string call)
1717
{
1818
var code = @"
19+
#pragma warning disable CS8602
1920
namespace N
2021
{
2122
public class C
@@ -40,6 +41,7 @@ public static void M()
4041
public static void WhenUsedInAssert(string call)
4142
{
4243
var code = @"
44+
#pragma warning disable CS8602
4345
namespace N
4446
{
4547
using NUnit.Framework;
@@ -64,6 +66,7 @@ public static void M()
6466
public static void AssigningLocal()
6567
{
6668
var code = @"
69+
#pragma warning disable CS8602, CS8605
6770
namespace N
6871
{
6972
public class C
@@ -84,6 +87,7 @@ public C()
8487
public static void AssigningField()
8588
{
8689
var code = @"
90+
#pragma warning disable CS8602, CS8605
8791
namespace N
8892
{
8993
public class C
@@ -106,6 +110,7 @@ public C()
106110
public static void UsingInExpression()
107111
{
108112
var code = @"
113+
#pragma warning disable CS8602, CS8605
109114
namespace N
110115
{
111116
public class C

ReflectionAnalyzers.Tests/REFL003MemberDoesNotExistTests/Diagnostics.GetAccessor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ReflectionAnalyzers.Tests.REFL003MemberDoesNotExistTests
1+
namespace ReflectionAnalyzers.Tests.REFL003MemberDoesNotExistTests
22
{
33
using Gu.Roslyn.Asserts;
44
using NUnit.Framework;
@@ -16,13 +16,14 @@ public static class GetAccessor
1616
public static void MissingGetter(string call)
1717
{
1818
var code = @"
19+
#pragma warning disable CS8602
1920
namespace N
2021
{
2122
class C
2223
{
2324
private int p;
2425
25-
public object Get => typeof(C).GetProperty(nameof(this.P)).↓GetMethod;
26+
public object? Get => typeof(C).GetProperty(nameof(this.P)).↓GetMethod;
2627
2728
public int P
2829
{
@@ -39,11 +40,12 @@ public int P
3940
public static void MissingSetter(string call)
4041
{
4142
var code = @"
43+
#pragma warning disable CS8602
4244
namespace N
4345
{
4446
class C
4547
{
46-
public object Get => typeof(C).GetProperty(nameof(P)).↓SetMethod;
48+
public object? Get => typeof(C).GetProperty(nameof(P)).↓SetMethod;
4749
4850
4951
public int P { get; }

ReflectionAnalyzers.Tests/REFL003MemberDoesNotExistTests/Diagnostics.GetMethod.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace N
3333
{
3434
public class C
3535
{
36-
public object M(C c) => typeof(C).GetMethod(↓""MISSING"");
36+
public object? M(C c) => typeof(C).GetMethod(↓""MISSING"");
3737
}
3838
}".AssertReplace("typeof(C).GetMethod(↓\"MISSING\")", type);
3939

@@ -52,7 +52,7 @@ namespace N
5252
{
5353
public sealed class C
5454
{
55-
public object M(C c) => typeof(C).GetMethod(↓""MISSING"");
55+
public object? M(C c) => typeof(C).GetMethod(↓""MISSING"");
5656
}
5757
}".AssertReplace("typeof(C).GetMethod(↓\"MISSING\")", type);
5858

@@ -135,7 +135,7 @@ namespace N
135135
136136
class C
137137
{
138-
public MethodInfo M(Type unused) => typeof(string).GetMethod(↓""MISSING"");
138+
public MethodInfo? M(Type unused) => typeof(string).GetMethod(↓""MISSING"");
139139
}
140140
}".AssertReplace("typeof(string).GetMethod(↓\"MISSING\")", type);
141141

0 commit comments

Comments
 (0)