-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertExtensions.cs
More file actions
97 lines (85 loc) · 3.89 KB
/
AssertExtensions.cs
File metadata and controls
97 lines (85 loc) · 3.89 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
using System.Text.RegularExpressions;
using WebExpress.WebCore.WebHtml;
namespace WebExpress.WebCore.Test.Fixture
{
/// <summary>
/// Provides extension methods for assertions.
/// </summary>
public static partial class AssertExtensions
{
/// <summary>
/// Gets a regular expression that matches whitespace between '>' and '<' characters.
/// </summary>
/// <returns>A <see cref="Regex"/> object that matches whitespace between '>' and '<' characters.</returns>
[GeneratedRegex(@">\s+<")]
private static partial Regex WhitespaceRegex();
/// <summary>
/// Asserts that the actual string is equal to the expected string, allowing for placeholders.
/// </summary>
/// <param name="assert">The Assert instance (not used, but required for extension method).</param>
/// <param name="expected">The expected string with placeholders.</param>
/// <param name="actual">The actual string to compare.</param>
public static void EqualWithPlaceholders(string expected, string actual)
{
if (expected is null && actual is null)
{
return;
}
Assert.NotNull(expected);
Assert.NotNull(actual);
var str = RemoveLineBreaks(actual?.ToString());
Assert.True(AreEqualWithPlaceholders(expected, str), $"Expected: {expected}{Environment.NewLine}Actual: {str}");
}
/// <summary>
/// Asserts that the actual node is equal to the expected string, allowing for placeholders.
/// </summary>
/// <param name="assert">The Assert instance (not used, but required for extension method).</param>
/// <param name="expected">The expected string with placeholders.</param>
/// <param name="actual">The actual string to compare.</param>
public static void EqualWithPlaceholders(string expected, IHtmlNode actual)
{
var str = RemoveLineBreaks(actual?.ToString());
Assert.True(AreEqualWithPlaceholders(expected, str), $"Expected: {expected}{Environment.NewLine}Actual: {str}");
}
/// <summary>
/// Compares two strings, allowing for placeholders in the expected string.
/// </summary>
/// <param name="expected">The expected string, which may contain '*' as a wildcard character.</param>
/// <param name="actual">The actual string to compare against the expected string.</param>
/// <returns>True if the actual string matches the expected string with placeholders; otherwise, false.</returns>
private static bool AreEqualWithPlaceholders(string expected, string actual)
{
if (expected is null && actual is null)
{
return true;
}
else if (expected is not null && actual is null)
{
return false;
}
else if (expected is null && actual is not null)
{
return false;
}
var pattern = "^" + Regex.Escape(expected).Replace(@"\*", ".*") + "$";
return Regex.IsMatch(actual, pattern);
}
/// <summary>
/// Removes all line breaks from the input string.
/// </summary>
/// <param name="input">The input string from which to remove line breaks.</param>
/// <returns>A string with all line breaks removed.</returns>
public static string RemoveLineBreaks(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
// remove all line breaks
string result = input.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
// remove whitespace of any length between '>' and '<'
result = WhitespaceRegex().Replace(result, "><");
return result;
}
}
}