Skip to content

Commit

Permalink
Made it a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Sep 18, 2014
1 parent 50787d2 commit a0fb16f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ PackageBuild/*
Build/*
TestResult.xml
TestStack.BDDfy.sln.ide/graph
_NCrunch_TestStack.BDDfy
5 changes: 3 additions & 2 deletions TestStack.BDDfy/Processors/ScenarioExecutor.cs
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using TestStack.BDDfy.Configuration;

Expand Down Expand Up @@ -28,8 +29,8 @@ public void InitializeScenario()

var possibleTargets = memberInfos
.OfType<FieldInfo>()
.Select(f => new StepArgument(f, () => _scenario.TestObject))
.Union(memberInfos.OfType<PropertyInfo>().Select(m => new StepArgument(m, () => _scenario.TestObject)))
.Select(f => new StepArgument(f.Name, f.FieldType, () => f.GetValue(_scenario.TestObject), o => f.SetValue(_scenario.TestObject, o)))
.Union(memberInfos.OfType<PropertyInfo>().Select(m => new StepArgument(m.Name, m.PropertyType, () => m.GetValue(_scenario.TestObject, null), o => m.SetValue(_scenario.TestObject, o, null))))
.Union(_scenario.Steps.SelectMany(s=>s.Arguments))
.ToArray();

Expand Down
Expand Up @@ -47,15 +47,27 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)a;
var field = memberExpression.Member as FieldInfo;
string name;
Type parameterType;
bool isReadOnly;
if (field != null)
{
var o = field.IsStatic ? null : GetValue(memberExpression.Expression);
return new StepArgument(field, o);
name = field.Name;
parameterType = field.FieldType;
isReadOnly = field.IsInitOnly;
}
var propertyInfo = (PropertyInfo)memberExpression.Member;
var methodInfo = propertyInfo.GetGetMethod(true);
var declaringObject = methodInfo == null || methodInfo.IsStatic ? null : GetValue(memberExpression.Expression);
return new StepArgument(propertyInfo, declaringObject);
else
{
var propertyInfo = (PropertyInfo)memberExpression.Member;
name = propertyInfo.Name;
parameterType = propertyInfo.PropertyType;
isReadOnly = !propertyInfo.CanWrite;
}
var getValue = GetValue(memberExpression);
var setValue = isReadOnly ? null : SetValue(memberExpression, parameterType);
return new StepArgument(name, parameterType, getValue, setValue);
default:
return new StepArgument(GetValue(a));
}
Expand All @@ -68,6 +80,14 @@ private static Func<object> GetValue(Expression a)
{
return Expression.Lambda<Func<object>>(Expression.Convert(a, typeof(object))).Compile();
}

private static Action<object> SetValue(Expression a, Type parameterType)
{
var parameter = Expression.Parameter(typeof(object));
var unaryExpression = Expression.Convert(parameter, parameterType);
var assign = Expression.Assign(a, unaryExpression);
return Expression.Lambda<Action<object>>(assign, parameter).Compile();
}
}
}
}
41 changes: 6 additions & 35 deletions TestStack.BDDfy/Scanners/StepScanners/Fluent/StepArgument.cs
@@ -1,5 +1,4 @@
using System;
using System.Reflection;

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

public StepArgument(FieldInfo member, Func<object> declaringObject)
public StepArgument(string name, Type argumentType, Func<object> getValue, Action<object> setValue)
{
Name = member.Name;
_get = () => member.GetValue(declaringObject == null ? null : declaringObject());
_set = o => member.SetValue(declaringObject == null ? null : declaringObject(), o);
ArgumentType = member.FieldType;
}

public StepArgument(PropertyInfo member, Func<object> declaringObject)
{
Name = member.Name;
_get = () =>
{
if (declaringObject == null)
return member.GetGetMethod(true).Invoke(null, null);
var declaringObjectValue = declaringObject();
if (declaringObjectValue == null)
return null;
return member.GetGetMethod(true).Invoke(declaringObjectValue, null);
};
_set = o =>
{
if (declaringObject == null)
{
member.GetSetMethod(true).Invoke(null, new[] {o});
return;
}
var declaringObjectValue = declaringObject();
if (declaringObjectValue == null)
return;
member.GetSetMethod(true).Invoke(declaringObjectValue, new[] { o });
};
ArgumentType = member.PropertyType;
Name = name;
_get = getValue;
if (setValue != null)
_set = setValue;
ArgumentType = argumentType;
}

public StepArgument(Func<object> value)
Expand Down
3 changes: 0 additions & 3 deletions TestStack.BDDfy/Scanners/StepScanners/StepActionFactory.cs
@@ -1,8 +1,5 @@
using System;
using System.Reflection;
using System.Security;
using System.Threading;
using TestStack.BDDfy.Processors;
using System.Threading.Tasks;

namespace TestStack.BDDfy
Expand Down

0 comments on commit a0fb16f

Please sign in to comment.