Skip to content

Commit b566a80

Browse files
committed
Merge pull request #77 from Tynamix/69_IgnoreInheritance
69 ignore inheritance
2 parents 05a7c0b + 132f412 commit b566a80

File tree

8 files changed

+96
-22
lines changed

8 files changed

+96
-22
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace ObjectFiller.Test
7+
{
8+
using ObjectFiller.Test.TestPoco.Person;
9+
10+
using Xunit;
11+
using Tynamix.ObjectFiller;
12+
13+
public class Student : Person
14+
{
15+
public string Class { get; set; }
16+
}
17+
18+
public class TestIgnoranceOfInheritance
19+
{
20+
[Fact]
21+
public void IfIgnoreInheritanceIsSetToTrueTheNameOfTheStudentShouldBeNull()
22+
{
23+
Filler<Student> filler = new Filler<Student>();
24+
filler.Setup().IgnoreInheritance();
25+
var student = filler.Create();
26+
27+
Assert.Null(student.FirstName);
28+
Assert.NotNull(student.Class);
29+
}
30+
31+
[Fact]
32+
public void IfIgnoreInheritanceIsSetToFalseTheNameOfTheStudentShouldNotBeNull()
33+
{
34+
Filler<Student> filler = new Filler<Student>();
35+
filler.Setup()
36+
.OnType<IAddress>().CreateInstanceOf<Address>();
37+
var student = filler.Create();
38+
39+
Assert.NotNull(student.FirstName);
40+
Assert.NotNull(student.Class);
41+
}
42+
}
43+
}

Tynamix.ObjectFiller.Test/project.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
"tags": [ "" ],
66
"projectUrl": "",
77
"licenseUrl": "",
8+
89
"commands": {
910
"test": "xunit.runner.dnx"
1011
},
12+
1113
"frameworks": {
1214
"dnx451": {
13-
"compilationOptions": { "define": [ "NET4X" ] },
14-
"dependencies": {
15-
"xunit": "2.1.0",
16-
"xunit.runner.dnx": "2.1.0-rc1-build204",
17-
"System.Text.RegularExpressions": "4.0.11-beta-23516",
18-
"Tynamix.ObjectFiller": "1.4.2"
19-
}
15+
"compilationOptions": { "define": [ "NET4X" ] }
2016
}
17+
},
18+
19+
"dependencies": {
20+
"xunit": "2.1.0",
21+
"xunit.runner.dnx": "2.1.0-rc1-build204",
22+
"System.Text.RegularExpressions": "4.0.11-beta-23516",
23+
"Tynamix.ObjectFiller": "1.5.0-*"
2124
}
2225
}

Tynamix.ObjectFiller.Test/project.lock.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"lib/net45/_._": {}
143143
}
144144
},
145-
"Tynamix.ObjectFiller/1.4.2": {
145+
"Tynamix.ObjectFiller/1.5.0": {
146146
"type": "project",
147147
"framework": ".NETFramework,Version=v4.5"
148148
},
@@ -396,7 +396,7 @@
396396
"lib/net45/_._": {}
397397
}
398398
},
399-
"Tynamix.ObjectFiller/1.4.2": {
399+
"Tynamix.ObjectFiller/1.5.0": {
400400
"type": "project",
401401
"framework": ".NETFramework,Version=v4.5"
402402
},
@@ -650,7 +650,7 @@
650650
"lib/net45/_._": {}
651651
}
652652
},
653-
"Tynamix.ObjectFiller/1.4.2": {
653+
"Tynamix.ObjectFiller/1.5.0": {
654654
"type": "project",
655655
"framework": ".NETFramework,Version=v4.5"
656656
},
@@ -766,7 +766,7 @@
766766
}
767767
},
768768
"libraries": {
769-
"Tynamix.ObjectFiller/1.4.2": {
769+
"Tynamix.ObjectFiller/1.5.0": {
770770
"type": "project",
771771
"path": "../Tynamix.ObjectFiller/project.json"
772772
},
@@ -1123,12 +1123,12 @@
11231123
}
11241124
},
11251125
"projectFileDependencyGroups": {
1126-
"": [],
1127-
"DNX,Version=v4.5.1": [
1126+
"": [
11281127
"xunit >= 2.1.0",
11291128
"xunit.runner.dnx >= 2.1.0-rc1-build204",
11301129
"System.Text.RegularExpressions >= 4.0.11-beta-23516",
1131-
"Tynamix.ObjectFiller >= 1.4.2"
1132-
]
1130+
"Tynamix.ObjectFiller >= 1.5.0-*"
1131+
],
1132+
"DNX,Version=v4.5.1": []
11331133
}
11341134
}

