Exemplo n.º 1
0
        public void Should_set_MethodParametersType_to_TypedListStep_when_parameter_is_List_of_object_type()
        {
            Action <List <ActionMethodInfo> > action = _ => { };
            var x = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");

            Assert.AreEqual(MethodParametersType.TypedListStep, x.MethodParametersType);
        }
Exemplo n.º 2
0
        public void Should_set_MethodParametersType_to_UntypedStep_when_parameter_is_string_type()
        {
            Action <string> action = _ => { };
            var             x      = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");

            Assert.AreEqual(MethodParametersType.UntypedStep, x.MethodParametersType);
        }
Exemplo n.º 3
0
 private object[] GetParametersForStep(StringStep stringStep, ActionMethodInfo action)
 {
     var match = action.ActionStepMatcher.Match(stringStep.MatchableStep);
     Func<string, string> getValues = _ => match.Groups[_].Value;
     var paramNames = GetParameterNames(action);
     return GetParametersForStep(action, paramNames, getValues);
 }
Exemplo n.º 4
0
        private object CreateAction(object instance, ActionMethodInfo method)
        {
            object action     = null;
            var    methodInfo = method.MethodInfo;

            switch (CountParameters(method))
            {
            case 0:
                action = GetActionWithNoParameters(instance, method);
                break;

            case 1:
                action = GetActionForOneParameter(instance, methodInfo);
                break;

            case 2:
                action = GetActionForTwoParameters(instance, methodInfo);
                break;

            case 3:
                action = GetActionForThreeParameters(instance, methodInfo);
                break;

            case 4:
                action = GetActionForFourParameters(instance, methodInfo);
                break;
            }

            return(action);
        }
Exemplo n.º 5
0
 private void ForEachOverStep(StringTableStep actionStep, ActionMethodInfo info)
 {
     foreach (var example in actionStep.TableSteps)
     {
         RunStep(info, () => ParameterConverter.GetParametersForStep(actionStep, example));
     }
 }
Exemplo n.º 6
0
        public void Should_set_MethodParametersType_to_TypedStep()
        {
            Action <ActionMethodInfo> action = _ => { };
            var x = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");

            Assert.AreEqual(MethodParametersType.TypedStep, x.MethodParametersType);
        }
Exemplo n.º 7
0
 private void BeforeEachScenario(ActionMethodInfo info)
 {
     if (isFirstStepInScenario)
     {
         isFirstStepInScenario = false;
         info.ExecuteNotificationMethod(typeof(BeforeScenarioAttribute));
     }
 }
Exemplo n.º 8
0
 private object[] GetParametersForListStep(ActionMethodInfo action, StringStep stringStep)
 {
     var itemType = action.ParameterInfo[0].ParameterType.GetGenericArguments()[0];
     var values = itemType.CreateInstanceOfGenericList();
     var addMethodOnList = values.GetType().GetMethod("Add");
     Func<Example, string, string> getValues = (e, name) => e.ColumnValues[name];
     var method = new ActionMethodInfo(action.ActionStepMatcher, addMethodOnList, addMethodOnList, action.ActionType);
     GetValuesFromTableStep((StringTableStep)stringStep, getValues, addMethodOnList, values, method);
     return new object[] { values };
 }
Exemplo n.º 9
0
 public void FindActionStepMethods(Type actionSteps, object instance)
 {
     var methods = methodWithAttributeFinder.FindMethodsWithAttribute<ActionMethodInfo, ActionStepAttribute>(actionSteps, BuildActionMethodInfo);
     foreach (var method in methods)
     {
         var action = CreateAction(instance, method);
         var m = new ActionMethodInfo(method.ActionStepMatcher, action, method.MethodInfo, method.ActionType, instance);
         AddFileMatcher(m, instance);
         actionCatalog.Add(m);
     }
 }
Exemplo n.º 10
0
 private void AddFileMatcher(ActionMethodInfo action, object instance)
 {
     var fileMatcher = instance as IMatchFiles;
     if (fileMatcher != null)
     {
         action.FileMatcher = fileMatcher.FileMatcher;
     }
     else
     {
         action.FileMatcher = new MatchAllFiles();
     }
 }
