Skip to content

Commit a0fb16f

Browse files
committed
Made it a bit better
1 parent 50787d2 commit a0fb16f

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ PackageBuild/*
1616
Build/*
1717
TestResult.xml
1818
TestStack.BDDfy.sln.ide/graph
19+
_NCrunch_TestStack.BDDfy

TestStack.BDDfy/Processors/ScenarioExecutor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Linq.Expressions;
34
using System.Reflection;
45
using TestStack.BDDfy.Configuration;
56

@@ -28,8 +29,8 @@ public void InitializeScenario()
2829

2930
var possibleTargets = memberInfos
3031
.OfType<FieldInfo>()
31-
.Select(f => new StepArgument(f, () => _scenario.TestObject))
32-
.Union(memberInfos.OfType<PropertyInfo>().Select(m => new StepArgument(m, () => _scenario.TestObject)))
32+
.Select(f => new StepArgument(f.Name, f.FieldType, () => f.GetValue(_scenario.TestObject), o => f.SetValue(_scenario.TestObject, o)))
33+
.Union(memberInfos.OfType<PropertyInfo>().Select(m => new StepArgument(m.Name, m.PropertyType, () => m.GetValue(_scenario.TestObject, null), o => m.SetValue(_scenario.TestObject, o, null))))
3334
.Union(_scenario.Steps.SelectMany(s=>s.Arguments))
3435
.ToArray();
3536

TestStack.BDDfy/Scanners/StepScanners/Fluent/ExpressionExtensions.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,27 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
4747
case ExpressionType.MemberAccess:
4848
var memberExpression = (MemberExpression)a;
4949
var field = memberExpression.Member as FieldInfo;
50+
string name;
51+
Type parameterType;
52+
bool isReadOnly;
5053
if (field != null)
5154
{
52-
var o = field.IsStatic ? null : GetValue(memberExpression.Expression);
53-
return new StepArgument(field, o);
55+
name = field.Name;
56+
parameterType = field.FieldType;
57+
isReadOnly = field.IsInitOnly;
5458
}
55-
var propertyInfo = (PropertyInfo)memberExpression.Member;
56-
var methodInfo = propertyInfo.GetGetMethod(true);
57-
var declaringObject = methodInfo == null || methodInfo.IsStatic ? null : GetValue(memberExpression.Expression);
58-
return new StepArgument(propertyInfo, declaringObject);
59+
else
60+
{
61+
var propertyInfo = (PropertyInfo)memberExpression.Member;
62+
name = propertyInfo.Name;
63+
parameterType = propertyInfo.PropertyType;
64+
isReadOnly = !propertyInfo.CanWrite;
65+
}
66+
67+
var getValue = GetValue(memberExpression);
68+
var setValue = isReadOnly ? null : SetValue(memberExpression, parameterType);
69+
70+
return new StepArgument(name, parameterType, getValue, setValue);
5971
default:
6072
return new StepArgument(GetValue(a));
6173
}
@@ -68,6 +80,14 @@ private static Func<object> GetValue(Expression a)
6880
{
6981
return Expression.Lambda<Func<object>>(Expression.Convert(a, typeof(object))).Compile();
7082
}
83+
84+
private static Action<object> SetValue(Expression a, Type parameterType)
85+
{
86+
var parameter = Expression.Parameter(typeof(object));
87+
var unaryExpression = Expression.Convert(parameter, parameterType);
88+
var assign = Expression.Assign(a, unaryExpression);
89+
return Expression.Lambda<Action<object>>(assign, parameter).Compile();
90+
}
7191
}
7292
}
7393
}

TestStack.BDDfy/Scanners/StepScanners/Fluent/StepArgument.cs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Reflection;
32

43
namespace TestStack.BDDfy
54
{
@@ -8,41 +7,13 @@ public class StepArgument
87
private readonly Action<object> _set = o => { };
98
private readonly Func<object> _get;
109

11-
public StepArgument(FieldInfo member, Func<object> declaringObject)
10+
public StepArgument(string name, Type argumentType, Func<object> getValue, Action<object> setValue)
1211
{
13-
Name = member.Name;
14-
_get = () => member.GetValue(declaringObject == null ? null : declaringObject());
15-
_set = o => member.SetValue(declaringObject == null ? null : declaringObject(), o);
16-
ArgumentType = member.FieldType;
17-
}
18-
19-
public StepArgument(PropertyInfo member, Func<object> declaringObject)
20-
{
21-
Name = member.Name;
22-
_get = () =>
23-
{
24-
if (declaringObject == null)
25-
return member.GetGetMethod(true).Invoke(null, null);
26-
27-
var declaringObjectValue = declaringObject();
28-
if (declaringObjectValue == null)
29-
return null;
30-
return member.GetGetMethod(true).Invoke(declaringObjectValue, null);
31-
};
32-
_set = o =>
33-
{
34-
if (declaringObject == null)
35-
{
36-
member.GetSetMethod(true).Invoke(null, new[] {o});
37-
return;
38-
}
39-
40-
var declaringObjectValue = declaringObject();
41-
if (declaringObjectValue == null)
42-
return;
43-
member.GetSetMethod(true).Invoke(declaringObjectValue, new[] { o });
44-
};
45-
ArgumentType = member.PropertyType;
12+
Name = name;
13+
_get = getValue;
14+
if (setValue != null)
15+
_set = setValue;
16+
ArgumentType = argumentType;
4617
}
4718

4819
public StepArgument(Func<object> value)

TestStack.BDDfy/Scanners/StepScanners/StepActionFactory.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Reflection;
3-
using System.Security;
4-
using System.Threading;
5-
using TestStack.BDDfy.Processors;
63
using System.Threading.Tasks;
74

85
namespace TestStack.BDDfy

0 commit comments

Comments
 (0)