Skip to content
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

Add an ability to create custom formatter for step parameters #330

Merged
Merged
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
40 changes: 40 additions & 0 deletions Allure.NUnit.Examples/AllureStepArgumentFormattingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Allure.Net.Commons;
using NUnit.Allure.Attributes;
using NUnit.Framework;

namespace Allure.NUnit.Examples;

[AllureSuite("Tests - Steps")]
public class AllureStepArgumentFormattingTest : BaseTest
{
[AllureStep("Step with params #{0}")]
private static void StepWithCustomClassParams(CustomClass firstParam)
{
Console.WriteLine(firstParam);
}

[OneTimeSetUp]
public static void OneTimeSetUp()
{
AllureLifecycle.Instance.AddTypeFormatter(new CustomClassFormatter());
}

[Test]
[AllureName("Test with custom formatting for step argument")]
public void StepWithCustomParamTest()
{
StepWithCustomClassParams(new CustomClass { I = 10, S = "string" });
}

private class CustomClass
{
public int I { get; init; }
public string S { get; init; }
}

private class CustomClassFormatter : TypeFormatter<CustomClass>
{
public override string Format(CustomClass value) => $"___{value.S} + {value.I}___";
}
}
12 changes: 10 additions & 2 deletions Allure.NUnit/Core/Steps/AllureStepAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using Allure.Net.Commons;
using AspectInjector.Broker;
Expand All @@ -16,12 +17,19 @@ public object WrapStep(
[Argument(Source.Arguments)] object[] arguments,
[Argument(Source.Target)] Func<object[], object> method)
{
var parameterInfos = methodBase.GetParameters();
var stepName = methodBase.GetCustomAttribute<AllureStepAttribute>().StepName;

var typeFormatters = AllureLifecycle.Instance.TypeFormatters;

for (var i = 0; i < arguments.Length; i++)
{

stepName = stepName?.Replace("{" + i + "}", arguments[i]?.ToString() ?? "null");
var parameterType = parameterInfos.ElementAtOrDefault(i)?.ParameterType;
var typeFormatter = parameterType is null || !typeFormatters.ContainsKey(parameterType)
? null
: typeFormatters[parameterType];
var formattedArgument = typeFormatter?.Format(arguments[i]) ?? arguments[i]?.ToString() ?? "null";
stepName = stepName?.Replace("{" + i + "}", formattedArgument);
}

var stepResult = string.IsNullOrEmpty(stepName)
Expand Down
13 changes: 13 additions & 0 deletions Allure.Net.Commons/AllureLifecycle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -15,6 +17,11 @@ namespace Allure.Net.Commons
{
public class AllureLifecycle
{
private readonly Dictionary<Type, ITypeFormatter> typeFormatters = new();

public IReadOnlyDictionary<Type, ITypeFormatter> TypeFormatters =>
new ReadOnlyDictionary<Type, ITypeFormatter>(typeFormatters);

private static readonly object Lockobj = new();
private static AllureLifecycle instance;
private readonly AllureStorage storage;
Expand Down Expand Up @@ -60,6 +67,12 @@ public static AllureLifecycle Instance
}
}

public void AddTypeFormatter<T>(TypeFormatter<T> typeFormatter) =>
AddTypeFormatterImpl(typeof(T), typeFormatter);

private void AddTypeFormatterImpl(Type type, ITypeFormatter formatter) =>
typeFormatters[type] = formatter;

#region TestContainer

public virtual AllureLifecycle StartTestContainer(TestResultContainer container)
Expand Down
16 changes: 16 additions & 0 deletions Allure.Net.Commons/TypeFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Allure.Net.Commons;

public interface ITypeFormatter
{
string Format(object value);
}

public abstract class TypeFormatter<T> : ITypeFormatter
{
public abstract string Format(T value);

string ITypeFormatter.Format(object value)
{
return Format((T)value);
}
}