Exemplo n.º 11
0
        public void FindActionStepMethods(Type actionSteps, object instance)
        {
            var methods = methodWithAttributeFinder.FindMethodsWithAttribute <ActionMethodInfo, ActionStepAttribute>(actionSteps, BuildActionMethodInfo);

            foreach (var method in methods)
            {
                var action = CreateAction(instance, method);
                var m      = new ActionMethodInfo(method.ActionStepMatcher, action, method.MethodInfo, method.ActionType, instance);
                AddFileMatcher(m, instance);
                actionCatalog.Add(m);
            }
        }
Exemplo n.º 12
0
        private void AddFileMatcher(ActionMethodInfo action, object instance)
        {
            var fileMatcher = instance as IMatchFiles;

            if (fileMatcher != null)
            {
                action.FileMatcher = fileMatcher.FileMatcher;
            }
            else
            {
                action.FileMatcher = new MatchAllFiles();
            }
        }
Exemplo n.º 13
0
        private void RunStep(ActionMethodInfo info, Func <object[]> getParametersForActionStepText)
        {
            var actionType        = GetActionType(info.Action);
            var methodInfo        = actionType.GetMethod("DynamicInvoke");
            var actionParamValues = getParametersForActionStepText();

            methodInfo.Invoke(
                info.Action,
                BindingFlags.InvokeMethod,
                null,
                new object[] { actionParamValues },
                CultureInfo.CurrentCulture);
        }
Exemplo n.º 14
0
        private void FindStaticActionStepMethods(Type actionSteps)
        {
            var instance = new object(); //ExpandoObject? And add the methods to it?
            var methods  = methodWithAttributeFinder.FindStaticMethodsWithAttribute <ActionMethodInfo, ActionStepAttribute>(actionSteps, BuildActionMethodInfo);

            foreach (var method in methods)
            {
                var action = CreateAction(instance, method);
                var m      = new ActionMethodInfo(method.ActionStepMatcher, action, method.MethodInfo, method.ActionType, instance);
                AddFileMatcher(m, instance);
                actionCatalog.Add(m);
            }
        }
Exemplo n.º 15
0
            public void EstablishContext()
            {
                _wasCalled     = false;
                _actionCatalog = new ActionCatalog();
                Action firstAction  = () => Assert.Fail("This action shouldnt be called");
                var    actionMethod = new ActionMethodInfo(new Regex("def$"), firstAction, firstAction.Method, "Given", this);

                _actionCatalog.Add(actionMethod);

                Action secondAction       = () => { _wasCalled = true; };
                var    secondActionMethod = new ActionMethodInfo(new Regex("abc def$"), secondAction, secondAction.Method, "Given", this);

                _actionCatalog.Add(secondActionMethod);
            }
Exemplo n.º 16
0
            public void ShouldConsiderThe2ActionsAsEqual()
            {
                var catalog = new ActionCatalog();

                var action = new ActionMethodInfo(
                    "my savings account balance is $balance".AsRegex(),
                    new object(),
                    GetDummyParameterInfo(),
                    null);

                catalog.Add(action);
                var actionExists = catalog.ActionExists("Given my savings account balance is 500".AsStringStep(""));

                Assert.That(actionExists, Is.True);
            }
Exemplo n.º 17
0
            public void ShouldConsiderAllWhitespaceAsEqual()
            {
                var catalog = new ActionCatalog();

                var action = new ActionMethodInfo(
                    "my savings account\nbalance is $balance".AsRegex(),
                    new object(),
                    GetDummyParameterInfo(),
                    null);

                catalog.Add(action);
                var actionExists = catalog.ActionExists("Given my\tsavings account balance is 500".AsStringStep(""));

                Assert.That(actionExists, Is.True);
            }
