|
4 | 4 | using System.Linq; |
5 | 5 | using AngleSharp; |
6 | 6 | using AngleSharp.Diffing.Core; |
| 7 | +using AngleSharp.Dom; |
7 | 8 | using Bunit; |
8 | 9 | using Bunit.Asserting; |
9 | 10 |
|
10 | 11 | namespace Bunit |
11 | 12 | { |
12 | | - /// <summary> |
13 | | - /// Represents an differences between pieces of markup. |
14 | | - /// </summary> |
15 | | - [SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "<Pending>")] |
16 | | - public class HtmlEqualException : ActualExpectedAssertException |
17 | | - { |
18 | | - /// <summary> |
19 | | - /// Creates an instance of the <see cref="HtmlEqualException"/> type. |
20 | | - /// </summary> |
21 | | - public HtmlEqualException(IEnumerable<IDiff> diffs, IMarkupFormattable expected, IMarkupFormattable actual, string? userMessage) |
22 | | - : base(PrintHtml(actual), PrintHtml(expected), "Actual HTML", "Expected HTML", CreateUserMessage(diffs, userMessage)) |
23 | | - { |
24 | | - } |
| 13 | + /// <summary> |
| 14 | + /// Represents an differences between pieces of markup. |
| 15 | + /// </summary> |
| 16 | + [SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "<Pending>")] |
| 17 | + public class HtmlEqualException : ActualExpectedAssertException |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Creates an instance of the <see cref="HtmlEqualException"/> type. |
| 21 | + /// </summary> |
| 22 | + public HtmlEqualException(IEnumerable<IDiff> diffs, IMarkupFormattable expected, IMarkupFormattable actual, string? userMessage) |
| 23 | + : base(actual.PrintHtml(), expected.PrintHtml(), "Actual HTML", "Expected HTML", CreateUserMessage(diffs, userMessage)) |
| 24 | + { |
| 25 | + } |
25 | 26 |
|
26 | | - private static string CreateUserMessage(IEnumerable<IDiff> diffs, string? userMessage) |
27 | | - { |
28 | | - return $"HTML comparison failed. {userMessage}{Environment.NewLine}{Environment.NewLine}The following errors were found:{Environment.NewLine}{PrintDiffs(diffs)}"; |
29 | | - } |
| 27 | + private static string CreateUserMessage(IEnumerable<IDiff> diffs, string? userMessage) |
| 28 | + { |
| 29 | + return $"HTML comparison failed. {userMessage}{Environment.NewLine}{Environment.NewLine}The following errors were found:{Environment.NewLine}{PrintDiffs(diffs)}"; |
| 30 | + } |
30 | 31 |
|
31 | | - [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "<Pending>")] |
32 | | - private static string PrintDiffs(IEnumerable<IDiff> diffs) |
33 | | - { |
34 | | - return string.Join(Environment.NewLine, diffs.Select((x, i) => |
35 | | - { |
36 | | - var diffText = x switch |
37 | | - { |
38 | | - NodeDiff diff => $"The expected {diff.Control.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Control.Path} and the actual {diff.Test.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Test.Path} are different.", |
39 | | - AttrDiff diff => $"The value of the expected attribute {diff.Control.Path} and actual attribute {diff.Test.Path} are different.", |
40 | | - MissingNodeDiff diff => $"The {diff.Control.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Control.Path} is missing.", |
41 | | - MissingAttrDiff diff => $"The attribute at {diff.Control.Path} is missing.", |
42 | | - UnexpectedNodeDiff diff => $"The {diff.Test.Node.NodeType.ToString().ToLowerInvariant()} at {diff.Test.Path} was not expected.", |
43 | | - UnexpectedAttrDiff diff => $"The attribute at {diff.Test.Path} was not expected.", |
44 | | - _ => throw new InvalidOperationException($"Unknown diff type detected: {x.GetType()}") |
45 | | - }; |
46 | | - return $" {i + 1}: {diffText}"; |
47 | | - })) + Environment.NewLine; |
48 | | - } |
| 32 | + [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "<Pending>")] |
| 33 | + private static string PrintDiffs(IEnumerable<IDiff> diffs) |
| 34 | + { |
| 35 | + return string.Join(Environment.NewLine, diffs.Select((x, i) => |
| 36 | + { |
| 37 | + var diffText = x switch |
| 38 | + { |
| 39 | + NodeDiff diff when diff.Target == DiffTarget.Text && diff.Control.Path.Equals(diff.Test.Path) |
| 40 | + => $"The text in {diff.Control.Path} is different.", |
| 41 | + NodeDiff diff when diff.Target == DiffTarget.Text |
| 42 | + => $"The expected {diff.Control.NodeName()} at {diff.Control.Path} and the actual {diff.Test.NodeName()} at {diff.Test.Path} is different.", |
| 43 | + NodeDiff diff when diff.Control.Path.Equals(diff.Test.Path) |
| 44 | + => $"The {diff.Control.NodeName()}s at {diff.Control.Path} are different.", |
| 45 | + NodeDiff diff => $"The expected {diff.Control.NodeName()} at {diff.Control.Path} and the actual {diff.Test.NodeName()} at {diff.Test.Path} are different.", |
| 46 | + AttrDiff diff when diff.Control.Path.Equals(diff.Test.Path) |
| 47 | + => $"The values of the attributes at {diff.Control.Path} are different.", |
| 48 | + AttrDiff diff => $"The value of the attribute {diff.Control.Path} and actual attribute {diff.Test.Path} are different.", |
| 49 | + MissingNodeDiff diff => $"The {diff.Control.NodeName()} at {diff.Control.Path} is missing.", |
| 50 | + MissingAttrDiff diff => $"The attribute at {diff.Control.Path} is missing.", |
| 51 | + UnexpectedNodeDiff diff => $"The {diff.Test.NodeName()} at {diff.Test.Path} was not expected.", |
| 52 | + UnexpectedAttrDiff diff => $"The attribute at {diff.Test.Path} was not expected.", |
| 53 | + _ => throw new InvalidOperationException($"Unknown diff type detected: {x.GetType()}") |
| 54 | + }; |
| 55 | + return $" {i + 1}: {diffText}"; |
| 56 | + })) + Environment.NewLine; |
| 57 | + } |
| 58 | + } |
49 | 59 |
|
50 | | - private static string PrintHtml(IMarkupFormattable nodes) => nodes.ToDiffMarkup() + Environment.NewLine; |
51 | | - } |
| 60 | + internal static class ComparisonFormatHelpers |
| 61 | + { |
| 62 | + public static string NodeName(this ComparisonSource source) => source.Node.NodeType.ToString().ToLowerInvariant(); |
| 63 | + public static string PrintHtml(this IMarkupFormattable nodes) => nodes.ToDiffMarkup() + Environment.NewLine; |
| 64 | + } |
52 | 65 | } |
0 commit comments