Skip to content

Commit c57abec

Browse files
committed
renamed PropertyNameGetter to Reflector to show additional behaviour
1 parent 205c61e commit c57abec

7 files changed

Lines changed: 38 additions & 57 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Prior to v2.0 this library was known as NTestDataBuilder.
7979
}
8080
}
8181

82+
Note that you can optionally override the `BuildObject` method if you want full control over how your object is constructed, for example if you want to use a particular constructor. If you don't provide this then Dossier will create the object for you.
8283

8384
3. Use the builder in a test, e.g.
8485

TestStack.Dossier/AnonymousValueFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public AnonymousValueFixture()
8282
/// <returns>The anonymous value, taking into account any registered conventions</returns>
8383
public T Get<TObject, T>(Expression<Func<TObject, T>> property)
8484
{
85-
var propertyName = PropertyNameGetter.Get(property);
85+
var propertyName = Reflector.GetPropertyFor(property);
8686
var valueSupplier = LocalValueSuppliers
8787
.Concat(GlobalValueSuppliers)
8888
.Concat(DefaultValueSuppliers)

TestStack.Dossier/PropertyNameGetter.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

TestStack.Dossier/ProxyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public ProxyBuilder(Dictionary<string, object> properties)
2929
public T Build()
3030
{
3131
var proxy = Substitute.For<T>();
32-
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
32+
var properties = Reflector.GetSettablePropertiesFor<T>();
3333
foreach (var property in properties.Where(property => _properties.ContainsKey(property.Name)))
3434
{
3535
if (property.GetGetMethod().IsVirtual)

TestStack.Dossier/Reflector.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using System.Reflection;
4+
5+
namespace TestStack.Dossier
6+
{
7+
internal static class Reflector
8+
{
9+
public static string GetPropertyFor<TObject, TValue>(Expression<Func<TObject, TValue>> property)
10+
{
11+
var memExp = property.Body as MemberExpression;
12+
if (memExp == null)
13+
throw new ArgumentException(
14+
string.Format(
15+
"Given property expression ({0}) didn't specify a property on {1}",
16+
property,
17+
typeof(TObject).Name
18+
),
19+
"property"
20+
);
21+
22+
return memExp.Member.Name;
23+
}
24+
25+
public static PropertyInfo[] GetSettablePropertiesFor<TObject>()
26+
{
27+
return typeof (TObject).GetProperties(BindingFlags.Public | BindingFlags.Instance);
28+
}
29+
}
30+
}

TestStack.Dossier/TestDataBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected virtual TObject BuildObject()
7979
{
8080
var model = Any.Fixture.Create<TObject>();
8181

82-
var properties = typeof (TObject).GetProperties(BindingFlags.Public | BindingFlags.Instance);
82+
var properties = Reflector.GetSettablePropertiesFor<TObject>();
8383
foreach (var property in properties)
8484
{
8585
var val = Get(property.PropertyType, property.Name);
@@ -114,7 +114,7 @@ protected virtual void AlterProxy(TObject proxy) {}
114114
/// <param name="value">The value to record</param>
115115
public TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, TValue value)
116116
{
117-
_properties[PropertyNameGetter.Get(property)] = value;
117+
_properties[Reflector.GetPropertyFor(property)] = value;
118118
return this as TBuilder;
119119
}
120120

@@ -130,7 +130,7 @@ public TValue Get<TValue>(Expression<Func<TObject, TValue>> property)
130130
if (!Has(property))
131131
return Any.Get(property);
132132

133-
return (TValue)_properties[PropertyNameGetter.Get(property)];
133+
return (TValue)_properties[Reflector.GetPropertyFor(property)];
134134
}
135135

136136
public object Get(Type type, string propertyName)
@@ -174,7 +174,7 @@ public static ListBuilder<TObject, TBuilder> CreateListOfSize(int size)
174174
/// <returns>Whether or not there is a recorded value for the property</returns>
175175
protected bool Has<TValue>(Expression<Func<TObject, TValue>> property)
176176
{
177-
return Has(PropertyNameGetter.Get(property));
177+
return Has(Reflector.GetPropertyFor(property));
178178
}
179179

180180
/// <summary>

TestStack.Dossier/TestStack.Dossier.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<Compile Include="Lists\ListBuilderGenerator.cs" />
8888
<Compile Include="Lists\ListBuilderInterceptor.cs" />
8989
<Compile Include="NullingExpandoObject.cs" />
90-
<Compile Include="PropertyNameGetter.cs" />
90+
<Compile Include="Reflector.cs" />
9191
<Compile Include="Suppliers\DefaultEmailValueSupplier.cs" />
9292
<Compile Include="Suppliers\DefaultLastNameValueSupplier.cs" />
9393
<Compile Include="Suppliers\DefaultFirstNameValueSupplier.cs" />

0 commit comments

Comments
 (0)