|
| 1 | +using myNOC.EntityFramework.Query.Extensions; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace myNOC.Tests.EntityFramework.Query.Extensions |
| 5 | +{ |
| 6 | + [TestClass] |
| 7 | + public class TypeExtensionTests |
| 8 | + { |
| 9 | + private IEnumerable<Type> _types = default!; |
| 10 | + |
| 11 | + [TestInitialize] |
| 12 | + public void Initialize() |
| 13 | + { |
| 14 | + _types = Assembly.GetExecutingAssembly().GetTypes(); |
| 15 | + } |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + public void CanImplement_Types_ImplementsInterface_DirectInterface_ReturnsTypesThatInheritFromITestInterface2() |
| 19 | + { |
| 20 | + // Arrange |
| 21 | + var implementedInterface = typeof(ITestInterface2); |
| 22 | + |
| 23 | + // Act |
| 24 | + var results = _types.CanImplement(implementedInterface); |
| 25 | + |
| 26 | + // Assert |
| 27 | + Assert.IsNotNull(results); |
| 28 | + Assert.AreEqual(1, results.Count()); |
| 29 | + Assert.AreEqual(1, results[0].Interfaces.Count()); |
| 30 | + Assert.AreEqual(implementedInterface, results[0].Interfaces[0]); |
| 31 | + Assert.AreEqual(typeof(TestClass3), results[0].Type); |
| 32 | + Assert.IsNull(results.FirstOrDefault(x => x.Type == typeof(TestClass4))); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void CanImplement_Types_ImplementsInterface_SupportsNestedInterfaces_ReturnsTypesThatInheritFromITestInterface() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var implementedInterface = typeof(ITestInterface); |
| 40 | + |
| 41 | + // Act |
| 42 | + var results = _types.CanImplement(implementedInterface); |
| 43 | + |
| 44 | + // Assert |
| 45 | + Assert.IsNotNull(results); |
| 46 | + Assert.AreEqual(3, results.Count()); |
| 47 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type == typeof(TestClass1))); |
| 48 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type == typeof(TestClass2))); |
| 49 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type == typeof(TestClass5))); |
| 50 | + Assert.IsNull(results.FirstOrDefault(x => x.Type == typeof(TestClass4))); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void CanImplement_Types_ImplementsInterface_NestedClassesRecursion() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var implementedInterface = typeof(ITestInterface3); |
| 58 | + |
| 59 | + // Act |
| 60 | + var results = _types.CanImplement(implementedInterface); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.IsNotNull(results); |
| 64 | + Assert.AreEqual(2, results.Count()); |
| 65 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type == typeof(TestClass6))); |
| 66 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type == typeof(TestClass8))); |
| 67 | + Assert.IsNull(results.FirstOrDefault(x => x.Type == typeof(TestClass4))); |
| 68 | + } |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void CanImplement_Types_ImplementsInterface_SkipIDisposableInterfaceInGetInterfaces() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var implementedInterface = typeof(ITestInterface5); |
| 75 | + |
| 76 | + // Act |
| 77 | + var results = _types.CanImplement(implementedInterface); |
| 78 | + |
| 79 | + // Assert |
| 80 | + Assert.IsNotNull(results); |
| 81 | + Assert.AreEqual(1, results.Count()); |
| 82 | + Assert.IsNotNull(results.FirstOrDefault(x => x.Type.Equals(typeof(TestClass7)))); |
| 83 | + Assert.IsNull(results.FirstOrDefault(x => x.Type == typeof(TestClass4))); |
| 84 | + } |
| 85 | + |
| 86 | + internal interface ITestInterface { } |
| 87 | + internal interface ITestInterface2 { } |
| 88 | + internal interface ITestInterface3 { } |
| 89 | + internal interface ITestInterface4 : ITestInterface { } |
| 90 | + internal interface ITestInterface5 : IDisposable { } |
| 91 | + internal interface ITestInterface7 : ITestInterface3 { } |
| 92 | + |
| 93 | + internal class TestClass1 : ITestInterface { } |
| 94 | + internal class TestClass2 : ITestInterface { } |
| 95 | + internal class TestClass3 : ITestInterface2 { } |
| 96 | + internal class TestClass4 { } |
| 97 | + internal class TestClass5 : ITestInterface4 { } |
| 98 | + internal class TestClass6 : TestClass4, ITestInterface7 { } |
| 99 | + internal class TestClass7 : ITestInterface5 |
| 100 | + { |
| 101 | + public void Dispose() |
| 102 | + { |
| 103 | + throw new NotImplementedException(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + internal class TestClass8 : TestClass6 { } |
| 108 | + } |
| 109 | +} |
0 commit comments