Skip to content

Commit 76f7369

Browse files
committed
added GetOrDefault method
1 parent 70c9307 commit 76f7369

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

NTestDataBuilder.Tests/Builders/BasicCustomerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BasicCustomerBuilder : DataBuilder<Customer, BasicCustomerBuilder>
66
{
77
public override Customer Build()
88
{
9-
return new Customer("First Name", "Last Name");
9+
return new Customer("First Name", "Last Name", 2013);
1010
}
1111
}
1212
}

NTestDataBuilder.Tests/Entities/Customer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace NTestDataBuilder.Tests.Entities
44
{
55
public class Customer
66
{
7-
public Customer(string firstName, string lastName)
7+
public Customer(string firstName, string lastName, int yearJoined)
88
{
99
if (string.IsNullOrEmpty(firstName))
1010
throw new ArgumentNullException("firstName");
@@ -13,9 +13,11 @@ public Customer(string firstName, string lastName)
1313

1414
FirstName = firstName;
1515
LastName = lastName;
16+
YearJoined = yearJoined;
1617
}
1718

1819
public string FirstName { get; private set; }
1920
public string LastName { get; private set; }
21+
public int YearJoined { get; private set; }
2022
}
2123
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NTestDataBuilder.Tests.Builders;
2+
using NUnit.Framework;
3+
4+
namespace NTestDataBuilder.Tests
5+
{
6+
class GetOrDefaultTests
7+
{
8+
private BasicCustomerBuilder _b;
9+
const string SetValue = "Value";
10+
11+
[SetUp]
12+
public void Setup()
13+
{
14+
_b = new BasicCustomerBuilder();
15+
}
16+
17+
[Test]
18+
public void GivenAValueHasBeenSetAgainstAProperty_WhenRetrievingTheValueForThatProperty_ThenTheSetValueIsReturned()
19+
{
20+
_b.Set(x => x.FirstName, SetValue);
21+
22+
var retrieved = _b.GetOrDefault(x => x.FirstName);
23+
24+
Assert.That(retrieved, Is.EqualTo(SetValue));
25+
}
26+
27+
[Test]
28+
public void GivenNoValueHasBeenSetForAStringProperty_WhenRetrievingTheValueForThatProperty_ThenReturnNull()
29+
{
30+
var retrieved = _b.GetOrDefault(x => x.FirstName);
31+
32+
Assert.That(retrieved, Is.EqualTo(null));
33+
}
34+
35+
[Test]
36+
public void GivenNoValueHasBeenSetForAnIntProperty_WhenRetrievingTheValueForThatProperty_ThenReturn0()
37+
{
38+
var retrieved = _b.GetOrDefault(x => x.YearJoined);
39+
40+
Assert.That(retrieved, Is.EqualTo(0));
41+
}
42+
}
43+
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<Compile Include="Builders\BasicCustomerBuilder.cs" />
4444
<Compile Include="BuildTests.cs" />
4545
<Compile Include="Entities\Customer.cs" />
46+
<Compile Include="GetOrDefaultTests.cs" />
4647
<Compile Include="GetSetTests.cs" />
4748
<Compile Include="Properties\AssemblyInfo.cs" />
4849
</ItemGroup>

NTestDataBuilder/DataBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ public TValue Get<TValue>(Expression<Func<TEntity, TValue>> property)
6565
return (TValue)_properties[GetPropertyName(property)];
6666
}
6767

68+
/// <summary>
69+
/// Gets the recorded value for the given property from <see cref="TEntity"/> or if no
70+
/// value has been recorded the default value for <see cref="TValue"/>.
71+
/// </summary>
72+
/// <typeparam name="TValue">The type of the property</typeparam>
73+
/// <param name="property">A lambda expression specifying the property to retrieve the recorded value for</param>
74+
/// <returns>The recorded value of the property or teh default value for <see cref="TValue"/> if no value recorded</returns>
75+
public TValue GetOrDefault<TValue>(Expression<Func<TEntity, TValue>> property)
76+
{
77+
return Has(property)
78+
? Get(property)
79+
: default(TValue);
80+
}
81+
6882
/// <summary>
6983
/// Returns whether or not there is currently a value recorded against the given property from <see cref="TEntity"/>.
7084
/// </summary>

0 commit comments

Comments
 (0)