-
Notifications
You must be signed in to change notification settings - Fork 758
/
TestFrameworkOptions.cs
108 lines (93 loc) · 4.14 KB
/
TestFrameworkOptions.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
105
106
107
108
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Xunit.Abstractions;
namespace Xunit
{
/// <summary>
/// Represents options passed to a test framework for discovery or execution.
/// </summary>
[DebuggerDisplay("{ToDebuggerDisplay(),nq}")]
#if NETFRAMEWORK
[System.Serializable]
#endif
public class TestFrameworkOptions : ITestFrameworkDiscoveryOptions, ITestFrameworkExecutionOptions
{
readonly Dictionary<string, object> properties = new Dictionary<string, object>();
// Force users to use one of the factory methods
TestFrameworkOptions() { }
/// <summary>
/// Creates an instance of <see cref="TestFrameworkOptions"/>
/// </summary>
/// <param name="configuration">The optional configuration to copy values from.</param>
public static ITestFrameworkDiscoveryOptions ForDiscovery(TestAssemblyConfiguration configuration = null)
{
ITestFrameworkDiscoveryOptions result = new TestFrameworkOptions();
if (configuration != null)
{
result.SetDiagnosticMessages(configuration.DiagnosticMessages);
result.SetInternalDiagnosticMessages(configuration.InternalDiagnosticMessages);
result.SetMethodDisplay(configuration.MethodDisplay);
result.SetMethodDisplayOptions(configuration.MethodDisplayOptions);
result.SetPreEnumerateTheories(configuration.PreEnumerateTheories);
}
return result;
}
/// <summary>
/// Creates an instance of <see cref="TestFrameworkOptions"/>
/// </summary>
/// <param name="configuration">The optional configuration to copy values from.</param>
public static ITestFrameworkExecutionOptions ForExecution(TestAssemblyConfiguration configuration = null)
{
ITestFrameworkExecutionOptions result = new TestFrameworkOptions();
if (configuration != null)
{
result.SetDiagnosticMessages(configuration.DiagnosticMessages);
result.SetInternalDiagnosticMessages(configuration.InternalDiagnosticMessages);
result.SetParallelAlgorithm(configuration.ParallelAlgorithm);
result.SetDisableParallelization(!configuration.ParallelizeTestCollections);
result.SetMaxParallelThreads(configuration.MaxParallelThreads);
result.SetStopOnTestFail(configuration.StopOnFail);
}
return result;
}
/// <summary>
/// Gets a value from the options collection.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="name">The name of the value.</param>
/// <returns>Returns the value.</returns>
public TValue GetValue<TValue>(string name)
{
object result;
if (properties.TryGetValue(name, out result))
return (TValue)result;
return default(TValue);
}
/// <summary>
/// Sets a value into the options collection.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="name">The name of the value.</param>
/// <param name="value">The value.</param>
public void SetValue<TValue>(string name, TValue value)
{
if (value == null)
properties.Remove(name);
else
properties[name] = value;
}
string ToDebuggerDisplay()
=> string.Format(CultureInfo.CurrentCulture, "{{ {0} }}", string.Join(", ", properties.Select(p => string.Format(CultureInfo.CurrentCulture, "{{ {0} = {1} }}", p.Key, ToDebuggerDisplay(p.Value))).ToArray()));
string ToDebuggerDisplay(object value)
{
if (value == null)
return "null";
var stringValue = value as string;
if (stringValue != null)
return string.Format(CultureInfo.CurrentCulture, "\"{0}\"", stringValue);
return value.ToString();
}
}
}