/
ReflectionTest.cs
115 lines (109 loc) · 3.46 KB
/
ReflectionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
namespace Rystem.Test.UnitTest.Reflection
{
public class ReflectionTest
{
public class Zalo : Sulo
{
}
public class Sulo
{
}
public class Folli : Zalo
{
}
public class Foo
{
public IEnumerable<string> Values { get; }
public bool X { get; }
public void FooToon()
{
}
public Foo(IEnumerable<string> values)
{
Values = values;
}
public Foo(IEnumerable<string> values, bool x)
{
Values = values;
X = x;
}
}
public class Foo2
{
public IEnumerable<string> Values { get; }
public bool X { get; }
public Dictionary<string, string> Complex { get; init; }
public HashSet<int> Integers { get; init; }
private readonly Foo _tiny;
public Foo Tiny => _tiny;
public void FooToon()
{
}
public Foo2(IEnumerable<string> values)
{
Values = values;
}
public Foo2(IEnumerable<string> values, bool x)
{
Values = values;
X = x;
}
}
[Fact]
public void IsTheSameTypeOrASonTest()
{
Zalo zalo = new();
Zalo zalo2 = new();
Folli folli = new();
Sulo sulo = new();
object quo = new();
int x = 2;
decimal y = 3;
Assert.True(zalo.IsTheSameTypeOrASon(sulo));
Assert.True(folli.IsTheSameTypeOrASon(sulo));
Assert.True(zalo.IsTheSameTypeOrASon(zalo2));
Assert.True(zalo.IsTheSameTypeOrASon(quo));
Assert.False(sulo.IsTheSameTypeOrASon(zalo));
Assert.True(sulo.IsTheSameTypeOrAParent(zalo));
Assert.False(y.IsTheSameTypeOrAParent(x));
}
[Fact]
public void IsTheSameTypeOrAParentTest()
{
Zalo zalo = new();
Sulo sulo = new();
int x = 2;
decimal y = 3;
Assert.True(sulo.IsTheSameTypeOrAParent(zalo));
Assert.False(y.IsTheSameTypeOrAParent(x));
}
[Fact]
public void CreateInstance()
{
var instance = typeof(Foo).CreateWithDefault<Foo>()!;
Assert.NotNull(instance);
//instance.Values = new List<string>();
(instance.Values as List<string>)!.Add("aaa");
Assert.Equal("aaa", instance.Values.First());
}
[Fact]
public void CreateInstanceWithDefaultConstructorPropertyAndField()
{
var instance = typeof(Foo2).CreateWithDefaultConstructorPropertiesAndField<Foo2>()!;
Assert.NotNull(instance);
//instance.Values = new List<string>();
(instance.Values as List<string>)!.Add("aaa");
Assert.Equal("aaa", instance.Values.First());
instance.Complex.Add("x", "y");
Assert.Equal(1, instance.Complex.Count!);
Assert.NotNull(instance.Tiny);
(instance.Tiny.Values as List<string>)!.Add("aaa");
Assert.Equal("aaa", instance.Tiny.Values.First());
Assert.Equal(0, instance.Integers.Count!);
}
}
}