-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathObjectFillerRecursiveTests.cs
187 lines (151 loc) · 5.25 KB
/
ObjectFillerRecursiveTests.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
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tynamix.ObjectFiller.Test
{
[TestClass]
public class ObjectFillerRecursiveTests
{
// ReSharper disable ClassNeverInstantiated.Local
private class TestParent
{
public TestChild Child { get; set; }
}
private class TestChild
{
public TestParent Parent { get; set; }
}
private class TestGrandParent
{
public TestParent SubObject { get; set; }
}
private class TestEmptyNode
{
public string Value { get; set; }
}
private class TestSelf
{
public TestSelf Self { get; set; }
public string Foo { get; set; }
}
private class TestDuplicate
{
public TestEmptyNode Node1 { get; set; }
public TestEmptyNode Node2 { get; set; }
}
public class Children
{
public Parent Parent { get; set; }
}
public class Parent
{
public List<Children> Childrens { get; set; }
}
public class ParentDictionary
{
public Dictionary<string, Children> Childrens { get; set; }
}
// ReSharper restore ClassNeverInstantiated.Local
[TestMethod]
public void RecursiveFill_RecursiveType_ThrowsException()
{
var filler = new Filler<TestParent>();
filler.Setup().OnCircularReference().ThrowException();
Assert.ThrowsException<InvalidOperationException>(() => filler.Create());
}
[TestMethod]
public void RecursiveFill_WithIgnoredProperties_Succeeds()
{
var filler = new Filler<TestParent>();
filler.Setup().OnProperty(p => p.Child).IgnoreIt();
var result = filler.Create();
Assert.IsNotNull(result);
}
[TestMethod]
public void RecursiveFill_WithFunc_Succeeds()
{
var filler = new Filler<TestParent>();
filler.Setup().OnProperty(p => p.Child).Use(() => new TestChild());
var result = filler.Create();
Assert.IsNotNull(result.Child);
}
[TestMethod]
public void RecursiveFill_RecursiveType_Parent_First_Fails()
{
var filler = new Filler<TestParent>();
filler.Setup().OnCircularReference().ThrowException();
Assert.ThrowsException<InvalidOperationException>(() => filler.Create());
}
[TestMethod]
public void RecursiveFill_RecursiveType_Child_First_Fails()
{
var filler = new Filler<TestChild>();
filler.Setup().OnCircularReference().ThrowException();
Assert.ThrowsException<InvalidOperationException>(() => filler.Create());
}
[TestMethod]
public void RecursiveFill_DeepRecursiveType_Fails()
{
var filler = new Filler<TestGrandParent>();
filler.Setup().OnCircularReference().ThrowException();
Assert.ThrowsException<InvalidOperationException>(() => filler.Create());
}
[TestMethod]
public void RecursiveFill_SelfReferencing_Fails()
{
var filler = new Filler<TestSelf>();
filler.Setup().OnCircularReference().ThrowException();
Assert.ThrowsException<InvalidOperationException>(() => filler.Create());
}
[TestMethod]
public void RecursiveFill_DuplicateProperty_Succeeds()
{
// reason: a filler should not complain if it encounters the same type
// in two separate branches of a type hierarchy
var filler = new Filler<TestDuplicate>();
var result = filler.Create();
Assert.IsNotNull(result);
}
[TestMethod]
public void RecursiveFill_RecursiveType_Parent_First_Succeeds()
{
var filler = new Filler<TestParent>();
var r = filler.Create();
Assert.IsNull(r.Child.Parent.Child);
}
[TestMethod]
public void RecursiveFill_RecursiveType_Child_First_Succeeds()
{
var filler = new Filler<TestChild>();
var r = filler.Create();
Assert.IsNull(r.Parent.Child.Parent);
}
[TestMethod]
public void RecursiveFill_DeepRecursiveType_Succeeds()
{
var filler = new Filler<TestGrandParent>();
var r = filler.Create();
Assert.IsNull(r.SubObject.Child.Parent);
}
[TestMethod]
public void RecursiveFill_SelfReferencing_Succeeds()
{
var filler = new Filler<TestSelf>();
var r = filler.Create();
Assert.IsNull(r.Self.Self);
}
[TestMethod]
public void RecursiveFill_ParentList_Succeeds()
{
var filler = new Filler<Parent>();
var r = filler.Create();
Assert.IsNull(r.Childrens[0].Parent.Childrens);
}
[TestMethod]
public void RecursiveFill_ParentDictionary_Succeeds()
{
var filler = new Filler<ParentDictionary>();
var r = filler.Create();
}
}
}