Example #1
0
 public BindingMatch(IStepDefinitionBinding stepBinding, int scopeMatches, object[] arguments, StepContext stepContext)
 {
     StepBinding = stepBinding;
     ScopeMatches = scopeMatches;
     Arguments = arguments;
     StepContext = stepContext;
 }
        public BindingMatch Match(IStepDefinitionBinding stepDefinitionBinding, StepInstance stepInstance, CultureInfo bindingCulture, bool useRegexMatching = true, bool useParamMatching = true, bool useScopeMatching = true)
        {
            if (useParamMatching)
                useRegexMatching = true;

            if (stepDefinitionBinding.StepDefinitionType != stepInstance.StepDefinitionType)
                return BindingMatch.NonMatching;

            Match match = null;
            if (useRegexMatching && stepDefinitionBinding.Regex != null && !(match = stepDefinitionBinding.Regex.Match(stepInstance.Text)).Success)
                return BindingMatch.NonMatching;

            int scopeMatches = 0;
            if (useScopeMatching && stepDefinitionBinding.IsScoped && stepInstance.StepContext != null && !stepDefinitionBinding.BindingScope.Match(stepInstance.StepContext, out scopeMatches))
                return BindingMatch.NonMatching;

            var arguments = match == null ? new object[0] : CalculateArguments(match, stepInstance);

            if (useParamMatching)
            {
                Debug.Assert(match != null); // useParamMatching -> useRegexMatching
                var bindingParameters = stepDefinitionBinding.Method.Parameters.ToArray();

                // check if the regex + extra arguments match to the binding method parameters
                if (arguments.Length != bindingParameters.Length)
                    return BindingMatch.NonMatching;

                // Check if regex & extra arguments can be converted to the method parameters
                //if (arguments.Zip(bindingParameters, (arg, parameter) => CanConvertArg(arg, parameter.Type)).Any(canConvert => !canConvert))
                if (arguments.Where((arg, argIndex) => !CanConvertArg(arg, bindingParameters[argIndex].Type, bindingCulture)).Any())
                    return BindingMatch.NonMatching;
            }

            return new BindingMatch(stepDefinitionBinding, scopeMatches, arguments, stepInstance.StepContext);
        }
Example #3
0
        public void AddBinding(IStepDefinitionBinding stepBinding)
        {
            try
            {
                var item = new BoundStepSuggestions <TNativeSuggestionItem>(stepBinding, nativeSuggestionItemFactory);

                var affectedSuggestions = new List <IBoundStepSuggestion <TNativeSuggestionItem> >(
                    boundStepSuggestions.GetRelatedItems(stepBinding.Regex).SelectMany(relatedItem => relatedItem.Suggestions).Where(s => s.Match(stepBinding, GetBindingCulture(s), true, BindingMatchService)));
                affectedSuggestions.AddRange(notMatchingSteps[item.StepDefinitionType].Suggestions.Where(s => s.Match(stepBinding, GetBindingCulture(s), true, BindingMatchService)));

                foreach (var affectedSuggestion in affectedSuggestions)
                {
                    RemoveBoundStepSuggestion(affectedSuggestion);
                }

                boundStepSuggestions.Add(item);

                foreach (var affectedSuggestion in affectedSuggestions)
                {
                    AddStepSuggestion(affectedSuggestion);
                }
            }
            catch (Exception ex)
            {
                projectScope.Tracer.Trace("Error while adding step definition binding: " + ex, GetType().Name);
                throw;
            }
        }
Example #4
0
 public BindingMatch(IStepDefinitionBinding stepBinding, int scopeMatches, object[] arguments, StepContext stepContext)
 {
     StepBinding  = stepBinding;
     ScopeMatches = scopeMatches;
     Arguments    = arguments;
     StepContext  = stepContext;
 }
Example #5
0
        private string GetSuggestionText(IStepDefinitionBinding stepBinding)
        {
            string suggestionTextBase = stepBinding.Regex == null ? "[...]" :
                                        "[" + RegexSampler.GetRegexSample(stepBinding.Regex.ToString(), stepBinding.Method.Parameters.Select(p => p.ParameterName).ToArray()) + "]";

            return(string.Format("{0} -> {1}", suggestionTextBase, stepBinding.Method.GetShortDisplayText()));
        }
