Skip to content

Commit 7f40a2c

Browse files
committed
Provide an extension method registering the default services, and add HtmlParser, HtmlComparer to the services collection by default.
Closes issue #114
1 parent 2542c0d commit 7f40a2c

15 files changed

Lines changed: 66 additions & 30 deletions

src/bunit.web.tests/Mocking/JSInterop/JsRuntimeAssertExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void Test201()
117117
[Fact(DisplayName = "ShouldBeElementReferenceTo throws if element reference does not point to the provided element")]
118118
public void Test202()
119119
{
120-
using var htmlParser = new TestHtmlParser();
120+
using var htmlParser = new HtmlParser();
121121
var elmRef = new ElementReference(Guid.NewGuid().ToString());
122122
var elm = (IElement)htmlParser.Parse($"<p {Htmlizer.ELEMENT_REFERENCE_ATTR_NAME}=\"ASDF\" />").First();
123123

File renamed without changes.

src/bunit.web/Asserting/CompareToDiffingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
2525
if (actual is null) throw new ArgumentNullException(nameof(actual));
2626
if (expected is null) throw new ArgumentNullException(nameof(expected));
2727

28-
var htmlParser = actual.Services.GetRequiredService<TestHtmlParser>();
28+
var htmlParser = actual.Services.GetRequiredService<HtmlParser>();
2929
var expectedNodes = htmlParser.Parse(expected);
3030

3131
return actual.Nodes.CompareTo(expectedNodes);

src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
2424
if (actual is null) throw new ArgumentNullException(nameof(actual));
2525
if (expected is null) throw new ArgumentNullException(nameof(expected));
2626

27-
var htmlParser = actual.Services.GetRequiredService<TestHtmlParser>();
27+
var htmlParser = actual.Services.GetRequiredService<HtmlParser>();
2828
var expectedNodes = htmlParser.Parse(expected);
2929

3030
actual.Nodes.MarkupMatches(expectedNodes, userMessage);
@@ -100,7 +100,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
100100
}
101101
else
102102
{
103-
using var newParser = new TestHtmlParser();
103+
using var newParser = new HtmlParser();
104104
expectedNodes = newParser.Parse(expected);
105105
}
106106
MarkupMatches(actual, expectedNodes, userMessage);
@@ -126,7 +126,7 @@ public static void MarkupMatches(this INodeList actual, string expected, string?
126126
}
127127
else
128128
{
129-
using var newParser = new TestHtmlParser();
129+
using var newParser = new HtmlParser();
130130
expectedNodes = newParser.Parse(expected);
131131
}
132132
MarkupMatches(actual, expectedNodes, userMessage);

src/bunit.web/Asserting/ShouldBeAdditionAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void ShouldBeAddition(this IDiff actualChange, string expectedChan
3535
}
3636
else
3737
{
38-
using var newParser = new TestHtmlParser();
38+
using var newParser = new HtmlParser();
3939
expected = newParser.Parse(expectedChange);
4040
}
4141

src/bunit.web/Asserting/ShouldBeRemovalAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void ShouldBeRemoval(this IDiff actualChange, string expectedChang
3535
}
3636
else
3737
{
38-
using var newParser = new TestHtmlParser();
38+
using var newParser = new HtmlParser();
3939
expected = newParser.Parse(expectedChange);
4040
}
4141

