-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstanceFromTypeCreationTests.cs
176 lines (142 loc) · 6.13 KB
/
InstanceFromTypeCreationTests.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
namespace CreateInstanceFromType.Tests
{
using System;
using TestClasses;
using Xunit;
public class InstanceFromTypeCreationTests
{
[Fact]
public void ShouldUseAParameterlessCtor()
{
var instance = (Parameterless)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(Parameterless));
Assert.NotNull(instance);
}
[Fact]
public void ShouldUseAParameterlessValueTypeCtor()
{
var instance = (Guid)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(Guid));
Assert.Equal(default, instance);
}
[Fact]
public void ShouldUseAParameterlessValueTypeCtorDesign()
{
var instance = (Guid)CreateInstanceFromType2020DesignTimeArgs
.GetInstance(typeof(Guid));
Assert.Equal(default, instance);
}
[Fact]
public void ShouldUseASingleParameterCtor()
{
var instance = (OneParamCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(OneParamCtor), "hello!");
Assert.NotNull(instance);
Assert.Equal("hello!", instance.Value);
}
[Fact]
public void ShouldUseASingleParameterValueTypeCtor()
{
var instance = (Guid)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(Guid), "5e55498a-86e1-495c-b829-0c5170346ef5");
Assert.Equal(Guid.Parse("5e55498a-86e1-495c-b829-0c5170346ef5"), instance);
}
[Fact]
public void ShouldUseASingleParameterValueTypeCtorDesign()
{
var instance = (Guid)CreateInstanceFromType2020DesignTimeArgs
.GetInstance(typeof(Guid), "5e55498a-86e1-495c-b829-0c5170346ef5");
Assert.Equal(Guid.Parse("5e55498a-86e1-495c-b829-0c5170346ef5"), instance);
}
[Fact]
public void ShouldUseATwoParameterCtor()
{
var instance = (TwoParamCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(TwoParamCtor), "hello again!", 123);
Assert.NotNull(instance);
Assert.Equal("hello again!", instance.StringValue);
Assert.Equal(123, instance.IntValue);
}
[Fact]
public void ShouldSelectACtorFromArguments()
{
var twoParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiCtor), "hello there!", 456);
Assert.NotNull(twoParamInstance);
Assert.Equal("hello there!", twoParamInstance.StringValue);
Assert.Equal(456, twoParamInstance.IntValue);
Assert.Equal(default, twoParamInstance.DateValue);
var oneParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiCtor), "hello you!");
Assert.NotNull(oneParamInstance);
Assert.Equal("hello you!", oneParamInstance.StringValue);
Assert.Equal(default, oneParamInstance.IntValue);
Assert.Equal(default, twoParamInstance.DateValue);
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiCtor), "hello blah!", 999, DateTime.MinValue);
Assert.NotNull(threeParamInstance);
Assert.Equal("hello blah!", threeParamInstance.StringValue);
Assert.Equal(999, threeParamInstance.IntValue);
Assert.Equal(DateTime.MinValue, threeParamInstance.DateValue);
}
[Fact]
public void ShouldHandleANullArgument()
{
var threeParamInstance = (MultiCtor)CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiCtor), default(string), 456, DateTime.MaxValue);
Assert.NotNull(threeParamInstance);
Assert.Null(threeParamInstance.StringValue);
Assert.Equal(456, threeParamInstance.IntValue);
Assert.Equal(DateTime.MaxValue, threeParamInstance.DateValue);
}
[Fact]
public void ShouldErrorIfNoMatchingConstructorExists()
{
var ctorNotFoundEx = Assert.Throws<NotSupportedException>(() =>
{
CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(OneParamCtor), DateTime.MinValue, DateTime.MaxValue);
});
Assert.Equal("Failed to find a matching constructor", ctorNotFoundEx.Message);
}
[Fact]
public void ShouldErrorIfNullArgumentAndNoMatchingConstructorExists()
{
var ctorNotFoundEx = Assert.Throws<NotSupportedException>(() =>
{
CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiTwoParamCtor), DateTime.Now, default(string));
});
Assert.Contains("Failed to find a matching constructor", ctorNotFoundEx.Message);
Assert.Contains("null arguments", ctorNotFoundEx.Message);
}
[Fact]
public void ShouldErrorIfNullArgumentAndMultipleMatchingConstructorsExist()
{
var ctorNotFoundEx = Assert.Throws<NotSupportedException>(() =>
{
CreateInstanceFromType2020RuntimeArgs
.GetInstance(typeof(MultiTwoParamCtor), 123, default(string));
});
Assert.Contains("Failed to find a single matching constructor", ctorNotFoundEx.Message);
Assert.Contains("null arguments", ctorNotFoundEx.Message);
}
#region Test Classes
public class MultiTwoParamCtor
{
public MultiTwoParamCtor(int intValue, string stringValue)
{
IntValue = intValue;
StringValue = stringValue;
}
public MultiTwoParamCtor(int intValue, int? nullableIntValue)
{
IntValue = intValue;
StringValue = nullableIntValue.ToString();
}
public int IntValue { get; }
public string StringValue { get; }
}
#endregion
}
}