Example #6
0
        private void ReplaceStepBindingAttribute(CodeFunction codeFunction, IStepDefinitionBinding binding, string newRegex)
        {
            if (!codeFunction.ProjectItem.IsOpen)
            {
                codeFunction.ProjectItem.Open();
            }

            var formattedOldRegex = FormatRegexForDisplay(binding.Regex);

            var navigatePoint = codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader);

            navigatePoint.TryToShow();
            navigatePoint.Parent.Selection.MoveToPoint(navigatePoint);

            var stepBindingEditorContext = GherkinEditorContext.FromDocument(codeFunction.DTE.ActiveDocument, _gherkinLanguageServiceFactory);
            var attributeLinesToUpdate   = stepBindingEditorContext.TextView.TextViewLines.Where(x => x.Start.GetContainingLine().GetText().Contains("\"" + formattedOldRegex + "\""));

            foreach (var attributeLineToUpdate in attributeLinesToUpdate)
            {
                using (var textEdit = attributeLineToUpdate.Snapshot.TextBuffer.CreateEdit())
                {
                    var regexStart = attributeLineToUpdate.Start.GetContainingLine().GetText().IndexOf(formattedOldRegex);
                    textEdit.Replace(attributeLineToUpdate.Start.Position + regexStart, formattedOldRegex.Length, newRegex);
                    textEdit.Apply();
                }
            }
        }
        public BindingObsoletion(IStepDefinitionBinding stepBinding)
        {
            ObsoleteAttribute possibleObsoletionAttribute;

            try
            {
                possibleObsoletionAttribute = stepBinding?.Method.AssertMethodInfo()
                                              .GetCustomAttributes(false).OfType <ObsoleteAttribute>()
                                              .FirstOrDefault();
            }
            catch (Exception)
            {
                possibleObsoletionAttribute = null;
            }

            if (possibleObsoletionAttribute == null)
            {
                IsObsolete = false;
                message    = null;
                methodName = null;
            }
            else
            {
                IsObsolete = true;
                message    = possibleObsoletionAttribute.Message ?? defaultObsoletionMessage;
                methodName = stepBinding?.Method.Name;
            }
        }
Example #8
0
        public BindingMatch(IStepDefinitionBinding stepDefinitionBinding, StepArgs stepArgs, string[] regexArguments, object[] extraArguments, int scopeMatches)
        {
            if (stepDefinitionBinding == null)
            {
                throw new ArgumentNullException("stepDefinitionBinding");
            }
            if (stepArgs == null)
            {
                throw new ArgumentNullException("stepArgs");
            }
            if (regexArguments == null)
            {
                throw new ArgumentNullException("regexArguments");
            }
            if (extraArguments == null)
            {
                throw new ArgumentNullException("extraArguments");
            }

            StepBinding = stepDefinitionBinding;
            StepArgs    = stepArgs;

            RegexArguments = regexArguments;
            ExtraArguments = extraArguments;

            ScopeMatches = scopeMatches;

            Arguments = RegexArguments.Concat(ExtraArguments).ToArray();
        }
Example #9
0
        public StepFailureEventArgs(IStepDefinitionBinding stepDefiniton, StepContext stepContext, Exception exception)
        {
            IsHandled = false;

            StepDefiniton = stepDefiniton;
            StepContext = stepContext;
            Exception = exception;
        }
Example #10
0
        public StepFailureEventArgs(IStepDefinitionBinding stepDefiniton, StepContext stepContext, Exception exception)
        {
            IsHandled = false;

            StepDefiniton = stepDefiniton;
            StepContext   = stepContext;
            Exception     = exception;
        }
 static public StepDefinitionBindingItem FromStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     return new StepDefinitionBindingItem()
                {
                    Method = stepDefinitionBinding.Method,
                    StepDefinitionType =  stepDefinitionBinding.StepDefinitionType,
                    Regex = stepDefinitionBinding.Regex,
                    BindingScope = stepDefinitionBinding.BindingScope
                };
 }
 static public StepDefinitionBindingItem FromStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     return(new StepDefinitionBindingItem()
     {
         Method = stepDefinitionBinding.Method,
         StepDefinitionType = stepDefinitionBinding.StepDefinitionType,
         Regex = stepDefinitionBinding.Regex,
         BindingScope = stepDefinitionBinding.BindingScope
     });
 }
