private void ProcessScenarioLine(ScenarioStyle style, string line, ref StepType stepType, StringBuilder builder) { if (stepType == StepType.When) { stepType = StepType.Then; } if (line.StartsWith("When")) { stepType = StepType.When; } const string tagToken = "@"; if (line.StartsWith(tagToken)) { var tags = line.Split('@'); foreach (var tag in tags .Select(t => t.Trim().Trim('@')) .Where(t => !string.IsNullOrWhiteSpace(t))) { builder.AppendLine($" [Tag(\"{tag}\")]"); } return; } const string scenarioToken = "Scenario:"; if (line.StartsWith(scenarioToken)) { if (style == ScenarioStyle.Classic) builder.AppendLine(" [Scenario(Feature.Unknown)]"); string className = Sanitize(line.Substring(scenarioToken.Length)); builder.AppendLine($" public class {className} : {GetScenarioTypeName(style)}"); builder.AppendLine(" {"); if (style == ScenarioStyle.Fluent) { builder.AppendLine($" public {className}()"); builder.AppendLine(" {"); builder.AppendLine(" TODO"); builder.AppendLine(" }"); builder.AppendLine(); } return; } if (line.StartsWith("-> done: ")) { return; } var methodName = ProcessStepLine(line, stepType); if (style == ScenarioStyle.Classic) builder.AppendLine($" [{stepType}]"); builder.AppendLine($" {GetAccessModifierForStep(style)}void {methodName}()"); builder.AppendLine(" {"); builder.AppendLine(" }"); builder.AppendLine(); }
static string GetScenarioTypeName(ScenarioStyle style) { switch (style) { case ScenarioStyle.Classic: return "Test"; case ScenarioStyle.Fluent: return "FluentTest"; default: throw new NotSupportedException($"{style} is not supported"); } }
static string GetAccessModifierForStep(ScenarioStyle style) { switch (style) { case ScenarioStyle.Classic: return "public "; case ScenarioStyle.Fluent: return ""; default: throw new NotSupportedException($"{style} is not supported"); } }
void ProcessScenario(ScenarioStyle style) { var builder = new StringBuilder(); var stepType = StepType.Given; foreach (var line in _textBox.Text.Split(Environment.NewLine.ToCharArray()) .Select(t => t.Trim()) .Where(t => !string.IsNullOrWhiteSpace(t))) { ProcessScenarioLine(style, line, ref stepType, builder); } // remove the extra whitespace line after methods builder.Remove(builder.Length - 2, 2); builder.AppendLine(" }"); var scenario = builder.ToString(); Clipboard.SetText(scenario); }