forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionExtensionTests.cs
More file actions
154 lines (128 loc) · 5.43 KB
/
ReflectionExtensionTests.cs
File metadata and controls
154 lines (128 loc) · 5.43 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.DataAnnotations;
namespace ServiceStack.Text.Tests
{
public class TestModel
{
public TestModel()
{
var i = 0;
this.PublicInt = i++;
this.PublicGetInt = i++;
this.PublicSetInt = i++;
this.PublicIntField = i++;
this.PrivateInt = i++;
this.ProtectedInt = i++;
}
public int PublicInt { get; set; }
public int PublicGetInt { get; private set; }
public int PublicSetInt { private get; set; }
public int PublicIntField;
private int PrivateInt { get; set; }
protected int ProtectedInt { get; set; }
public int IntMethod()
{
return this.PublicInt;
}
}
public class MethodsForReflection
{
public string Result = String.Empty;
public void HelloVoid()
{
Result = "Hello";
}
public void Hello(bool a, int b)
{
Result = String.Format($"Hello {a} {b}");
}
}
[TestFixture]
public class ReflectionExtensionTests
: TestBase
{
[Test]
public void Only_serializes_public_readable_properties()
{
var model = new TestModel();
var modelStr = TypeSerializer.SerializeToString(model);
Assert.That(modelStr, Is.EqualTo("{PublicInt:0,PublicGetInt:1}"));
Serialize(model);
}
[Test]
public void Can_create_instance_of_string()
{
Assert.That(typeof(string).CreateInstance(), Is.EqualTo(String.Empty));
}
[Test]
public void Can_create_instances_of_common_collections()
{
Assert.That(typeof(IEnumerable<TestModel>).CreateInstance() as IEnumerable<TestModel>, Is.Not.Null);
Assert.That(typeof(ICollection<TestModel>).CreateInstance() as ICollection<TestModel>, Is.Not.Null);
Assert.That(typeof(IList<TestModel>).CreateInstance() as IList<TestModel>, Is.Not.Null);
Assert.That(typeof(IDictionary<string, TestModel>).CreateInstance() as IDictionary<string, TestModel>, Is.Not.Null);
Assert.That(typeof(IDictionary<int, TestModel>).CreateInstance() as IDictionary<int, TestModel>, Is.Not.Null);
Assert.That(typeof(TestModel[]).CreateInstance() as TestModel[], Is.Not.Null);
}
[Test]
public void Can_create_intances_of_generic_types()
{
Assert.That(typeof(GenericType<>).CreateInstance(), Is.Not.Null);
Assert.That(typeof(GenericType<,>).CreateInstance(), Is.Not.Null);
Assert.That(typeof(GenericType<,,>).CreateInstance(), Is.Not.Null);
Assert.That(typeof(GenericType<GenericType<object>>).CreateInstance(), Is.Not.Null);
}
[Test]
public void Can_create_intances_of_recursive_generic_type()
{
//Assert.That(typeof(GenericType<>).MakeGenericType(new[] { typeof(GenericType<>) }).CreateInstance(), Is.Not.Null);
}
[Test]
public void Can_get_method_from_type()
{
var testInstance = new MethodsForReflection();
var helloVoidMethod = typeof(MethodsForReflection).GetMethodInfo(nameof(MethodsForReflection.HelloVoid));
Assert.That(helloVoidMethod, Is.Not.Null);
var helloVoidDelegate = (Action<MethodsForReflection>)helloVoidMethod.MakeDelegate(typeof(Action<MethodsForReflection>));
Assert.That(helloVoidDelegate, Is.Not.Null);
helloVoidDelegate(testInstance);
Assert.That(testInstance.Result, Is.EqualTo("Hello"));
var helloVoidBoolIntMethod = typeof(MethodsForReflection).GetMethodInfo(nameof(MethodsForReflection.Hello), new Type[] { typeof(bool), typeof(int) });
Assert.That(helloVoidBoolIntMethod, Is.Not.Null);
var helloVoidBoolIntDelegate = (Action<MethodsForReflection, bool, int>)helloVoidBoolIntMethod.MakeDelegate(typeof(Action<MethodsForReflection, bool, int>));
Assert.That(helloVoidBoolIntDelegate, Is.Not.Null);
helloVoidBoolIntDelegate(testInstance, true, 5);
Assert.That(testInstance.Result, Is.EqualTo("Hello True 5"));
}
[Test]
public void Does_GetCollectionType()
{
Assert.That(new[] { new TestModel() }.GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
Assert.That(new[] { new TestModel() }.ToList().GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
Assert.That(new[] { new TestModel() }.Select(x => x).GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
Assert.That(new[] { "" }.Select(x => new TestModel()).GetType().GetCollectionType(), Is.EqualTo(typeof(TestModel)));
}
[EnumAsChar]
public enum CharEnum : int
{
Value1 = 'A',
Value2 = 'B',
Value3 = 'C',
Value4 = 'D'
}
[Test]
public void Can_use_HasAttributeCached()
{
Assert.That(typeof(CharEnum).HasAttributeCached<EnumAsCharAttribute>());
Assert.That(typeof(CharEnum).HasAttribute<EnumAsCharAttribute>());
}
}
public class GenericType<T> { }
public class GenericType<T1, T2> { }
public class GenericType<T1, T2, T3> { }
}