Skip to content

Commit

Permalink
Generate proper scenario name for camel/pascalcase test naming
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Apr 2, 2022
1 parent 5acad27 commit ae4c26a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/NScenario.Demo/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ await scenario.Step("This is the third step", () =>
});
}



[Test]
public async Task shouldPresentBASICScenarioWithCamelCaseTitle()
{
var scenario = TestScenarioFactory.Default();

await scenario.Step("This is the first step", () =>
{
// Here comes the logic
});

await scenario.Step("This is the second step", () =>
{
// Here comes the logic
});

await scenario.Step("This is the third step", () =>
{
// Here comes the logic
});
}

[TestCase(false)]
[TestCase(true)]
public async Task should_present_basic_scenario_with_explicit_title(bool someFlag)
Expand Down
8 changes: 6 additions & 2 deletions src/NScenario/StepExecutors/OutputScenarioStepExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NScenario.OutputWriters;

Expand Down Expand Up @@ -47,9 +49,11 @@ private void TryWriteScenarioTitle(string scenario)
{
if (_startedScenarios.TryAdd(scenario, true))
{
var scenarioTitle = scenario.Replace("_", " ");
_scenarioOutputWriter.WriteScenarioTitle(scenarioTitle);

_scenarioOutputWriter.WriteScenarioTitle(scenario);
}
}


}
}
18 changes: 15 additions & 3 deletions src/NScenario/TestScenarioFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using NScenario.OutputWriters;
using NScenario.StepExecutors;

Expand All @@ -13,21 +15,31 @@ public static class TestScenarioFactory

public static ITestScenario Default(TextWriter outputWriter = null, string scenarioPrefix = null, string stepPrefix = null, [CallerMemberName] string testMethodName = "", string title = null)
{
title ??= testMethodName;
title ??= GenerateScenarioTitle(testMethodName);
EnsureTestScenarioTitleUniqueness(title);
var selectedOutputWriter = outputWriter != null ? new StreamScenarioOutputWriter(outputWriter) : DefaultScenarioOutputWriter;
var stepExecutor = BuildScenarioStepExecutor(selectedOutputWriter, scenarioPrefix, stepPrefix);
return new TestScenario(stepExecutor, title);
}

public static ITestScenario Default(IScenarioOutputWriter outputWriter, string scenarioPrefix = null, string stepPrefix = null, [CallerMemberName] string testMethodName = "", string title = null)
{
title ??= testMethodName;
title ??= GenerateScenarioTitle(testMethodName);
var selectedOutputWriter = outputWriter;
var stepExecutor = BuildScenarioStepExecutor(selectedOutputWriter, scenarioPrefix, stepPrefix);
return new TestScenario(stepExecutor, title ?? testMethodName);
}

private static readonly Regex InWordBreakPattern = new Regex("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])", RegexOptions.Compiled);

private static readonly Regex WordSplitPattern = new Regex(@"[^a-zA-Z0-9]", RegexOptions.Compiled);

private static string GenerateScenarioTitle(string scenario)
{
var parts = WordSplitPattern.Split(scenario).Select(word => InWordBreakPattern.Split(word)).SelectMany(x => x).Where(x => !string.IsNullOrWhiteSpace(x));
var scenarioTitle = string.Join(" ", parts);
return scenarioTitle;
}

private static readonly HashSet<string> ScenarioTitles = new HashSet<string>();

private static void EnsureTestScenarioTitleUniqueness(string scenarioTitle)
Expand Down

0 comments on commit ae4c26a

Please sign in to comment.