|
| 1 | +namespace ReflectionAnalyzers.Tests.REFL019NoMemberMatchesTheTypesTests |
| 2 | +{ |
| 3 | + using Gu.Roslyn.Asserts; |
| 4 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | + using NUnit.Framework; |
| 6 | + |
| 7 | + public class ValidCode |
| 8 | + { |
| 9 | + private static readonly DiagnosticAnalyzer Analyzer = new GetXAnalyzer(); |
| 10 | + private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL019NoMemberMatchesTheTypes.Descriptor); |
| 11 | + |
| 12 | + [TestCase("GetConstructor(new[] { typeof(int) })")] |
| 13 | + [TestCase("GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null)")] |
| 14 | + public void GetConstructor(string call) |
| 15 | + { |
| 16 | + var code = @" |
| 17 | +namespace RoslynSandbox |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Reflection; |
| 21 | +
|
| 22 | + public class Foo |
| 23 | + { |
| 24 | + public Foo(int value) |
| 25 | + { |
| 26 | + var ctor = typeof(Foo).GetConstructor(new[] { typeof(int) }); |
| 27 | + } |
| 28 | + } |
| 29 | +}".AssertReplace("GetConstructor(new[] { typeof(int) })", call); |
| 30 | + |
| 31 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 32 | + } |
| 33 | + |
| 34 | + [TestCase("GetConstructor(Type.EmptyTypes)")] |
| 35 | + [TestCase("GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null)")] |
| 36 | + [TestCase("GetConstructor(new[] { typeof(int) })")] |
| 37 | + [TestCase("GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null)")] |
| 38 | + [TestCase("GetConstructor(new[] { typeof(double) })")] |
| 39 | + [TestCase("GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(double) }, null)")] |
| 40 | + public void GetConstructorWhenOverloaded(string call) |
| 41 | + { |
| 42 | + var code = @" |
| 43 | +namespace RoslynSandbox |
| 44 | +{ |
| 45 | + using System; |
| 46 | + using System.Reflection; |
| 47 | +
|
| 48 | + public class Foo |
| 49 | + { |
| 50 | + public Foo() |
| 51 | + { |
| 52 | + var ctor = typeof(Foo).GetConstructor(Type.EmptyTypes); |
| 53 | + } |
| 54 | +
|
| 55 | + public Foo(int value) |
| 56 | + { |
| 57 | + } |
| 58 | +
|
| 59 | + public Foo(double value) |
| 60 | + { |
| 61 | + } |
| 62 | + } |
| 63 | +}".AssertReplace("GetConstructor(Type.EmptyTypes)", call); |
| 64 | + |
| 65 | + AnalyzerAssert.Valid(Analyzer, ExpectedDiagnostic, code); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments