Skip to content

Commit ab38795

Browse files
committed
Added basic get/set functionality
1 parent 9ec2772 commit ab38795

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using NTestDataBuilder.Tests.Builders;
2+
using NUnit.Framework;
3+
4+
namespace NTestDataBuilder.Tests
5+
{
6+
class GetSetTests
7+
{
8+
[Test]
9+
public void GivenAValueHasBeenSetAgainstAProperty_WhenRetrievingTheValueForThatProperty_ThenTheSetValueIsReturned()
10+
{
11+
const string set = "Value";
12+
var b = new BasicCustomerBuilder();
13+
b.Set(x => x.FirstName, set);
14+
15+
var retrieved = b.Get(x => x.FirstName);
16+
17+
Assert.That(retrieved, Is.EqualTo(set));
18+
}
19+
20+
[Test]
21+
public void GivenTwoValuesHaveBeenSetAgainstAProperty_WhenRetrievingTheValueForThatProperty_ThenTheLastSetValueIsReturned()
22+
{
23+
const string set = "Value";
24+
var b = new BasicCustomerBuilder();
25+
b.Set(x => x.FirstName, "random");
26+
b.Set(x => x.FirstName, set);
27+
28+
var retrieved = b.Get(x => x.FirstName);
29+
30+
Assert.That(retrieved, Is.EqualTo(set));
31+
}
32+
33+
[Test]
34+
public void GivenAValueHasBeenSetAgainstTwoProperties_WhenRetrievingTheValueForTheFirstSetProperty_ThenTheSetValueIsReturned()
35+
{
36+
const string set = "Value";
37+
var b = new BasicCustomerBuilder();
38+
b.Set(x => x.FirstName, set);
39+
b.Set(x => x.LastName, "random");
40+
41+
var retrieved = b.Get(x => x.FirstName);
42+
43+
Assert.That(retrieved, Is.EqualTo(set));
44+
}
45+
46+
[Test]
47+
public void GivenAValueHasBeenSetAgainstTwoProperties_WhenRetrievingTheValueForTheSecondSetProperty_ThenTheSetValueIsReturned()
48+
{
49+
const string set = "Value";
50+
var b = new BasicCustomerBuilder();
51+
b.Set(x => x.FirstName, "random");
52+
b.Set(x => x.LastName, set);
53+
54+
var retrieved = b.Get(x => x.LastName);
55+
56+
Assert.That(retrieved, Is.EqualTo(set));
57+
}
58+
}
59+
}

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="GetSetTests.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
4748
</ItemGroup>
4849
<ItemGroup>

NTestDataBuilder/DataBuilder.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace NTestDataBuilder
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
5+
namespace NTestDataBuilder
26
{
37
/// <summary>
48
/// Generates objects of type <see cref="T"/>.
@@ -22,10 +26,43 @@ public abstract class DataBuilder<TEntity, TBuilder> : IDataBuilder<TEntity>
2226
where TEntity : class
2327
where TBuilder : class, IDataBuilder<TEntity>
2428
{
29+
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
30+
2531
/// <summary>
2632
/// Build the object.
2733
/// </summary>
2834
/// <returns>The built object</returns>
2935
public abstract TEntity Build();
36+
37+
/// <summary>
38+
/// Records the given value for the given property from <see cref="TEntity"/>.
39+
/// </summary>
40+
/// <typeparam name="TValue">The type of the property</typeparam>
41+
/// <param name="property">A lambda expression specifying the property to record a value for</param>
42+
/// <param name="value">The value to record</param>
43+
public void Set<TValue>(Expression<Func<TEntity, TValue>> property, TValue value)
44+
{
45+
_properties[GetPropertyName(property)] = value;
46+
}
47+
48+
/// <summary>
49+
/// Gets the recorded value for the given property from <see cref="TEntity"/>.
50+
/// </summary>
51+
/// <typeparam name="TValue">The type of the property</typeparam>
52+
/// <param name="property">A lambda expression specifying the property to retrieve the recorded value for</param>
53+
/// <returns>The recorded value of the property</returns>
54+
public TValue Get<TValue>(Expression<Func<TEntity, TValue>> property)
55+
{
56+
return (TValue)_properties[GetPropertyName(property)];
57+
}
58+
59+
private static string GetPropertyName<TValue>(Expression<Func<TEntity, TValue>> property)
60+
{
61+
var memExp = property.Body as MemberExpression;
62+
if (memExp == null)
63+
throw new ArgumentException("Property must be valid on the entity object", "property");
64+
65+
return memExp.Member.Name;
66+
}
3067
}
3168
}

NTestDataBuilder/NTestDataBuilder.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
https://raw.github.com/robdmoore/NTestDataBuilder/master/logo.png
2424
</iconUrl>
2525
<tags>
26-
testing, data generation, test fixture, nbuilder, nsubstitute, mocking
26+
testing data generation test fixture nbuilder nsubstitute mocking
2727
</tags>
2828
<language>
2929
en-US

0 commit comments

Comments
 (0)