-
Notifications
You must be signed in to change notification settings - Fork 5
/
ForTest.cs
81 lines (75 loc) · 3.08 KB
/
ForTest.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SparkyTestHelpers.Scenarios.MsTest
{
/// <summary>
/// "Syntactic sugar" methods for working with <see cref="MsTestScenarioTester{TScenario}" />,
/// </summary>
public static class ForTest
{
/// <summary>
/// Creates an array of <typeparamref name="TScenario" /> from a "paramref name="scenarios"" array,
/// for use with the ".TestEach" extension method.
/// </summary>
/// <returns>Array of <typeparamref name="TScenario" />.</returns>
/// <example>
/// <code><![CDATA[
/// ForTest.Scenarios
/// (
/// new { DateString = "1/31/2023", ShouldBeValid = true },
/// new { DateString = "2/31/2023", ShouldBeValid = false },
/// new { DateString = "3/31/2023", ShouldBeValid = true },
/// new { DateString = "4/31/2023", ShouldBeValid = false },
/// new { DateString = "5/31/2023", ShouldBeValid = true },
/// new { DateString = "6/31/2023", ShouldBeValid = false }
/// )
/// .TestEach(scenario =>
/// {
/// DateTime dt;
/// Assert.AreEqual(scenario.ShouldBeValid, DateTime.TryParse(scenario.DateString, out dt));
/// });
/// ]]></code>
/// </example>
public static TScenario[] Scenarios<TScenario>(params TScenario[] scenarios)
{
return scenarios;
}
/// <summary>
/// Creates an IEnumerable of values for the specified enum typeparamref name="TEnum",
/// for use with the ".TestEach" extension method.
/// </summary>
/// <typeparam name="TEnum">The enum type.</typeparam>
/// <returns>IEnumerable of <typeparamref name="TEnum" />.</returns>
/// <example>
/// <code><![CDATA[
/// ForTest.EnumValues<OrderStatus>().TestEach(orderStatus => {});
/// ]]></code>
/// </example>
public static IEnumerable<TEnum> EnumValues<TEnum>() where TEnum : struct
{
Type enumType = typeof(TEnum);
if (!enumType.GetTypeInfo().IsEnum)
{
throw new InvalidOperationException($"{enumType} is not an Enum type.");
}
return Enum.GetValues(enumType).Cast<TEnum>();
}
/// <summary>
/// "params" alternative to IEnumerable.Except,
/// for use with the ".TestEach" extension method.
/// </summary>
/// <typeparam name="TEnum">The enum type.</typeparam>
/// <returns>IEnumerable of <typeparamref name="TEnum" />.</returns>
/// <example>
/// <code><![CDATA[
/// ForTest.EnumValues<OrderStatus>().ExceptFor(OrderStatus.Cancelled).TestEach(orderStatus => {});
/// ]]></code>
/// </example>
public static IEnumerable<TEnum> ExceptFor<TEnum>(this IEnumerable<TEnum> values, params TEnum[] valuesToExclude)
{
return values.Except(valuesToExclude);
}
}
}