示例#1
0
        private void AddStep(StringBuilder content, string keyword, string keywordType = null, string[] soHeaders = null)
        {
            StepCount++;

            keywordType = keywordType ?? keyword;
            if (!stepDefinitions.TryGetValue(keywordType, out var sdList))
            {
                throw new Exception("keyword not found: " + keywordType);
            }
            var stepDef = sdList[LoremIpsum.Rnd.Next(sdList.Length)];

            content.AppendLine($"  {keyword} {GetStepText(stepDef, soHeaders)}");
            if (stepDef.DataTable)
            {
                int cellCount = LoremIpsum.Rnd.Next(4) + 2;
                AppendTable(content, cellCount);
            }
            else if (stepDef.DocString)
            {
                content.AppendLine($"    \"\"\"");
                content.AppendLine($"    {LoremIpsum.GetShortText(5)}");
                content.AppendLine($"    {LoremIpsum.GetShortText(4)}");
                content.AppendLine($"    {LoremIpsum.GetShortText(6)}");
                content.AppendLine($"    {LoremIpsum.GetShortText(5)}");
                content.AppendLine($"    \"\"\"");
            }
        }
示例#2
0
        private IEnumerable <StepDef> GetStepDefinitionList(int stepDefinitionCount)
        {
            var result = new List <StepDef>();

            while (result.Count < stepDefinitionCount)
            {
                var paramList = new List <string>();
                var stepDef   = new StepDef
                {
                    Regex = string.Join(" ",
                                        LoremIpsum.GetWords(LoremIpsum.Rnd.Next(3) + 3).Select(s => ConvertToParam(s, ref paramList))),
                    DataTable = LoremIpsum.Rnd.Next(8) == 0,
                    DocString = LoremIpsum.Rnd.Next(16) == 0,
                    Keyword   = GetKeyword(result.Count)
                };
                stepDef.StepTextParams = paramList;

                var sampleText = GetStepText(stepDef, null);
                if (result.Where(sd => sd.Keyword == stepDef.Keyword).Any(sd => Regex.IsMatch(sampleText, "^" + sd.Regex + "$")))
                {
                    continue;
                }

                result.Add(stepDef);
            }
            return(result);
        }
示例#3
0
        private static void AppendTable(StringBuilder content, int cellCount, string[] headers = null)
        {
            var rows = new List <string[]>();

            headers = headers ?? LoremIpsum.GetUniqueWords(cellCount);
            rows.Add(headers);
            for (int i = 0; i < 4; i++)
            {
                rows.Add(LoremIpsum.GetWords(cellCount));
            }

            var cellWiths = Enumerable.Range(0, cellCount)
                            .Select(i => rows.Max(r => r[i].Length)).ToArray();

            foreach (var row in rows)
            {
                content.Append("    | ");
                for (int i = 0; i < cellCount; i++)
                {
                    content.Append(row[i].PadRight(cellWiths[i]));
                    content.Append(" | ");
                }
                content.AppendLine();
            }
        }
示例#4
0
        public string GenerateFeatureFileContent(int scenarioCount, int scenarioOutlineCount = 0, string featureTitle = null)
        {
            var content = new StringBuilder();

            content.AppendLine($"Feature: {featureTitle ?? ToTitle(LoremIpsum.GetShortText())}");
            content.AppendLine();
            content.AppendLine(LoremIpsum.GetShortText());
            content.AppendLine(LoremIpsum.GetShortText());
            content.AppendLine();

            var scenarioDefs = LoremIpsum.Randomize(
                Enumerable.Range(0, scenarioCount).Select(i => "S")
                .Concat(Enumerable.Range(0, scenarioOutlineCount).Select(i => "O")));

            for (int i = 0; i < scenarioDefs.Length; i++)
            {
                if (scenarioDefs[i] == "S")
                {
                    GenerateScenario(content);
                }
                else
                {
                    GenerateScenarioOutline(content);
                }
            }

            return(content.ToString());
        }
示例#5
0
        private string GenerateStepDefClass(string folder, IEnumerable <StepDef> stepDefs)
        {
            var className = ToPascalCase(LoremIpsum.GetShortText()) + "Steps";
            var filePath  = Path.Combine(folder, className + ".cs");

            File.WriteAllText(filePath, GenerateStepDefClassContent(stepDefs, className));
            return(filePath);
        }
