Ejemplo n.º 1
0
        private Dictionary <string, CucumberExpressionParameterType> InitializeRegistry()
        {
            var boolBindingType     = new RuntimeBindingType(typeof(bool));
            var byteBindingType     = new RuntimeBindingType(typeof(byte));
            var charBindingType     = new RuntimeBindingType(typeof(char));
            var dateTimeBindingType = new RuntimeBindingType(typeof(DateTime));
            var decimalBindingType  = new RuntimeBindingType(typeof(decimal));
            var doubleBindingType   = new RuntimeBindingType(typeof(double));
            var floatBindingType    = new RuntimeBindingType(typeof(float));
            var shortBindingType    = new RuntimeBindingType(typeof(short));
            var intBindingType      = new RuntimeBindingType(typeof(int));
            var longBindingType     = new RuntimeBindingType(typeof(long));
            var objectBindingType   = new RuntimeBindingType(typeof(object));
            var stringBindingType   = new RuntimeBindingType(typeof(string));
            var guidBindingType     = new RuntimeBindingType(typeof(Guid));

            // exposing built-in transformations

            var builtInTransformations = new[]
            {
                // official cucumber expression types
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, objectBindingType, name: string.Empty),
                new BuiltInCucumberExpressionParameterTypeTransformation(@"(-?\d+)", intBindingType, "int"),
                new BuiltInCucumberExpressionParameterTypeTransformation(@"(.*?)", doubleBindingType, "float"), //TODO: make it specific based on the binding culture
                new BuiltInCucumberExpressionParameterTypeTransformation(@"([^\s]+)", stringBindingType, "word"),

                // other types supported by SpecFlow by default: Make them accessible with type name (e.g. Int32)
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, boolBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, byteBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, charBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, dateTimeBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, decimalBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, doubleBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, floatBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, shortBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, intBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, longBindingType),
                new BuiltInCucumberExpressionParameterTypeTransformation(CucumberExpressionParameterType.MatchAllRegex, guidBindingType),
            };

            var convertQuotedStringMethod = new RuntimeBindingMethod(GetType().GetMethod(nameof(ConvertQuotedString)));

            _bindingRegistry.RegisterStepArgumentTransformationBinding(new CucumberExpressionParameterTypeBinding(@"""([^""]*)""", convertQuotedStringMethod, "string"));
            _bindingRegistry.RegisterStepArgumentTransformationBinding(new CucumberExpressionParameterTypeBinding(@"'([^']*)'", convertQuotedStringMethod, "string"));

            var userTransformations = _bindingRegistry.GetStepTransformations().Select(t => new UserDefinedCucumberExpressionParameterTypeTransformation(t));

            var parameterTypes = builtInTransformations.Cast <ICucumberExpressionParameterTypeTransformation>()
                                 .Concat(userTransformations)
                                 .GroupBy(t => new Tuple <IBindingType, string>(t.TargetType, t.Name))
                                 .Select(g => new CucumberExpressionParameterType(g.Key.Item2 ?? g.Key.Item1.Name, g.Key.Item1, g))
                                 .ToDictionary(pt => pt.Name, pt => pt);

            DumpParameterTypes(parameterTypes);

            return(parameterTypes);
        }
        public void Should_not_execute_step_argument_transformations_when_there_was_an_error_earlier()
        {
            var testExecutionEngine = CreateTestExecutionEngine();

            var bindingTypeStub = new Mock<IBindingType>();
            RegisterStepDefinitionWithTransformation(bindingTypeStub.Object);

            scenarioContext.ScenarioExecutionStatus = ScenarioExecutionStatus.TestError;

            UserCreator stepTransformationInstance = new UserCreator();
            var transformMethod = new RuntimeBindingMethod(stepTransformationInstance.GetType().GetMethod("Create"));
            var stepTransformationBinding = CreateStepTransformationBinding(@"user (\w+)", transformMethod);
            stepTransformations.Add(stepTransformationBinding);

            testExecutionEngine.Step(StepDefinitionKeyword.Given, null, "user bar", null, null);

            _stepArgumentTypeConverterMock.Verify(i => i.Convert(It.IsAny<object>(), bindingTypeStub.Object, It.IsAny<CultureInfo>()), Times.Never);
        }
        public void StepArgumentTypeConverterShouldUseUserConverterForConversion()
        {
            UserCreator stepTransformationInstance = new UserCreator();
            var         transformMethod            = new RuntimeBindingMethod(stepTransformationInstance.GetType().GetMethod("Create"));
            var         stepTransformationBinding  = CreateStepTransformationBinding(@"user (\w+)", transformMethod);

            stepTransformations.Add(stepTransformationBinding);
            TimeSpan duration;
            var      resultUser = new User();

            methodBindingInvokerStub.Setup(i => i.InvokeBinding(stepTransformationBinding, It.IsAny <IContextManager>(), It.IsAny <object[]>(), It.IsAny <ITestTracer>(), out duration))
            .Returns(resultUser);

            var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();

            var result = stepArgumentTypeConverter.Convert("user xyz", typeof(User), new CultureInfo("en-US"));

            Assert.That(result, Is.EqualTo(resultUser));
        }