Example #13
0
        private string GetInsertionText(IStepDefinitionBinding stepBinding)
        {
            if (stepBinding.Regex == null)
            {
                return("...");
            }

            var paramNames = stepBinding.Method.Parameters.Select(p => p.ParameterName);

            return(RegexSampler.GetRegexSample(stepBinding.Regex.ToString(), paramNames.ToArray()));
        }
Example #14
0
        private string GetScope(IStepDefinitionBinding stepDefinitionBinding)
        {
            if (!stepDefinitionBinding.IsScoped)
            {
                return(null);
            }
            if (stepDefinitionBinding.BindingScope.Tag == null)
            {
                return(null);
            }

            return("@" + stepDefinitionBinding.BindingScope.Tag);
        }
        public bool Match(IStepDefinitionBinding binding, CultureInfo bindingCulture, bool includeRegexCheck, IStepDefinitionMatchService stepDefinitionMatchService)
        {
            if (binding.StepDefinitionType != StepDefinitionType)
            {
                return(false);
            }

            if (instances.Count == 0)
            {
                return(false);
            }

            return(instances.Any(i => i.Match(binding, bindingCulture, true, stepDefinitionMatchService)));
        }
Example #16
0
        public BoundStepSuggestions(IStepDefinitionBinding stepBinding, INativeSuggestionItemFactory <TNativeSuggestionItem> nativeSuggestionItemFactory)
        {
            if (stepBinding == null)
            {
                throw new ArgumentNullException("stepBinding");
            }

            StepBinding        = stepBinding;
            StepDefinitionType = stepBinding.StepDefinitionType;
            string suggestionText = GetSuggestionText(stepBinding);

            NativeSuggestionItem = nativeSuggestionItemFactory.Create(suggestionText, GetInsertionText(StepBinding), 0, StepDefinitionType.ToString().Substring(0, 1) + "-b", this);
            suggestions          = new StepSuggestionList <TNativeSuggestionItem>(nativeSuggestionItemFactory);
        }