Tynamix.ObjectFiller/Filler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private static bool TypeIsCollection(Type type)
291291
/// </returns>
292292
private static bool TypeIsPoco(Type type)
293293
{
294-
return !type.IsValueType() && !type.IsArray && type.IsClass() && type.GetProperties().Any()
294+
return !type.IsValueType() && !type.IsArray && type.IsClass() && type.GetProperties(false).Any()
295295
&& (type.Namespace == null
296296
|| (!type.Namespace.StartsWith("System") && !type.Namespace.StartsWith("Microsoft")));
297297
}
@@ -616,8 +616,9 @@ private void FillInternal(object objectToFill, HashStack<Type> typeTracker = nul
616616
return;
617617
}
618618

619-
var properties =
620-
targetType.GetProperties().Where(prop => this.GetSetMethodOnDeclaringType(prop) != null).ToArray();
619+
var properties = targetType.GetProperties(currentSetup.IgnoreInheritance)
620+
.Where(prop => this.GetSetMethodOnDeclaringType(prop) != null)
621+
.ToArray();
621622

622623
if (properties.Length == 0)
623624
{

Tynamix.ObjectFiller/NetTypeApiExtension.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,23 @@ internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)
115115
#endif
116116
}
117117

118-
internal static IEnumerable<PropertyInfo> GetProperties(this Type source)
118+
internal static IEnumerable<PropertyInfo> GetProperties(this Type source, bool ignoreInheritance)
119119
{
120120
#if (NET3X || NET4X)
121+
122+
if (ignoreInheritance)
123+
{
124+
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
125+
}
126+
121127
return source.GetProperties();
122128
#endif
123129

124130
#if (NETSTD)
125131

126132
var propertyInfos = source.GetTypeInfo().DeclaredProperties.ToList();
127-
if (source.GetTypeInfo().BaseType != null)
133+
134+
if (ignoreInheritance == false && source.GetTypeInfo().BaseType != null)
128135
{
129136
foreach (var property in source.GetTypeInfo().BaseType.GetTypeInfo().DeclaredProperties)
130137
{

Tynamix.ObjectFiller/Setup/FillerSetupItem.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal FillerSetupItem()
3636
this.TypesToIgnore = new List<Type>();
3737
this.InterfaceToImplementation = new Dictionary<Type, Type>();
3838
this.IgnoreAllUnknownTypes = false;
39+
this.IgnoreInheritance = false;
3940

4041
this.SetDefaultRandomizer();
4142
}
@@ -100,6 +101,11 @@ internal FillerSetupItem()
100101
/// </summary>
101102
internal bool IgnoreAllUnknownTypes { get; set; }
102103

104+
/// <summary>
105+
/// Gets or sets a value indicating whether the properties of the base type will be filled or not.
106+
/// </summary>
107+
internal bool IgnoreInheritance { get; set; }
108+
103109
/// <summary>
104110
/// Gets or sets a value indicating whether a e exception shall be thrown on circular reference.
105111
/// </summary>

Tynamix.ObjectFiller/Setup/FluentFillerApi.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ public FluentFillerApi<TTargetObject> IgnoreAllUnknownTypes()
144144
return this;
145145
}
146146

147+
/// <summary>
148+
/// Call this if the ObjectFiller should ignore all properties of the base class. For example you have a class
149+
/// 'Student' which derives from class 'Person' and the class Person has a property 'Name'. If you want to use ObjectFiller
150+
/// to fill/create a student and you call this method, the name will be null because it is defined in the base class 'Person'
151+
/// </summary>
152+
/// <returns>The <see cref="FluentFillerApi{TTargetObject}"/></returns>
153+
public FluentFillerApi<TTargetObject> IgnoreInheritance()
154+
{
155+
this.setupManager.GetFor<TTargetObject>().IgnoreInheritance = true;
156+
157+
return this;
158+
}
159+
147160
/// <summary>
148161
/// Setup the minimum and maximum item count for lists. The ObjectFiller will not generate more or less list items then this limits.
149162
/// The default value for <see cref="minCount"/> is 1. The default value for <see cref="maxCount"/> is 25.

Tynamix.ObjectFiller/project.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{
1+
{
22
"title": ".NET ObjectFiller - Fill .NET objects with customized random data - by Tynamix",
3-
"version": "1.4.2",
3+
"version": "1.5.0",
44
"description": "The Tynamix ObjectFiller.NET fills the properties of your objects with random data. Use it for unittest, prototyping and whereever you need some random testdata. It has a fluent API and is highly customizable. It supports also IEnumerables and Dictionaries and constructors WITH parameters. It is also possible to fill instances and to write private properties.",
55
"summary": "The Tynamix ObjectFiller.NET fills the properties of your objects with customized random data. Use it for unittests, prototyping and whereever you need some random testdata.",
66
"authors": [ "Roman Köhler", "Hendrik L.", "Christian Harlass", "GothikX" ],
@@ -39,6 +39,7 @@
3939
"System.Linq.Expressions": "4.0.10-*",
4040
"System.Runtime.Extensions": "4.0.10-*",
4141
"System.Text.RegularExpressions": "4.0.10-*"
42+
4243
}
4344

4445
}

0 commit comments

Comments
 (0)