-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToTestTests.cs
389 lines (357 loc) · 12.3 KB
/
ToTestTests.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System;
using Xunit;
using Xunit.Abstractions;
using ObjectToTest.UnitTests.Models;
using ObjectToTest.UnitTests.Data;
using System.Collections.Generic;
namespace ObjectToTest.UnitTests
{
public class ToTestTests
{
private readonly ITestOutputHelper _output;
public ToTestTests(ITestOutputHelper output)
{
_output = output;
}
#pragma warning disable CS8604
[Fact]
public void ThrowArgumentNullException()
{
object? obj = null;
Assert.Throws<ArgumentNullException>(() => obj.ToTest());
}
#pragma warning restore CS8604
[Fact]
public void DefaultConstructor()
{
Assert.Equal(
"new EmptyObject()",
new EmptyObject().ToTest().Log(_output)
);
}
[Fact]
public void NullPropertyInitializer()
{
Assert.Equal(
"new WithOnePublicProperty(){PropertyName = null}",
new WithOnePublicProperty().ToTest().Log(_output)
);
}
[Fact]
public void StringPropertyInitializer()
{
Assert.Equal(
"new WithOnePublicProperty(){PropertyName = \"Test\"}",
new WithOnePublicProperty
{
PropertyName = "Test"
}.ToTest().Log(_output)
);
}
[Fact]
public void IntPropertyInitializer()
{
Assert.Equal(
"new WithOnePublicIntProperty(){PropertyName = 42}",
new WithOnePublicIntProperty
{
PropertyName = 42
}.ToTest().Log(_output)
);
}
[Fact]
public void MultipleValueTypesPropertyInitializer()
{
Assert.Equal(
"new WithTwoProperties(){IntProperty = 42, StringProperty = \"Test\"}",
new WithTwoProperties
{
IntProperty = 42,
StringProperty = "Test"
}.ToTest().Log(_output)
);
}
[Fact]
public void CtorWithIntArgumentForProperty()
{
Assert.Equal(
"new WithOneParameterContructorAndPublicReadProperty(42)",
new WithOneParameterContructorAndPublicReadProperty(42)
.ToTest()
.Log(_output)
);
}
[Fact]
public void CtorWithIntArgumentForPrivateField()
{
Assert.Equal(
"new WithOneParamAndPrivateField(42)",
new WithOneParamAndPrivateField(42).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithMultipleValueTypeArguments()
{
Assert.Equal(
"new WithTwoParamOneFieldAndOneProperty(42,\"Test\")",
new WithTwoParamOneFieldAndOneProperty(
42,
"Test"
).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithReferenceTypeArgument()
{
Assert.Equal(
"new WithClassParam(new EmptyObject())",
new WithClassParam(new EmptyObject()).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithMultipleComplexTypesArguments()
{
Assert.Equal(
"new WithClassAndIntParams(42,new EmptyObject())",
new WithClassAndIntParams(
42,
new EmptyObject()
).ToTest().Log(_output)
);
}
[Fact]
public void TimeSpanConstructor()
{
Assert.Equal(
"new TimeSpan(18,17,34,24,5)",
new TimeSpan(18, 17, 34, 24, 5)
.ToTest()
.Log(_output)
);
}
[Fact]
public void CtorWithComplexDependcyArgument()
{
Assert.Equal(
"new WithClassParamThatDependsOnClass(new WithClassParam(new EmptyObject()))",
new WithClassParamThatDependsOnClass(
new WithClassParam(new EmptyObject())
).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithComplexDependencySeveralArguments()
{
/*
* @todo #:60m/DEV Proper declaration format is expected. It would be nice to start each constructor argument on its own line with proper intend
* new WithTwoClassParamAndIntParam(
* new WithClassParam(
* new EmptyObject()
* ),
* new WithClassAndIntParams(
* 42,
* new EmptyObject()
* ),
* 42
* )
*/
Assert.Equal(
"new WithTwoClassParamAndIntParam(new WithClassParam(new EmptyObject()),new WithClassAndIntParams(42,new EmptyObject()),42)",
new WithTwoClassParamAndIntParam(
new WithClassParam(new EmptyObject()),
new WithClassAndIntParams(
42,
new EmptyObject()
),
42
).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithComplexArgumentsAndProperties()
{
Assert.Equal(
"new WithClassParamWithProp(new WithOnePublicProperty(){PropertyName = \"Test\"},42)",
new WithClassParamWithProp(
new WithOnePublicProperty { PropertyName = "Test" },
42
).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithGenericArgument()
{
Assert.Equal(
"new WithGenericArgument<IPrice>()",
new WithGenericArgument<IPrice>().ToTest().Log(_output)
);
}
[Fact]
public void With2GenericArguments()
{
Assert.Equal(
"new With2GenericArguments<IPrice,IUser>(new Price(10))",
new With2GenericArguments<IPrice, IUser>(new Price(10))
.ToTest()
.Log(_output)
);
}
[Fact]
public void With3GenericArguments()
{
Assert.Equal(
"new With3GenericArguments<int,decimal,string>()",
new With3GenericArguments<int, decimal, string>()
.ToTest()
.Log(_output)
);
}
[Fact]
public void CtorWithIEnumerableInt()
{
Assert.Equal(
"new WithIEnumerableInt(new[] { 1, 2, 4, 5 })",
new WithIEnumerableInt(new[] { 1, 2, 4, 5 })
.ToTest()
.Log(_output)
);
}
[Fact]
public void CtorWithListInt()
{
Assert.Equal(
"new WithListArgument(new List<int> { 1, 2, 4, 5 })",
new WithListArgument(new List<int> { 1, 2, 4, 5 })
.ToTest()
.Log(_output)
);
}
[Fact]
public void CtorWithDictionaryIntString()
{
Assert.Equal(
"new WithDictionaryArgument(new Dictionary<int,string> { { 1, \"1\" }, { 2, \"2\" }, { 3, \"3\" } })",
new WithDictionaryArgument(
new Dictionary<int,string> { { 1, "1" }, { 2, "2" }, { 3, "3" } }
).ToTest().Log(_output)
);
}
[Fact]
public void CtorWithInterfaceArguments()
{
Assert.Equal(
"new Foo(new Price(10),new User(\"User Name\"))",
new Foo(
new Price(10),
new User("User Name")
).ToTest().Log(_output)
);
}
[Fact]
public void IncorrectArgumentsClass()
{
Assert.Equal(
"Can not find a constructor for IncorrectArgumentsClass object, not valid constructor available",
new IncorrectArgumentsClass(1, 2).ToTest().Log(_output)
);
}
[Fact(Skip = "Need to fix this test")]
public void CircularReferenceDetection()
{
/*
* @todo #:60m/DEV Make CircularReferenceDetection test to be green.
* Now the circular references between the objects are not detected. It would
* be nice to fix the issue
*/
var o1 = new CircularRefPublicProperty1();
var o2 = new CircularRefPublicProperty2();
o1.PropertyName = o2;
o2.PropertyName1 = o1;
Assert.Equal(
"var o1 = new CircularRefPublicProperty1();var o2 = new CircularRefPublicProperty2();o1.PropertyName = o2;o2.PropertyName = o1;",
o1.ToTest().Log(_output)
);
}
[Fact(Skip = "Need to fix this test")]
public void ComplexCircularReferenceDetection()
{
/*
* @todo #11:60m/DEV Make ComplexCircularReferenceDetection test to be green.
* Now the circular references between the objects are not detected. It would
* be nice to fix the issue
*/
var o1 = new CircularRefPublicProperty1();
var o2 = new CircularRefPublicProperty2();
var o3 = new CircularRefPublicProperty3();
o1.PropertyName = o2;
o2.PropertyName3 = o3;
o3.PropertyName = o1;
Assert.Equal(
"var o1 = new CircularRefPublicProperty1();" +
"var o2 = new CircularRefPublicProperty2();" +
"var o3 = new CircularRefPublicProperty3();" +
"o1.PropertyName = o2;" +
"o2.PropertyName3 = o3;" +
"o3.PropertyName3 = o1;",
o1.ToTest().Log(_output)
);
}
[Fact]
public void TheSameObjectDetection()
{
var user = new User("user name");
var withUser = new WithUserArgument(
user,
new WithUserPublicProperty
{
User = user
}
);
Assert.Equal(
"var user = new User(\"user name\");" + Environment.NewLine +
"new WithUserArgument(user,new WithUserPublicProperty(){User = user})",
withUser.ToTest().Log(_output)
);
}
[Fact(Skip = "Need to fix this test")]
public void SingletonAsArgument()
{
/*
* @todo #:60m/DEV Make SingletonAsArgument test to be green.
* Now the singletons are not detected. It would be nice to fix the issue
*/
Assert.Equal(
"new WithSingletonArgument(SingletonClass.Instance)",
new WithSingletonArgument(SingletonClass.Instance)
.ToTest()
.Log(_output)
);
}
[Fact(Skip = "Need to fix this test")]
public void NotFullyRecreatedWarningComment()
{
/*
* @todo #:60m/LEAD Warning comment when object has an internal state that can not be initialized through the constructor.
* It means that the state of an object has been changed after its been created and it's happened through the method or event
*/
Assert.Equal(
@"
/*
* Warning!!!
* Current object was not fully recreated because of some internal object field was changed
* after the object was created.
*/
new ChangedStateObject(new Price(10),new WithUserPublicProperty(){User = new User(""Test Name"")})",
new ChangedStateObject(
new Price(10),
new WithUserPublicProperty
{
User = new User("Test Name")
}
).ChangeState()
.ToTest()
.Log(_output)
);
}
}
}