Exemplo n.º 18
0
            public void ShouldGetAction()
            {
                var catalog = new ActionCatalog();

                var action = new ActionMethodInfo(
                    "my savings account balance is $balance".AsRegex(),
                    new object(),
                    GetDummyParameterInfo(),
                    null);

                catalog.Add(action);

                var actionResult = catalog.GetAction(new StringStep("Given", "my savings account balance is 500", ""));

                Assert.That(actionResult, Is.Not.Null);
            }
Exemplo n.º 19
0
            public void ShouldGetAction()
            {
                var catalog = new ActionCatalog();

                var action = new ActionMethodInfo(
                    "my savings account balance is $balance".AsRegex(),
                    new object(),
                    GetDummyParameterInfo(),
                    null);

                catalog.Add(action);

                var actionResult = catalog.GetAction(new StringStep("Given my savings account balance is 500", ""));

                Assert.That(actionResult, Is.Not.Null);
            }
Exemplo n.º 20
0
            public void ShouldGetActionWithTokenInMiddleOfString()
            {
                var          catalog = new ActionCatalog();
                Action <int> action  = accountBalance => { };

                var actionMethodInfo = new ActionMethodInfo(
                    "I have $amount euros on my cash account".AsRegex(),
                    action,
                    action.Method,
                    null);

                catalog.Add(actionMethodInfo);

                var actionFetched = catalog.GetAction(new StringStep("Given", "I have 20 euros on my cash account", ""));

                Assert.That(actionFetched, Is.Not.Null);
            }
Exemplo n.º 21
0
            public void ShouldGetActionWithTokenInMiddleOfString()
            {
                var catalog = new ActionCatalog();
                Action<int> action = accountBalance => { };

                var actionMethodInfo = new ActionMethodInfo(
                    "I have $amount euros on my cash account".AsRegex(),
                    action,
                    action.Method,
                    null);

                catalog.Add(actionMethodInfo);

                var actionFetched = catalog.GetAction(new StringStep("Given I have 20 euros on my cash account", ""));

                Assert.That(actionFetched, Is.Not.Null);
            }
Exemplo n.º 22
0
            public void EstablishContext()
            {
                _actionCatalog = new ActionCatalog();
                Action <string, string> firstAction = (a, b) => { };
                var stepMatcher  = "$a and $b".AsRegex();
                var actionMethod = new ActionMethodInfo(stepMatcher, firstAction, firstAction.Method, "Given", this);

                _actionCatalog.Add(actionMethod);

                Action <FooBar> secondAction       = _ => { };
                var             secondActionMethod = new ActionMethodInfo(stepMatcher, secondAction, secondAction.Method, "Given", this);

                _actionCatalog.Add(secondActionMethod);

                Action <List <FooBar> > thirdAction = _ => { };
                var thirdActionMethod = new ActionMethodInfo(stepMatcher, thirdAction, thirdAction.Method, "Given", this);

                _actionCatalog.Add(thirdActionMethod);
            }
Exemplo n.º 23
0
 private object[] GetParametersForStep(ActionMethodInfo action, ICollection<string> paramNames, Func<string, string> getValue)
 {
     var args = action.ParameterInfo;
     var values = new object[args.Length];
     if (args.Length == 1 && args[0].ParameterType.IsClass && args[0].ParameterType != typeof(string)
         && IsArrayOrIEnumerable(args[0]) == false)
     {
         values[0] = CreateInstanceOfComplexType(paramNames, args, getValue);
     }
     else
     {
         for (var argNumber = 0; argNumber < paramNames.Count; argNumber++)
         {
             string argName = args[argNumber].Name;
             var strParam = getValue(argName);
             values[argNumber] = typeConverter.ChangeParameterType(strParam, args[argNumber]);
         }
     }
     return values;
 }
Exemplo n.º 24
0
        private void RunStep(StringStep actionStep)
        {
            var info = ActionCatalog.GetAction(actionStep);

            try
            {
                //TODO: Move Before-/After-EachStep to RunContext !!
                BeforeEachScenario(info);
                BeforeEachStep(info);
                if (actionStep is StringTableStep && !ShouldForEachOverStep(info))
                {
                    ForEachOverStep(actionStep as StringTableStep, info);
                }
                else
                {
                    RunStep(info, () => ParameterConverter.GetParametersForStep(actionStep));
                }
            }
            finally
            {
                lastAction = info;
                AfterEachStep(info);
            }
        }
Exemplo n.º 25
0
            public void EstablishContext()
            {
                _wasCalled = false;
                _actionCatalog = new ActionCatalog();
                Action firstAction = () => Assert.Fail("This action shouldnt be called");
                var actionMethod = new ActionMethodInfo(new Regex("def$"), firstAction, firstAction.Method, "Given", this);
                _actionCatalog.Add(actionMethod);

                Action secondAction = () => { _wasCalled = true; };
                var secondActionMethod = new ActionMethodInfo(new Regex("abc def$"), secondAction, secondAction.Method, "Given", this);
                _actionCatalog.Add(secondActionMethod);
            }
Exemplo n.º 26
0
 private bool MatchesFileName(ActionMethodInfo action, StringStep stringStep)
 {
     return action.MatchesFileName(stringStep.Source);
 }
Exemplo n.º 27
0
 public void Add(ActionMethodInfo actionValue)
 {
     _actions.Add(actionValue);
 }
Exemplo n.º 28
0
 private void ForEachOverStep(StringTableStep actionStep, ActionMethodInfo info)
 {
     foreach (var example in actionStep.TableSteps)
         RunStep(info, () => ParameterConverter.GetParametersForStep(actionStep, example));
 }
Exemplo n.º 29
0
 private bool IsListStep(ActionMethodInfo action, StringStep step)
 {
     return (action.MethodParametersType == MethodParametersType.TypedListStep
            || action.MethodParametersType == MethodParametersType.UntypedListStep)
            && step is StringTableStep;
 }
Exemplo n.º 30
0
 private List<string> GetParameterNames(ActionMethodInfo actionValue)
 {
     return actionValue.GetParameterNames();
 }
Exemplo n.º 31
0
        private object GetActionWithNoParameters(object instance, ActionMethodInfo method)
        {
            Action action = () => method.MethodInfo.Invoke(instance, null);

            return(action);
        }
Exemplo n.º 32
0
 private int CountParameters(ActionMethodInfo actionMethodInfo)
 {
     return(actionMethodInfo.ParameterInfo.Count());
 }
Exemplo n.º 33
0
 private void BeforeEachScenario(ActionMethodInfo info)
 {
     if (isFirstStepInScenario)
     {
         isFirstStepInScenario = false;
         info.ExecuteNotificationMethod(typeof(BeforeScenarioAttribute));
     }
 }
Exemplo n.º 34
0
 private void GetValuesFromTableStep(StringTableStep stringStep, Func<Example, string, string> getValues, MethodInfo addMethodOnList, object values, ActionMethodInfo method)
 {
     List<string> paramNames = (stringStep.TableSteps.FirstOrDefault() ?? Example.EmptyExample).ColumnNames.Select(_ => _.Name).ToList();
     foreach (var example in (stringStep).TableSteps)
     {
         var value = GetParametersForStep(method, paramNames, _ => getValues(example, _))[0];
         addMethodOnList.Invoke(values, new[] { value });
     }
 }
Exemplo n.º 35
0
 private void RunStep(ActionMethodInfo info, Func<object[]> getParametersForActionStepText)
 {
     var actionType = GetActionType(info.Action);
     var methodInfo = actionType.GetMethod("DynamicInvoke");
     var actionParamValues = getParametersForActionStepText();
     methodInfo.Invoke(
         info.Action,
         BindingFlags.InvokeMethod,
         null,
         new object[] { actionParamValues },
         CultureInfo.CurrentCulture);
 }
