|
| 1 | +using clojure.lang; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace Csharp.Tests.TypeNameTests; |
| 6 | + |
| 7 | + |
| 8 | +public class Simple { } |
| 9 | + |
| 10 | +public class Outer |
| 11 | +{ |
| 12 | + public class Inner { } |
| 13 | +} |
| 14 | + |
| 15 | +public class OneG<T> { } |
| 16 | +public class TwoG<T1, T2> { } |
| 17 | + |
| 18 | +public class GenParent<T1, T2> |
| 19 | +{ |
| 20 | + public class Child |
| 21 | + { |
| 22 | + public class GrandChild<T3> |
| 23 | + { |
| 24 | + public class GreatGrandChild<T4, T5> |
| 25 | + { |
| 26 | + |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +[TestFixture] |
| 34 | +public class TypeNameResolvingTests |
| 35 | +{ |
| 36 | + |
| 37 | + public static Type TR(string typename, bool throwOnError) => Type.GetType(typename, throwOnError); |
| 38 | + |
| 39 | + |
| 40 | + public static Type Resolve(string typeName) |
| 41 | + { |
| 42 | + var spec = ClrTypeSpec.Parse(typeName); |
| 43 | + return spec?.Resolve(null, (assemblyName, typename, throwOnError) => TR(typename, throwOnError), false, false); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public void SimpleClassName_ResolvesCorrectly() |
| 48 | + { |
| 49 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.Simple"), Is.EqualTo(typeof(Simple))); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void NestedClassName_ResolvesCorrectly() |
| 54 | + { |
| 55 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.Outer+Inner"), Is.EqualTo(typeof(Outer.Inner))); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void GenericClassName_ResolvesCorrectly() |
| 60 | + { |
| 61 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.OneG`1[System.String]"), Is.EqualTo(typeof(OneG<string>))); |
| 62 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.TwoG`2[System.String,System.Int32]"), Is.EqualTo(typeof(TwoG<string, int>))); |
| 63 | + Assert.That( |
| 64 | + Resolve("Csharp.Tests.TypeNameTests.GenParent`2+Child+GrandChild`1+GreatGrandChild`2[System.String, System.Int32, System.Double, System.String,System.Object]"), |
| 65 | + Is.EqualTo(typeof(GenParent<string, int>.Child.GrandChild<double>.GreatGrandChild<string, object>))); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void NonExistentType_ResolvesToNull() |
| 70 | + { |
| 71 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.NonExistent"), Is.Null); |
| 72 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.Simple+NonExistent"), Is.Null); |
| 73 | + Assert.That(Resolve("Csharp.Tests.TypeNameTests.OneG`1[Non.Existent]"), Is.Null); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void PointerType_ResolvesCorrectly() |
| 78 | + { |
| 79 | + Assert.That(Resolve("System.String*"), Is.EqualTo(typeof(string).MakePointerType())); |
| 80 | + Assert.That(Resolve("System.String**"), Is.EqualTo(typeof(string).MakePointerType().MakePointerType())); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void ByRefType_ResolvesCorrectly() |
| 85 | + { |
| 86 | + Assert.That(Resolve("System.String&"), Is.EqualTo(typeof(string).MakeByRefType())); |
| 87 | + Assert.That(Resolve("System.String*&"), Is.EqualTo(typeof(string).MakePointerType().MakeByRefType())); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void ArrayType_ResolvesCorrectly() |
| 92 | + { |
| 93 | + Assert.That(Resolve("System.String[]"), Is.EqualTo(typeof(string).MakeArrayType())); |
| 94 | + Assert.That(Resolve("System.String[*]"), Is.EqualTo(typeof(string).MakeArrayType(1))); |
| 95 | + Assert.That(Resolve("System.String[,]"), Is.EqualTo(typeof(string).MakeArrayType(2))); |
| 96 | + Assert.That(Resolve("System.String[,,]"), Is.EqualTo(typeof(string).MakeArrayType(3))); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void Everything_ResolvesCorrectly() |
| 102 | + { |
| 103 | + Assert.That( |
| 104 | + Resolve("Csharp.Tests.TypeNameTests.GenParent`2+Child+GrandChild`1+GreatGrandChild`2[System.String, System.Int32, System.Double, System.String,System.Object][]**&"), |
| 105 | + Is.EqualTo(typeof(GenParent<string, int>.Child.GrandChild<double>.GreatGrandChild<string, object>).MakeArrayType().MakePointerType().MakePointerType().MakeByRefType())); |
| 106 | + } |
| 107 | +} |
0 commit comments