diff --git a/Tynamix.ObjectFiller.Test/TestIgnoranceOfInheritance.cs b/Tynamix.ObjectFiller.Test/TestIgnoranceOfInheritance.cs new file mode 100644 index 0000000..ad38718 --- /dev/null +++ b/Tynamix.ObjectFiller.Test/TestIgnoranceOfInheritance.cs @@ -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 filler = new Filler(); + filler.Setup().IgnoreInheritance(); + var student = filler.Create(); + + Assert.Null(student.FirstName); + Assert.NotNull(student.Class); + } + + [Fact] + public void IfIgnoreInheritanceIsSetToFalseTheNameOfTheStudentShouldNotBeNull() + { + Filler filler = new Filler(); + filler.Setup() + .OnType().CreateInstanceOf
(); + var student = filler.Create(); + + Assert.NotNull(student.FirstName); + Assert.NotNull(student.Class); + } + } +} diff --git a/Tynamix.ObjectFiller.Test/project.json b/Tynamix.ObjectFiller.Test/project.json index 5c01fef..e552ea3 100644 --- a/Tynamix.ObjectFiller.Test/project.json +++ b/Tynamix.ObjectFiller.Test/project.json @@ -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-*" } } diff --git a/Tynamix.ObjectFiller.Test/project.lock.json b/Tynamix.ObjectFiller.Test/project.lock.json index 6316a4f..386d33a 100644 --- a/Tynamix.ObjectFiller.Test/project.lock.json +++ b/Tynamix.ObjectFiller.Test/project.lock.json @@ -142,7 +142,7 @@ "lib/net45/_._": {} } }, - "Tynamix.ObjectFiller/1.4.2": { + "Tynamix.ObjectFiller/1.5.0": { "type": "project", "framework": ".NETFramework,Version=v4.5" }, @@ -396,7 +396,7 @@ "lib/net45/_._": {} } }, - "Tynamix.ObjectFiller/1.4.2": { + "Tynamix.ObjectFiller/1.5.0": { "type": "project", "framework": ".NETFramework,Version=v4.5" }, @@ -650,7 +650,7 @@ "lib/net45/_._": {} } }, - "Tynamix.ObjectFiller/1.4.2": { + "Tynamix.ObjectFiller/1.5.0": { "type": "project", "framework": ".NETFramework,Version=v4.5" }, @@ -766,7 +766,7 @@ } }, "libraries": { - "Tynamix.ObjectFiller/1.4.2": { + "Tynamix.ObjectFiller/1.5.0": { "type": "project", "path": "../Tynamix.ObjectFiller/project.json" }, @@ -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": [] } } \ No newline at end of file diff --git a/Tynamix.ObjectFiller/Filler.cs b/Tynamix.ObjectFiller/Filler.cs index fa014e8..f4ddcea 100644 --- a/Tynamix.ObjectFiller/Filler.cs +++ b/Tynamix.ObjectFiller/Filler.cs @@ -291,7 +291,7 @@ private static bool TypeIsCollection(Type type) /// 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"))); } @@ -616,8 +616,9 @@ private void FillInternal(object objectToFill, HashStack 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) { diff --git a/Tynamix.ObjectFiller/NetTypeApiExtension.cs b/Tynamix.ObjectFiller/NetTypeApiExtension.cs index 203f268..d718ce6 100644 --- a/Tynamix.ObjectFiller/NetTypeApiExtension.cs +++ b/Tynamix.ObjectFiller/NetTypeApiExtension.cs @@ -115,16 +115,23 @@ internal static IEnumerable GetImplementedInterfaces(this Type source) #endif } - internal static IEnumerable GetProperties(this Type source) + internal static IEnumerable 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) { diff --git a/Tynamix.ObjectFiller/Setup/FillerSetupItem.cs b/Tynamix.ObjectFiller/Setup/FillerSetupItem.cs index 5a700c5..0e0977d 100644 --- a/Tynamix.ObjectFiller/Setup/FillerSetupItem.cs +++ b/Tynamix.ObjectFiller/Setup/FillerSetupItem.cs @@ -36,6 +36,7 @@ internal FillerSetupItem() this.TypesToIgnore = new List(); this.InterfaceToImplementation = new Dictionary(); this.IgnoreAllUnknownTypes = false; + this.IgnoreInheritance = false; this.SetDefaultRandomizer(); } @@ -100,6 +101,11 @@ internal FillerSetupItem() /// internal bool IgnoreAllUnknownTypes { get; set; } + /// + /// Gets or sets a value indicating whether the properties of the base type will be filled or not. + /// + internal bool IgnoreInheritance { get; set; } + /// /// Gets or sets a value indicating whether a e exception shall be thrown on circular reference. /// diff --git a/Tynamix.ObjectFiller/Setup/FluentFillerApi.cs b/Tynamix.ObjectFiller/Setup/FluentFillerApi.cs index 109b151..c9d6975 100644 --- a/Tynamix.ObjectFiller/Setup/FluentFillerApi.cs +++ b/Tynamix.ObjectFiller/Setup/FluentFillerApi.cs @@ -144,6 +144,19 @@ public FluentFillerApi IgnoreAllUnknownTypes() return this; } + /// + /// 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' + /// + /// The + public FluentFillerApi IgnoreInheritance() + { + this.setupManager.GetFor().IgnoreInheritance = true; + + return this; + } + /// /// 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 is 1. The default value for is 25. diff --git a/Tynamix.ObjectFiller/project.json b/Tynamix.ObjectFiller/project.json index 85ea78f..8c9f438 100644 --- a/Tynamix.ObjectFiller/project.json +++ b/Tynamix.ObjectFiller/project.json @@ -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" ], @@ -39,6 +39,7 @@ "System.Linq.Expressions": "4.0.10-*", "System.Runtime.Extensions": "4.0.10-*", "System.Text.RegularExpressions": "4.0.10-*" + } }