public BindingMatch(StepBinding stepBinding, Match match, object[] extraArguments, StepArgs stepArgs) { StepBinding = stepBinding; Match = match; ExtraArguments = extraArguments; StepArgs = stepArgs; }
public void TraceNoMatchingStepDefinition(StepArgs stepArgs, ProgrammingLanguage targetLanguage, List <BindingMatch> matchesWithoutScopeCheck) { // string stepDescription = stepFormatter.GetStepDescription(stepArgs); // return new BindingException( // string.Format("Multiple step definitions found, but none of them have matching scope for step '{0}': {1}", // stepDescription, // string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.MethodInfo)).ToArray()))); IStepDefinitionSkeletonProvider stepDefinitionSkeletonProvider = ObjectContainer.StepDefinitionSkeletonProvider(targetLanguage); StringBuilder message = new StringBuilder(); if (matchesWithoutScopeCheck == null || matchesWithoutScopeCheck.Count == 0) { message.AppendLine("No matching step definition found for the step. Use the following code to create one:"); } else { string preMessage = string.Format("No matching step definition found for the step. There are matching step definitions, but none of them have matching scope for this step: {0}.", string.Join(", ", matchesWithoutScopeCheck.Select(m => stepFormatter.GetMatchText(m, null)).ToArray())); traceListener.WriteToolOutput(preMessage); message.AppendLine("Change the scope or use the following code to create a new step definition:"); } message.Append( stepDefinitionSkeletonProvider.GetBindingClassSkeleton( stepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(stepArgs)) .Indent(StepDefinitionSkeletonProviderBase.CODEINDENT)); traceListener.WriteToolOutput(message.ToString()); }
public override string GetStepDefinitionSkeleton(StepArgs stepArgs) { List <string> extraArgs = new List <string>(); if (stepArgs.MultilineTextArgument != null) { extraArgs.Add("string multilineText"); } if (stepArgs.TableArgument != null) { extraArgs.Add("Table table"); } StringBuilder result = new StringBuilder(); result.AppendFormat(@"[{0}(@""{2}"")] public void {1}{3}({4}) {{ ScenarioContext.Current.Pending(); }}", stepArgs.Type, LanguageHelper.GetDefaultKeyword(stepArgs.StepContext.FeatureInfo.Language, stepArgs.Type).ToIdentifier(), EscapeRegex(stepArgs.Text), stepArgs.Text.ToIdentifier(), string.Join(", ", extraArgs.ToArray()) ); result.AppendLine(); return(result.ToString()); }
public Exception GetAmbiguousMatchError(IEnumerable<BindingMatch> matches, StepArgs stepArgs) { string stepDescription = stepFormatter.GetStepDescription(stepArgs); return new BindingException( string.Format("Ambiguous step definitions found for step '{0}': {1}", stepDescription, string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.MethodInfo)).ToArray()))); }
public Exception GetAmbiguousMatchError(IEnumerable <BindingMatch> matches, StepArgs stepArgs) { string stepDescription = stepFormatter.GetStepDescription(stepArgs); return(new BindingException( string.Format("Ambiguous step definitions found for step '{0}': {1}", stepDescription, string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.MethodInfo)).ToArray())))); }
public void TraceNoMatchingStepDefinition(StepArgs stepArgs) { StringBuilder message = new StringBuilder(); message.AppendLine("No matching step definition found for the step. Use the following code to create one:"); message.Append( stepDefinitionSkeletonProvider.GetBindingClassSkeleton( stepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(stepArgs)) .Indent(StepDefinitionSkeletonProvider.CODEINDENT)); traceListener.WriteToolOutput(message.ToString()); }
private object[] CalculateExtraArgs(StepArgs stepArgs) { if (stepArgs.MultilineTextArgument == null && stepArgs.TableArgument == null) { return(emptyExtraArgs); } var extraArgsList = new List <object>(); if (stepArgs.MultilineTextArgument != null) { extraArgsList.Add(stepArgs.MultilineTextArgument); } if (stepArgs.TableArgument != null) { extraArgsList.Add(stepArgs.TableArgument); } return(extraArgsList.ToArray()); }
private BindingMatch GetStepMatch(StepArgs stepArgs) { List <BindingMatch> matches = stepDefinitionMatcher.GetMatches(stepArgs); if (matches.Count == 0) { // there were either no regex match or it was filtered out by the param/scope matching // to provide better error message for the param matching error, we re-run // the matching without param check List <BindingMatch> matchesWithoutScopeCheck = stepDefinitionMatcher.GetMatchesWithoutScopeCheck(stepArgs); if (matchesWithoutScopeCheck.Count == 0) { List <BindingMatch> matchesWithoutParamCheck = stepDefinitionMatcher.GetMatchesWithoutParamCheck(stepArgs); if (matchesWithoutParamCheck.Count == 1) { // no ambiguouity, but param error -> execute will find it out return(matchesWithoutParamCheck[0]); } if (matchesWithoutParamCheck.Count > 1) { // ambiguouity, because of param error throw errorProvider.GetAmbiguousBecauseParamCheckMatchError(matchesWithoutParamCheck, stepArgs); } } testTracer.TraceNoMatchingStepDefinition(stepArgs, contextManager.FeatureContext.FeatureInfo.GenerationTargetLanguage, matchesWithoutScopeCheck); contextManager.ScenarioContext.MissingSteps.Add( currentStepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(stepArgs)); throw errorProvider.GetMissingStepDefinitionError(); } if (matches.Count > 1) { if (runtimeConfiguration.DetectAmbiguousMatches) { throw errorProvider.GetAmbiguousMatchError(matches, stepArgs); } } return(matches[0]); }
public string GetStepText(StepArgs stepArgs) { StringBuilder result = new StringBuilder(); result.Append(LanguageHelper.GetKeyword(FeatureContext.Current.FeatureInfo.Language, stepArgs.StepDefinitionKeyword)); result.Append(" "); result.AppendLine(stepArgs.Text); if (stepArgs.MultilineTextArgument != null) { result.AppendLine("--- multiline step argument ---".Indent(INDENT)); result.AppendLine(stepArgs.MultilineTextArgument.Indent(INDENT)); } if (stepArgs.TableArgument != null) { result.AppendLine("--- table step argument ---".Indent(INDENT)); result.AppendLine(stepArgs.TableArgument.ToString().Indent(INDENT)); } return result.ToString(); }
private BindingMatch Match(StepBinding stepBinding, StepArgs stepArgs, bool useParamMatching, bool useScopeMatching) { Match match = stepBinding.Regex.Match(stepArgs.Text); // Check if regexp is a match if (!match.Success) { return(null); } int scopeMatches = 0; if (useScopeMatching && stepBinding.IsScoped) { if (!stepBinding.BindingScope.Match(stepArgs.StepContext, out scopeMatches)) { return(null); } } var bindingMatch = new BindingMatch(stepBinding, match, CalculateExtraArgs(stepArgs), stepArgs, scopeMatches); if (useParamMatching) { // check if the regex + extra arguments match to the binding method parameters if (bindingMatch.Arguments.Length != stepBinding.ParameterTypes.Length) { return(null); } // Check if regex & extra arguments can be converted to the method parameters if (bindingMatch.Arguments.Where( (arg, argIndex) => !CanConvertArg(arg, stepBinding.ParameterTypes[argIndex])).Any()) { return(null); } } return(bindingMatch); }
public override string GetStepDefinitionSkeleton(StepArgs stepArgs) { List <string> extraArgs = new List <string>(); if (stepArgs.MultilineTextArgument != null) { extraArgs.Add("ByVal multilineText As String"); } if (stepArgs.TableArgument != null) { extraArgs.Add("ByVal table As Table"); } StringBuilder result = new StringBuilder(); // in VB "When" and "Then" are language keywords - use the fully qualified namespace string namespaceAddition = "TechTalk.SpecFlow."; if (stepArgs.Type == BindingType.Given) { namespaceAddition = ""; } result.AppendFormat(@"<{5}{0}(""{2}"")> _ Public Sub {1}{3}({4}) ScenarioContext.Current.Pending() End Sub", stepArgs.Type, LanguageHelper.GetDefaultKeyword(stepArgs.StepContext.FeatureInfo.Language, stepArgs.Type).ToIdentifier(), EscapeRegex(stepArgs.Text), stepArgs.Text.ToIdentifier(), string.Join(", ", extraArgs.ToArray()), namespaceAddition ); result.AppendLine(); return(result.ToString()); }
public List <BindingMatch> GetMatches(StepArgs stepArgs) { var matches = bindingRegistry .Where(b => b.Type == stepArgs.Type) .Select(binding => Match(binding, stepArgs, true, true)) .Where(match => match != null) .ToList(); if (matches.Count > 1) { // if there are both scoped and non-scoped matches, we take the ones with the higher degree of scope matches int maxScopeMatches = matches.Max(m => m.ScopeMatches); matches.RemoveAll(m => m.ScopeMatches < maxScopeMatches); } if (matches.Count > 1) { // we remove duplicate maches for the same method (take the first from each) matches = matches.GroupBy(m => m.StepBinding.MethodInfo, (methodInfo, methodMatches) => methodMatches.First()).ToList(); } return(matches); }
public string GetStepText(StepArgs stepArgs) { StringBuilder result = new StringBuilder(); result.Append(stepArgs.OriginalStepKeyword ?? LanguageHelper.GetDefaultKeyword(FeatureContext.Current.FeatureInfo.Language, stepArgs.StepDefinitionKeyword)); result.Append(" "); result.AppendLine(stepArgs.Text); if (stepArgs.MultilineTextArgument != null) { result.AppendLine("--- multiline step argument ---".Indent(INDENT)); result.AppendLine(stepArgs.MultilineTextArgument.Indent(INDENT)); } if (stepArgs.TableArgument != null) { result.AppendLine("--- table step argument ---".Indent(INDENT)); result.AppendLine(stepArgs.TableArgument.ToString().Indent(INDENT)); } return(result.ToString()); }
public string GetStepDefinitionSkeleton(StepArgs stepArgs) { List<string> extraArgs = new List<string>(); if (stepArgs.MultilineTextArgument != null) extraArgs.Add("string multilineText"); if (stepArgs.TableArgument != null) extraArgs.Add("Table table"); StringBuilder result = new StringBuilder(); result.AppendFormat(@"[{0}(@""{2}"")] public void {1}{3}({4}) {{ ScenarioContext.Current.Pending(); }}", stepArgs.Type, LanguageHelper.GetKeyword(FeatureContext.Current.FeatureInfo.Language, stepArgs.Type).ToIdentifier(), EscapeRegex(stepArgs.Text), stepArgs.Text.ToIdentifier(), string.Join(", ", extraArgs.ToArray()) ); result.AppendLine(); return result.ToString(); }
public Exception GetNoMatchBecauseOfScopeFilterError(List <BindingMatch> matches, StepArgs stepArgs) { string stepDescription = stepFormatter.GetStepDescription(stepArgs); return(new BindingException( string.Format("Multiple step definitions found, but none of them have matching scope for step '{0}': {1}", stepDescription, string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.MethodInfo)).ToArray())))); }
private BindingMatch GetStepMatch(StepArgs stepArgs) { List<BindingMatch> matches = new List<BindingMatch>(); foreach (StepBinding binding in bindingRegistry.Where(b => b.Type == stepArgs.Type)) { BindingMatch match = Match(binding, stepArgs); if (match == null) continue; matches.Add(match); if (!RuntimeConfiguration.Current.DetectAmbiguousMatches) break; } if (matches.Count == 0) { testTracer.TraceNoMatchingStepDefinition(stepArgs); ObjectContainer.ScenarioContext.MissingSteps.Add( stepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(stepArgs)); throw errorProvider.GetMissingStepDefinitionError(); } if (matches.Count > 1) { throw errorProvider.GetAmbiguousMatchError(matches, stepArgs); } return matches[0]; }
public Exception GetAmbiguousBecauseParamCheckMatchError(List <BindingMatch> matches, StepArgs stepArgs) { string stepDescription = stepFormatter.GetStepDescription(stepArgs); return(new BindingException( string.Format("Multiple step definitions found, but none of them have matching parameter count and type for step '{0}': {1}", stepDescription, string.Join(", ", matches.Select(m => GetMethodText(m.StepBinding.MethodInfo)).ToArray())))); }
public string GetStepDescription(StepArgs stepArgs) { return(string.Format("{0} {1}", stepArgs.Type, stepArgs.Text)); }
private BindingMatch Match(StepBinding stepBinding, StepArgs stepArgs) { Match match = stepBinding.Regex.Match(stepArgs.Text); if (!match.Success) return null; object[] extraArgs = null; if (stepArgs.MultilineTextArgument != null || stepArgs.TableArgument != null) { List<object> extraArgsList = new List<object>(); if (stepArgs.MultilineTextArgument != null) extraArgsList.Add(stepArgs.MultilineTextArgument); if (stepArgs.TableArgument != null) extraArgsList.Add(stepArgs.TableArgument); extraArgs = extraArgsList.ToArray(); } return new BindingMatch(stepBinding, match, extraArgs, stepArgs); }
public abstract string GetStepDefinitionSkeleton(StepArgs stepArgs);
private void KeyBindForm_Load(object sender, EventArgs e) { mPhoneTask = new PhoneTestTask(); mPhoneTask.KeyNumber = int.Parse(ConfigurationManager.AppSettings["KeysNumber"].ToString()); mPhoneTask.TryCnts = int.Parse(ConfigurationManager.AppSettings["TryCnts"].ToString()); mPhoneTask.UpdateWorkStatusHandler += (object _sender, EventArgs _e) => { StepArgs mArgs = _e as StepArgs; if (mArgs != null) { SetMainText(mArgs.level); } }; mPhoneTask.UpdateValidSNHandler += (object _sender, EventArgs _e) => { UIEventArgs mArgs = _e as UIEventArgs; if (mArgs != null) { SetValidSN(mArgs.level); } }; mPhoneTask.BindKey1Handler += (object _sender, EventArgs _e) => { UIEventArgs mArgs = _e as UIEventArgs; if (mArgs != null) { SetBindKey1(mArgs.level); } }; mPhoneTask.BindKey2Handler += (object _sender, EventArgs _e) => { UIEventArgs mArgs = _e as UIEventArgs; if (mArgs != null) { SetBindKey2(mArgs.level); } }; mPhoneTask.ListViewHandler += (object _sender, EventArgs _e) => { UIEventArgs mArgs = _e as UIEventArgs; if (mArgs != null) { SetListView(mArgs.msg, mArgs.submsg); } }; mPhoneTask.KeyValueHandler += (object _sender, EventArgs _e) => { KeyValueArgs mArgs = _e as KeyValueArgs; if (mArgs != null) { SetKeyValue(mArgs.type, mArgs.value); } }; mPhoneTask.WriteNVHandler += (object _sender, EventArgs _e) => { UIEventArgs mArgs = _e as UIEventArgs; if (mArgs != null) { SetWriteNVFlag(mArgs.level); } }; TestTimeTicker = new System.Timers.Timer(1000); TestTimeTicker.Enabled = false; TestTimeTicker.Elapsed += new ElapsedEventHandler((object source, ElapsedEventArgs ElapsedEventArgs) => { TimeCounts++; SetCountNum(TimeCounts); }); //设置界面 if (mPhoneTask.KeyNumber < 1 || mPhoneTask.KeyNumber > 2) { MessageBox.Show("绑定钥匙至少1把,最多2把!"); } else { if (mPhoneTask.KeyNumber == 1) { this.panel_BindKey2.BackColor = Color.Gray; } else if (mPhoneTask.KeyNumber == 2) { this.panel_BindKey2.BackColor = Color.White; } } SetMainText(STEP_LEVEL.NONE); SetValidSN(INFO_LEVEL.INIT); SetBindKey1(INFO_LEVEL.INIT); SetBindKey2(INFO_LEVEL.INIT); SetWriteNVFlag(INFO_LEVEL.INIT); SetKeyValue(KeyType.NONE_KEY, 0); }
public void TraceStep(StepArgs stepArgs, bool showAdditionalArguments) { string stepText = stepFormatter.GetStepText(stepArgs); traceListener.WriteTestOutput(stepText.TrimEnd()); }
private void ExecuteStep(StepArgs stepArgs) { HandleBlockSwitch(stepArgs.Type.ToScenarioBlock()); testTracer.TraceStep(stepArgs, true); bool isStepSkipped = contextManager.ScenarioContext.TestStatus != TestStatus.OK; BindingMatch match = null; object[] arguments = null; try { match = GetStepMatch(stepArgs); arguments = GetExecuteArguments(match); if (isStepSkipped) { testTracer.TraceStepSkipped(); } else { OnStepStart(); TimeSpan duration = ExecuteStepMatch(match, arguments); if (runtimeConfiguration.TraceSuccessfulSteps) { testTracer.TraceStepDone(match, arguments, duration); } } } catch (PendingStepException) { Debug.Assert(match != null); Debug.Assert(arguments != null); testTracer.TraceStepPending(match, arguments); contextManager.ScenarioContext.PendingSteps.Add( stepFormatter.GetMatchText(match, arguments)); if (contextManager.ScenarioContext.TestStatus < TestStatus.StepDefinitionPending) { contextManager.ScenarioContext.TestStatus = TestStatus.StepDefinitionPending; } } catch (MissingStepDefinitionException) { if (contextManager.ScenarioContext.TestStatus < TestStatus.MissingStepDefinition) { contextManager.ScenarioContext.TestStatus = TestStatus.MissingStepDefinition; } } catch (BindingException ex) { testTracer.TraceBindingError(ex); if (contextManager.ScenarioContext.TestStatus < TestStatus.BindingError) { contextManager.ScenarioContext.TestStatus = TestStatus.BindingError; contextManager.ScenarioContext.TestError = ex; } } catch (Exception ex) { testTracer.TraceError(ex); if (contextManager.ScenarioContext.TestStatus < TestStatus.TestError) { contextManager.ScenarioContext.TestStatus = TestStatus.TestError; contextManager.ScenarioContext.TestError = ex; } if (runtimeConfiguration.StopAtFirstError) { throw; } } finally { if (!isStepSkipped) { OnStepEnd(); } } }
public void TraceStep(StepArgs stepArgs, bool showAdditionalArguments) { }
public string GetStepDescription(StepArgs stepArgs) { return string.Format("{0} {1}", stepArgs.Type, stepArgs.Text); }
private List <BindingMatch> GetMatchesWithoutScopeCheck(StepArgs stepArgs) { return(bindingRegistry.Where(b => b.Type == stepArgs.Type).Select(binding => Match(binding, stepArgs, true, false)).Where(match => match != null).ToList()); }
private BindingMatch GetStepMatch(StepArgs stepArgs) { List <BindingMatch> matches = bindingRegistry .Where(b => b.Type == stepArgs.Type) .Select(binding => Match(binding, stepArgs, true, true)) .Where(match => match != null) .ToList(); if (matches.Count > 1) { // if there are both scoped and non-scoped matches, we take the ones with the higher degree of scope matches int maxScopeMatches = matches.Max(m => m.ScopeMatches); matches.RemoveAll(m => m.ScopeMatches < maxScopeMatches); } if (matches.Count > 1) { // we remove duplicate maches for the same method (take the first from each) matches = matches.GroupBy(m => m.StepBinding.MethodInfo, (methodInfo, methodMatches) => methodMatches.First()).ToList(); } if (matches.Count == 0) { // there were either no regex match or it was filtered out by the param/scope matching // to provide better error message for the param matching error, we re-run // the matching without param check List <BindingMatch> matchesWithoutScopeCheck = GetMatchesWithoutScopeCheck(stepArgs); // if (matchesWithoutScopeCheck.Count > 0) // { // no match, because of scope filter // throw errorProvider.GetNoMatchBecauseOfScopeFilterError(matchesWithoutScopeCheck, stepArgs); // } if (matchesWithoutScopeCheck.Count == 0) { List <BindingMatch> matchesWithoutParamCheck = GetMatchesWithoutParamCheck(stepArgs); if (matchesWithoutParamCheck.Count == 1) { // no ambiguouity, but param error -> execute will find it out return(matchesWithoutParamCheck[0]); } if (matchesWithoutParamCheck.Count > 1) { // ambiguouity, because of param error throw errorProvider.GetAmbiguousBecauseParamCheckMatchError(matchesWithoutParamCheck, stepArgs); } } testTracer.TraceNoMatchingStepDefinition(stepArgs, ObjectContainer.FeatureContext.FeatureInfo.GenerationTargetLanguage, matchesWithoutScopeCheck); ObjectContainer.ScenarioContext.MissingSteps.Add( stepDefinitionSkeletonProvider.GetStepDefinitionSkeleton(stepArgs)); throw errorProvider.GetMissingStepDefinitionError(); } if (matches.Count > 1) { if (RuntimeConfiguration.Current.DetectAmbiguousMatches) { throw errorProvider.GetAmbiguousMatchError(matches, stepArgs); } } return(matches[0]); }
private void ExecuteStep(StepArgs stepArgs) { testTracer.TraceStep(stepArgs, true); HandleBlockSwitch(stepArgs.Type.ToScenarioBlock()); BindingMatch match = null; object[] arguments = null; try { match = GetStepMatch(stepArgs); arguments = GetExecuteArguments(match); if (ObjectContainer.ScenarioContext.TestStatus == TestStatus.OK) { TimeSpan duration = ExecuteStepMatch(match, arguments); if (RuntimeConfiguration.Current.TraceSuccessfulSteps) testTracer.TraceStepDone(match, arguments, duration); } else { testTracer.TraceStepSkipped(); } } catch(PendingStepException) { Debug.Assert(match != null); Debug.Assert(arguments != null); testTracer.TraceStepPending(match, arguments); ObjectContainer.ScenarioContext.PendingSteps.Add( stepFormatter.GetMatchText(match, arguments)); if (ObjectContainer.ScenarioContext.TestStatus < TestStatus.StepDefinitionPending) ObjectContainer.ScenarioContext.TestStatus = TestStatus.StepDefinitionPending; } catch(MissingStepDefinitionException) { if (ObjectContainer.ScenarioContext.TestStatus < TestStatus.MissingStepDefinition) ObjectContainer.ScenarioContext.TestStatus = TestStatus.MissingStepDefinition; } catch(BindingException ex) { testTracer.TraceBindingError(ex); if (ObjectContainer.ScenarioContext.TestStatus < TestStatus.BindingError) { ObjectContainer.ScenarioContext.TestStatus = TestStatus.BindingError; ObjectContainer.ScenarioContext.TestError = ex; } } catch(Exception ex) { testTracer.TraceError(ex); if (ObjectContainer.ScenarioContext.TestStatus < TestStatus.TestError) { ObjectContainer.ScenarioContext.TestStatus = TestStatus.TestError; ObjectContainer.ScenarioContext.TestError = ex; } if (RuntimeConfiguration.Current.StopAtFirstError) throw; } }
public void TraceNoMatchingStepDefinition(StepArgs stepArgs, ProgrammingLanguage targetLanguage, List <BindingMatch> matchesWithoutScopeCheck) { Console.WriteLine("TraceNoMatchingStepDefinition"); }