Exemplo n.º 36
0
        private void RunStep(StringStep actionStep)
        {
            var info = ActionCatalog.GetAction(actionStep);

            try
            {
                //TODO: Move Before-/After-EachStep to RunContext !!
                BeforeEachScenario(info);
                BeforeEachStep(info);
                if (actionStep is StringTableStep && !ShouldForEachOverStep(info))
                    ForEachOverStep(actionStep as StringTableStep, info);
                else
                    RunStep(info, () => ParameterConverter.GetParametersForStep(actionStep));
            }
            finally
            {
                lastAction = info;
                AfterEachStep(info);
            }
        }
Exemplo n.º 37
0
            public void EstablishContext()
            {
                _actionCatalog = new ActionCatalog();
                Action<string, string> firstAction = (a, b) => { };
                var stepMatcher = "$a and $b".AsRegex();
                var actionMethod = new ActionMethodInfo(stepMatcher, firstAction, firstAction.Method, "Given", this);
                _actionCatalog.Add(actionMethod);

                Action<FooBar> secondAction = _ => { };
                var secondActionMethod = new ActionMethodInfo(stepMatcher, secondAction, secondAction.Method, "Given", this);
                _actionCatalog.Add(secondActionMethod);

                Action<List<FooBar>> thirdAction = _ => { };
                var thirdActionMethod = new ActionMethodInfo(stepMatcher, thirdAction, thirdAction.Method, "Given", this);
                _actionCatalog.Add(thirdActionMethod);
            }
Exemplo n.º 38
0
 private void BecauseOf()
 {
     var actionText = new StringStep("Given foo and bar", "somestory.story");
     _action = _actionCatalog.GetAction(actionText);
 }
Exemplo n.º 39
0
 private void AfterEachStep(ActionMethodInfo info)
 {
     info.ExecuteNotificationMethod(typeof(AfterStepAttribute));
 }
Exemplo n.º 40
0
 public void Should_set_MethodParametersType_to_TypedListStep_when_parameter_is_List_of_object_type()
 {
     Action<List<ActionMethodInfo>> action = _ => { };
     var x = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");
     Assert.AreEqual(MethodParametersType.TypedListStep, x.MethodParametersType);
 }
Exemplo n.º 41
0
 public void Should_set_MethodParametersType_to_UntypedStep_when_parameter_is_string_type()
 {
     Action<string> action = _ => { };
     var x = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");
     Assert.AreEqual(MethodParametersType.UntypedStep, x.MethodParametersType);
 }
Exemplo n.º 42
0
            private void BecauseOf()
            {
                var actionText = new StringStep("Given", "foo and bar", "somestory.story");

                _action = _actionCatalog.GetAction(actionText);
            }
Exemplo n.º 43
0
 private List<string> GetParameterNames(ActionMethodInfo actionValue)
 {
     return actionValue.GetParameterNames();
 }
Exemplo n.º 44
0
 private object[] GetParametersForListStep(ActionMethodInfo action, StringStep stringStep)
 {
     var itemType = action.ParameterInfo[0].ParameterType.GetGenericArguments()[0];
     var values = itemType.CreateInstanceOfGenericList();
     var addMethodOnList = values.GetType().GetMethod("Add");
     Func<Example, string, string> getValues = (e, name) => e.ColumnValues[name];
     var method = new ActionMethodInfo(action.ActionStepMatcher, addMethodOnList, addMethodOnList, action.ActionType);
     GetValuesFromTableStep((StringTableStep)stringStep, getValues, addMethodOnList, values, method);
     return new object[] { values };
 }
Exemplo n.º 45
0
 private void BeforeEachStep(ActionMethodInfo info)
 {
     info.ExecuteNotificationMethod(typeof(BeforeStepAttribute));
 }
Exemplo n.º 46
0
 private int CountParameters(ActionMethodInfo actionMethodInfo)
 {
     return actionMethodInfo.ParameterInfo.Count();
 }
Exemplo n.º 47
0
 private bool MatchesFileName(ActionMethodInfo action, StringStep stringStep)
 {
     return(action.MatchesFileName(stringStep.Source));
 }
Exemplo n.º 48
0
 private bool ShouldForEachOverStep(ActionMethodInfo info)
 {
     return(info.MethodParametersType == MethodParametersType.TypedListStep);
 }