Ejemplo n.º 4
0
        public async Task StepArgumentTypeConverterShouldUseUserConverterForConversion()
        {
            UserCreator stepTransformationInstance = new UserCreator();
            var         transformMethod            = new RuntimeBindingMethod(stepTransformationInstance.GetType().GetMethod("Create"));
            var         stepTransformationBinding  = CreateStepTransformationBinding(@"user (\w+)", transformMethod);

            stepTransformations.Add(stepTransformationBinding);

            var resultUser = new User();

            methodBindingInvokerStub.Setup(i => i.InvokeBindingAsync(stepTransformationBinding, It.IsAny <IContextManager>(), It.IsAny <object[]>(), It.IsAny <ITestTracer>()))
            .ReturnsAsync((resultUser, new TimeSpan()));

            var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();

            var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", typeof(User), new CultureInfo("en-US"));

            result.Should().Be(resultUser);
        }
        public void ShouldUseStepArgumentTransformationToConvertTable()
        {
            var table = new Table("Name");

            UserCreator stepTransformationInstance = new UserCreator();
            var         transformMethod            = new RuntimeBindingMethod(stepTransformationInstance.GetType().GetMethod("CreateUsers"));
            var         stepTransformationBinding  = CreateStepTransformationBinding(@"", transformMethod);

            stepTransformations.Add(stepTransformationBinding);
            TimeSpan duration;
            var      resultUsers = new User[3];

            methodBindingInvokerStub.Setup(i => i.InvokeBinding(stepTransformationBinding, It.IsAny <IContextManager>(), new object[] { table }, It.IsAny <ITestTracer>(), out duration))
            .Returns(resultUsers);

            var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();


            var result = stepArgumentTypeConverter.Convert(table, typeof(IEnumerable <User>), new CultureInfo("en-US"));

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.EqualTo(resultUsers));
        }
Ejemplo n.º 6
0
        public async Task ShouldUseStepArgumentTransformationToConvertTable()
        {
            var table = new Table("Name");

            UserCreator stepTransformationInstance = new UserCreator();
            var         transformMethod            = new RuntimeBindingMethod(stepTransformationInstance.GetType().GetMethod("CreateUsers"));
            var         stepTransformationBinding  = CreateStepTransformationBinding(@"", transformMethod);

            stepTransformations.Add(stepTransformationBinding);

            var resultUsers = new User[3];

            methodBindingInvokerStub.Setup(i => i.InvokeBindingAsync(stepTransformationBinding, It.IsAny <IContextManager>(), new object[] { table }, It.IsAny <ITestTracer>()))
            .ReturnsAsync((resultUsers, new TimeSpan()));

            var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();


            var result = await stepArgumentTypeConverter.ConvertAsync(table, typeof(IEnumerable <User>), new CultureInfo("en-US"));

            result.Should().NotBeNull();
            result.Should().Be(resultUsers);
        }