示例#6
0
        private void GenerateScenario(StringBuilder content)
        {
            content.AppendLine(LoremIpsum.GetShortText(LoremIpsum.Rnd.Next(3), "@"));
            content.AppendLine($"Scenario: {ToTitle(LoremIpsum.GetShortText())}");

            AddSteps(content);
            content.AppendLine();
        }
示例#7
0
        public string GenerateFeatureFile(string folder, int scenarioCount, int scenarioOutlineCount = 0)
        {
            var featureTitle = ToTitle(LoremIpsum.GetShortText());
            var fileName     = $"{ToPascalCase(featureTitle)}.feature";
            var filePath     = Path.Combine(folder, fileName);

            File.WriteAllText(filePath, GenerateFeatureFileContent(scenarioCount, scenarioOutlineCount, featureTitle));
            return(filePath);
        }
示例#8
0
        private void GenerateScenarioOutline(StringBuilder content)
        {
            var headers = LoremIpsum.GetUniqueWords(3);

            content.AppendLine(LoremIpsum.GetShortText(LoremIpsum.Rnd.Next(3), "@"));
            content.AppendLine($"Scenario Outline: {ToTitle(LoremIpsum.GetShortText())}");
            AddSteps(content, headers);
            content.AppendLine($"Examples: {ToTitle(LoremIpsum.GetShortText())}");
            AppendTable(content, 3, headers);
            content.AppendLine();
        }
示例#9
0
        public List <string> GenerateStepDefClasses(string targetFolder, int stepDefPerClassCount)
        {
            var sdList     = LoremIpsum.Randomize(stepDefinitions.SelectMany(g => g.Value));
            int startIndex = 0;
            var result     = new List <string>();

            result.Add(Path.Combine(targetFolder, "..", "AutomationStub.cs"));

            while (startIndex < sdList.Length)
            {
                result.Add(
                    GenerateStepDefClass(targetFolder,
                                         sdList.Skip(startIndex).Take(Math.Min(stepDefPerClassCount, sdList.Length - startIndex))));
                startIndex += stepDefPerClassCount;
            }

            return(result);
        }
示例#10
0
        private string GenerateStepDefClassContent(IEnumerable <StepDef> stepDefs, string className)
        {
            var content = new StringBuilder();

            content.AppendLine("using System;");
            content.AppendLine("using TechTalk.SpecFlow;");
            content.AppendLine("namespace DeveroomSample.StepDefinitions");
            content.AppendLine("{");
            content.AppendLine("    [Binding]");
            content.AppendLine($"    public class {className}");
            content.AppendLine("    {");

            foreach (var stepDef in stepDefs)
            {
                var asyncPrefix = stepDef.Async ? "async " : "";
                content.AppendLine($"        [{stepDef.Keyword}(@\"{stepDef.Regex.Replace("\"", "\"\"")}\")]");
                content.AppendLine($"        public {asyncPrefix}void {stepDef.Keyword}{ToPascalCase(LoremIpsum.GetShortText())}({GetParams(stepDef)})");
                content.AppendLine("        {");
                content.AppendLine($"           AutomationStub.DoStep({string.Join(", ", stepDef.Params.Select((p, i) => $"p{i}"))});");
                if (stepDef.Async)
                {
                    content.AppendLine($"           await System.Threading.Tasks.Task.Delay(200);");
                }
                content.AppendLine("        }");
                content.AppendLine();
            }

            content.AppendLine("    }");
            content.AppendLine("}");
            return(content.ToString());
        }
示例#11
0
        private string GetStepText(StepDef stepDef, string[] soHeaders)
        {
            var result = stepDef.Regex;

            result = result.Replace(NumberPattern, LoremIpsum.Rnd.Next(2009).ToString());
            result = Regex.Replace(result, Regex.Escape(StringPattern), me => "\"" + GetSOPlaceHolder(soHeaders, LoremIpsum.GetShortText(LoremIpsum.Rnd.Next(5) + 1)) + "\"");
            //result = result.Replace(StringPattern, "\"" + GetSOPlaceHolder(soHeaders, LoremIpsum.GetShortText(LoremIpsum.Rnd.Next(5) + 1)) + "\"");
            return(result);
        }