private void AddSteps(List<NodeStep> steps, NodeScenario currentScenario, List<string> privateContents)
        {
            foreach (var step in steps)
            {
                List<string> parametersInStep = new List<string>();

                if ((step.Parameters.Count == 0) && (step.Rows.Count == 0))
                {
                    privateContents.Add(string.Format("\t\tvoid {0}();", step.Name));
                }
                else if ((step.Parameters.Count > 0) && (step.Rows.Count == 0))
                {
                    foreach (var Parameter in step.Parameters)
                    {
                        parametersInStep.Add(Parameter.Type + " " + Parameter.Name);
                    }
                    privateContents.Add(string.Format("\t\tvoid {0}({1}", step.Name, string.Join(", ", parametersInStep) + ");"));
                }
                else if ((step.Parameters.Count == 0) && (step.Rows.Count > 0))
                {
                    privateContents.Add(string.Format("\t\tvoid {0}({1});", step.Name, LanguageConfig.TableDeclaration));
                }
                else
                {
                    foreach (var Parameter in step.Parameters)
                    {
                        parametersInStep.Add(Parameter.Type + " " + Parameter.Name);
                    }
                    privateContents.Add(string.Format("\t\tvoid {0}({1}, {2}", step.Name, LanguageConfig.TableDeclaration, string.Join(", ", parametersInStep) + ");"));
                }
            }
        }
        public void StepGeneratorCreatesOneScenarioOneStep()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            //Add step
            NodeStep step1 = new NodeStep("GivenMethod1");

            //Add scenario
            NodeScenario scenario1 = new NodeScenario("Scenario1");
            scenario1.Steps.Add(step1);

            //Add feature
            NodeFeature feature1 = new NodeFeature("Feature1");
            feature1.Scenarios.Add(scenario1);
            features.Add(feature1);

            var files = GeneratorFactory.Generate(GeneratorType.StepDefinitionGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"Feature1.h\"",
                "",
                "namespace CppUnitTest",
                "{",
                "\tvoid Feature1::GivenMethod1()",
                "\t{",
                "\t\tAssert::Fail(L\"Pending implementation...\");",
                "\t}",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
 private void AddScenario(NodeScenario scenario, List<NodeHook> featureHooks, List<string> publicContents)
 {
     string temp = string.Empty;
     foreach (var scenarioHook in scenario.Hooks)
     {
         temp += "_" + scenarioHook.Name;
     }
     publicContents.Add(string.Format("\t\t{0};", LanguageConfig.ScenarioMethodDeclaration(temp, scenario.Name)));
 }
        public static IList<NodeFeature> FeatureWithScenarioAndNoStep()
        {
            IList<NodeFeature> features = new List<NodeFeature>();
            NodeFeature featureWithScenario = new NodeFeature("Feature1");
            NodeScenario scenario = new NodeScenario("Scenario1");
            featureWithScenario.Scenarios.Add(scenario);    // scenario to feature
            features.Add(featureWithScenario);              // feature to feature list

            return features;
        }
        public void HeaderGeneratorCreatesOneFeatureWithOneTable()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();
            List<NodeHook> featureHooks = new List<NodeHook>();

            List<string[]> rows = new List<string[]>();

            //Create row array & add
            string[] row1 = new string[] { "a", "b", "c" };
            string[] row2 = new string[] { "1", "2", "3" };
            rows.Add(row1);
            rows.Add(row2);

            //Create step & add
            NodeStep step1 = new NodeStep("GivenStep1WithTable");
            step1.Rows = rows;

            //Create scenario & add
            NodeScenario scenario1 = new NodeScenario("MyScenario1", new List<NodeHook>() { new NodeHook("MyFeatureHook1"), new NodeHook("MyScenarioHook1") });
            scenario1.Steps.Add(step1);

            //Create feature & add
            NodeFeature feature1 = new NodeFeature("MyFeature1", new List<NodeHook>() { new NodeHook("MyFeatureHook1") });
            feature1.Scenarios.Add(scenario1);
            features.Add(feature1);

            //Call output generator
            var files = GeneratorFactory.Generate(GeneratorType.HeaderGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"CppUnitTest.h\"",
                "#include \"CppUnitTestHooks.h\"",
                "#include \"trim.hpp\"",
                "#include <vector>",
                "",
                "using namespace Microsoft::VisualStudio::CppUnitTestFramework;",
                "using namespace std;",
                "",
                "namespace CppUnitTest",
                "{",
                "\tTEST_CLASS(MyFeature1)",
                "\t{",
                "\tpublic:",
                "\t\tTEST_CLASS_HOOK_MYFEATUREHOOK1()",
                "\t\tTEST_METHOD_HOOK_MYFEATUREHOOK1_MYSCENARIOHOOK1(MyScenario1);",
                "",
                "\tprivate:",
                "\t\tvoid GivenStep1WithTable(std::vector<std::vector<std::string>> table, int rows, int cols);",
                "\t};",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
 private static NodeExamples GetExamples(NodeScenario scenario)
 {
     if (scenario is NodeScenarioOutline)
     {
         return ((NodeScenarioOutline)scenario).Examples;
     }
     else
     {
         return new NodeExamples();
     }
 }
        public void HeaderGeneratorCreatesOneFeatureWithDuplicateSteps()
        {
            IList<NodeFeature> features = new List<NodeFeature>();

            //Create scenario & add
            NodeScenario scenario1 = new NodeScenario("MyScenario1");
            scenario1.Steps.Add(new NodeStep("GivenAMethod1"));
            scenario1.Steps.Add(new NodeStep("WhenUsingAMethod1"));
            scenario1.Steps.Add(new NodeStep("ThenUsingAMethod1"));
            scenario1.Steps.Add(new NodeStep("GivenAMethod1"));
            scenario1.Steps.Add(new NodeStep("WhenUsingAMethod1"));
            scenario1.Steps.Add(new NodeStep("ThenUsingAMethod1"));

            //Create feature & add
            NodeFeature feature1 = new NodeFeature("MyFeature1");
            feature1.Scenarios.Add(scenario1);
            features.Add(feature1);

            //Call output generator
            var files = GeneratorFactory.Generate(GeneratorType.HeaderGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"CppUnitTest.h\"",
                "#include \"CppUnitTestHooks.h\"",
                "#include \"trim.hpp\"",
                "#include <vector>",
                "",
                "using namespace Microsoft::VisualStudio::CppUnitTestFramework;",
                "using namespace std;",
                "",
                "namespace CppUnitTest",
                "{",
                "\tTEST_CLASS(MyFeature1)",
                "\t{",
                "\tpublic:",
                "\t\tTEST_METHOD(MyScenario1);",
                "",
                "\tprivate:",
                "\t\tvoid GivenAMethod1();",
                "\t\tvoid WhenUsingAMethod1();",
                "\t\tvoid ThenUsingAMethod1();",
                "\t};",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
        public void HeaderGeneratorCreatesOneFeatureTwoFeatureHooksTwoScenarioHooksAndTwoSteps()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            List<NodeHook> featureHooks = new List<NodeHook>() {
                new NodeHook("featurehook1"),
                new NodeHook("featurehook2")
            };

            List<NodeHook> scenarioHooks = new List<NodeHook>() {
                new NodeHook("featurehook1"),
                new NodeHook("featurehook2"),
                new NodeHook("scenariohook1"),
                new NodeHook("scenariohook2")
            };

            //Creates Step 1
            NodeStep step1 = new NodeStep("GivenMethod1");

            //Creates Step 2
            NodeStep step2 = new NodeStep("GivenMethod2");

            //Creates Scenario 1
            NodeScenario scenario1 = new NodeScenario("TestScenario1", scenarioHooks);
            scenario1.Steps.Add(step1);
            scenario1.Steps.Add(step2);

            //Creates Feature 1
            NodeFeature testFeature = new NodeFeature("TestFeature1", featureHooks);
            testFeature.Scenarios.Add(scenario1);

            features.Add(testFeature);

            var files = GeneratorFactory.Generate(GeneratorType.HeaderGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"CppUnitTest.h\"",
                "#include \"CppUnitTestHooks.h\"",
                "#include \"trim.hpp\"",
                "#include <vector>",
                "",
                "using namespace Microsoft::VisualStudio::CppUnitTestFramework;",
                "using namespace std;",
                "",
                "namespace CppUnitTest",
                "{",
                "\tTEST_CLASS(TestFeature1)",
                "\t{",
                "\tpublic:",
                "\t\tTEST_CLASS_HOOK_FEATUREHOOK1()",
                "\t\tTEST_CLASS_HOOK_FEATUREHOOK2()",
                "\t\tTEST_METHOD_HOOK_FEATUREHOOK1_FEATUREHOOK2_SCENARIOHOOK1_SCENARIOHOOK2(TestScenario1);",
                "",
                "\tprivate:",
                "\t\tvoid GivenMethod1();",
                "\t\tvoid GivenMethod2();",
                "\t};",
                "}"
            };

            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
        public void HeaderGeneratorCreatesOneFeatureOneScenarioAndOneStep()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            //Creates Step 1 & Adds to list
            NodeStep step1 = new NodeStep("GivenMethod1");

            //Creates Scenario 1
            NodeScenario scenario1 = new NodeScenario("TestScenario1");
            scenario1.Steps.Add(step1);

            //Creates Feature & Adds to list
            NodeFeature feature = new NodeFeature("TestFeature1");
            feature.Scenarios.Add(scenario1);
            features.Add(feature);

            var files = GeneratorFactory.Generate(GeneratorType.HeaderGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"CppUnitTest.h\"",
                "#include \"CppUnitTestHooks.h\"",
                "#include \"trim.hpp\"",
                "#include <vector>",
                "",
                "using namespace Microsoft::VisualStudio::CppUnitTestFramework;",
                "using namespace std;",
                "",
                "namespace CppUnitTest",
                "{",
                "\tTEST_CLASS(TestFeature1)",
                "\t{",
                "\tpublic:",
                "\t\tTEST_METHOD(TestScenario1);",
                "",
                "\tprivate:",
                "\t\tvoid GivenMethod1();",
                "\t};",
                "}"
            };

            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
 private void RepeatStepsIfScenarioOutline(NodeScenario scenario)
 {
     var examples = GetExamples(scenario);
     int repeat = 1;
     _tableNumberInScenario = -1;
     int countOfRowsExcludingHeaderRow = examples.Rows.Count - 1;
     do
     {
         foreach (var step in scenario.Steps)
         {
             BuildStepMethodCall(step, examples, repeat);
         }
         repeat++;
     } while (repeat <= countOfRowsExcludingHeaderRow);
 }
        public void StepGeneratorCreatesTwoFeaturesWithScenario()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            NodeFeature feature = new NodeFeature("Feature1");
            NodeScenario scenario = new NodeScenario("Scenario1");
            scenario.Steps.Add(new NodeStep("GivenMethod"));
            feature.Scenarios.Add(scenario);
            features.Add(feature);
            feature = new NodeFeature("Feature2");
            scenario = new NodeScenario("Scenario2");
            scenario.Steps.Add(new NodeStep("GivenMethod"));
            feature.Scenarios.Add(scenario);
            features.Add(feature);

            var files = GeneratorFactory.Generate(GeneratorType.StepDefinitionGenerator, features);

            Assert.AreEqual(2, files.Count, "File count mismatch.");
            for (int i = 0; i < files.Count; i++)
            {
                string[] stringsExpected = new string[] {
                    string.Format("#include \"Feature{0}.h\"", i+1),
                    "",
                    "namespace CppUnitTest",
                    "{",
                    string.Format("\tvoid Feature{0}::GivenMethod()", i+1),
                    "\t{",
                    "\t\tAssert::Fail(L\"Pending implementation...\");",
                    "\t}",
                    "}"
                };
                AssertExt.ContentsOfStringArray(stringsExpected, files[i]);
            }
        }
        public void HeaderGeneratorCreatesOneFeatureOneScenarioOneStepAndTwoParameters()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            //Creates Gherkin Step 1
            TokenGherkinStep tokenStep1 = new TokenGherkinStep();
            tokenStep1.MethodName = "GivenMethod1";
            List<string> tokenParameters1 = new List<string>();
            string parameter1 = "";
            tokenParameters1.Add(parameter1);
            tokenStep1.ParameterTokens = tokenParameters1;

            //Creates Parameter For Step 1
            Parameter p1 = new Parameter();
            p1.Name = "Parameter1";
            p1.Type = "string";
            p1.Value = "ValueOfParameter1";
            Parameter p2 = new Parameter();
            p2.Name = "Parameter2";
            p2.Type = "int";
            p2.Value = "2";

            //Creates Step 1 & Adds to list
            NodeStep step1 = new NodeStep("GivenMethod1");
            step1.Parameters.Add(p1);
            step1.Parameters.Add(p2);

            //Creates Scenario 1
            NodeScenario scenario1 = new NodeScenario("TestScenario1");
            scenario1.Steps.Add(step1);

            //Creates Feature & Adds to list
            NodeFeature feature = new NodeFeature("TestFeature1");
            feature.Scenarios.Add(scenario1);
            features.Add(feature);

            var files = GeneratorFactory.Generate(GeneratorType.HeaderGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"CppUnitTest.h\"",
                "#include \"CppUnitTestHooks.h\"",
                "#include \"trim.hpp\"",
                "#include <vector>",
                "",
                "using namespace Microsoft::VisualStudio::CppUnitTestFramework;",
                "using namespace std;",
                "",
                "namespace CppUnitTest",
                "{",
                "\tTEST_CLASS(TestFeature1)",
                "\t{",
                "\tpublic:",
                "\t\tTEST_METHOD(TestScenario1);",
                "",
                "\tprivate:",
                "\t\tvoid GivenMethod1(string Parameter1, int Parameter2);",
                "\t};",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
        public void StepGeneratorCreatesOneScenarioOneStepOneParameterAndOneRow()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            //Add parameter
            Parameter p1 = new Parameter();
            p1.Name = "Parameter1";
            p1.Type = "string";
            p1.Value = "ValueOfParameter1";

            //Add step
            NodeStep step1 = new NodeStep("GivenMethod1");
            step1.Parameters.Add(p1);
            step1.Rows = new List<string[]>() {
                new [] { "a", "b", "c" }
            };

            //Add scenario
            NodeScenario scenario1 = new NodeScenario("Scenario1");
            scenario1.Steps.Add(step1);

            //Add feature
            NodeFeature feature1 = new NodeFeature("Feature1");
            feature1.Scenarios.Add(scenario1);
            features.Add(feature1);

            var files = GeneratorFactory.Generate(GeneratorType.StepDefinitionGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"Feature1.h\"",
                "",
                "namespace CppUnitTest",
                "{",
                "\tvoid Feature1::GivenMethod1(std::vector<std::vector<std::string>> table, int rows, int cols, string Parameter1)",
                "\t{",
                "\t\tAssert::Fail(L\"Pending implementation...\");",
                "\t}",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
        public void StepGeneratorCreatesOneScenarioOneStepTwoParameters()
        {
            IList<NodeFeature> features;
            features = new List<NodeFeature>();

            //Add parameter 1
            Parameter p1 = new Parameter();
            p1.Name = "Parameter1";
            p1.Type = "string";
            p1.Value = "ValueOfParameter1";

            //Add parameter 2
            Parameter p2 = new Parameter();
            p2.Name = "Parameter2";
            p2.Type = "int";
            p2.Value = "2";

            //Add step
            NodeStep step1 = new NodeStep("GivenMethod1");
            step1.Parameters.Add(p1);
            step1.Parameters.Add(p2);

            //Add scenario
            NodeScenario scenario1 = new NodeScenario("Scenario1");
            scenario1.Steps.Add(step1);

            //Add feature
            NodeFeature feature1 = new NodeFeature("Feature1");
            feature1.Scenarios.Add(scenario1);
            features.Add(feature1);

            var files = GeneratorFactory.Generate(GeneratorType.StepDefinitionGenerator, features);

            string[] stringsExpected = new string[] {
                "#include \"Feature1.h\"",
                "",
                "namespace CppUnitTest",
                "{",
                "\tvoid Feature1::GivenMethod1(string Parameter1, int Parameter2)",
                "\t{",
                "\t\tAssert::Fail(L\"Pending implementation...\");",
                "\t}",
                "}"
            };
            AssertExt.ContentsOfStringArray(stringsExpected, files[0]);
        }
        public List<NodeFeature> Load(string[] contents)
        {
            bool isBuildingExampleTable = false;
            bool isHeaderRow = false;

            // since we read top to bottom, sometimes we don't know if a hook is for the next scenario or a new feature
            foreach (string line in contents)
            {
                string formattedLine = line.Trim();
                string label = GherkinParser.DetermineLabel(formattedLine);

                if (label != EnumNames.slim)
                {
                    isBuildingExampleTable = false; // reset at start of new label other than slims
                }

                switch (label)
                {
                    case EnumNames.hook:
                        AddHook(formattedLine);
                        break;

                    case EnumNames.Feature:
                        AddFeature(formattedLine, label);
                        break;

                    case EnumNames.Scenario:
                        lastScenario = new NodeScenario(GherkinParser.ParseNameWithLabel(formattedLine, label), scenarioHooks);
                        AddScenario(lastScenario);
                        break;

                    case EnumNames.ScenarioOutline:
                        lastScenario = new NodeScenarioOutline(GherkinParser.ParseNameWithLabel(formattedLine, label), scenarioHooks);
                        AddScenario(lastScenario);
                        break;

                    case EnumNames.Examples:
                        isBuildingExampleTable = true;
                        isHeaderRow = true;
                        break;

                    case EnumNames.Given:
                    case EnumNames.When:
                    case EnumNames.Then:
                    case EnumNames.And:
                        AddStep(formattedLine);
                        break;

                    case EnumNames.slim:
                        if (isHeaderRow)
                        {
                            formattedLine = GherkinParser.RemoveWhiteSpace(formattedLine);
                        }
                        if (isBuildingExampleTable)
                        {
                            AddExampleRow(formattedLine);
                        }
                        else
                        {
                            AddTableRow(formattedLine);
                        }
                        isHeaderRow = false;
                        break;

                    default: // do nothing
                        break;
                }
            }

            return features;
        }
        private void MakePreviousScenarioHookAFeatureHook()
        {
            if (hooksToAdd == null)
            {
                return;
            }
            foreach (var hook in hooksToAdd)
            {
                scenarioHooks.Remove(hook);
            }
            lastScenario = null;
            lastStep = null;

            featureHooks = new List<NodeHook>();
            scenarioHooks = new List<NodeHook>();
            AddDistinctHooksByName(featureHooks, hooksToAdd);
            AddDistinctHooksByName(scenarioHooks, hooksToAdd);
        }
 private void AddScenario(NodeScenario scenario)
 {
     MakePreviousScenarioHookAFeatureHookHack = false; // any preceding hooks belong to this scenario
     lastFeature.Scenarios.Add(scenario);
 }