src/bunit.web/Asserting/ShouldBeTextChangeAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void ShouldBeTextChange(this IDiff actualChange, string expectedCh
3939

4040
var actual = actualChange as NodeDiff ?? throw new DiffChangeAssertException(actualChange.Result, DiffResult.Different, "The change was not a text change.");
4141

42-
var parser = actual.Control.Node.Owner.Context.GetService<TestHtmlParser>();
42+
var parser = actual.Control.Node.Owner.Context.GetService<HtmlParser>();
4343
var expected = parser.Parse(expectedChange);
4444

4545
ShouldBeTextChange(actualChange, expected, userMessage);
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Bunit.Diffing
1010
/// A AngleSharp based HTML Parse that can parse markup strings
1111
/// into a <see cref="INodeList"/>.
1212
/// </summary>
13-
public sealed class TestHtmlParser : IDisposable
13+
public sealed class HtmlParser : IDisposable
1414
{
1515
private readonly IBrowsingContext _context;
1616
private readonly IHtmlParser _htmlParser;
@@ -20,7 +20,7 @@ public sealed class TestHtmlParser : IDisposable
2020
/// Creates an instance of the parser with a AngleSharp context
2121
/// without a <see cref="TestRenderer"/> registered.
2222
/// </summary>
23-
public TestHtmlParser()
23+
public HtmlParser()
2424
{
2525
var config = Configuration.Default
2626
.WithCss()
@@ -36,13 +36,12 @@ public TestHtmlParser()
3636
/// Creates an instance of the parser with a AngleSharp context
3737
/// with the <paramref name="testRenderer"/> registered.
3838
/// </summary>
39-
/// <param name="testRenderer"></param>
40-
public TestHtmlParser(ITestRenderer testRenderer)
39+
public HtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer)
4140
{
4241
var config = Configuration.Default
4342
.WithCss()
4443
.With(testRenderer)
45-
.With(new HtmlComparer())
44+
.With(htmlComparer)
4645
.With(this);
4746

4847
_context = BrowsingContext.New(config);

src/bunit.web/EventDispatchExtensions/GeneralEventDispatchExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public static Task TriggerEventAsync(this IElement element, string eventName, Ev
2626
{
2727
if (element is null) throw new ArgumentNullException(nameof(element));
2828

29-
var eventHandlerIdString = element.GetAttribute(Htmlizer.ToBlazorAttribute(eventName));
29+
var blazorEventAttr = Htmlizer.ToBlazorAttribute(eventName);
30+
var eventHandlerIdString = element.GetAttribute(blazorEventAttr);
3031

31-
if (string.IsNullOrEmpty(eventHandlerIdString))
32+
if (string.IsNullOrEmpty(eventHandlerIdString))
3233
throw new MissingEventHandlerException(element, eventName);
3334

3435
var eventHandlerId = ulong.Parse(eventHandlerIdString, CultureInfo.InvariantCulture);

src/bunit.web/Extensions/Internal/AngleSharpExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
2020
}
2121

2222
/// <summary>
23-
/// Gets the <see cref="TestHtmlParser"/> stored in the <paramref name="node"/>s
23+
/// Gets the <see cref="HtmlParser"/> stored in the <paramref name="node"/>s
2424
/// owning context, if one is available.
2525
/// </summary>
2626
/// <param name="node"></param>
27-
/// <returns>The <see cref="TestHtmlParser"/> or null if not found.</returns>
28-
public static TestHtmlParser? GetHtmlParser(this INode? node)
27+
/// <returns>The <see cref="HtmlParser"/> or null if not found.</returns>
28+
public static HtmlParser? GetHtmlParser(this INode? node)
2929
{
30-
return node?.Owner.Context.GetService<TestHtmlParser>();
30+
return node?.Owner.Context.GetService<HtmlParser>();
3131
}
3232

3333
/// <summary>
34-
/// Gets the <see cref="TestHtmlParser"/> stored in the <paramref name="nodes"/>s
34+
/// Gets the <see cref="HtmlParser"/> stored in the <paramref name="nodes"/>s
3535
/// owning context, if one is available.
3636
/// </summary>
3737
/// <param name="nodes"></param>
38-
/// <returns>The <see cref="TestHtmlParser"/> or null if not found.</returns>
39-
public static TestHtmlParser? GetHtmlParser(this INodeList nodes)
38+
/// <returns>The <see cref="HtmlParser"/> or null if not found.</returns>
39+
public static HtmlParser? GetHtmlParser(this INodeList nodes)
4040
{
4141
return nodes?.Length > 0 ? nodes[0].GetHtmlParser() : null;
4242
}

0 commit comments

Comments
 (0)