-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathFsCheckPropertyTestExecutor.cs
More file actions
291 lines (248 loc) · 9.18 KB
/
Copy pathFsCheckPropertyTestExecutor.cs
File metadata and controls
291 lines (248 loc) · 9.18 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text;
using FsCheck;
using FsCheck.Fluent;
using TUnit.Core;
using TUnit.Core.Interfaces;
namespace TUnit.FsCheck;
/// <summary>
/// A test executor that runs FsCheck property-based tests.
/// </summary>
#pragma warning disable IL2046 // RequiresUnreferencedCode attribute mismatch
#pragma warning disable IL3051 // RequiresDynamicCode attribute mismatch
#pragma warning disable IL2072 // DynamicallyAccessedMembers warning
public class FsCheckPropertyTestExecutor : ITestExecutor
{
#if NET8_0_OR_GREATER
private static readonly System.Buffers.SearchValues<char> LineEndings = System.Buffers.SearchValues.Create("\r\n");
#endif
private readonly FsCheckPropertyAttribute _propertyAttribute;
public FsCheckPropertyTestExecutor(FsCheckPropertyAttribute propertyAttribute)
{
_propertyAttribute = propertyAttribute;
}
public ValueTask ExecuteTest(TestContext context, Func<ValueTask> action)
{
var testDetails = context.Metadata.TestDetails;
var classInstance = testDetails.ClassInstance;
var classType = testDetails.ClassType;
var methodName = testDetails.MethodName;
// Get MethodInfo via reflection from the class type
var methodInfo = GetMethodInfo(classType, methodName, testDetails.MethodMetadata.Parameters);
var config = CreateConfig();
RunPropertyCheck(methodInfo, classInstance, config);
return default;
}
private static MethodInfo GetMethodInfo(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
Type classType,
string methodName,
ParameterMetadata[] parameters)
{
// Try to find the method by name and parameter count
var methods = classType
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.Where(m => m.Name == methodName && m.GetParameters().Length == parameters.Length)
.ToArray();
if (methods.Length == 0)
{
throw new InvalidOperationException($"Could not find method '{methodName}' on type '{classType.FullName}'");
}
if (methods.Length == 1)
{
return methods[0];
}
// Multiple overloads - try to match by parameter types
foreach (var method in methods)
{
var methodParams = method.GetParameters();
var match = true;
for (var i = 0; i < methodParams.Length; i++)
{
if (methodParams[i].ParameterType != parameters[i].Type)
{
match = false;
break;
}
}
if (match)
{
return method;
}
}
// Just return the first one if no exact match
return methods[0];
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(CancellationTokenArbitrary))]
private Config CreateConfig()
{
var config = Config.QuickThrowOnFailure
.WithMaxTest(_propertyAttribute.MaxTest)
.WithMaxRejected(_propertyAttribute.MaxFail)
.WithStartSize(_propertyAttribute.StartSize)
.WithEndSize(_propertyAttribute.EndSize);
if (!string.IsNullOrEmpty(_propertyAttribute.Replay))
{
var parts = _propertyAttribute.Replay!.Split(',');
if (parts.Length >= 1 && ulong.TryParse(parts[0].Trim(), out var seed1))
{
var seed2 = parts.Length >= 2 && ulong.TryParse(parts[1].Trim(), out var s2) ? s2 : 0UL;
config = config.WithReplay(seed1, seed2);
}
}
// Register a default Arbitrary<CancellationToken> that surfaces TestContext's
// timeout-backed token. User-supplied arbitraries are listed first; FsCheck's
// WithArbitrary resolves the first type in the list as highest priority, so
// user registrations override the default for conflicting types.
config = config.WithArbitrary(
(_propertyAttribute.Arbitrary ?? []).Append(typeof(CancellationTokenArbitrary)));
return config;
}
[UnconditionalSuppressMessage("Trimming", "IL2060", Justification = "FsCheck requires reflection")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "FsCheck requires dynamic code")]
private static void RunPropertyCheck(MethodInfo methodInfo, object classInstance, Config config)
{
try
{
Check.Method(config, methodInfo, classInstance);
}
catch (Exception ex)
{
throw new PropertyFailedException(FormatCounterexample(methodInfo, ex));
}
}
private static string FormatCounterexample(MethodInfo methodInfo, Exception ex)
{
var parameters = methodInfo.GetParameters();
var args = parameters
.Select((p, i) => p.Name ?? $"arg{i}")
.ToArray();
var methodName = methodInfo.Name;
var sb = new StringBuilder();
sb.AppendLine($"Property '{methodName}' failed with counterexample:");
// Unwrap TargetInvocationException to get to the actual FsCheck exception
var innerEx = ex;
while (innerEx is TargetInvocationException { InnerException: not null } tie)
{
innerEx = tie.InnerException;
}
// Try to extract shrunk values from FsCheck message
var shrunkValues = TryParseShrunkValues(innerEx?.Message);
// Display args, using shrunk values if available
for (int i = 0; i < args.Length; i++)
{
var name = args[i];
var value = shrunkValues?[i];
if (value != null)
{
sb.AppendLine($" {name} = {value}");
}
}
// Append the FsCheck message for full details
if (innerEx != null && !string.IsNullOrEmpty(innerEx.Message))
{
sb.AppendLine();
sb.AppendLine("FsCheck output:");
// Indent each line of the FsCheck message
foreach (var line in innerEx.Message.Split('\n'))
{
sb.Append(" ");
sb.AppendLine(line.TrimEnd('\r'));
}
}
return sb.ToString();
}
private static string[]? TryParseShrunkValues(string? message)
{
if (string.IsNullOrEmpty(message))
return null;
// Look for "Shrunk:" followed by values on the next line
var shrunkIndex = message!.IndexOf("Shrunk:", StringComparison.Ordinal);
if (shrunkIndex < 0)
return null;
var afterShrunk = message[(shrunkIndex + 7)..].TrimStart();
// Take only the first line
#if NET8_0_OR_GREATER
var newlineIndex = afterShrunk.AsSpan().IndexOfAny(LineEndings);
#else
var newlineIndex = afterShrunk.IndexOfAny(['\r', '\n']);
#endif
var shrunkLine = newlineIndex >= 0 ? afterShrunk[..newlineIndex] : afterShrunk;
if (shrunkLine.StartsWith('('))
{
return ParseTupleValues(shrunkLine);
}
else
{
// Single value (no brackets)
return [shrunkLine.Trim()];
}
}
private static string[]? ParseTupleValues(string tupleString)
{
if (!tupleString.StartsWith('('))
return null;
var values = new List<string>();
var current = new StringBuilder();
var depth = 0;
var inString = false;
var escaped = false;
for (var i = 1; i < tupleString.Length; i++)
{
var c = tupleString[i];
if (escaped)
{
current.Append(c);
escaped = false;
continue;
}
if (c == '\\' && inString)
{
current.Append(c);
escaped = true;
continue;
}
if (c == '"')
{
inString = !inString;
current.Append(c);
continue;
}
if (inString)
{
current.Append(c);
continue;
}
switch (c)
{
case '(':
depth++;
current.Append(c);
break;
case ')':
if (depth == 0)
{
if (current.Length > 0)
values.Add(current.ToString().Trim());
return values.ToArray();
}
depth--;
current.Append(c);
break;
case ',' when depth == 0:
values.Add(current.ToString().Trim());
current.Clear();
break;
default:
current.Append(c);
break;
}
}
return values.ToArray();
}
}
#pragma warning restore IL2046
#pragma warning restore IL3051
#pragma warning restore IL2072