|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | +using GraphQL.Conventions.Types.Resolution.Extensions; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Tests.Types.Resolution.Extensions |
| 10 | +{ |
| 11 | + public class ReflectionExtensionsTests |
| 12 | + { |
| 13 | + [Theory] |
| 14 | + [MemberData(nameof(IsEnumerableGraphType_Should_Return_True_For_Common_Collection_Types_Data))] |
| 15 | + public void IsEnumerableGraphType_Should_Return_True_For_Common_Collection_Types(Type type) |
| 16 | + { |
| 17 | + Assert.IsTrue(type.GetTypeInfo().IsEnumerableGraphType()); |
| 18 | + } |
| 19 | + |
| 20 | + public static TheoryData<Type> IsEnumerableGraphType_Should_Return_True_For_Common_Collection_Types_Data() => new() |
| 21 | + { |
| 22 | + typeof(IEnumerable<>), |
| 23 | + typeof(ConcurrentQueue<>), |
| 24 | + typeof(HashSet<>), |
| 25 | + typeof(int[]), |
| 26 | + typeof(List<>), |
| 27 | + typeof(IList<>), |
| 28 | + typeof(IReadOnlyList<>), |
| 29 | + typeof(IReadOnlyCollection<>), |
| 30 | + }; |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [MemberData(nameof(IsEnumerableGraphType_Should_Return_False_For_Common_Dictionary_Types_Data))] |
| 34 | + public void IsEnumerableGraphType_Should_Return_False_For_Common_Dictionary_Types(Type type) |
| 35 | + { |
| 36 | + Assert.IsFalse(type.GetTypeInfo().IsEnumerableGraphType()); |
| 37 | + } |
| 38 | + |
| 39 | + public static TheoryData<Type> IsEnumerableGraphType_Should_Return_False_For_Common_Dictionary_Types_Data() => new() |
| 40 | + { |
| 41 | + typeof(IDictionary<,>), |
| 42 | + typeof(IDictionary), |
| 43 | + }; |
| 44 | + |
| 45 | + [Theory] |
| 46 | + [MemberData(nameof(GetImplementationInterface_WithoutFuse_AcquireSpecifiedInterfaceOnly_Data))] |
| 47 | + public void GetImplementationInterface_WithoutFuse_AcquireSpecifiedInterfaceOnly(Type type, Type assignableInterface) |
| 48 | + => Assert.IsTrue(type.IsImplementingInterface(assignableInterface, false)); |
| 49 | + |
| 50 | + public static TheoryData<Type, Type> GetImplementationInterface_WithoutFuse_AcquireSpecifiedInterfaceOnly_Data() => |
| 51 | + new() |
| 52 | + { |
| 53 | + { typeof(ITestInterface1<int>), typeof(ITestInterface1<int>) }, |
| 54 | + { typeof(ITestInterface2<int>), typeof(ITestInterface1<int>) }, |
| 55 | + { typeof(ITestInterface2<int>), typeof(ITestInterface2<int>) }, |
| 56 | + { typeof(TestClass11<int>), typeof(ITestInterface1<int>) }, |
| 57 | + { typeof(TestClass12), typeof(ITestInterface1<string>) }, |
| 58 | + { typeof(TestClass12), typeof(ITestInterface2<string>) }, |
| 59 | + { typeof(TestClass2), typeof(ITestInterface1<int>) }, |
| 60 | + { typeof(TestClass2), typeof(ITestInterface1<string>) }, |
| 61 | + { typeof(TestClass2), typeof(ITestInterface2<string>) }, |
| 62 | + }; |
| 63 | + |
| 64 | + [Theory] |
| 65 | + [MemberData(nameof(GetImplementationInterface_WithFuse_AcquireSpecifiedInterfaceOnly_Data))] |
| 66 | + public void GetImplementationInterface_WithFuse_AcquireSpecifiedInterfaceOnly(Type type, Type assignableInterface) => |
| 67 | + Assert.IsTrue(type.IsImplementingInterface(assignableInterface)); |
| 68 | + |
| 69 | + public static TheoryData<Type, Type> GetImplementationInterface_WithFuse_AcquireSpecifiedInterfaceOnly_Data() => |
| 70 | + new() |
| 71 | + { |
| 72 | + { typeof(ITestInterface1<int>), typeof(ITestInterface1<>) }, |
| 73 | + { typeof(ITestInterface2<int>), typeof(ITestInterface1<>) }, |
| 74 | + { typeof(ITestInterface2<int>), typeof(ITestInterface2<>) }, |
| 75 | + { typeof(TestClass11<int>), typeof(ITestInterface1<>) }, |
| 76 | + // |
| 77 | + { typeof(ITestInterface1<string>), typeof(ITestInterface1<>) }, |
| 78 | + { typeof(ITestInterface2<string>), typeof(ITestInterface1<>) }, |
| 79 | + { typeof(ITestInterface2<string>), typeof(ITestInterface2<>) }, |
| 80 | + { typeof(TestClass11<string>), typeof(ITestInterface1<>) }, |
| 81 | + // |
| 82 | + { typeof(TestClass12), typeof(ITestInterface1<>) }, |
| 83 | + { typeof(TestClass12), typeof(ITestInterface2<>) }, |
| 84 | + { typeof(TestClass2), typeof(ITestInterface1<>) }, |
| 85 | + { typeof(TestClass2), typeof(ITestInterface2<>) }, |
| 86 | + }; |
| 87 | + |
| 88 | + private interface ITestInterface1<out T> |
| 89 | + { |
| 90 | + // ReSharper disable once UnusedMember.Global |
| 91 | + T Item => throw new NotImplementedException(); |
| 92 | + } |
| 93 | + |
| 94 | + private interface ITestInterface2<out T> : ITestInterface1<T> |
| 95 | + { |
| 96 | + } |
| 97 | + |
| 98 | + private class TestClass11<T> : ITestInterface1<T> |
| 99 | + { |
| 100 | + } |
| 101 | + |
| 102 | + private class TestClass12 : ITestInterface2<string> |
| 103 | + { |
| 104 | + } |
| 105 | + |
| 106 | + private class TestClass2 : ITestInterface2<string>, ITestInterface1<int> |
| 107 | + { |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments