-
Notifications
You must be signed in to change notification settings - Fork 4
/
TestHelper.cs
197 lines (148 loc) · 7.43 KB
/
TestHelper.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using FluentAssertions;
using JetBrains.Annotations;
using Nemesis.Essentials.Design;
using Nemesis.Essentials.Runtime;
namespace Nemesis.TextParsers.Tests.Utils;
internal static partial class TestHelper
{
public static string AssertException(Exception actual, Type expectedException, string expectedErrorMessagePart, bool logMessage = false)
{
if (actual is TargetInvocationException tie && tie.InnerException is { } inner)
actual = inner;
Assert.That(actual, Is.TypeOf(expectedException), () => $@"Unexpected external exception: {actual}");
if (expectedErrorMessagePart != null)
Assert.That(
IgnoreNewLinesComparer.RemoveNewLines(actual?.Message),
Does.Contain(IgnoreNewLinesComparer.RemoveNewLines(expectedErrorMessagePart))
);
if (!logMessage) return "";
string message = actual switch
{
OverflowException oe => $"Expected overflow: {oe.Message}",
FormatException fe => $"Expected bad format: {fe.Message}",
ArgumentException ae => $"Expected argument exception: {ae.Message}",
InvalidOperationException ioe => $"Expected invalid operation: {ioe.Message}",
_ => $"Expected other: {actual?.Message}"
};
return message;
}
public static void IsMutuallyEquivalent(object o1, object o2, string because = "")
{
if (o1 is Regex re1 && o2 is Regex re2)
{
Assert.Multiple(() =>
{
Assert.That(re1.Options, Is.EqualTo(re2.Options));
Assert.That(re1.ToString(), Is.EqualTo(re2.ToString()));
});
}
else if (o1 != null && TypeMeta.TryGetGenericRealization(o1.GetType(), typeof(ArraySegment<>), out var as1) &&
as1.GenericTypeArguments[0] is var ase1 &&
o2 != null && TypeMeta.TryGetGenericRealization(o2.GetType(), typeof(ArraySegment<>), out var as2) &&
as2.GenericTypeArguments[0] is var ase2 &&
ase1 == ase2
)
{
var method = typeof(TestHelper).GetMethod(nameof(ArraySegmentEquals), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
?? throw new MissingMethodException(nameof(TestHelper), nameof(ArraySegmentEquals));
method = method.MakeGenericMethod(ase1);
var equals = (bool)method.Invoke(null, [o1, o2]);
Assert.That(equals, Is.True, "o1 != o2 when treated as ArraySegment");
}
else
{
o1.Should().BeEquivalentTo(o2, options => options.WithStrictOrdering(), because);
o2.Should().BeEquivalentTo(o1, options => options.WithStrictOrdering(), because);
}
}
private static bool ArraySegmentEquals<T>(ArraySegment<T> o1, ArraySegment<T> o2) =>
EnumerableEqualityComparer<T>.DefaultInstance.Equals(o1.Array, o2.Array) &&
o1.Offset == o2.Offset &&
o1.Count == o2.Count;
public static TDelegate MakeDelegate<TDelegate>(Expression<TDelegate> expression, params Type[] typeArguments)
where TDelegate : Delegate
{
var method = Method.OfExpression(expression);
if (method.IsGenericMethod)
{
method = method.GetGenericMethodDefinition();
method = method.MakeGenericMethod(typeArguments);
}
var parameters = method.GetParameters()
.Select(p => Expression.Parameter(p.ParameterType, p.Name))
.ToList();
var @this = Expression.Parameter(
method.ReflectedType ?? throw new NotSupportedException("Method type cannot be empty"), "this");
var call = method.IsStatic
? Expression.Call(method, parameters)
: Expression.Call(@this, method, parameters);
if (!method.IsStatic)
parameters.Insert(0, @this);
return Expression.Lambda<TDelegate>(call, parameters).Compile();
}
public static void ParseAndFormat<T>(T instance, string text, ITransformerStore store = null)
{
var sut = (store ?? Sut.DefaultStore).GetTransformer<T>();
var actualParsed1 = sut.Parse(text);
string formattedInstance = sut.Format(instance);
string formattedActualParsed = sut.Format(actualParsed1);
Assert.That(formattedInstance, Is.EqualTo(formattedActualParsed));
var actualParsed2 = sut.Parse(formattedInstance);
IsMutuallyEquivalent(actualParsed1, instance);
IsMutuallyEquivalent(actualParsed2, instance);
IsMutuallyEquivalent(actualParsed1, actualParsed2);
}
public static void ParseAndFormatObject([NotNull] object instance, string text, ITransformerStore store = null)
{
if (instance == null) throw new ArgumentNullException(nameof(instance));
store ??= Sut.DefaultStore;
Type transType = instance.GetType();
var sut = store.GetTransformer(transType);
string formattedInstance = sut.FormatObject(instance);
var actualParsed1 = sut.ParseObject(text);
string formattedActualParsed = sut.FormatObject(actualParsed1);
Assert.That(formattedInstance, Is.EqualTo(formattedActualParsed));
var actualParsed2 = sut.ParseObject(formattedInstance);
IsMutuallyEquivalent(actualParsed1, instance);
IsMutuallyEquivalent(actualParsed2, instance);
IsMutuallyEquivalent(actualParsed1, actualParsed2);
}
public static void RoundTrip([NotNull] object instance, ITransformerStore store = null) =>
RoundTrip(instance, (store ?? Sut.DefaultStore).GetTransformer(instance.GetType()));
public static void RoundTrip([NotNull] object instance, ITransformer sut = null)
{
if (instance == null) throw new ArgumentNullException(nameof(instance));
var text = sut.FormatObject(instance);
var parsed1 = sut.ParseObject(text);
var parsed2 = sut.ParseObject(text);
IsMutuallyEquivalent(parsed1, instance);
IsMutuallyEquivalent(parsed2, parsed1);
var text3 = sut.FormatObject(parsed1);
var parsed3 = sut.ParseObject(text3);
IsMutuallyEquivalent(parsed3, instance);
IsMutuallyEquivalent(parsed1, parsed3);
}
#if NET7_0_OR_GREATER
[GeneratedRegex(@"\W", RegexOptions.IgnoreCase | RegexOptions.Compiled)]
private static partial Regex GetInvalidTestNamePattern();
public static string SanitizeTestName(this string text) =>
GetInvalidTestNamePattern().Replace(text, "_");
#else
private static readonly Regex _invalidTestName = new(@"\W", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static string SanitizeTestName(this string text) => _invalidTestName.Replace(text, "_");
#endif
}
#nullable enable
internal class IgnoreNewLinesComparer : IComparer<string>, IEqualityComparer<string>
{
public static readonly IComparer<string> Comparer = new IgnoreNewLinesComparer();
public static readonly IEqualityComparer<string> EqualityComparer = new IgnoreNewLinesComparer();
public int Compare(string? x, string? y) => string.CompareOrdinal(RemoveNewLines(x), RemoveNewLines(y));
public bool Equals(string? x, string? y) => RemoveNewLines(x) == RemoveNewLines(y);
public int GetHashCode(string s) => RemoveNewLines(s)?.GetHashCode() ?? 0;
//for NET 6+ use string.ReplaceLineEndings()
public static string? RemoveNewLines(string? s) => s?
.Replace(Environment.NewLine, "")
.Replace("\n", "")
.Replace("\r", "");
}