|
| 1 | +namespace ReflectionAnalyzers.Tests.REFL003MemberDoesNotExistTests |
| 2 | +{ |
| 3 | + using Gu.Roslyn.Asserts; |
| 4 | + using NUnit.Framework; |
| 5 | + |
| 6 | + internal partial class ValidCode |
| 7 | + { |
| 8 | + internal class GetMethod |
| 9 | + { |
| 10 | + [TestCase("typeof(Foo).GetMethod(nameof(PublicStatic))")] |
| 11 | + [TestCase("typeof(Foo).GetMethod(nameof(ReferenceEquals))")] |
| 12 | + [TestCase("typeof(Foo).GetMethod(nameof(ReferenceEquals), BindingFlags.Public | BindingFlags.Static)")] |
| 13 | + [TestCase("typeof(Foo).GetMethod(nameof(this.PublicInstance))")] |
| 14 | + [TestCase("typeof(Foo).GetMethod(nameof(PublicInstance))")] |
| 15 | + [TestCase("typeof(Foo).GetMethod(nameof(this.ToString))")] |
| 16 | + [TestCase("typeof(Foo).GetMethod(nameof(ToString))")] |
| 17 | + [TestCase("typeof(Foo).GetMethod(nameof(this.ToString), BindingFlags.Public | BindingFlags.Instance)")] |
| 18 | + [TestCase("typeof(Foo).GetMethod(nameof(PrivateStatic))")] |
| 19 | + [TestCase("typeof(Foo).GetMethod(nameof(this.PrivateInstance))")] |
| 20 | + [TestCase("typeof(Foo).GetMethod(nameof(PrivateInstance))")] |
| 21 | + [TestCase("typeof(string).GetMethod(nameof(string.Clone))")] |
| 22 | + [TestCase("typeof(string).GetMethod(\"op_Equality\")")] |
| 23 | + public void Vanilla(string call) |
| 24 | + { |
| 25 | + var code = @" |
| 26 | +namespace RoslynSandbox |
| 27 | +{ |
| 28 | + using System.Reflection; |
| 29 | +
|
| 30 | + class Foo |
| 31 | + { |
| 32 | + public Foo() |
| 33 | + { |
| 34 | + var methodInfo = typeof(Foo).GetMethod(nameof(this.ToString)); |
| 35 | + } |
| 36 | +
|
| 37 | + public static int PublicStatic() => 0; |
| 38 | +
|
| 39 | + public int PublicInstance() => 0; |
| 40 | +
|
| 41 | + private static int PrivateStatic() => 0; |
| 42 | +
|
| 43 | + private int PrivateInstance() => 0; |
| 44 | + } |
| 45 | +}".AssertReplace("typeof(Foo).GetMethod(nameof(this.ToString))", call); |
| 46 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 47 | + } |
| 48 | + |
| 49 | + [TestCase("typeof(string).GetMethod(\"MISSING\", BindingFlags.NonPublic | BindingFlags.Instance)")] |
| 50 | + [TestCase("typeof(string).GetMethod(\"MISSING\", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)")] |
| 51 | + [TestCase("typeof(string).GetMethod(\"MISSING\", BindingFlags.NonPublic | BindingFlags.Static)")] |
| 52 | + [TestCase("typeof(string).GetMethod(\"MISSING\", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly)")] |
| 53 | + public void ExlucdeNonPublicNotInSource(string invocation) |
| 54 | + { |
| 55 | + var code = @" |
| 56 | +namespace RoslynSandbox |
| 57 | +{ |
| 58 | + using System.Reflection; |
| 59 | +
|
| 60 | + class Foo |
| 61 | + { |
| 62 | + public Foo() |
| 63 | + { |
| 64 | + var methodInfo = typeof(string).GetMethod(""MISSING"", BindingFlags.NonPublic | BindingFlags.Static); |
| 65 | + } |
| 66 | + } |
| 67 | +}".AssertReplace("typeof(string).GetMethod(\"MISSING\", BindingFlags.NonPublic | BindingFlags.Static)", invocation); |
| 68 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void ToStringOverridden() |
| 73 | + { |
| 74 | + var code = @" |
| 75 | +namespace RoslynSandbox |
| 76 | +{ |
| 77 | + class Foo |
| 78 | + { |
| 79 | + public Foo() |
| 80 | + { |
| 81 | + var methodInfo = typeof(Foo).GetMethod(nameof(this.ToString)); |
| 82 | + } |
| 83 | +
|
| 84 | + public override string ToString() => base.ToString(); |
| 85 | + } |
| 86 | +}"; |
| 87 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void ToStringShadowing() |
| 92 | + { |
| 93 | + var code = @" |
| 94 | +namespace RoslynSandbox |
| 95 | +{ |
| 96 | + class Foo |
| 97 | + { |
| 98 | + public Foo() |
| 99 | + { |
| 100 | + var methodInfo = typeof(Foo).GetMethod(nameof(this.ToString)); |
| 101 | + } |
| 102 | +
|
| 103 | + public new string ToString() => base.ToString(); |
| 104 | + } |
| 105 | +}"; |
| 106 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void OverloadedMethodInSameType() |
| 111 | + { |
| 112 | + var code = @" |
| 113 | +namespace RoslynSandbox |
| 114 | +{ |
| 115 | + class Foo |
| 116 | + { |
| 117 | + public Foo() |
| 118 | + { |
| 119 | + var methodInfo = typeof(Foo).GetMethod(nameof(this.Bar)); |
| 120 | + } |
| 121 | +
|
| 122 | + public void Bar() |
| 123 | + { |
| 124 | + } |
| 125 | +
|
| 126 | + public int Bar(int i) => i; |
| 127 | + } |
| 128 | +}"; |
| 129 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 130 | + } |
| 131 | + |
| 132 | + [TestCase("GetMethod(nameof(IConvertible.ToBoolean))")] |
| 133 | + [TestCase("GetMethod(nameof(IConvertible.ToBoolean), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)")] |
| 134 | + public void ExplicitImplementation(string call) |
| 135 | + { |
| 136 | + var code = @" |
| 137 | +namespace RoslynSandbox |
| 138 | +{ |
| 139 | + using System; |
| 140 | + using System.Reflection; |
| 141 | +
|
| 142 | + class Foo |
| 143 | + { |
| 144 | + public Foo() |
| 145 | + { |
| 146 | + var methodInfo = typeof(string).GetMethod(nameof(IConvertible.ToBoolean)); |
| 147 | + } |
| 148 | + } |
| 149 | +}".AssertReplace("GetMethod(nameof(IConvertible.ToBoolean))", call); |
| 150 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void UnknownType() |
| 155 | + { |
| 156 | + var code = @" |
| 157 | +namespace RoslynSandbox |
| 158 | +{ |
| 159 | + using System; |
| 160 | +
|
| 161 | + class Foo |
| 162 | + { |
| 163 | + public Foo(Type type) |
| 164 | + { |
| 165 | + var methodInfo = type.GetMethod(""Bar""); |
| 166 | + } |
| 167 | + } |
| 168 | +}"; |
| 169 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 170 | + } |
| 171 | + |
| 172 | + [Test] |
| 173 | + public void TypeParameter() |
| 174 | + { |
| 175 | + var code = @" |
| 176 | +namespace RoslynSandbox |
| 177 | +{ |
| 178 | + using System.Reflection; |
| 179 | +
|
| 180 | + class Foo |
| 181 | + { |
| 182 | + public MethodInfo Bar<T>() => typeof(T).GetMethod(nameof(this.GetHashCode)); |
| 183 | + } |
| 184 | +}"; |
| 185 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 186 | + } |
| 187 | + |
| 188 | + [TestCase("where T : Foo", "GetMethod(nameof(this.Baz))")] |
| 189 | + [TestCase("where T : Foo", "GetMethod(nameof(this.Baz), BindingFlags.Public | BindingFlags.Instance)")] |
| 190 | + [TestCase("where T : IConvertible", "GetMethod(nameof(IConvertible.ToString))")] |
| 191 | + [TestCase("where T : IConvertible", "GetMethod(nameof(IConvertible.ToString), BindingFlags.Public | BindingFlags.Instance)")] |
| 192 | + [TestCase("where T : IConvertible", "GetMethod(nameof(IConvertible.ToBoolean))")] |
| 193 | + [TestCase("where T : IConvertible", "GetMethod(nameof(IConvertible.ToBoolean), BindingFlags.Public | BindingFlags.Instance)")] |
| 194 | + [TestCase("where T : Foo, IConvertible", "GetMethod(nameof(IConvertible.ToString))")] |
| 195 | + [TestCase("where T : Foo, IConvertible", "GetMethod(nameof(IConvertible.ToString), BindingFlags.Public | BindingFlags.Instance)")] |
| 196 | + [TestCase("where T : Foo, IConvertible", "GetMethod(nameof(IConvertible.ToBoolean))")] |
| 197 | + [TestCase("where T : Foo, IConvertible", "GetMethod(nameof(IConvertible.ToBoolean), BindingFlags.Public | BindingFlags.Instance)")] |
| 198 | + public void ConstrainedTypeParameter(string constraint, string call) |
| 199 | + { |
| 200 | + var code = @" |
| 201 | +namespace RoslynSandbox |
| 202 | +{ |
| 203 | + using System; |
| 204 | + using System.Reflection; |
| 205 | +
|
| 206 | + class Foo |
| 207 | + { |
| 208 | + public MethodInfo Bar<T>() |
| 209 | + where T : Foo |
| 210 | + { |
| 211 | + return typeof(T).GetMethod(nameof(this.Baz)); |
| 212 | + } |
| 213 | +
|
| 214 | + public int Baz() => 0; |
| 215 | + } |
| 216 | +}".AssertReplace("where T : Foo", constraint) |
| 217 | + .AssertReplace("GetMethod(nameof(this.Baz))", call); |
| 218 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 219 | + } |
| 220 | + |
| 221 | + [Test] |
| 222 | + public void Generic() |
| 223 | + { |
| 224 | + var code = @" |
| 225 | +namespace RoslynSandbox |
| 226 | +{ |
| 227 | + using System.Reflection; |
| 228 | +
|
| 229 | + public class Foo |
| 230 | + { |
| 231 | + public Foo() |
| 232 | + { |
| 233 | + _ = typeof(Foo).GetMethod(nameof(Foo.Id), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); |
| 234 | + } |
| 235 | +
|
| 236 | + public T Id<T>(T value) => value; |
| 237 | + } |
| 238 | +}"; |
| 239 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 240 | + } |
| 241 | + |
| 242 | + [TestCase("get_Bar")] |
| 243 | + [TestCase("set_Bar")] |
| 244 | + public void PropertyAccessors(string name) |
| 245 | + { |
| 246 | + var code = @" |
| 247 | +namespace RoslynSandbox |
| 248 | +{ |
| 249 | + class Foo |
| 250 | + { |
| 251 | + public Foo() |
| 252 | + { |
| 253 | + var methodInfo = typeof(Foo).GetMethod(""get_Bar""); |
| 254 | + } |
| 255 | +
|
| 256 | + public int Bar { get; set; } |
| 257 | + } |
| 258 | +}".AssertReplace("get_Bar", name); |
| 259 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments