Skip to content

Commit fb62c7a

Browse files
committed
Merge pull request #46 from cur3n4/master
Added support for nested properties
2 parents 0148af0 + f6b0ad2 commit fb62c7a

File tree

9 files changed

+92
-3
lines changed

9 files changed

+92
-3
lines changed

TestStack.Dossier.Tests/GetSetTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ public void GivenAValueHasBeenSetAgainstAProperty_WhenRetrievingTheValueForThatP
2525
retrieved.ShouldBe(SetValue);
2626
}
2727

28+
[Fact]
29+
public void GivenAValueHasBeenSetAgainstANestedProperty_WhenRetrievingTheValueForThatProperty_ThenTheSetValueIsReturned()
30+
{
31+
_b.Set(x => x.PostalAddress.Identifier, SetValue);
32+
33+
var retrieved = _b.Get(x => x.PostalAddress.Identifier);
34+
35+
retrieved.ShouldBe(SetValue);
36+
}
37+
38+
[Fact]
39+
public void GivenAValueHasBeenSetAgainstANestedPropertyAndDifferentVaLueHasBeenSetAgainstANonNestedPropertyWithTheSameName_WhenRetrievingTheValueForThatNestedProperty_ThenTheSetValueIsReturned()
40+
{
41+
_b.Set(x => x.PostalAddress.Identifier, SetValue);
42+
_b.Set(x => x.Identifier, (string)null);
43+
44+
var retrieved = _b.Get(x => x.PostalAddress.Identifier);
45+
46+
retrieved.ShouldBe(SetValue);
47+
}
48+
2849
[Fact]
2950
public void GivenTwoValuesHaveBeenSetAgainstAProperty_WhenRetrievingTheValueForThatProperty_ThenTheLastSetValueIsReturned()
3051
{

TestStack.Dossier.Tests/TestHelpers/Builders/BasicCustomerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class BasicCustomerBuilder : TestDataBuilder<Customer, BasicCustomerBuild
66
{
77
protected override Customer BuildObject()
88
{
9-
return new Customer("customer1", "First Name", "Last Name", 2013, CustomerClass.Normal);
9+
return new Customer("customer1", "First Name", "Last Name", 2013, new Address("AddressIdentifier", 5, "Street Name", "Suburb", "City", "PostCode"), CustomerClass.Normal);
1010
}
1111
}
1212
}

TestStack.Dossier.Tests/TestHelpers/Builders/CustomerBuilder.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ public virtual CustomerBuilder WhoJoinedIn(int yearJoined)
1919
return Set(x => x.YearJoined, yearJoined);
2020
}
2121

