Skip to content

Added support for nested properties #46

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

Merged
merged 1 commit into from
Nov 8, 2015
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
21 changes: 21 additions & 0 deletions TestStack.Dossier.Tests/GetSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ public void GivenAValueHasBeenSetAgainstAProperty_WhenRetrievingTheValueForThatP
retrieved.ShouldBe(SetValue);
}

[Fact]
public void GivenAValueHasBeenSetAgainstANestedProperty_WhenRetrievingTheValueForThatProperty_ThenTheSetValueIsReturned()
{
_b.Set(x => x.PostalAddress.Identifier, SetValue);

var retrieved = _b.Get(x => x.PostalAddress.Identifier);

retrieved.ShouldBe(SetValue);
}

[Fact]
public void GivenAValueHasBeenSetAgainstANestedPropertyAndDifferentVaLueHasBeenSetAgainstANonNestedPropertyWithTheSameName_WhenRetrievingTheValueForThatNestedProperty_ThenTheSetValueIsReturned()
{
_b.Set(x => x.PostalAddress.Identifier, SetValue);
_b.Set(x => x.Identifier, (string)null);

var retrieved = _b.Get(x => x.PostalAddress.Identifier);

retrieved.ShouldBe(SetValue);
}

[Fact]
public void GivenTwoValuesHaveBeenSetAgainstAProperty_WhenRetrievingTheValueForThatProperty_ThenTheLastSetValueIsReturned()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BasicCustomerBuilder : TestDataBuilder<Customer, BasicCustomerBuild
{
protected override Customer BuildObject()
{
return new Customer("customer1", "First Name", "Last Name", 2013, CustomerClass.Normal);
return new Customer("customer1", "First Name", "Last Name", 2013, new Address("AddressIdentifier", 5, "Street Name", "Suburb", "City", "PostCode"), CustomerClass.Normal);
}
}
}
13 changes: 13 additions & 0 deletions TestStack.Dossier.Tests/TestHelpers/Builders/CustomerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ public virtual CustomerBuilder WhoJoinedIn(int yearJoined)
return Set(x => x.YearJoined, yearJoined);
}

public virtual CustomerBuilder WithPostalAdressIdentifier(string identifier)
{
return Set(x => x.PostalAddress.Identifier, identifier);
}

protected override Customer BuildObject()
{
return new Customer(
Get(x => x.Identifier),
Get(x => x.FirstName),
Get(x => x.LastName),
Get(x => x.YearJoined),
new Address(
Get(x => x.PostalAddress.Identifier),
Get(x => x.PostalAddress.StreetNo),
Get(x => x.PostalAddress.StreetName),
Get(x => x.PostalAddress.Suburb),
Get(x => x.PostalAddress.City),
Get(x => x.PostalAddress.PostCode)
),
Get(x => x.CustomerClass)
);
}
Expand Down
30 changes: 30 additions & 0 deletions TestStack.Dossier.Tests/TestHelpers/Objects/Entities/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestStack.Dossier.Tests.TestHelpers.Objects.Entities
{
public class Address
{
protected Address() { }

public Address(string identifier, int streetNo, string streetName, string suburb, string city, string postCode)
{
Identifier = identifier;
StreetNo = streetNo;
StreetName = streetName;
Suburb = suburb;
City = city;
PostCode = postCode;
}

public string Identifier { get; private set; }
public virtual int StreetNo { get; private set; }
public string StreetName { get; private set; }
public string Suburb { get; private set; }
public string City { get; private set; }
public string PostCode { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Customer
{
protected Customer() {}

public Customer(string identifier, string firstName, string lastName, int yearJoined, CustomerClass customerClass)
public Customer(string identifier, string firstName, string lastName, int yearJoined, Address postalAddress, CustomerClass customerClass)
{
if (string.IsNullOrEmpty(identifier))
throw new ArgumentNullException("identifier");
Expand All @@ -19,6 +19,7 @@ public Customer(string identifier, string firstName, string lastName, int yearJo
FirstName = firstName;
LastName = lastName;
YearJoined = yearJoined;
PostalAddress = postalAddress;
CustomerClass = customerClass;
}

Expand All @@ -33,6 +34,7 @@ public virtual int CustomerForHowManyYears(DateTime since)
public virtual string FirstName { get; private set; }
public virtual string LastName { get; private set; }
public virtual int YearJoined { get; private set; }
public virtual Address PostalAddress { get; private set; }
public virtual CustomerClass CustomerClass { get; private set; }
}
}
1 change: 1 addition & 0 deletions TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Factories\CallConstructorFactoryTests.cs" />
<Compile Include="Factories\PublicPropertySettersFactoryTests.cs" />
<Compile Include="TestHelpers\Builders\BuilderWithDefaults.cs" />
<Compile Include="TestHelpers\Objects\Entities\Address.cs" />
<Compile Include="TestHelpers\Objects\Entities\Company.cs" />
<Compile Include="TestHelpers\Objects\Entities\Customer.cs" />
<Compile Include="TestHelpers\Objects\Entities\CustomerClass.cs" />
Expand Down
17 changes: 17 additions & 0 deletions TestStack.Dossier/PathExpressionVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Linq.Expressions;

namespace TestStack.Dossier
{
internal class PathExpressionVisitor : ExpressionVisitor
{
internal readonly List<string> Path = new List<string>();

protected override Expression VisitMember(MemberExpression node)
{
Path.Add(node.Member.Name);
return base.VisitMember(node);
}
}

}
6 changes: 5 additions & 1 deletion TestStack.Dossier/Reflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal static class Reflector
public static string GetPropertyNameFor<TObject, TValue>(Expression<Func<TObject, TValue>> property)
{
var memExp = property.Body as MemberExpression;

if (memExp == null)
throw new ArgumentException(
string.Format(
Expand All @@ -21,7 +22,10 @@ public static string GetPropertyNameFor<TObject, TValue>(Expression<Func<TObject
"property"
);

return memExp.Member.Name;
var visitor = new PathExpressionVisitor();
visitor.Visit(memExp);

return string.Join(".", Enumerable.Reverse(visitor.Path));
}

public static PropertyInfo[] GetSettablePropertiesFor<T>()
Expand Down
1 change: 1 addition & 0 deletions TestStack.Dossier/TestStack.Dossier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Lists\ListBuilderInterceptor.cs" />
<Compile Include="NullingExpandoObject.cs" />
<Compile Include="Factories\IFactory.cs" />
<Compile Include="PathExpressionVisitor.cs" />
<Compile Include="Reflector.cs" />
<Compile Include="Suppliers\DefaultEmailValueSupplier.cs" />
<Compile Include="Suppliers\DefaultLastNameValueSupplier.cs" />
Expand Down