Esempio 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 bool CanConvert(object value, IBindingType typeToConvertTo, CultureInfo cultureInfo)
            {
                if (!(typeToConvertTo is RuntimeBindingType))
                {
                    Type systemType = Type.GetType(typeToConvertTo.FullName, false);
                    if (systemType == null)
                    {
                        return(false);
                    }
                    typeToConvertTo = new RuntimeBindingType(systemType);
                }

                return(StepArgumentTypeConverter.CanConvertSimple(typeToConvertTo, value, cultureInfo));
            }
Esempio n. 3
0
        protected static bool IsVerticalTable(Table table, RuntimeBindingType runtimeBindingType)
        {
            if (table.Header.Count != 2)
            {
                return(false);
            }

            if (table.RowCount == 0)
            {
                return(true);
            }

            var writableProperties = GetWritableProperties(runtimeBindingType.Type);

            return(writableProperties.ContainsKey(SanitizePropertyName(table.Rows[0][0])));
        }
Esempio n. 4
0
        private static Type TryGetBindingType(IBinding binding)
        {
            StepDefinitionBinding stepDefinitionBinding = binding as StepDefinitionBinding;

            if (stepDefinitionBinding == null)
            {
                return(null);
            }
            var type = stepDefinitionBinding.Method.Type;
            RuntimeBindingType runtimeBindingType = type as RuntimeBindingType;

            if (runtimeBindingType == null)
            {
                return(null);
            }
            return(runtimeBindingType.Type);
        }
Esempio n. 5
0
        private Table GetVerticalTable(Table table, RuntimeBindingType runtimeBindingType)
        {
            if (IsVerticalTable(table, runtimeBindingType))
            {
                return(table);
            }

            if (table.Rows.Count == 1)
            {
                var pivotedTable = new PivotTable(table).GetInstanceTable(0);

                if (IsVerticalTable(pivotedTable, runtimeBindingType))
                {
                    return(pivotedTable);
                }
            }

            return(null);
        }
Esempio n. 6
0
            public bool CanConvert(object value, IBindingType typeToConvertTo, CultureInfo cultureInfo)
            {
                if (!(typeToConvertTo is RuntimeBindingType))
                {
                    try
                    {
                        // in some special cases, Type.GetType throws exception
                        // one of such case, if a Dictionary<string,string> step parameter is specified, see issue #340
                        Type systemType = Type.GetType(typeToConvertTo.FullName, false);
                        if (systemType == null)
                        {
                            return(false);
                        }
                        typeToConvertTo = new RuntimeBindingType(systemType);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        return(false);
                    }
                }

                return(StepArgumentTypeConverter.CanConvertSimple(typeToConvertTo, value, cultureInfo));
            }