22+
public virtual CustomerBuilder WithPostalAdressIdentifier(string identifier)
23+
{
24+
return Set(x => x.PostalAddress.Identifier, identifier);
25+
}
26+
2227
protected override Customer BuildObject()
2328
{
2429
return new Customer(
2530
Get(x => x.Identifier),
2631
Get(x => x.FirstName),
2732
Get(x => x.LastName),
2833
Get(x => x.YearJoined),
34+
new Address(
35+
Get(x => x.PostalAddress.Identifier),
36+
Get(x => x.PostalAddress.StreetNo),
37+
Get(x => x.PostalAddress.StreetName),
38+
Get(x => x.PostalAddress.Suburb),
39+
Get(x => x.PostalAddress.City),
40+
Get(x => x.PostalAddress.PostCode)
41+
),
2942
Get(x => x.CustomerClass)
3043
);
3144
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace TestStack.Dossier.Tests.TestHelpers.Objects.Entities
8+
{
9+
public class Address
10+
{
11+
protected Address() { }
12+
13+
public Address(string identifier, int streetNo, string streetName, string suburb, string city, string postCode)
14+
{
15+
Identifier = identifier;
16+
StreetNo = streetNo;
17+
StreetName = streetName;
18+
Suburb = suburb;
19+
City = city;
20+
PostCode = postCode;
21+
}
22+
23+
public string Identifier { get; private set; }
24+
public virtual int StreetNo { get; private set; }
25+
public string StreetName { get; private set; }
26+
public string Suburb { get; private set; }
27+
public string City { get; private set; }
28+
public string PostCode { get; private set; }
29+
}
30+
}

TestStack.Dossier.Tests/TestHelpers/Objects/Entities/Customer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Customer
66
{
77
protected Customer() {}
88

9-
public Customer(string identifier, string firstName, string lastName, int yearJoined, CustomerClass customerClass)
9+
public Customer(string identifier, string firstName, string lastName, int yearJoined, Address postalAddress, CustomerClass customerClass)
1010
{
1111
if (string.IsNullOrEmpty(identifier))
1212
throw new ArgumentNullException("identifier");
@@ -19,6 +19,7 @@ public Customer(string identifier, string firstName, string lastName, int yearJo
1919
FirstName = firstName;
2020
LastName = lastName;
2121
YearJoined = yearJoined;
22+
PostalAddress = postalAddress;
2223
CustomerClass = customerClass;
2324
}
2425

@@ -33,6 +34,7 @@ public virtual int CustomerForHowManyYears(DateTime since)
3334
public virtual string FirstName { get; private set; }
3435
public virtual string LastName { get; private set; }
3536
public virtual int YearJoined { get; private set; }
37+
public virtual Address PostalAddress { get; private set; }
3638
public virtual CustomerClass CustomerClass { get; private set; }
3739
}
3840
}

TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="Factories\CallConstructorFactoryTests.cs" />
6767
<Compile Include="Factories\PublicPropertySettersFactoryTests.cs" />
6868
<Compile Include="TestHelpers\Builders\BuilderWithDefaults.cs" />
69+
<Compile Include="TestHelpers\Objects\Entities\Address.cs" />
6970
<Compile Include="TestHelpers\Objects\Entities\Company.cs" />
7071
<Compile Include="TestHelpers\Objects\Entities\Customer.cs" />
7172
<Compile Include="TestHelpers\Objects\Entities\CustomerClass.cs" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using System.Linq.Expressions;
3+
4+
namespace TestStack.Dossier
5+
{
6+
internal class PathExpressionVisitor : ExpressionVisitor
7+
{
8+
internal readonly List<string> Path = new List<string>();
9+
10+
protected override Expression VisitMember(MemberExpression node)
11+
{
12+
Path.Add(node.Member.Name);
13+
return base.VisitMember(node);
14+
}
15+
}
16+
17+
}

TestStack.Dossier/Reflector.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal static class Reflector
1111
public static string GetPropertyNameFor<TObject, TValue>(Expression<Func<TObject, TValue>> property)
1212
{
1313
var memExp = property.Body as MemberExpression;
14+
1415
if (memExp == null)
1516
throw new ArgumentException(
1617
string.Format(
@@ -21,7 +22,10 @@ public static string GetPropertyNameFor<TObject, TValue>(Expression<Func<TObject
2122
"property"
2223
);
2324

24-
return memExp.Member.Name;
25+
var visitor = new PathExpressionVisitor();
26+
visitor.Visit(memExp);
27+
28+
return string.Join(".", Enumerable.Reverse(visitor.Path));
2529
}
2630

2731
public static PropertyInfo[] GetSettablePropertiesFor<T>()

TestStack.Dossier/TestStack.Dossier.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="Lists\ListBuilderInterceptor.cs" />
9393
<Compile Include="NullingExpandoObject.cs" />
9494
<Compile Include="Factories\IFactory.cs" />
95+
<Compile Include="PathExpressionVisitor.cs" />
9596
<Compile Include="Reflector.cs" />
9697
<Compile Include="Suppliers\DefaultEmailValueSupplier.cs" />
9798
<Compile Include="Suppliers\DefaultLastNameValueSupplier.cs" />

0 commit comments

Comments
 (0)