Skip to content

Commit 23e91e0

Browse files
author
Roman Köhler
committed
#31 - fixed the abstract class bug!
1 parent 2ad33d4 commit 23e91e0

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Security.Cryptography.X509Certificates;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Tynamix.ObjectFiller;
6+
7+
namespace ObjectFiller.Test
8+
{
9+
10+
public abstract class Parent
11+
{
12+
public Parent(int id)
13+
{
14+
Id = id;
15+
}
16+
17+
public int Id { get; set; }
18+
19+
}
20+
21+
public class ChildOne : Parent
22+
{
23+
public ChildOne(int id)
24+
: base(id)
25+
{
26+
}
27+
28+
public string Name { get; set; }
29+
}
30+
31+
public class ChildTwo : Parent
32+
{
33+
public ChildTwo(int id)
34+
: base(id)
35+
{
36+
}
37+
38+
public string OtherName { get; set; }
39+
}
40+
41+
public class ParentOfParent
42+
{
43+
public ParentOfParent()
44+
{
45+
Parents = new List<Parent>();
46+
}
47+
48+
public List<Parent> Parents { get; private set; }
49+
50+
}
51+
52+
53+
[TestClass]
54+
public class CreateInstanceTest
55+
{
56+
57+
[TestMethod]
58+
public void TestCreateInstanceOfChildOne()
59+
{
60+
Filler<ParentOfParent> p = new Filler<ParentOfParent>();
61+
62+
p.Setup().OnType<Parent>().CreateInstanceOf<ChildOne>()
63+
.SetupFor<ChildOne>()
64+
.OnProperty(x => x.Name).Use(() => "TEST");
65+
66+
var pop = p.Create();
67+
68+
Assert.IsNotNull(pop);
69+
Assert.IsNotNull(pop.Parents);
70+
Assert.IsTrue(pop.Parents.All(x => x is ChildOne));
71+
Assert.IsFalse(pop.Parents.Any(x => x is ChildTwo));
72+
73+
Assert.IsTrue(pop.Parents.Cast<ChildOne>().All(x => x.Name == "TEST"));
74+
75+
76+
}
77+
78+
}
79+
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="AddressFillingTest.cs" />
49+
<Compile Include="CreateInstanceTest.cs" />
4950
<Compile Include="EnumTest.cs" />
5051
<Compile Include="HashStackTests.cs" />
5152
<Compile Include="LibraryFillingTest.cs" />

ObjectFiller/Filler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ private object GetFilledObject(Type type, FillerSetupItem currentSetupItem, Hash
253253
return list;
254254
}
255255

256-
if (type.IsInterface)
256+
if (type.IsInterface || type.IsAbstract)
257257
{
258-
return GetInterfaceInstance(type, currentSetupItem, typeTracker);
258+
return CreateInstanceOfInterfaceOrAbstractClass(type, currentSetupItem, typeTracker);
259259
}
260260

261261
if (TypeIsPoco(type))
@@ -394,7 +394,7 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
394394
return list;
395395
}
396396

397-
private object GetInterfaceInstance(Type interfaceType, FillerSetupItem setupItem, HashStack<Type> typeTracker)
397+
private object CreateInstanceOfInterfaceOrAbstractClass(Type interfaceType, FillerSetupItem setupItem, HashStack<Type> typeTracker)
398398
{
399399
object result;
400400
if (setupItem.TypeToRandomFunc.ContainsKey(interfaceType))

0 commit comments

Comments
 (0)