Skip to content

Commit

Permalink
Addressed issue petabridge#3 for flat document trees. Handling anothe…
Browse files Browse the repository at this point in the history
…r edge case for instances where a tree can have a collection of children.
  • Loading branch information
Aaronontheweb committed Aug 30, 2012
1 parent f058018 commit bbaa036
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Faker.Tests/Faker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="MatcherTests\ListMatcherTests.cs" />
<Compile Include="MatcherTests\NestedPocoMatcherTests.cs" />
<Compile Include="MatcherTests\SimplePocoMatcherTests.cs" />
<Compile Include="MatcherTests\TreeMatcherTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SelectorTests\DateTimeSelectorTests.cs" />
<Compile Include="SelectorTests\NumberSelectorTests.cs" />
Expand Down
74 changes: 74 additions & 0 deletions Faker.Tests/MatcherTests/TreeMatcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Faker.Tests.MatcherTests
{
[TestFixture(Description = "Tests to verify that Faker's behavior when working with tree structures doesn't cause problems")]
public class TreeMatcherTests
{
private Matcher _matcher;

#region Tree test classes

public class FlatObjectTree
{
public string Id { get; set; }
public string Name { get; set; }
public FlatObjectTree Parent { get; set; }
}

public class ObjectTreeWithMultipleChildren
{
public string Id { get; set; }
public string Name { get; set; }
public ObjectTreeWithMultipleChildren Parent { get; set; }
public List<ObjectTreeWithMultipleChildren> Children { get; set; }
}

#endregion

#region Setup / Teardown

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//Create a new matcher using the default type table
_matcher = new Matcher();
}

#endregion

#region Tests

[Test(Description = "Should only create a tree structure that has a single node in this instance")]
public void Should_Terminate_Tree_Structure_After_One_Node()
{
var flatObjectTree = new FlatObjectTree();

_matcher.Match(flatObjectTree);

Assert.IsNotNullOrEmpty(flatObjectTree.Id);
Assert.IsNotNullOrEmpty(flatObjectTree.Name);
Assert.IsNull(flatObjectTree.Parent, "Should only create one node (the root) in a tree structure");
}

[Test(Description = "In an enviromment where a tree can support a list of child nodes, make sure the list only goes 1 tier deep")]
public void Should_Create_Tree_Structure_with_only_one_layer_of_children()
{
var objectTreeWithMultipleChildren = new ObjectTreeWithMultipleChildren();

_matcher.Match(objectTreeWithMultipleChildren);

Assert.IsNotNullOrEmpty(objectTreeWithMultipleChildren.Id);
Assert.IsNotNullOrEmpty(objectTreeWithMultipleChildren.Name);
Assert.IsNull(objectTreeWithMultipleChildren.Parent, "Should only create one node (the root) in a tree structure");
Assert.IsNotNull(objectTreeWithMultipleChildren.Children);
Assert.IsTrue(objectTreeWithMultipleChildren.Children.All(x => x.Children == null));
}

#endregion
}
}
3 changes: 3 additions & 0 deletions Faker/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ protected virtual void ProcessProperties(PropertyInfo[] properties, object targe
if (!property.CanWrite) //Bail if we can't write to the property
continue;

if(property.PropertyType == targetObject.GetType()) //If the property is a tree structure, bail (causes infinite recursion otherwise)
continue;

ProcessProperty(property, targetObject);
}
}
Expand Down

0 comments on commit bbaa036

Please sign in to comment.