Skip to content

Commit fa7a1b2

Browse files
committed
add struct constraint and enum constraint test
1 parent f6f7322 commit fa7a1b2

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

src/SourceGeneration.Reflection.SourceGenerator.Test/UnitTest1.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ public class UnitTest1
88
[TestMethod]
99
public void TestMethod1()
1010
{
11-
string source = @"
12-
//[assembly: SourceReflectionAttribute<SourceGeneration.Reflection.Sample2.EnumTestObject>]
13-
//[assembly: SourceReflectionType(typeof(System.Collections.Generic.List<string>))]
14-
//[assembly: SourceReflectionType<int[]>]
15-
using System;
16-
using System.Threading.Tasks;
17-
using SourceGeneration.Reflection;
11+
string source = """
12+
//[assembly: SourceReflectionAttribute<SourceGeneration.Reflection.Sample2.EnumTestObject>]
13+
//[assembly: SourceReflectionType(typeof(System.Collections.Generic.List<string>))]
14+
//[assembly: SourceReflectionType<int[]>]
15+
using System;
16+
using System.Threading.Tasks;
17+
using SourceGeneration.Reflection;
18+
19+
namespace SourceGeneration.Reflection.Sample2
20+
{
21+
[SourceReflection]
22+
public class TestClass
23+
{
24+
public T InvokeStructConstraint<T>(T t) where T: struct => t;
25+
}
26+
}
27+
""";
1828

19-
namespace SourceGeneration.Reflection.Sample2
20-
{
21-
public class BaseObject { }
22-
[SourceReflection] public class InheritedObject1 : BaseObject { }
23-
[SourceReflection] public class InheritedObject2 : BaseObject { }
24-
";
2529
var result = CSharpTestGenerator.Generate<ReflectionSourceGenerator>(source, typeof(SourceReflectionAttribute).Assembly, typeof(UnitTest1).Assembly);
2630
var script = result.RunResult.GeneratedTrees.FirstOrDefault()?.GetText();
2731
var script2 = result.RunResult.GeneratedTrees.LastOrDefault()?.GetText();

src/SourceGeneration.Reflection.Test/GenericMethodTest.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ public void NotNullConstraint()
6868
Assert.AreEqual(null, method.Invoke(instance, [null]));
6969
}
7070

71+
[TestMethod]
72+
public void StructConstraint()
73+
{
74+
SourceTypeInfo type = SourceReflector.GetRequiredType<GenericMethodTestObject>();
75+
76+
var method = type.GetMethod("InvokeStructConstraint")!;
77+
78+
GenericMethodTestObject instance = new();
79+
80+
Assert.ThrowsException<InvalidOperationException>(() =>
81+
{
82+
Assert.AreEqual(1, method.Invoke(instance, [1]));
83+
});
84+
85+
var value = method.MethodInfo.MakeGenericMethod(typeof(int)).Invoke(instance, [1]);
86+
Assert.AreEqual(1, value);
87+
}
88+
89+
[TestMethod]
90+
public void EnumConstraint()
91+
{
92+
SourceTypeInfo type = SourceReflector.GetRequiredType<GenericMethodTestObject>();
93+
94+
var method = type.GetMethod("InvokeEnumConstraint")!;
95+
96+
GenericMethodTestObject instance = new();
97+
98+
99+
var aa = method.Invoke(instance, [EnumTestObject.B]);
100+
101+
Assert.AreEqual(EnumTestObject.A, method.Invoke(instance, [EnumTestObject.A]));
102+
Assert.AreEqual(EnumTestObject.B, method.Invoke(instance, [EnumTestObject.B]));
103+
Assert.ThrowsException<InvalidCastException>(() =>
104+
{
105+
Assert.AreEqual("abc", method.Invoke(instance, ["abc"]));
106+
});
107+
}
108+
71109
[TestMethod]
72110
public void DoubleInternfaceConstraint()
73111
{
@@ -87,7 +125,6 @@ public void DoubleInternfaceConstraint()
87125
});
88126
}
89127

90-
91128
[TestMethod]
92129
public void DoubleTypeParameter()
93130
{
@@ -138,6 +175,10 @@ public class GenericMethodTestObject
138175
public T Invoke5<T>(T t) where T : ICloneable, IComparable => t;
139176
public T Invoke6<T, K>(T t, K k) where T : ICloneable where K : IComparable => t;
140177
public T[] InvokeArray1<T>(T[] t) => t;
178+
179+
public T InvokeStructConstraint<T>(T t) where T: struct => t;
180+
public T InvokeEnumConstraint<T>(T t) where T : Enum => t;
181+
141182
}
142183

143184

0 commit comments

Comments
 (0)