-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathShouldBeAdditionAssertExtensions.cs
More file actions
87 lines (78 loc) · 3.71 KB
/
ShouldBeAdditionAssertExtensions.cs
File metadata and controls
87 lines (78 loc) · 3.71 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
using System;
using System.Linq;
using AngleSharp.Diffing.Core;
using AngleSharp.Dom;
using Bunit.Diffing;
using Bunit.Rendering;
namespace Bunit
{
/// <summary>
/// A set of addition diff assert extensions
/// </summary>
public static class ShouldBeAdditionAssertExtensions
{
/// <summary>
/// Verifies that the <paramref name="actualChange"/> <see cref="IDiff"/> is an addition,
/// i.e. that one or more nodes have been added, and verifies that the additions are equal
/// to the markup specified in the <paramref name="expectedChange"/> input.
/// </summary>
/// <param name="actualChange">The change to verify</param>
/// <param name="expectedChange">The expected additions to verify against</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
public static void ShouldBeAddition(this IDiff actualChange, string expectedChange, string? userMessage = null)
{
if (actualChange is null)
throw new ArgumentNullException(nameof(actualChange));
if (expectedChange is null)
throw new ArgumentNullException(nameof(expectedChange));
var actual = actualChange as UnexpectedNodeDiff ?? throw new DiffChangeAssertException(actualChange.Result, DiffResult.Unexpected, "The change was not an addition.");
INodeList expected;
if (actual.Test.Node.GetHtmlParser() is BunitHtmlParser parser)
{
expected = parser.Parse(expectedChange);
}
else
{
using var newParser = new BunitHtmlParser();
expected = newParser.Parse(expectedChange);
}
ShouldBeAddition(actualChange, expected, userMessage);
}
/// <summary>
/// Verifies that the <paramref name="actualChange"/> <see cref="IDiff"/> is an addition,
/// i.e. that one or more nodes have been added, and verifies that the additions are equal
/// to the rendered markup from the <paramref name="expectedChange"/> <see cref="IRenderedFragmentBase"/>.
/// </summary>
/// <param name="actualChange">The change to verify</param>
/// <param name="expectedChange">The expected additions to verify against</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
public static void ShouldBeAddition(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
{
if (expectedChange is null)
throw new ArgumentNullException(nameof(expectedChange));
ShouldBeAddition(actualChange, expectedChange.Nodes, userMessage);
}
/// <summary>
/// Verifies that the <paramref name="actualChange"/> <see cref="IDiff"/> is an addition,
/// i.e. that one or more nodes have been added, and verifies that the additions are equal
/// to the <paramref name="expectedChange"/> nodes.
/// </summary>
/// <param name="actualChange">The change to verify</param>
/// <param name="expectedChange">The expected additions to verify against</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
public static void ShouldBeAddition(this IDiff actualChange, INodeList expectedChange, string? userMessage = null)
{
if (actualChange is null)
throw new ArgumentNullException(nameof(actualChange));
if (expectedChange is null)
throw new ArgumentNullException(nameof(expectedChange));
var actual = actualChange as UnexpectedNodeDiff ?? throw new DiffChangeAssertException(actualChange.Result, DiffResult.Unexpected, "The change was not an addition.");
var comparer = actual.Test.Node.GetHtmlComparer() ?? new HtmlComparer();
var diffs = comparer.Compare(expectedChange, actual.Test.Node.AsEnumerable()).ToList();
if (diffs.Count != 0)
{
throw new HtmlEqualException(diffs, expectedChange, actual.Test.Node, userMessage);
}
}
}
}