Skip to content

Commit

Permalink
Provide an extension method registering the default services, and add…
Browse files Browse the repository at this point in the history
… HtmlParser, HtmlComparer to the services collection by default.

Closes issue #114
  • Loading branch information
egil committed May 4, 2020
1 parent 2542c0d commit 7f40a2c
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void Test201()
[Fact(DisplayName = "ShouldBeElementReferenceTo throws if element reference does not point to the provided element")]
public void Test202()
{
using var htmlParser = new TestHtmlParser();
using var htmlParser = new HtmlParser();
var elmRef = new ElementReference(Guid.NewGuid().ToString());
var elm = (IElement)htmlParser.Parse($"<p {Htmlizer.ELEMENT_REFERENCE_ATTR_NAME}=\"ASDF\" />").First();

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/bunit.web/Asserting/CompareToDiffingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
if (actual is null) throw new ArgumentNullException(nameof(actual));
if (expected is null) throw new ArgumentNullException(nameof(expected));

var htmlParser = actual.Services.GetRequiredService<TestHtmlParser>();
var htmlParser = actual.Services.GetRequiredService<HtmlParser>();
var expectedNodes = htmlParser.Parse(expected);

return actual.Nodes.CompareTo(expectedNodes);
Expand Down
6 changes: 3 additions & 3 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
if (actual is null) throw new ArgumentNullException(nameof(actual));
if (expected is null) throw new ArgumentNullException(nameof(expected));

var htmlParser = actual.Services.GetRequiredService<TestHtmlParser>();
var htmlParser = actual.Services.GetRequiredService<HtmlParser>();
var expectedNodes = htmlParser.Parse(expected);

actual.Nodes.MarkupMatches(expectedNodes, userMessage);
Expand Down Expand Up @@ -100,7 +100,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
}
else
{
using var newParser = new TestHtmlParser();
using var newParser = new HtmlParser();
expectedNodes = newParser.Parse(expected);
}
MarkupMatches(actual, expectedNodes, userMessage);
Expand All @@ -126,7 +126,7 @@ public static void MarkupMatches(this INodeList actual, string expected, string?
}
else
{
using var newParser = new TestHtmlParser();
using var newParser = new HtmlParser();
expectedNodes = newParser.Parse(expected);
}
MarkupMatches(actual, expectedNodes, userMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void ShouldBeAddition(this IDiff actualChange, string expectedChan
}
else
{
using var newParser = new TestHtmlParser();
using var newParser = new HtmlParser();
expected = newParser.Parse(expectedChange);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bunit.web/Asserting/ShouldBeRemovalAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void ShouldBeRemoval(this IDiff actualChange, string expectedChang
}
else
{
using var newParser = new TestHtmlParser();
using var newParser = new HtmlParser();
expected = newParser.Parse(expectedChange);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void ShouldBeTextChange(this IDiff actualChange, string expectedCh

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

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

ShouldBeTextChange(actualChange, expected, userMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Bunit.Diffing
/// A AngleSharp based HTML Parse that can parse markup strings
/// into a <see cref="INodeList"/>.
/// </summary>
public sealed class TestHtmlParser : IDisposable
public sealed class HtmlParser : IDisposable
{
private readonly IBrowsingContext _context;
private readonly IHtmlParser _htmlParser;
Expand All @@ -20,7 +20,7 @@ public sealed class TestHtmlParser : IDisposable
/// Creates an instance of the parser with a AngleSharp context
/// without a <see cref="TestRenderer"/> registered.
/// </summary>
public TestHtmlParser()
public HtmlParser()
{
var config = Configuration.Default
.WithCss()
Expand All @@ -36,13 +36,12 @@ public TestHtmlParser()
/// Creates an instance of the parser with a AngleSharp context
/// with the <paramref name="testRenderer"/> registered.
/// </summary>
/// <param name="testRenderer"></param>
public TestHtmlParser(ITestRenderer testRenderer)
public HtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer)
{
var config = Configuration.Default
.WithCss()
.With(testRenderer)
.With(new HtmlComparer())
.With(htmlComparer)
.With(this);

_context = BrowsingContext.New(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public static Task TriggerEventAsync(this IElement element, string eventName, Ev
{
if (element is null) throw new ArgumentNullException(nameof(element));

var eventHandlerIdString = element.GetAttribute(Htmlizer.ToBlazorAttribute(eventName));
var blazorEventAttr = Htmlizer.ToBlazorAttribute(eventName);
var eventHandlerIdString = element.GetAttribute(blazorEventAttr);

if (string.IsNullOrEmpty(eventHandlerIdString))
if (string.IsNullOrEmpty(eventHandlerIdString))
throw new MissingEventHandlerException(element, eventName);

var eventHandlerId = ulong.Parse(eventHandlerIdString, CultureInfo.InvariantCulture);
Expand Down
14 changes: 7 additions & 7 deletions src/bunit.web/Extensions/Internal/AngleSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
}

/// <summary>
/// Gets the <see cref="TestHtmlParser"/> stored in the <paramref name="node"/>s
/// Gets the <see cref="HtmlParser"/> stored in the <paramref name="node"/>s
/// owning context, if one is available.
/// </summary>
/// <param name="node"></param>
/// <returns>The <see cref="TestHtmlParser"/> or null if not found.</returns>
public static TestHtmlParser? GetHtmlParser(this INode? node)
/// <returns>The <see cref="HtmlParser"/> or null if not found.</returns>
public static HtmlParser? GetHtmlParser(this INode? node)
{
return node?.Owner.Context.GetService<TestHtmlParser>();
return node?.Owner.Context.GetService<HtmlParser>();
}

/// <summary>
/// Gets the <see cref="TestHtmlParser"/> stored in the <paramref name="nodes"/>s
/// Gets the <see cref="HtmlParser"/> stored in the <paramref name="nodes"/>s
/// owning context, if one is available.
/// </summary>
/// <param name="nodes"></param>
/// <returns>The <see cref="TestHtmlParser"/> or null if not found.</returns>
public static TestHtmlParser? GetHtmlParser(this INodeList nodes)
/// <returns>The <see cref="HtmlParser"/> or null if not found.</returns>
public static HtmlParser? GetHtmlParser(this INodeList nodes)
{
return nodes?.Length > 0 ? nodes[0].GetHtmlParser() : null;
}
Expand Down
34 changes: 34 additions & 0 deletions src/bunit.web/Extensions/TestServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bunit.Diffing;
using Bunit.Mocking.JSInterop;
using Bunit.Rendering;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;

namespace Bunit.Extensions
{
/// <summary>
/// Helper methods for correctly registering test dependencies
/// </summary>
public static class TestServiceProviderExtensions
{
/// <summary>
/// Registers the default services required by the web <see cref="TestContext"/>.
/// </summary>
public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services)
{
services.AddSingleton<IJSRuntime>(new PlaceholderJsRuntime());
services.AddSingleton<HtmlComparer>(srv => new HtmlComparer());
services.AddSingleton<HtmlParser>(srv => new HtmlParser(
srv.GetRequiredService<ITestRenderer>(),
srv.GetRequiredService<HtmlComparer>()
)
);
return services;
}
}
}
4 changes: 2 additions & 2 deletions src/bunit.web/RazorTesting/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Mocking.JSInterop;
using Bunit.RazorTesting;
using Bunit.Rendering;
Expand Down Expand Up @@ -165,8 +166,7 @@ private IRenderedComponent<TComponent> TryCastTo<TComponent>(IRenderedFragment t
/// <inheritdoc/>
protected override Task Run()
{
Services.AddSingleton<IJSRuntime>(new PlaceholderJsRuntime());
Services.AddSingleton<TestHtmlParser>(srv => new TestHtmlParser(srv.GetRequiredService<ITestRenderer>()));
Services.AddDefaultTestContextServices();
return base.Run(this);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/bunit.web/RazorTesting/SnapshotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;

using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Mocking.JSInterop;
using Bunit.RazorTesting;
using Bunit.Rendering;
Expand Down Expand Up @@ -45,7 +46,8 @@ public class SnapshotTest : RazorTestBase
protected override async Task Run()
{
Validate();
Services.AddSingleton<IJSRuntime>(new PlaceholderJsRuntime());

Services.AddDefaultTestContextServices();

if (Setup is { })
TryRun(Setup, this);
Expand All @@ -58,7 +60,7 @@ protected override async Task Run()
var expectedRenderId = Renderer.RenderFragment(ExpectedOutput);
var expectedHtml = Htmlizer.GetHtml(Renderer, expectedRenderId);

var parser = new TestHtmlParser();
var parser = new HtmlParser();
var inputNodes = parser.Parse(inputHtml);
var expectedNodes = parser.Parse(expectedHtml);

Expand Down
4 changes: 2 additions & 2 deletions src/bunit.web/Rendering/RenderedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RenderedFragment : IRenderedFragment
private INodeList? _latestRenderNodes;
private INodeList? _snapshotNodes;

private TestHtmlParser HtmlParser { get; }
private HtmlParser HtmlParser { get; }

/// <summary>
/// Gets the renderer used to render the <see cref="IRenderedFragmentBase"/>.
Expand Down Expand Up @@ -76,7 +76,7 @@ public RenderedFragment(IServiceProvider services, int componentId)
throw new ArgumentNullException(nameof(services));

Services = services;
HtmlParser = services.GetRequiredService<TestHtmlParser>();
HtmlParser = services.GetRequiredService<HtmlParser>();
Renderer = services.GetRequiredService<ITestRenderer>();
ComponentId = componentId;
RenderEvents = new RenderEventFilter(Renderer.RenderEvents, RenderFilter);
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.web/TestContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AngleSharp.Dom;
using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Mocking.JSInterop;
using Bunit.Rendering;
using Bunit.Rendering.RenderEvents;
Expand All @@ -22,8 +23,7 @@ public class TestContext : TestContextBase, ITestContext
/// </summary>
public TestContext()
{
Services.AddSingleton<IJSRuntime>(new PlaceholderJsRuntime());
Services.AddSingleton<TestHtmlParser>(srv => new TestHtmlParser(srv.GetRequiredService<ITestRenderer>()));
Services.AddDefaultTestContextServices();
}

/// <summary>
Expand Down

0 comments on commit 7f40a2c

Please sign in to comment.