Example #17
0
        public BindingMatch Match(IStepDefinitionBinding stepDefinitionBinding, StepInstance stepInstance, CultureInfo bindingCulture, bool useRegexMatching = true, bool useParamMatching = true, bool useScopeMatching = true)
        {
            if (useParamMatching)
            {
                useRegexMatching = true;
            }

            //if (stepDefinitionBinding.StepDefinitionType != stepInstance.StepDefinitionType)
            //    return BindingMatch.NonMatching;

            Match match = null;

            if (useRegexMatching && stepDefinitionBinding.Regex != null && !(match = stepDefinitionBinding.Regex.Match(stepInstance.Text)).Success)
            {
                throw new Exception(String.Format("Error 1: Keyword:  {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text));
                return(BindingMatch.NonMatching);
            }

            int scopeMatches = 0;

            if (useScopeMatching && stepDefinitionBinding.IsScoped && stepInstance.StepContext != null && !stepDefinitionBinding.BindingScope.Match(stepInstance.StepContext, out scopeMatches))
            {
                throw new Exception(String.Format("Error 2: Keyword:  {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text));
                return(BindingMatch.NonMatching);
            }

            var arguments = match == null ? new object[0] : CalculateArguments(match, stepInstance);

            if (useParamMatching)
            {
                Debug.Assert(match != null); // useParamMatching -> useRegexMatching
                var bindingParameters = stepDefinitionBinding.Method.Parameters.ToArray();

                // check if the regex + extra arguments match to the binding method parameters
                if (arguments.Length != bindingParameters.Length)
                {
                    return(BindingMatch.NonMatching);
                }

                // Check if regex & extra arguments can be converted to the method parameters
                //if (arguments.Zip(bindingParameters, (arg, parameter) => CanConvertArg(arg, parameter.Type)).Any(canConvert => !canConvert))
                if (arguments.Where((arg, argIndex) => !CanConvertArg(arg, bindingParameters[argIndex].Type, bindingCulture)).Any())
                {
                    throw new Exception(String.Format("Error 3: Keyword:  {0} Step Def type {1} TEXT: {2}", stepInstance.Keyword, stepInstance.StepDefinitionType, stepInstance.Text));
                    return(BindingMatch.NonMatching);
                }
            }

            return(new BindingMatch(stepDefinitionBinding, scopeMatches, arguments, stepInstance.StepContext));
        }
        private StepDefinition CreateStepDefinition(IStepDefinitionBinding sdb, WarningCollector warningCollector)
        {
            var stepDefinition = new StepDefinition
            {
                Type           = sdb.StepDefinitionType.ToString(),
                Regex          = sdb.Regex?.ToString(),
                Method         = sdb.Method.ToString(),
                ParamTypes     = GetParamTypes(sdb.Method),
                Scope          = GetScope(sdb),
                SourceLocation = GetSourceLocation(sdb.Method, warningCollector),
                Expression     = GetSourceExpression(sdb),
                Error          = GetError(sdb)
            };

            return(stepDefinition);
        }
        private StepScope GetScope(IStepDefinitionBinding stepDefinitionBinding)
        {
            if (!stepDefinitionBinding.IsScoped)
            {
                return(null);
            }

            return(new StepScope
            {
                Tag = stepDefinitionBinding.BindingScope.Tag == null
                    ? null
                    : "@" + stepDefinitionBinding.BindingScope.Tag,
                FeatureTitle = stepDefinitionBinding.BindingScope.FeatureTitle,
                ScenarioTitle = stepDefinitionBinding.BindingScope.ScenarioTitle
            });
        }
Example #20
0
        public void RemoveBinding(IStepDefinitionBinding stepBinding)
        {
            var item = boundStepSuggestions.GetRelatedItems(stepBinding.Regex).FirstOrDefault(it => it.StepBinding == stepBinding);

            if (item == null)
            {
                return;
            }

            foreach (var stepSuggestion in item.Suggestions.ToArray())
            {
                item.RemoveSuggestion(stepSuggestion);
                if (!stepSuggestion.MatchGroups.Any())
                {
                    notMatchingSteps[item.StepDefinitionType].AddSuggestion(stepSuggestion);
                }
            }

            boundStepSuggestions.Remove(item);
        }
 protected override void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     stepDefinitionBindings.Add(stepDefinitionBinding);
 }
Example #22
0
 protected abstract void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding);
Example #23
0
 protected override void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     bindingRegistry.RegisterStepDefinitionBinding(stepDefinitionBinding);
 }
Example #24
0
        private static string BuildStepNameWithNewRegex(string stepName, string newStepRegex, IStepDefinitionBinding binding)
        {
            var originalMatch = Regex.Match(stepName, FormatRegexForDisplay(binding.Regex));
            var newRegexMatch = Regex.Match(newStepRegex, newStepRegex);

            var builder = new StringBuilder(newStepRegex);

            for (var i = newRegexMatch.Groups.Count - 1; i > 0; i--)
            {
                builder.Replace(newRegexMatch.Groups[i].Value, originalMatch.Groups[i].Value, newRegexMatch.Groups[i].Index, newRegexMatch.Groups[i].Length);
            }

            return(RemoveDoubleQuotes(builder.ToString()));
        }
 protected abstract void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding);
Example #26
0
 public StepBindingStartedEvent(IStepDefinitionBinding stepDefinitionBinding)
 {
     StepDefinitionBinding = stepDefinitionBinding;
 }
