Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Tynamix.ObjectFiller.Test/TestIgnoranceOfInheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ObjectFiller.Test
{
using ObjectFiller.Test.TestPoco.Person;

using Xunit;
using Tynamix.ObjectFiller;

public class Student : Person
{
public string Class { get; set; }
}

public class TestIgnoranceOfInheritance
{
[Fact]
public void IfIgnoreInheritanceIsSetToTrueTheNameOfTheStudentShouldBeNull()
{
Filler<Student> filler = new Filler<Student>();
filler.Setup().IgnoreInheritance();
var student = filler.Create();

Assert.Null(student.FirstName);
Assert.NotNull(student.Class);
}

[Fact]
public void IfIgnoreInheritanceIsSetToFalseTheNameOfTheStudentShouldNotBeNull()
{
Filler<Student> filler = new Filler<Student>();
filler.Setup()
.OnType<IAddress>().CreateInstanceOf<Address>();
var student = filler.Create();

Assert.NotNull(student.FirstName);
Assert.NotNull(student.Class);
}
}
}
17 changes: 10 additions & 7 deletions Tynamix.ObjectFiller.Test/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",

"commands": {
"test": "xunit.runner.dnx"
},

"frameworks": {
"dnx451": {
"compilationOptions": { "define": [ "NET4X" ] },
"dependencies": {
"xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204",
"System.Text.RegularExpressions": "4.0.11-beta-23516",
"Tynamix.ObjectFiller": "1.4.2"
}
"compilationOptions": { "define": [ "NET4X" ] }
}
},

"dependencies": {
"xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204",
"System.Text.RegularExpressions": "4.0.11-beta-23516",
"Tynamix.ObjectFiller": "1.5.0-*"
}
}
16 changes: 8 additions & 8 deletions Tynamix.ObjectFiller.Test/project.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"lib/net45/_._": {}
}
},
"Tynamix.ObjectFiller/1.4.2": {
"Tynamix.ObjectFiller/1.5.0": {
"type": "project",
"framework": ".NETFramework,Version=v4.5"
},
Expand Down Expand Up @@ -396,7 +396,7 @@
"lib/net45/_._": {}
}
},
"Tynamix.ObjectFiller/1.4.2": {
"Tynamix.ObjectFiller/1.5.0": {
"type": "project",
"framework": ".NETFramework,Version=v4.5"
},
Expand Down Expand Up @@ -650,7 +650,7 @@
"lib/net45/_._": {}
}
},
"Tynamix.ObjectFiller/1.4.2": {
"Tynamix.ObjectFiller/1.5.0": {
"type": "project",
"framework": ".NETFramework,Version=v4.5"
},
Expand Down Expand Up @@ -766,7 +766,7 @@
}
},
"libraries": {
"Tynamix.ObjectFiller/1.4.2": {
"Tynamix.ObjectFiller/1.5.0": {
"type": "project",
"path": "../Tynamix.ObjectFiller/project.json"
},
Expand Down Expand Up @@ -1123,12 +1123,12 @@
}
},
"projectFileDependencyGroups": {
"": [],
"DNX,Version=v4.5.1": [
"": [
"xunit >= 2.1.0",
"xunit.runner.dnx >= 2.1.0-rc1-build204",
"System.Text.RegularExpressions >= 4.0.11-beta-23516",
"Tynamix.ObjectFiller >= 1.4.2"
]
"Tynamix.ObjectFiller >= 1.5.0-*"
],
"DNX,Version=v4.5.1": []
}
}
7 changes: 4 additions & 3 deletions Tynamix.ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private static bool TypeIsCollection(Type type)
/// </returns>
private static bool TypeIsPoco(Type type)
{
return !type.IsValueType() && !type.IsArray && type.IsClass() && type.GetProperties().Any()
return !type.IsValueType() && !type.IsArray && type.IsClass() && type.GetProperties(false).Any()
&& (type.Namespace == null
|| (!type.Namespace.StartsWith("System") && !type.Namespace.StartsWith("Microsoft")));
}
Expand Down Expand Up @@ -616,8 +616,9 @@ private void FillInternal(object objectToFill, HashStack<Type> typeTracker = nul
return;
}

var properties =
targetType.GetProperties().Where(prop => this.GetSetMethodOnDeclaringType(prop) != null).ToArray();
var properties = targetType.GetProperties(currentSetup.IgnoreInheritance)
.Where(prop => this.GetSetMethodOnDeclaringType(prop) != null)
.ToArray();

if (properties.Length == 0)
{
Expand Down
11 changes: 9 additions & 2 deletions Tynamix.ObjectFiller/NetTypeApiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,23 @@ internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)
#endif
}

internal static IEnumerable<PropertyInfo> GetProperties(this Type source)
internal static IEnumerable<PropertyInfo> GetProperties(this Type source, bool ignoreInheritance)
{
#if (NET3X || NET4X)

if (ignoreInheritance)
{
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
}

return source.GetProperties();
#endif

#if (NETSTD)

var propertyInfos = source.GetTypeInfo().DeclaredProperties.ToList();
if (source.GetTypeInfo().BaseType != null)

if (ignoreInheritance == false && source.GetTypeInfo().BaseType != null)
{
foreach (var property in source.GetTypeInfo().BaseType.GetTypeInfo().DeclaredProperties)
{
Expand Down
6 changes: 6 additions & 0 deletions Tynamix.ObjectFiller/Setup/FillerSetupItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal FillerSetupItem()
this.TypesToIgnore = new List<Type>();
this.InterfaceToImplementation = new Dictionary<Type, Type>();
this.IgnoreAllUnknownTypes = false;
this.IgnoreInheritance = false;

this.SetDefaultRandomizer();
}
Expand Down Expand Up @@ -100,6 +101,11 @@ internal FillerSetupItem()
/// </summary>
internal bool IgnoreAllUnknownTypes { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the properties of the base type will be filled or not.
/// </summary>
internal bool IgnoreInheritance { get; set; }

/// <summary>
/// Gets or sets a value indicating whether a e exception shall be thrown on circular reference.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Tynamix.ObjectFiller/Setup/FluentFillerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ public FluentFillerApi<TTargetObject> IgnoreAllUnknownTypes()
return this;
}

/// <summary>
/// Call this if the ObjectFiller should ignore all properties of the base class. For example you have a class
/// 'Student' which derives from class 'Person' and the class Person has a property 'Name'. If you want to use ObjectFiller
/// to fill/create a student and you call this method, the name will be null because it is defined in the base class 'Person'
/// </summary>
/// <returns>The <see cref="FluentFillerApi{TTargetObject}"/></returns>
public FluentFillerApi<TTargetObject> IgnoreInheritance()
{
this.setupManager.GetFor<TTargetObject>().IgnoreInheritance = true;

return this;
}

/// <summary>
/// Setup the minimum and maximum item count for lists. The ObjectFiller will not generate more or less list items then this limits.
/// The default value for <see cref="minCount"/> is 1. The default value for <see cref="maxCount"/> is 25.
Expand Down
5 changes: 3 additions & 2 deletions Tynamix.ObjectFiller/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
{
"title": ".NET ObjectFiller - Fill .NET objects with customized random data - by Tynamix",
"version": "1.4.2",
"version": "1.5.0",
"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.",
"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.",
"authors": [ "Roman Köhler", "Hendrik L.", "Christian Harlass", "GothikX" ],
Expand Down Expand Up @@ -39,6 +39,7 @@
"System.Linq.Expressions": "4.0.10-*",
"System.Runtime.Extensions": "4.0.10-*",
"System.Text.RegularExpressions": "4.0.10-*"

}

}
Expand Down