-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathStringMatchesAssertion.patch
More file actions
88 lines (71 loc) · 3.34 KB
/
StringMatchesAssertion.patch
File metadata and controls
88 lines (71 loc) · 3.34 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
REQUIRED CHANGES TO: TUnit.Assertions/Conditions/StringAssertions.cs
This patch adds Match caching to StringMatchesAssertion class to enable clean, reflection-free access to regex match results.
================================================================================
CHANGE 1: Add _cachedMatch field
================================================================================
Location: Line 432 (after "private RegexOptions _options = RegexOptions.None;")
ADD THIS LINE:
private Match? _cachedMatch;
So it becomes:
private readonly string _pattern;
private readonly Regex? _regex;
private RegexOptions _options = RegexOptions.None;
private Match? _cachedMatch; // <-- ADD THIS LINE
================================================================================
CHANGE 2: Add GetMatch() method
================================================================================
Location: After line 464 (after the WithOptions() method, before CheckAsync())
ADD THIS METHOD:
/// <summary>
/// Gets the cached regex match result after the assertion has been executed.
/// Returns null if the assertion hasn't been executed yet or if the match failed.
/// </summary>
public Match? GetMatch() => _cachedMatch;
So it becomes:
public StringMatchesAssertion WithOptions(RegexOptions options)
{
_options = options;
Context.ExpressionBuilder.Append($".WithOptions({options})");
return this;
}
/// <summary>
/// Gets the cached regex match result after the assertion has been executed.
/// Returns null if the assertion hasn't been executed yet or if the match failed.
/// </summary>
public Match? GetMatch() => _cachedMatch; // <-- ADD THIS METHOD
protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<string> metadata)
{
================================================================================
CHANGE 3: Modify CheckAsync() to cache the Match
================================================================================
Location: Lines 486-492 in CheckAsync() method
REPLACE THIS:
// Use the validated regex to check the match
bool isMatch = regex.IsMatch(value);
if (isMatch)
{
return Task.FromResult(AssertionResult.Passed);
}
WITH THIS:
// Use the validated regex to check the match and cache it
var match = regex.Match(value);
_cachedMatch = match;
if (match.Success)
{
return Task.FromResult(AssertionResult.Passed);
}
EXPLANATION:
- Changed from regex.IsMatch(value) to regex.Match(value) to get the Match object
- Store the Match in _cachedMatch field
- Changed from checking isMatch to checking match.Success
================================================================================
SUMMARY
================================================================================
These changes enable StringMatchesAssertionExtensions.GetMatchAsync() to work
without reflection by calling the new GetMatch() method.
The implementation follows SOLID, KISS, DRY, and SRP principles:
- Single Responsibility: StringMatchesAssertion now caches its match
- Open/Closed: API extended without modifying existing behavior
- No reflection: Clean, AOT-compatible code
- DRY: Match is performed once and cached
- KISS: Simple, straightforward implementation