Skip to content

StepTitle Attribute - Retain original formatting flag #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 35 additions & 37 deletions src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,49 +176,47 @@ private ExecutionOrder FixConsecutiveStep(ExecutionOrder executionOrder)

private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepTitle, MethodInfo methodInfo, StepArgument[] inputArguments, string stepPrefix)
{
Func<string> createTitle = () =>
string Title()
{
var autoFormatText = true;
var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
var name = methodInfo.Name;
var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
if (stepTitleAttribute != null)
{
var titleAttribute = ((StepTitleAttribute) stepTitleAttribute);
name = string.Format(titleAttribute.StepTitle, flatInputArray);
if (titleAttribute.IncludeInputsInStepTitle != null) includeInputsInStepTitle = titleAttribute.IncludeInputsInStepTitle.Value;
autoFormatText = titleAttribute.AutoFormatStepText;
}

var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
var name = methodInfo.Name;
var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
if (stepTitleAttribute != null)
{
var titleAttribute = ((StepTitleAttribute)stepTitleAttribute);
name = string.Format(titleAttribute.StepTitle, flatInputArray);
if (titleAttribute.IncludeInputsInStepTitle != null)
includeInputsInStepTitle = titleAttribute.IncludeInputsInStepTitle.Value;
}
var stepTitle = AppendPrefix(autoFormatText ? Configurator.Scanners.Humanize(name) : name, stepPrefix);

var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(name), stepPrefix);

if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
else if (includeInputsInStepTitle)
{
var parameters = methodInfo.GetParameters();
var stringFlatInputs =
inputArguments
.Select((a, i) => new { ParameterName = parameters[i].Name, Value = a })
.Select(i =>
{
if (_testContext.Examples != null)
{
var matchingHeader = _testContext.Examples.Headers
.SingleOrDefault(header => ExampleTable.HeaderMatches(header, i.ParameterName) ||
ExampleTable.HeaderMatches(header, i.Value.Name));
if (matchingHeader != null)
return string.Format("<{0}>", matchingHeader);
}
return i.Value.Value.FlattenArray();
})
.ToArray();
stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs);
}
if (!string.IsNullOrEmpty(stepTextTemplate))
stepTitle = string.Format(stepTextTemplate, flatInputArray);
else if (includeInputsInStepTitle)
{
var parameters = methodInfo.GetParameters();
var stringFlatInputs = inputArguments.Select((a, i) => new {ParameterName = parameters[i].Name, Value = a})
.Select(i =>
{
if (_testContext.Examples != null)
{
var matchingHeader = _testContext.Examples.Headers.SingleOrDefault(header => ExampleTable.HeaderMatches(header, i.ParameterName) || ExampleTable.HeaderMatches(header, i.Value.Name));
if (matchingHeader != null) return string.Format("<{0}>", matchingHeader);
}

return i.Value.Value.FlattenArray();
})
.ToArray();
stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs);
}

return stepTitle.Trim();
};
return stepTitle.Trim();
}

return new StepTitle(createTitle);
return new StepTitle(Title);
}

private static MethodInfo GetMethodInfo(LambdaExpression stepAction)
Expand Down
12 changes: 5 additions & 7 deletions src/TestStack.BDDfy/StepTitleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ namespace TestStack.BDDfy
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class StepTitleAttribute : Attribute
{
public StepTitleAttribute(string stepTitle)
{
StepTitle = stepTitle;
}

public StepTitleAttribute(string stepTitle, bool includeInputsInStepTitle)
{
public StepTitleAttribute(string stepTitle, bool includeInputsInStepTitle = false, bool autoFormatStepText = false)
{
IncludeInputsInStepTitle = includeInputsInStepTitle;
StepTitle = stepTitle;
AutoFormatStepText = autoFormatStepText;
}

public string StepTitle { get; private set; }

public bool? IncludeInputsInStepTitle { get; private set; }

public bool AutoFormatStepText { get; }
}
}