|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using AngleSharp; |
| 8 | +using AngleSharp.Diffing.Core; |
| 9 | +using AngleSharp.Dom; |
| 10 | +using Egil.RazorComponents.Testing.Diffing; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Egil.RazorComponents.Testing |
| 14 | +{ |
| 15 | + public static class AssertExtensions |
| 16 | + { |
| 17 | + public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, IRenderedFragment expected) |
| 18 | + { |
| 19 | + if (actual is null) throw new ArgumentNullException(nameof(actual)); |
| 20 | + if (expected is null) throw new ArgumentNullException(nameof(expected)); |
| 21 | + |
| 22 | + return actual.GetNodes().CompareTo(expected.GetNodes()); |
| 23 | + } |
| 24 | + |
| 25 | + public static IReadOnlyList<IDiff> CompareTo(this INodeList actual, INodeList expected) |
| 26 | + { |
| 27 | + if (actual is null) throw new ArgumentNullException(nameof(actual)); |
| 28 | + if (expected is null) throw new ArgumentNullException(nameof(expected)); |
| 29 | + |
| 30 | + if (actual.Length == 0 && expected.Length == 0) return Array.Empty<IDiff>(); |
| 31 | + |
| 32 | + var comparer = actual.Length > 0 |
| 33 | + ? actual[0].Owner.Context.GetService<HtmlComparer>() |
| 34 | + : expected[0].Owner.Context.GetService<HtmlComparer>(); |
| 35 | + |
| 36 | + return comparer.Compare(expected, actual).ToArray(); |
| 37 | + } |
| 38 | + |
| 39 | + public static void ShouldBeAddition(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null) |
| 40 | + { |
| 41 | + if (actualChange is null) throw new ArgumentNullException(nameof(actualChange)); |
| 42 | + if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange)); |
| 43 | + |
| 44 | + var actual = Assert.IsType<UnexpectedNodeDiff>(actualChange); |
| 45 | + var expected = expectedChange.GetNodes(); |
| 46 | + var comparer = expected[0].Owner.Context.GetService<HtmlComparer>(); |
| 47 | + |
| 48 | + var diffs = comparer.Compare(expected, new[] { actual.Test.Node }).ToList(); |
| 49 | + Assert.True(diffs.Count == 0, $"{GetUserMessage()}{StringifyDiffs(expected.ToHtml(), actual.Test.Node.ToHtml(), diffs)}"); |
| 50 | + |
| 51 | + string GetUserMessage() => userMessage is null |
| 52 | + ? $"The actual change does not match the expected change.{Environment.NewLine}" |
| 53 | + : $"{userMessage}.{Environment.NewLine}"; |
| 54 | + } |
| 55 | + |
| 56 | + public static void ShouldBe(this IRenderedFragment actual, IRenderedFragment expected, string? userMessage = null) |
| 57 | + { |
| 58 | + if (actual is null) throw new ArgumentNullException(nameof(actual)); |
| 59 | + if (expected is null) throw new ArgumentNullException(nameof(expected)); |
| 60 | + |
| 61 | + actual.GetNodes().ShouldBe(expected.GetNodes(), userMessage); |
| 62 | + } |
| 63 | + |
| 64 | + public static void ShouldBe(this INodeList actual, INodeList expected, string? userMessage = null) |
| 65 | + { |
| 66 | + var diffs = CompareTo(actual, expected); |
| 67 | + |
| 68 | + Assert.True(diffs.Count == 0, $"{GetUserMessage()}{StringifyDiffs(expected.ToHtml(), actual.ToHtml(), diffs)}"); |
| 69 | + |
| 70 | + string GetUserMessage() => userMessage is null |
| 71 | + ? string.Empty |
| 72 | + : $"{userMessage}.{Environment.NewLine}"; |
| 73 | + } |
| 74 | + |
| 75 | + [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "<Pending>")] |
| 76 | + private static string StringifyDiffs(string controlHtml, string testHtml, IReadOnlyList<IDiff> diffs) |
| 77 | + { |
| 78 | + var diffsText = string.Join($"{Environment.NewLine}", diffs.Select((x, i) => |
| 79 | + { |
| 80 | + var diffText = x switch |
| 81 | + { |
| 82 | + //NodeDiff diff when diff.Target == DiffTarget.Text => |
| 83 | + // $"The control text '{diff.Control.Node.TextContent}' at {diff.Control.Path} is different from the test text '{diff.Test.Node.TextContent}' at {diff.Test.Path}.", |
| 84 | + NodeDiff diff => $"The control {diff.Control.Node.NodeType.ToString().ToLowerInvariant()} {diff.Control.Path} and the test {diff.Test.Node.NodeType.ToString().ToLowerInvariant()} {diff.Test.Path} are different.", |
| 85 | + AttrDiff diff => $"The value of the control attribute {diff.Control.Path} and test attribute {diff.Test.Path} are different.", |
| 86 | + MissingNodeDiff diff => $"The control {diff.Control.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Control.Path} is missing.", |
| 87 | + MissingAttrDiff diff => $"The control attribute at {diff.Control.Path} is missing.", |
| 88 | + UnexpectedNodeDiff diff => $"The test {diff.Test.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Test.Path} was not expected.", |
| 89 | + UnexpectedAttrDiff diff => $"The test attribute at {diff.Test.Path} was not expected.", |
| 90 | + _ => throw new InvalidOperationException($"Unknown diff type detected: {x.GetType()}") |
| 91 | + }; |
| 92 | + return $" {i + 1}: {diffText}"; |
| 93 | + })); |
| 94 | + |
| 95 | + return $"{Environment.NewLine}The following errors was found during diffing: {Environment.NewLine}{diffsText}{Environment.NewLine}{Environment.NewLine}" + |
| 96 | + $"Control HTML was:{Environment.NewLine}{controlHtml}{Environment.NewLine}" + |
| 97 | + $"Test HTML was:{Environment.NewLine}{testHtml}{Environment.NewLine}"; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments