forked from BrianSchroer/sparky-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomValuesHelper.cs
251 lines (206 loc) · 8.19 KB
/
RandomValuesHelper.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace SparkyTestHelpers.Mapping
{
/// <summary>
/// Test helper for updating class instance properties with random values
/// (usually for testing "mapping" from one type to another).
/// </summary>
public class RandomValuesHelper
{
private static readonly Dictionary<Type, Func<Random, string, object>> _generatorDictionary
= new Dictionary<Type, Func<Random, string, object>>
{
{ typeof(string), RandomString },
{ typeof(bool), RandomBool },
{ typeof(bool?), RandomBool },
{ typeof(DateTime), RandomDateTime },
{ typeof(DateTime?), RandomDateTime },
{ typeof(decimal), RandomDecimal },
{ typeof(decimal?), RandomDecimal },
{ typeof(double), RandomDouble },
{ typeof(double?), RandomDouble },
{ typeof(int), RandomInt },
{ typeof(int?), RandomInt },
{ typeof(long), RandomLong },
{ typeof(long?), RandomLong }
};
private static readonly TypeInfo _iEnumerableTypeInfo = typeof(IEnumerable).GetTypeInfo();
private static readonly TypeInfo _iListTypeInfo = typeof(IList).GetTypeInfo();
private Dictionary<Type, int> _createdTypes;
/// <summary>
/// Creae an instance of the specified type and populate its properties with random values.
/// </summary>
/// <typeparam name="T">The type of the instance for which properties are to be updated.</typeparam>
/// <returns>New instance.</returns>
public T CreateInstanceWithRandomValues<T>()
{
return UpdatePropertiesWithRandomValues((T)Activator.CreateInstance(typeof(T)));
}
/// <summary>
/// Update <typeparamref name="T"/> instance properties with random values.
/// </summary>
/// <typeparam name="T">The type of the instance for which properties are to be updated.</typeparam>
/// <param name="instance">An instance of <typeparamref name="T"/>.</param>
/// <returns>The <paramref name="instance"/> with updated property values.</returns>
public T UpdatePropertiesWithRandomValues<T>(T instance)
{
var random = new Random();
int depth = 0;
_createdTypes = new Dictionary<Type, int>();
UpdatePropertiesWithRandomValues(instance, random, typeof(T).GetTypeInfo(), depth);
return instance;
}
private void UpdatePropertiesWithRandomValues(object instance, Random random, TypeInfo typeInfo, int depth)
{
PropertyInfo[] properties = PropertyEnumerator.GetPublicInstanceReadWriteProperties(typeInfo);
foreach (PropertyInfo property in properties)
{
var value = GetRandomValue(random, property.PropertyType, property.Name, depth);
if (value != null)
{
property.SetValue(instance, value, null);
}
}
}
private object GetRandomValue(Random random, Type typ, string prefix, int depth)
{
object value = null;
TypeInfo typeInfo = typ.GetTypeInfo();
if (_generatorDictionary.ContainsKey(typ))
{
value = _generatorDictionary[typ](random, prefix);
}
else if (typeInfo.IsArray)
{
value = RandomArray(random, typ.GetElementType(), prefix, depth + 1);
}
else if (_iListTypeInfo.IsAssignableFrom(typ))
{
value = RandomList(random, typeInfo, prefix, depth + 1);
}
else if (_iEnumerableTypeInfo.IsAssignableFrom(typ))
{
value = RandomIEnumerable(random, typeInfo, prefix, depth + 1);
}
else if (typeInfo.IsClass)
{
value = RandomClassInstance(random, typ, depth + 1);
}
else if (typeInfo.IsEnum)
{
value = RandomEnum(random, typ);
}
return value;
}
private Array RandomArray(Random random, Type typ, string prefix, int depth)
{
Array array = null;
if (typ != null)
{
int itemCount = random.Next(1, 10);
array = TryToCreateInstance(() => Array.CreateInstance(typ, itemCount)) as Array;
if (array != null)
{
for (int i = 0; i < itemCount; i++)
{
string childPrefix = string.Format("{0}[{1}]", prefix, i);
var item = GetRandomValue(random, typ, childPrefix, depth);
array.SetValue(item, i);
}
}
}
return array;
}
private object RandomClassInstance(Random random, Type typ, int depth)
{
object value = null;
bool shouldCreateInstance;
if (_createdTypes.ContainsKey(typ))
{
shouldCreateInstance = (_createdTypes[typ] == depth);
}
else
{
shouldCreateInstance = true;
_createdTypes.Add(typ, depth);
}
if (shouldCreateInstance)
{
value = TryToCreateInstance(() => Activator.CreateInstance(typ));
if (value != null)
{
UpdatePropertiesWithRandomValues(value, random, typ.GetTypeInfo(), depth);
}
}
return value;
}
private object RandomEnum(Random random, Type enumType)
{
Array values = Enum.GetValues(enumType);
int index = random.Next(0, values.Length - 1);
return values.GetValue(index);
}
private object RandomIEnumerable(Random random, TypeInfo typeInfo, string prefix, int depth)
{
Array array = null;
Type[] types = typeInfo.GetGenericArguments();
if (types.Length == 1)
{
array = RandomArray(random, types[0], prefix, depth);
}
return array;
}
private object RandomList(Random random, TypeInfo typeInfo, string prefix, int depth)
{
object list = null;
Type[] genericArguments = typeInfo.GetGenericArguments();
if (genericArguments.Length == 1)
{
Array array = RandomArray(random, genericArguments[0], prefix, depth);
if (array != null)
{
Type genericListType = typeof(List<>).MakeGenericType(genericArguments);
TryToCreateInstance(() => list = Activator.CreateInstance(genericListType, array));
}
}
return list;
}
private static object RandomBool(Random random, string prefix)
{
return (random.Next() % 2 == 0);
}
private static object RandomDateTime(Random random, string prefix)
{
return DateTime.Today.AddDays(random.Next(-1000, 1000));
}
private static object RandomDecimal(Random random, string prefix)
{
return (decimal)random.NextDouble();
}
private static object RandomDouble(Random random, string prefix)
{
return random.NextDouble();
}
private static object RandomInt(Random random, string prefix)
{
return random.Next();
}
private static object RandomLong(Random random, string prefix)
{
return (long)random.Next();
}
private static object RandomString(Random random, string prefix)
{
return string.Format("{0}{1}", prefix, random.Next());
}
private object TryToCreateInstance(Func<object> callback)
{
object value = null;
try { value = callback(); } catch { }
return value;
}
}
}