private CodeMemberMethod CreateScenatioOutlineTestMethod(TestClassGenerationContext generationContext, ScenarioOutline scenarioOutline, ParameterSubstitution paramToIdentifier) { CodeMemberMethod testMethod = CreateMethod(generationContext.TestClass); testMethod.Attributes = MemberAttributes.Public; testMethod.Name = string.Format(TEST_NAME_FORMAT, scenarioOutline.Name.ToIdentifier()); foreach (var pair in paramToIdentifier) { testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), pair.Value)); } testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string[]), SCENARIO_OUTLINE_EXAMPLE_TAGS_PARAMETER)); return(testMethod); }
private CodeExpression GetStringArrayExpression(IEnumerable <string> items, ParameterSubstitution paramToIdentifier) { return(new CodeArrayCreateExpression(typeof(string[]), items.Select(item => GetSubstitutedString(item, paramToIdentifier)).ToArray())); }
private void GenerateScenarioOutlineExamplesAsIndividualMethods(ScenarioOutline scenarioOutline, TestClassGenerationContext generationContext, CodeMemberMethod scenatioOutlineTestMethod, ParameterSubstitution paramToIdentifier) { int exampleSetIndex = 0; foreach (var exampleSet in scenarioOutline.Examples) { bool useFirstColumnAsName = CanUseFirstColumnAsName(exampleSet.TableBody); string exampleSetIdentifier = string.IsNullOrEmpty(exampleSet.Name) ? scenarioOutline.Examples.Count(es => string.IsNullOrEmpty(es.Name)) > 1 ? string.Format("ExampleSet {0}", exampleSetIndex).ToIdentifier() : null : exampleSet.Name.ToIdentifier(); foreach (var example in exampleSet.TableBody.Select((r, i) => new { Row = r, Index = i })) { string variantName = useFirstColumnAsName ? example.Row.Cells.First().Value : string.Format("Variant {0}", example.Index); GenerateScenarioOutlineTestVariant(generationContext, scenarioOutline, scenatioOutlineTestMethod, paramToIdentifier, exampleSet.Name ?? "", exampleSetIdentifier, example.Row, exampleSet.Tags, variantName); } exampleSetIndex++; } }
private CodeExpression GetTableArgExpression(DataTable tableArg, CodeStatementCollection statements, ParameterSubstitution paramToIdentifier) { if (tableArg == null) { return(new CodeCastExpression(typeof(Table), new CodePrimitiveExpression(null))); } tableCounter++; //TODO[Gherkin3]: remove dependency on having the first row as header var header = tableArg.Rows.First(); var body = tableArg.Rows.Skip(1).ToArray(); //Table table0 = new Table(header...); var tableVar = new CodeVariableReferenceExpression("table" + tableCounter); statements.Add( new CodeVariableDeclarationStatement(typeof(Table), tableVar.VariableName, new CodeObjectCreateExpression( typeof(Table), GetStringArrayExpression(header.Cells.Select(c => c.Value), paramToIdentifier)))); foreach (var row in body) { //table0.AddRow(cells...); statements.Add( new CodeMethodInvokeExpression( tableVar, "AddRow", GetStringArrayExpression(row.Cells.Select(c => c.Value), paramToIdentifier))); } return(tableVar); }
private CodeExpression GetDocStringArgExpression(DocString docString, ParameterSubstitution paramToIdentifier) { return(GetSubstitutedString(docString == null ? null : docString.Content, paramToIdentifier)); }
internal void GenerateMethodBodyForNotSkippedScenarios(TestClassGenerationContext generationContext, StepsContainer scenario, CodeMemberMethod testMethod, ParameterSubstitution paramToIdentifier) { if (HasFeatureBackground(generationContext.Feature)) { AddLineDirective(testMethod.Statements, generationContext.Feature.Background); testMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), generationContext.FeatureBackgroundMethod.Name)); } foreach (var scenarioStep in scenario.Steps) { GenerateStep(testMethod, scenarioStep, paramToIdentifier); } }
private void GenerateTestBody(TestClassGenerationContext generationContext, ScenarioDefinition scenario, CodeMemberMethod testMethod, CodeExpression additionalTagsExpression = null, ParameterSubstitution paramToIdentifier = null) { //call test setup //ScenarioInfo scenarioInfo = new ScenarioInfo("xxxx", tags...); CodeExpression tagsExpression; if (additionalTagsExpression == null) { tagsExpression = GetStringArrayExpression(scenario.Tags); } else if (!scenario.HasTags()) { tagsExpression = additionalTagsExpression; } else { // merge tags list // var tags = tags1 // if (tags2 != null) // tags = Enumerable.ToArray(Enumerable.Concat(tags1, tags1)); testMethod.Statements.Add( new CodeVariableDeclarationStatement(typeof(string[]), "__tags", GetStringArrayExpression(scenario.Tags))); tagsExpression = new CodeVariableReferenceExpression("__tags"); testMethod.Statements.Add( new CodeConditionStatement( new CodeBinaryOperatorExpression( additionalTagsExpression, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)), new CodeAssignStatement( tagsExpression, new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Enumerable)), "ToArray", new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Enumerable)), "Concat", tagsExpression, additionalTagsExpression))))); } testMethod.Statements.Add( new CodeVariableDeclarationStatement(typeof(ScenarioInfo), "scenarioInfo", new CodeObjectCreateExpression(typeof(ScenarioInfo), new CodePrimitiveExpression(scenario.Name), tagsExpression))); AddLineDirective(testMethod.Statements, scenario); testMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), generationContext.ScenarioInitializeMethod.Name, new CodeVariableReferenceExpression("scenarioInfo"))); if (HasFeatureBackground(generationContext.Feature)) { AddLineDirective(testMethod.Statements, generationContext.Feature.Background); testMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), generationContext.FeatureBackgroundMethod.Name)); } foreach (var scenarioStep in scenario.Steps) { GenerateStep(testMethod, scenarioStep, paramToIdentifier); } AddLineDirectiveHidden(testMethod.Statements); // call scenario cleanup testMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), generationContext.ScenarioCleanupMethod.Name)); }
private CodeExpression GetMultilineTextArgExpression(string multiLineTextArgument, ParameterSubstitution paramToIdentifier) { return(GetSubstitutedString(multiLineTextArgument, paramToIdentifier)); }
internal void GenerateTestMethodBody(TestClassGenerationContext generationContext, StepsContainer scenario, CodeMemberMethod testMethod, ParameterSubstitution paramToIdentifier, SpecFlowFeature feature) { if (IsIgnoredFeature(feature) || IsIgnoredStepsContainer(scenario)) { GenerateMethodBodyForSkippedScenario(testMethod); } else { GenerateScenarioStartMethodCall(generationContext, testMethod); GenerateMethodBodyForNotSkippedScenarios(generationContext, scenario, testMethod, paramToIdentifier); } }
private CodeExpression GetTableArgExpression(Table tableArg, CodeStatementCollection statements, ParameterSubstitution paramToIdentifier) { if (tableArg == null) { return(new CodeCastExpression(TABLE_TYPE, new CodePrimitiveExpression(null))); } tableCounter++; //Table table0 = new Table(header...); var tableVar = new CodeVariableReferenceExpression("table" + tableCounter); statements.Add( new CodeVariableDeclarationStatement(TABLE_TYPE, tableVar.VariableName, new CodeObjectCreateExpression( TABLE_TYPE, GetStringArrayExpression(tableArg.Header.Cells.Select(c => c.Value), paramToIdentifier)))); foreach (var row in tableArg.Body) { //table0.AddRow(cells...); statements.Add( new CodeMethodInvokeExpression( tableVar, "AddRow", GetStringArrayExpression(row.Cells.Select(c => c.Value), paramToIdentifier))); } return(tableVar); }
private void GenerateStep(CodeMemberMethod testMethod, ScenarioStep scenarioStep, ParameterSubstitution paramToIdentifier) { var testRunnerField = GetTestRunnerExpression(); //testRunner.Given("something"); List <CodeExpression> arguments = new List <CodeExpression>(); arguments.Add( GetSubstitutedString(scenarioStep.Text, paramToIdentifier)); if (scenarioStep.MultiLineTextArgument != null || scenarioStep.TableArg != null) { AddLineDirectiveHidden(testMethod.Statements); arguments.Add( GetMultilineTextArgExpression(scenarioStep.MultiLineTextArgument, paramToIdentifier)); arguments.Add( GetTableArgExpression(scenarioStep.TableArg, testMethod.Statements, paramToIdentifier)); } AddLineDirective(testMethod.Statements, scenarioStep); testMethod.Statements.Add( new CodeMethodInvokeExpression( testRunnerField, scenarioStep.GetType().Name, arguments.ToArray())); }
private void GenerateTestBody(Feature feature, Scenario scenario, CodeExpression additionalTagsExpression, CodeMemberMethod testMethod, CodeMemberMethod testSetup, ParameterSubstitution paramToIdentifier) { //call test setup //ScenarioInfo scenarioInfo = new ScenarioInfo("xxxx", tags...); CodeExpression tagsExpression; if (additionalTagsExpression == null) { tagsExpression = GetStringArrayExpression(scenario.Tags); } else if (scenario.Tags == null) { tagsExpression = additionalTagsExpression; } else { // merge tags list // var tags = tags1 // if (tags2 != null) // tags = Enumerable.ToArray(Enumerable.Concat(tags1, tags1)); testMethod.Statements.Add( new CodeVariableDeclarationStatement(typeof(string[]), "__tags", GetStringArrayExpression(scenario.Tags))); tagsExpression = new CodeVariableReferenceExpression("__tags"); testMethod.Statements.Add( new CodeConditionStatement( new CodeBinaryOperatorExpression( additionalTagsExpression, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)), new CodeAssignStatement( tagsExpression, new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Enumerable)), "ToArray", new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Enumerable)), "Concat", tagsExpression, additionalTagsExpression))))); // tagsExpression = new CodeMethodInvokeExpression( // new CodeTypeReferenceExpression(typeof(Enumerable)), // "ToArray", // new CodeMethodInvokeExpression( // new CodeTypeReferenceExpression(typeof(Enumerable)), // "Concat", // GetStringArrayExpression(scenario.Tags), // additionalTagsExpression)); } testMethod.Statements.Add( new CodeVariableDeclarationStatement(SCENARIOINFO_TYPE, "scenarioInfo", new CodeObjectCreateExpression(SCENARIOINFO_TYPE, new CodePrimitiveExpression(scenario.Title), tagsExpression))); AddLineDirective(testMethod.Statements, scenario); testMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), testSetup.Name, new CodeVariableReferenceExpression("scenarioInfo"))); foreach (var scenarioStep in scenario.Steps) { GenerateStep(testMethod, scenarioStep, paramToIdentifier); } AddLineDirectiveHidden(testMethod.Statements); // call collect errors var testRunnerField = GetTestRunnerExpression(); //testRunner.CollectScenarioErrors(); testMethod.Statements.Add( new CodeMethodInvokeExpression( testRunnerField, "CollectScenarioErrors")); }
private CodeMemberMethod GenerateScenarioOutlineBody(Feature feature, ScenarioOutline scenarioOutline, ParameterSubstitution paramToIdentifier, CodeTypeDeclaration testType, string testMethodName, CodeMemberMethod testSetup) { CodeMemberMethod testMethod = new CodeMemberMethod(); testType.Members.Add(testMethod); testMethod.Attributes = MemberAttributes.Public; testMethod.Name = testMethodName; foreach (var pair in paramToIdentifier) { testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), pair.Value)); } testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string[]), "exampleTags")); var exampleTagsExpression = new CodeVariableReferenceExpression("exampleTags"); GenerateTestBody(feature, scenarioOutline, exampleTagsExpression, testMethod, testSetup, paramToIdentifier); return(testMethod); }
private void GenerateScenarioOutlineExamplesAsIndividualMethods(ScenarioOutline scenarioOutline, TestClassGenerationContext generationContext, CodeMemberMethod scenatioOutlineTestMethod, ParameterSubstitution paramToIdentifier) { int exampleSetIndex = 0; foreach (var exampleSet in scenarioOutline.Examples.ExampleSets) { bool useFirstColumnAsName = CanUseFirstColumnAsName(exampleSet.Table); string exampleSetIdentifier = string.IsNullOrEmpty(exampleSet.Title) ? scenarioOutline.Examples.ExampleSets.Count(es => string.IsNullOrEmpty(es.Title)) > 1 ? string.Format("ExampleSet {0}", exampleSetIndex).ToIdentifier() : null : exampleSet.Title.ToIdentifier(); for (int rowIndex = 0; rowIndex < exampleSet.Table.Body.Length; rowIndex++) { var row = exampleSet.Table.Body[rowIndex]; string variantName = useFirstColumnAsName ? row.Cells[0].Value : string.Format("Variant {0}", rowIndex); GenerateScenarioOutlineTestVariant(generationContext, scenarioOutline, scenatioOutlineTestMethod, paramToIdentifier, exampleSet.Title ?? "", exampleSetIdentifier, row, exampleSet.Tags, variantName); } exampleSetIndex++; } }