Skip to content

Commit 37fcee6

Browse files
committed
Added default value suppliers for first names and last names
1 parent 8fc5ceb commit 37fcee6

File tree

8 files changed

+77
-4
lines changed

8 files changed

+77
-4
lines changed

NTestDataBuilder.Tests/Builders/BasicCustomerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BasicCustomerBuilder : TestDataBuilder<Customer, BasicCustomerBuild
88
{
99
protected override Customer BuildObject()
1010
{
11-
return new Customer("First Name", "Last Name", 2013, CustomerClass.Normal);
11+
return new Customer("customer1", "First Name", "Last Name", 2013, CustomerClass.Normal);
1212
}
1313

1414
public new BasicCustomerBuilder Set<TValue>(Expression<Func<Customer, TValue>> property, TValue value)

NTestDataBuilder.Tests/Builders/CustomerBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public virtual CustomerBuilder WhoJoinedIn(int yearJoined)
2222
protected override Customer BuildObject()
2323
{
2424
return new Customer(
25+
Get(x => x.Identifier),
2526
Get(x => x.FirstName),
2627
Get(x => x.LastName),
2728
Get(x => x.YearJoined),

NTestDataBuilder.Tests/Entities/Customer.cs

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

9-
public Customer(string firstName, string lastName, int yearJoined, CustomerClass customerClass)
9+
public Customer(string identifier, string firstName, string lastName, int yearJoined, CustomerClass customerClass)
1010
{
11+
if (string.IsNullOrEmpty(identifier))
12+
throw new ArgumentNullException("identifier");
1113
if (string.IsNullOrEmpty(firstName))
1214
throw new ArgumentNullException("firstName");
1315
if (string.IsNullOrEmpty(lastName))
1416
throw new ArgumentNullException("lastName");
1517

18+
Identifier = identifier;
1619
FirstName = firstName;
1720
LastName = lastName;
1821
YearJoined = yearJoined;
@@ -26,6 +29,7 @@ public virtual int CustomerForHowManyYears(DateTime since)
2629
return since.Year - YearJoined;
2730
}
2831

32+
public virtual string Identifier { get; private set; }
2933
public virtual string FirstName { get; private set; }
3034
public virtual string LastName { get; private set; }
3135
public virtual int YearJoined { get; private set; }

NTestDataBuilder.Tests/GetAnonymousTests.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NTestDataBuilder.Lists;
1+
using NTestDataBuilder.DataSources.Person;
2+
using NTestDataBuilder.Lists;
23
using NTestDataBuilder.Tests.Builders;
34
using NTestDataBuilder.Tests.TestHelpers;
45
using Shouldly;
@@ -48,10 +49,28 @@ public void GivenNoValueHasBeenSetForAPropertyAndAGlobalSupplierHasBeenRegistere
4849
_b.Get(x => x.FirstName).ShouldBe(globalVal);
4950
}
5051

52+
[Fact]
53+
public void GivenNoValueHasBeenSetForAPropertyNamedFirstName_WhenRetrievingTheValueForTheProperty_ThenReturnAFirstName()
54+
{
55+
var firstName = _b.Get(x => x.FirstName);
56+
57+
new PersonNameFirstSource().Data
58+
.ShouldContain(firstName);
59+
}
60+
61+
[Fact]
62+
public void GivenNoValueHasBeenSetForAPropertyNamedLastName_WhenRetrievingTheValueForTheProperty_ThenReturnALastName()
63+
{
64+
var lastName = _b.Get(x => x.LastName);
65+
66+
new PersonNameLastSource().Data
67+
.ShouldContain(lastName);
68+
}
69+
5170
[Fact]
5271
public void GivenNoValueHasBeenSetForAStringProperty_WhenRetrievingTheValueForThatProperty_ThenReturnPropertyNameFollowedByGuid()
5372
{
54-
_b.Get(x => x.FirstName).ShouldMatch("^FirstName[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$");
73+
_b.Get(x => x.Identifier).ShouldMatch("^Identifier[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$");
5574
}
5675

5776
[Fact]

NTestDataBuilder/AnonymousValueFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ static AnonymousValueFixture()
1818
GlobalValueSuppliers = new List<IAnonymousValueSupplier>();
1919
DefaultValueSuppliers = new IAnonymousValueSupplier[]
2020
{
21+
new DefaultFirstNameValueSupplier(),
22+
new DefaultLastNameValueSupplier(),
2123
new DefaultStringValueSupplier(),
2224
new DefaultValueTypeValueSupplier(),
2325
new DefaultValueSupplier()

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
<Compile Include="Lists\ListBuilderGenerator.cs" />
8888
<Compile Include="Lists\ListBuilderInterceptor.cs" />
8989
<Compile Include="PropertyNameGetter.cs" />
90+
<Compile Include="Suppliers\DefaultLastNameValueSupplier.cs" />
91+
<Compile Include="Suppliers\DefaultFirstNameValueSupplier.cs" />
9092
<Compile Include="Suppliers\DefaultValueSupplier.cs" />
9193
<Compile Include="Suppliers\DefaultStringValueSupplier.cs" />
9294
<Compile Include="Suppliers\DefaultValueTypeValueSupplier.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NTestDataBuilder.EquivalenceClasses;
2+
3+
namespace NTestDataBuilder.Suppliers
4+
{
5+
/// <summary>
6+
/// Supplies default anonymous value for first names.
7+
/// </summary>
8+
public class DefaultFirstNameValueSupplier : IAnonymousValueSupplier
9+
{
10+
/// <inheritdoc />
11+
public bool CanSupplyValue<TObject, TValue>(string propertyName)
12+
{
13+
return typeof (TValue) == typeof(string) && propertyName.ToLower() == "firstname";
14+
}
15+
16+
/// <inheritdoc />
17+
public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName)
18+
{
19+
return (TValue) (object) any.PersonNameFirst();
20+
}
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NTestDataBuilder.EquivalenceClasses;
2+
3+
namespace NTestDataBuilder.Suppliers
4+
{
5+
/// <summary>
6+
/// Supplies default anonymous value for last names.
7+
/// </summary>
8+
public class DefaultLastNameValueSupplier : IAnonymousValueSupplier
9+
{
10+
/// <inheritdoc />
11+
public bool CanSupplyValue<TObject, TValue>(string propertyName)
12+
{
13+
return typeof (TValue) == typeof(string) &&
14+
(propertyName.ToLower() == "lastname" || propertyName.ToLower() == "surname");
15+
}
16+
17+
/// <inheritdoc />
18+
public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName)
19+
{
20+
return (TValue) (object) any.PersonNameLast();
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)