Example #27
0
        public string BuildStepNameWithNewRegex(string stepName, string newStepRegex, IStepDefinitionBinding binding)
        {
            var originalMatch = Regex.Match(stepName, FormatRegexForDisplay(binding.Regex));

            var newRegexMatch = Regex.Match(newStepRegex, newStepRegex);

            // Regex pattern "the number is (\d+)" will not match the input "the number is (\d+)"
            // replace (\d+) to (.*) in the pattern so it will match
            if (!newRegexMatch.Success)
            {
                var newStepRegexForMatching = Regex.Replace(newStepRegex, @"\(.*?\)", "(.*)");
                newRegexMatch = Regex.Match(newStepRegex, newStepRegexForMatching);
            }

            // we cannot support the parameter number change,
            // because we will not know where to put the values
            if (originalMatch.Groups.Count != newRegexMatch.Groups.Count)
            {
                throw new NotSupportedException("Changing the number of parameters is not supported!");
            }

            var builder = new StringBuilder(newStepRegex);

            for (var i = newRegexMatch.Groups.Count - 1; i > 0; i--)
            {
                builder.Replace(newRegexMatch.Groups[i].Value, originalMatch.Groups[i].Value, newRegexMatch.Groups[i].Index, newRegexMatch.Groups[i].Length);
            }

            return(RemoveDoubleQuotes(builder.ToString()));
        }
        private string GetSourceExpression(IStepDefinitionBinding sdb)
        {
            const string propertyName = "SourceExpression";

            return(sdb.ReflectionHasProperty(propertyName) ? sdb.ReflectionGetProperty <string>(propertyName) : null);
        }
 protected override void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     bindingRegistry.RegisterStepDefinitionBinding(stepDefinitionBinding);
 }
Example #30
0
        public BindingMatch(IStepDefinitionBinding stepDefinitionBinding, Match match, object[] extraArguments, StepArgs stepArgs, int scopeMatches) :
            this(stepDefinitionBinding, stepArgs, match.Groups.Cast <Group>().Skip(1).Select(g => g.Value).ToArray(), extraArguments, scopeMatches)

        {
        }
Example #31
0
 public void RegisterStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     stepDefinitions.Add(stepDefinitionBinding);
 }
 public void RegisterStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     throw new NotSupportedException();
 }
        private string GetError(IStepDefinitionBinding sdb)
        {
            const string propertyName = "Error";

            return(sdb.ReflectionHasProperty(propertyName) ? sdb.ReflectionGetProperty <string>(propertyName) : null);
        }
Example #34
0
        private void RenameStep(StepInstanceWithProjectScope stepInstance, string newStepRegex, IStepDefinitionBinding binding)
        {
            var featureFileDocument = JumpToStep(stepInstance);

            if (featureFileDocument == null)
            {
                return;
            }

            var stepEditorContext = GherkinEditorContext.FromDocument(featureFileDocument, _gherkinLanguageServiceFactory);

            var stepToRename = GetCurrentStep(stepEditorContext);

            if (stepToRename == null)
            {
                return;
            }

            if (!binding.Regex.IsMatch(stepToRename.Text))
            {
                return;
            }

            var stepLineStart = stepEditorContext.TextView.Selection.Start.Position.GetContainingLine();

            using (var stepNameTextEdit = stepLineStart.Snapshot.TextBuffer.CreateEdit())
            {
                var line                  = stepLineStart.Snapshot.GetLineFromLineNumber(stepLineStart.LineNumber);
                var lineText              = line.GetText();
                var trimmedText           = lineText.Trim();
                var numLeadingWhiteSpaces = lineText.Length - trimmedText.Length;

                var actualStepName = trimmedText.Substring(stepToRename.Keyword.Length);
                var newStepName    = BuildStepNameWithNewRegex(actualStepName, newStepRegex, binding);

                var stepNamePosition = line.Start.Position + numLeadingWhiteSpaces + stepToRename.Keyword.Length;
                stepNameTextEdit.Replace(stepNamePosition, actualStepName.Length, newStepName);

                stepNameTextEdit.Apply();
            }
        }
Example #35
0
 protected override void ProcessStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     StepDefinitionBindings.Add(stepDefinitionBinding);
 }
Example #36
0
 private static CodeFunction FindBindingMethodCodeFunction(GherkinEditorContext editorContext, IStepDefinitionBinding binding)
 {
     return(new VsBindingMethodLocator().FindCodeFunction(((VsProjectScope)editorContext.ProjectScope), binding.Method));
 }
Example #37
0
 public void RegisterStepDefinitionBinding(IStepDefinitionBinding stepDefinitionBinding)
 {
     stepDefinitions.Add(stepDefinitionBinding);
 }
Example #38
0
 public StepBindingFinishedEvent(IStepDefinitionBinding stepDefinitionBinding, TimeSpan duration)
 {
     StepDefinitionBinding = stepDefinitionBinding;
     Duration = duration;
 }