-
Notifications
You must be signed in to change notification settings - Fork 337
/
FixedNameTestMethodBuilder.cs
104 lines (91 loc) · 4.11 KB
/
FixedNameTestMethodBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Ploeh.AutoFixture.NUnit3
{
/// <summary>
/// Builder that generates <see cref="TestMethod"/> with fixed names.
/// This might be needed by some test runners to correctly identify tests between the discovery and execution
/// (e.g. Nunit test adaptor for Visual Studio, NCrunch).
/// </summary>
public class FixedNameTestMethodBuilder : ITestMethodBuilder
{
/// <inheritdoc />
public virtual TestMethod Build(
IMethodInfo method, Test suite, IEnumerable<object> parameterValues, int autoDataStartIndex)
{
if (method == null)
{
throw new ArgumentNullException(nameof(method));
}
if (parameterValues == null)
{
throw new ArgumentNullException(nameof(parameterValues));
}
return new NUnitTestCaseBuilder()
.BuildTestMethod(method, suite, GetParametersForMethod(method, parameterValues, autoDataStartIndex));
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
Justification = "This method is always expected to return an instance of the TestCaseParameters class.")]
private static TestCaseParameters GetParametersForMethod(
IMethodInfo method, IEnumerable<object> parameterValues, int autoDataStartIndex)
{
try
{
return GetParametersForMethod(method, parameterValues.ToArray(), autoDataStartIndex);
}
catch (Exception ex)
{
return new TestCaseParameters(ex);
}
}
private static TestCaseParameters GetParametersForMethod(IMethodInfo method, object[] args, int autoDataStartIndex)
{
var result = new TestCaseParameters(args);
EnsureOriginalArgumentsArrayIsNotShared(result);
var methodParameters = method.GetParameters();
for (int i = autoDataStartIndex; i < result.OriginalArguments.Length; i++)
{
result.OriginalArguments[i] = new TypeNameRenderer(methodParameters[i].ParameterType);
}
return result;
}
/// <summary>
/// Before NUnit 3.5 the Arguments and OriginalArguments properties are referencing the same array, so
/// we cannot safely update the OriginalArguments without touching the Arguments value.
/// This method fixes that by making the OriginalArguments array a standalone copy.
/// <para>
/// When running in NUnit3.5 and later the method is supposed to do nothing.
/// </para>
/// </summary>
private static void EnsureOriginalArgumentsArrayIsNotShared(TestCaseParameters parameters)
{
if (ReferenceEquals(parameters.Arguments, parameters.OriginalArguments))
{
var clonedArguments = new object[parameters.OriginalArguments.Length];
Array.Copy(parameters.OriginalArguments, clonedArguments, parameters.OriginalArguments.Length);
// Unfortunately the property has a private setter, so can be updated via reflection only.
// Should use the type where the property is declared as otherwise the private setter is not available.
var property = typeof(TestParameters).GetTypeInfo().GetProperty(nameof(TestCaseParameters.OriginalArguments));
property.SetValue(parameters, clonedArguments, null);
}
}
private class TypeNameRenderer
{
private Type Type { get; }
public TypeNameRenderer(Type type)
{
this.Type = type ?? throw new ArgumentNullException(nameof(type));
}
public override string ToString()
{
return "auto<" + this.Type.Name + ">";
}
}
}
}