Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#94 Feature: Also skip members defined via interface #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/AutoBogus.Tests.Models/Complex/IWithCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace AutoBogus.Tests.Models.Complex
{
public interface IWithCode
{
Guid? Code { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/AutoBogus.Tests.Models/Complex/Order.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System;
using System.Collections.Generic;

namespace AutoBogus.Tests.Models.Complex
{
public sealed class Order
public sealed class Order : IWithCode
{
public DateTime Timestamp;

Expand Down
12 changes: 12 additions & 0 deletions src/AutoBogus.Tests/AutoFakerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ public void Should_Skip_Configured_Members()
instance.Discounts.Should().BeNull();
instance.Items.Should().OnlyContain(i => i.Discounts == null);
}

[Fact]
public void Should_Skip_Configured_Members_Of_Interface()
{
var instance = AutoFaker.Generate<Order>(builder =>
{
builder
.WithSkip<IWithCode>(i => i.Code);
});

instance.Code.Should().BeNull();
}
}

public class Behaviors_Types
Expand Down
23 changes: 15 additions & 8 deletions src/AutoBogus/AutoBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public virtual void PopulateInstance<TType>(object instance, AutoGenerateContext
{
// Check if the member has a skip config or the type has already been generated as a parent
// If so skip this generation otherwise track it for use later in the object tree
if (ShouldSkip(member.Type, $"{type.FullName}.{member.Name}", context))
if (ShouldSkip(type, member, context))
{
continue;
}
Expand Down Expand Up @@ -111,19 +111,26 @@ public virtual void PopulateInstance<TType>(object instance, AutoGenerateContext
}
}

private bool ShouldSkip(Type type, string path, AutoGenerateContext context)
private bool ShouldSkip(Type type, AutoMember member, AutoGenerateContext context)
{
// Skip if the type is found
if (context.Config.SkipTypes.Contains(type))
// Skip if the member type is found
if (context.Config.SkipTypes.Contains(member.Type))
{
return true;
}

// Skip if the path is found
if (context.Config.SkipPaths.Contains(path))
// Skip if the path is found (both current type and its implemented interfaces)
if (context.Config.SkipPaths.Contains($"{type.FullName}.{member.Name}"))
{
return true;
}
foreach (var implementedInterfaceType in type.GetInterfaces())
{
if (context.Config.SkipPaths.Contains($"{implementedInterfaceType.FullName}.{member.Name}"))
{
return true;
}
}

//check if tree depth is reached
var treeDepth = context.Config.TreeDepth.Invoke(context);
Expand All @@ -132,8 +139,8 @@ private bool ShouldSkip(Type type, string path, AutoGenerateContext context)
return true;

// Finally check if the recursive depth has been reached
var count = context.TypesStack.Count(t => t == type);

var count = context.TypesStack.Count(t => t == member.Type);
var recursiveDepth = context.Config.RecursiveDepth.Invoke(context);

return count >= recursiveDepth;
Expand Down