TestNameGenerator
TestNameGenerator is able to create test names according to
a coded pattern.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
namespace NUnit.Framework.Internal
{
public class TestNameGenerator
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private abstract class NameFragment
{
private const string THREE_DOTS = "...";
public virtual void AppendTextTo(StringBuilder sb, TestMethod testMethod, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
AppendTextTo(sb, testMethod.Method.MethodInfo, args);
}
public abstract void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args);
protected static void AppendGenericTypeNames(StringBuilder sb, MethodInfo method)
{
sb.Append('<');
int num = 0;
Type[] genericArguments = method.GetGenericArguments();
foreach (Type type in genericArguments) {
if (num++ > 0)
sb.Append(',');
sb.Append(type.Name);
}
sb.Append('>');
}
protected static string GetDisplayString([System.Runtime.CompilerServices.Nullable(2)] object arg, int stringMax)
{
return DisplayName.GetValueString(arg, stringMax);
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class TestIDFragment : NameFragment
{
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append("{i}");
}
public override void AppendTextTo(StringBuilder sb, TestMethod testMethod, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(testMethod.Id);
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class FixedTextFragment : NameFragment
{
private readonly string _text;
public FixedTextFragment(string text)
{
_text = text;
}
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(_text);
}
}
private sealed class MethodNameFragment : NameFragment
{
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(method.Name);
if (method.IsGenericMethod)
NameFragment.AppendGenericTypeNames(sb, method);
}
}
private sealed class NamespaceFragment : NameFragment
{
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(method.DeclaringType.Namespace);
}
}
private sealed class MethodFullNameFragment : NameFragment
{
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(method.DeclaringType.FullName);
sb.Append('.');
sb.Append(method.Name);
if (method.IsGenericMethod)
NameFragment.AppendGenericTypeNames(sb, method);
}
}
private sealed class ClassNameFragment : NameFragment
{
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(method.DeclaringType.Name);
}
}
private sealed class ClassFullNameFragment : NameFragment
{
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
sb.Append(method.DeclaringType.FullName);
}
}
private sealed class ArgListFragment : NameFragment
{
private readonly int _maxStringLength;
public ArgListFragment(int maxStringLength)
{
_maxStringLength = maxStringLength;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] arglist)
{
if (arglist != null) {
sb.Append('(');
for (int i = 0; i < arglist.Length; i++) {
if (i > 0)
sb.Append(",");
sb.Append(NameFragment.GetDisplayString(arglist[i], _maxStringLength));
}
sb.Append(')');
}
}
}
private sealed class ArgumentFragment : NameFragment
{
private readonly int _index;
private readonly int _maxStringLength;
public ArgumentFragment(int index, int maxStringLength)
{
_index = index;
_maxStringLength = maxStringLength;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
if (args != null && _index < args.Length)
sb.Append(NameFragment.GetDisplayString(args[_index], _maxStringLength));
}
}
private sealed class ParamArgListFragment : NameFragment
{
private readonly int _maxStringLength;
public ParamArgListFragment(int maxStringLength)
{
_maxStringLength = maxStringLength;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override void AppendTextTo(StringBuilder sb, MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
if (args != null) {
sb.Append('(');
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < args.Length; i++) {
if (i > 0)
sb.Append(", ");
if (i < parameters.Length) {
sb.Append(parameters[i].Name);
sb.Append(": ");
}
sb.Append(NameFragment.GetDisplayString(args[i], _maxStringLength));
}
sb.Append(')');
}
}
}
[System.Runtime.CompilerServices.Nullable(1)]
public static string DefaultTestNamePattern = "{m}{a}";
[System.Runtime.CompilerServices.Nullable(1)]
private readonly string _pattern;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private List<NameFragment> _fragments;
public TestNameGenerator()
{
_pattern = DefaultTestNamePattern;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public TestNameGenerator(string pattern)
{
_pattern = pattern;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public string GetDisplayName(TestMethod testMethod)
{
return GetDisplayName(testMethod, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public string GetDisplayName(TestMethod testMethod, [System.Runtime.CompilerServices.Nullable(2)] object[] args)
{
if (_fragments == null)
_fragments = BuildFragmentList(_pattern);
StringBuilder stringBuilder = new StringBuilder();
foreach (NameFragment fragment in _fragments) {
fragment.AppendTextTo(stringBuilder, testMethod, args);
}
return stringBuilder.ToString();
}
[System.Runtime.CompilerServices.NullableContext(1)]
private static List<NameFragment> BuildFragmentList(string pattern)
{
List<NameFragment> list = new List<NameFragment>();
int num;
int num3;
for (num = 0; num < pattern.Length; num = num3 + 1) {
int num2 = pattern.IndexOf('{', num);
if (num2 < 0)
break;
num3 = pattern.IndexOf('}', num2);
if (num3 < 0)
break;
if (num2 > num)
list.Add(new FixedTextFragment(pattern.Substring(num, num2 - num)));
string text = pattern.Substring(num2, num3 - num2 + 1);
if (text != null) {
int length = text.Length;
if (length == 3) {
int index;
switch (text[1]) {
case 'm':
break;
case 'i':
goto IL_0130;
case 'n':
goto IL_0146;
case 'c':
goto IL_015c;
case 'C':
goto IL_0172;
case 'M':
goto IL_0188;
case 'a':
goto IL_019e;
case 'p':
goto IL_01b4;
case '0':
if (text == "{0}")
goto IL_0328;
goto IL_0348;
case '1':
if (text == "{1}")
goto IL_0328;
goto IL_0348;
case '2':
if (text == "{2}")
goto IL_0328;
goto IL_0348;
case '3':
if (text == "{3}")
goto IL_0328;
goto IL_0348;
case '4':
if (text == "{4}")
goto IL_0328;
goto IL_0348;
case '5':
if (text == "{5}")
goto IL_0328;
goto IL_0348;
case '6':
if (text == "{6}")
goto IL_0328;
goto IL_0348;
case '7':
if (text == "{7}")
goto IL_0328;
goto IL_0348;
case '8':
if (text == "{8}")
goto IL_0328;
goto IL_0348;
case '9':
if (text == "{9}")
goto IL_0328;
goto IL_0348;
default:
goto IL_0348;
IL_0328:
index = text[1] - 48;
list.Add(new ArgumentFragment(index, 0));
continue;
}
if (text == "{m}") {
list.Add(new MethodNameFragment());
continue;
}
}
}
goto IL_0348;
IL_019e:
if (text == "{a}") {
list.Add(new ArgListFragment(0));
continue;
}
goto IL_0348;
IL_0130:
if (text == "{i}") {
list.Add(new TestIDFragment());
continue;
}
goto IL_0348;
IL_0188:
if (text == "{M}") {
list.Add(new MethodFullNameFragment());
continue;
}
goto IL_0348;
IL_0172:
if (text == "{C}") {
list.Add(new ClassFullNameFragment());
continue;
}
goto IL_0348;
IL_0146:
if (text == "{n}") {
list.Add(new NamespaceFragment());
continue;
}
goto IL_0348;
IL_015c:
if (text == "{c}") {
list.Add(new ClassNameFragment());
continue;
}
goto IL_0348;
IL_01b4:
if (text == "{p}") {
list.Add(new ParamArgListFragment(0));
continue;
}
goto IL_0348;
IL_0348:
char c = text[1];
int result;
if (text.Length >= 5 && text[2] == ':' && (c == 'a' || c == 'p' || char.IsDigit(c)) && int.TryParse(text.Substring(3, text.Length - 4), out result) && result > 0) {
switch (c) {
case 'a':
list.Add(new ArgListFragment(result));
break;
case 'p':
list.Add(new ParamArgListFragment(result));
break;
default:
list.Add(new ArgumentFragment(c - 48, result));
break;
}
} else
list.Add(new FixedTextFragment(text));
}
if (num < pattern.Length)
list.Add(new FixedTextFragment(pattern.Substring(num)));
return list;
}
}
}