Exemplo n.º 49
0
 private void GetValuesFromTableStep(StringTableStep stringStep, Func<Example, string, string> getValues, MethodInfo addMethodOnList, object values, ActionMethodInfo method)
 {
     List<string> paramNames = (stringStep.TableSteps.FirstOrDefault() ?? Example.EmptyExample).ColumnNames.Select(_ => _.Name).ToList();
     foreach (var example in (stringStep).TableSteps)
     {
         var value = GetParametersForStep(method, paramNames, _ => getValues(example, _))[0];
         addMethodOnList.Invoke(values, new[] { value });
     }
 }
Exemplo n.º 50
0
 private void BeforeEachStep(ActionMethodInfo info)
 {
     info.ExecuteNotificationMethod(typeof(BeforeStepAttribute));
 }
Exemplo n.º 51
0
 private bool IsListStep(ActionMethodInfo action, StringStep step)
 {
     return (action.MethodParametersType == MethodParametersType.TypedListStep
            || action.MethodParametersType == MethodParametersType.UntypedListStep)
            && step is StringTableStep;
 }
Exemplo n.º 52
0
 private bool ShouldForEachOverStep(ActionMethodInfo info)
 {
     return info.MethodParametersType == MethodParametersType.TypedListStep;
 }
Exemplo n.º 53
0
 public void Should_set_MethodParametersType_to_TypedStep()
 {
     Action<ActionMethodInfo> action = _ => { };
     var x = new ActionMethodInfo(new Regex(".*"), action, action.Method, "Given");
     Assert.AreEqual(MethodParametersType.TypedStep, x.MethodParametersType);
 }
Exemplo n.º 54
0
 private object GetActionWithNoParameters(object instance, ActionMethodInfo method)
 {
     Action action = () => method.MethodInfo.Invoke(instance, null);
     return action;
 }
Exemplo n.º 55
0
 private void AfterEachStep(ActionMethodInfo info)
 {
     info.ExecuteNotificationMethod(typeof(AfterStepAttribute));
 }
Exemplo n.º 56
0
 private object[] GetParametersForStep(StringStep stringStep, ActionMethodInfo action)
 {
     var match = action.ActionStepMatcher.Match(stringStep.MatchableStep);
     Func<string, string> getValues = _ => match.Groups[_].Value;
     var paramNames = GetParameterNames(action);
     return GetParametersForStep(action, paramNames, getValues);
 }
Exemplo n.º 57
0
 private object[] GetParametersForStep(ActionMethodInfo action, ICollection<string> paramNames, Func<string, string> getValue)
 {
     var args = action.ParameterInfo;
     var values = new object[args.Length];
     if (args.Length == 1 && args[0].ParameterType.IsClass && args[0].ParameterType != typeof(string)
         && IsArrayOrIEnumerable(args[0]) == false)
     {
         values[0] = CreateInstanceOfComplexType(paramNames, args, getValue);
     }
     else
     {
         for (var argNumber = 0; argNumber < paramNames.Count; argNumber++)
         {
             string argName = args[argNumber].Name;
             var strParam = getValue(argName);
             values[argNumber] = typeConverter.ChangeParameterType(strParam, args[argNumber]);
         }
     }
     return values;
 }
Exemplo n.º 58
0
        private object CreateAction(object instance, ActionMethodInfo method)
        {
            object action = null;
            var methodInfo = method.MethodInfo;

            switch (CountParameters(method))
            {
                case 0:
                    action = GetActionWithNoParameters(instance, method);
                    break;
                case 1:
                    action = GetActionForOneParameter(instance, methodInfo);
                    break;
                case 2:
                    action = GetActionForTwoParameters(instance, methodInfo);
                    break;
                case 3:
                    action = GetActionForThreeParameters(instance, methodInfo);
                    break;
                case 4:
                    action = GetActionForFourParameters(instance, methodInfo);
                    break;
            }

            return action;
        }
Exemplo n.º 59
0
 public void Add(ActionMethodInfo actionValue)
 {
     _actions.Add(actionValue);
 }