Skip to content

Commit 67d51f3

Browse files
committed
refactor: Renamed TestRenderer to BunitRenderer
1 parent e02f42a commit 67d51f3

16 files changed

+118
-88
lines changed

src/bunit/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static void MarkupMatches(this RenderedFragment actual, RenderFragment ex
274274

275275
// TODO: This will be obsolete with: https://github.com/bUnit-dev/bUnit/issues/1018
276276
// As the renderer would be transient we don't have to new up an instance
277-
using var renderer = new TestRenderer(
277+
using var renderer = new BunitRenderer(
278278
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
279279
actual.Services.GetRequiredService<TestServiceProvider>(),
280280
actual.Services.GetRequiredService<ILoggerFactory>());

src/bunit/Diffing/HtmlComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Bunit.Diffing;
99

1010
/// <summary>
11-
/// Represents a test HTML comparer, that is configured to work with markup generated by the <see cref="TestRenderer"/> and <see cref="Htmlizer"/> classes.
11+
/// Represents a test HTML comparer, that is configured to work with markup generated by the <see cref="BunitRenderer"/> and <see cref="Htmlizer"/> classes.
1212
/// </summary>
1313
public sealed class HtmlComparer
1414
{

src/bunit/EventDispatchExtensions/TriggerEventDispatchExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static Task TriggerEventAsync(this IElement element, string eventName, Ev
6363
ArgumentNullException.ThrowIfNull(eventName);
6464

6565
var renderer = element.GetTestContext()?.Renderer
66-
?? throw new InvalidOperationException($"Blazor events can only be raised on elements rendered with the Blazor test renderer '{nameof(TestRenderer)}'.");
66+
?? throw new InvalidOperationException($"Blazor events can only be raised on elements rendered with the Blazor test renderer '{nameof(BunitRenderer)}'.");
6767

6868
// TriggerEventsAsync will traverse the DOM tree to find
6969
// all event handlers that needs to be triggered. This is done
@@ -78,7 +78,7 @@ public static Task TriggerEventAsync(this IElement element, string eventName, Ev
7878
}
7979

8080
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "HTML events are standardize to lower case and safe in this context.")]
81-
private static Task TriggerEventsAsync(TestRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
81+
private static Task TriggerEventsAsync(BunitRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
8282
{
8383
var isNonBubblingEvent = NonBubblingEvents.Contains(eventName.ToLowerInvariant());
8484
var unwrappedElement = element.Unwrap();
@@ -88,7 +88,7 @@ private static Task TriggerEventsAsync(TestRenderer renderer, IElement element,
8888
: TriggerBubblingEventAsync(renderer, unwrappedElement, eventName, eventArgs);
8989
}
9090

91-
private static Task TriggerNonBubblingEventAsync(TestRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
91+
private static Task TriggerNonBubblingEventAsync(BunitRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
9292
{
9393
var eventAttrName = Htmlizer.ToBlazorAttribute(eventName);
9494

@@ -105,7 +105,7 @@ private static Task TriggerNonBubblingEventAsync(TestRenderer renderer, IElement
105105
throw new MissingEventHandlerException(element, eventName);
106106
}
107107

108-
private static Task TriggerBubblingEventAsync(TestRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
108+
private static Task TriggerBubblingEventAsync(BunitRenderer renderer, IElement element, string eventName, EventArgs eventArgs)
109109
{
110110
var eventTasks = GetDispatchEventTasks(renderer, element, eventName, eventArgs);
111111

@@ -118,7 +118,7 @@ private static Task TriggerBubblingEventAsync(TestRenderer renderer, IElement el
118118
}
119119

120120
private static List<Task> GetDispatchEventTasks(
121-
TestRenderer renderer,
121+
BunitRenderer renderer,
122122
IElement element,
123123
string eventName,
124124
EventArgs eventArgs)

src/bunit/Extensions/TestRendererExtensions.cs renamed to src/bunit/Extensions/BunitRendererExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Bunit.Extensions;
44

55
/// <summary>
6-
/// Helper methods that make it easier to work directly with a <see cref="TestRenderer"/>
6+
/// Helper methods that make it easier to work directly with a <see cref="BunitRenderer"/>
77
/// in bUnit web.
88
/// </summary>
9-
public static class TestRendererExtensions
9+
public static class BunitRendererExtensions
1010
{
1111
/// <summary>
1212
/// Renders a <typeparamref name="TComponent"/> with the parameters <paramref name="parameters"/> passed to it.
@@ -15,7 +15,7 @@ public static class TestRendererExtensions
1515
/// <param name="renderer">The renderer to use.</param>
1616
/// <param name="parameters">The parameters to pass to the component.</param>
1717
/// <returns>A <see cref="RenderedComponent{TComponent}"/> that provides access to the rendered component.</returns>
18-
public static RenderedComponent<TComponent> RenderComponent<TComponent>(this TestRenderer renderer, params ComponentParameter[] parameters)
18+
public static RenderedComponent<TComponent> RenderComponent<TComponent>(this BunitRenderer renderer, params ComponentParameter[] parameters)
1919
where TComponent : IComponent
2020
{
2121
ArgumentNullException.ThrowIfNull(renderer);
@@ -34,7 +34,7 @@ public static RenderedComponent<TComponent> RenderComponent<TComponent>(this Tes
3434
/// <param name="renderer">The renderer to use.</param>
3535
/// <param name="parameterBuilder">The a builder to create parameters to pass to the component.</param>
3636
/// <returns>A <see cref="RenderedComponent{TComponent}"/> that provides access to the rendered component.</returns>
37-
public static RenderedComponent<TComponent> RenderComponent<TComponent>(this TestRenderer renderer, Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder)
37+
public static RenderedComponent<TComponent> RenderComponent<TComponent>(this BunitRenderer renderer, Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder)
3838
where TComponent : IComponent
3939
{
4040
ArgumentNullException.ThrowIfNull(renderer);

src/bunit/Extensions/RenderedFragmentInvokeAsyncExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Bunit;
88
public static class RenderedFragmentInvokeAsyncExtensions
99
{
1010
/// <summary>
11-
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="TestRenderer"/>.
11+
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="BunitRenderer"/>.
1212
/// </summary>
1313
/// <param name="renderedFragment">The rendered fragment whose dispatcher to invoke with.</param>
1414
/// <param name="workItem">The work item to execute on the renderer's thread.</param>
@@ -26,7 +26,7 @@ public static Task InvokeAsync(this RenderedFragment renderedFragment, Action wo
2626
}
2727

2828
/// <summary>
29-
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="TestRenderer"/>.
29+
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="BunitRenderer"/>.
3030
/// </summary>
3131
/// <param name="renderedFragment">The rendered component whose dispatcher to invoke with.</param>
3232
/// <param name="workItem">The work item to execute on the renderer's thread.</param>
@@ -44,7 +44,7 @@ public static Task InvokeAsync(this RenderedFragment renderedFragment, Func<Task
4444
}
4545

4646
/// <summary>
47-
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="TestRenderer"/>.
47+
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="BunitRenderer"/>.
4848
/// </summary>
4949
/// <param name="renderedFragment">The rendered component whose dispatcher to invoke with.</param>
5050
/// <param name="workItem">The work item to execute on the renderer's thread.</param>
@@ -62,7 +62,7 @@ public static Task<T> InvokeAsync<T>(this RenderedFragment renderedFragment, Fun
6262
}
6363

6464
/// <summary>
65-
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="TestRenderer"/>.
65+
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="BunitRenderer"/>.
6666
/// </summary>
6767
/// <param name="renderedFragment">The rendered component whose dispatcher to invoke with.</param>
6868
/// <param name="workItem">The work item to execute on the renderer's thread.</param>

src/bunit/Extensions/WaitForHelpers/WaitForHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class WaitForHelper<T> : IDisposable
1515
private readonly Func<(bool CheckPassed, T Content)> completeChecker;
1616
private readonly RenderedFragment renderedFragment;
1717
private readonly ILogger<WaitForHelper<T>> logger;
18-
private readonly TestRenderer renderer;
18+
private readonly BunitRenderer renderer;
1919
private bool isDisposed;
2020
private int checkCount;
2121
private Exception? capturedException;

src/bunit/Rendering/BunitHtmlParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class BunitHtmlParser : IDisposable
2525

2626
/// <summary>
2727
/// Initializes a new instance of the <see cref="BunitHtmlParser"/> class
28-
/// with a AngleSharp context without a <see cref="TestRenderer"/> registered.
28+
/// with a AngleSharp context without a <see cref="BunitRenderer"/> registered.
2929
/// </summary>
3030
public BunitHtmlParser()
3131
: this(Configuration.Default.WithCss().With(new HtmlComparer())) { }

src/bunit/Rendering/TestRenderer.cs renamed to src/bunit/Rendering/BunitRenderer.cs

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
namespace Bunit.Rendering;
77

88
/// <summary>
9-
/// Represents a bUnit <see cref="TestRenderer"/> used to render Blazor components and fragments during bUnit tests.
9+
/// Represents a bUnit <see cref="BunitRenderer"/> used to render Blazor components and fragments during bUnit tests.
1010
/// </summary>
11-
public sealed class TestRenderer : Renderer
11+
public sealed class BunitRenderer : Renderer
1212
{
1313
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_isBatchInProgress")]
14-
extern static ref bool GetIsBatchInProgressField(Renderer renderer);
14+
private static extern ref bool GetIsBatchInProgressField(Renderer renderer);
1515

1616
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "SetDirectParameters")]
17-
extern static void CallSetDirectParameters(ComponentState componentState, ParameterView parameters);
17+
private static extern void CallSetDirectParameters(ComponentState componentState, ParameterView parameters);
1818

1919
private readonly object renderTreeUpdateLock = new();
2020
private readonly Dictionary<int, RenderedFragment> renderedComponents = new();
2121
private readonly List<RootComponent> rootComponents = new();
22-
private readonly ILogger<TestRenderer> logger;
22+
private readonly ILogger<BunitRenderer> logger;
2323
private readonly IRenderedComponentActivator activator;
2424
private bool disposed;
2525
private TaskCompletionSource<Exception> unhandledExceptionTsc = new(TaskCreationOptions.RunContinuationsAsynchronously);
@@ -39,7 +39,10 @@ private bool IsBatchInProgress
3939
}
4040
}
4141

42-
/// <inheritdoc/>
42+
/// <summary>
43+
/// Gets a <see cref="Task{Exception}"/>, which completes when an unhandled exception
44+
/// is thrown during the rendering of a component, that is caught by the renderer.
45+
/// </summary>
4346
public Task<Exception> UnhandledException => unhandledExceptionTsc.Task;
4447

4548
/// <inheritdoc/>
@@ -51,32 +54,41 @@ private bool IsBatchInProgress
5154
internal int RenderCount { get; private set; }
5255

5356
/// <summary>
54-
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
57+
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
5558
/// </summary>
56-
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
59+
public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
5760
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
5861
{
59-
logger = loggerFactory.CreateLogger<TestRenderer>();
62+
logger = loggerFactory.CreateLogger<BunitRenderer>();
6063
activator = renderedComponentActivator;
6164
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
6265
}
6366

6467
/// <summary>
65-
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
68+
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
6669
/// </summary>
67-
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
70+
public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
6871
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
6972
{
70-
logger = loggerFactory.CreateLogger<TestRenderer>();
73+
logger = loggerFactory.CreateLogger<BunitRenderer>();
7174
activator = renderedComponentActivator;
7275
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
7376
}
7477

75-
/// <inheritdoc/>
78+
/// <summary>
79+
/// Renders the <paramref name="renderFragment"/>.
80+
/// </summary>
81+
/// <param name="renderFragment">The <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> to render.</param>
82+
/// <returns>A <see cref="RenderedFragment"/> that provides access to the rendered <paramref name="renderFragment"/>.</returns>
7683
public RenderedFragment RenderFragment(RenderFragment renderFragment)
7784
=> Render(renderFragment, id => activator.CreateRenderedFragment(id));
7885

79-
/// <inheritdoc/>
86+
/// <summary>
87+
/// Renders a <typeparamref name="TComponent"/> with the <paramref name="parameters"/> passed to it.
88+
/// </summary>
89+
/// <typeparam name="TComponent">The type of component to render.</typeparam>
90+
/// <param name="parameters">The parameters to pass to the component.</param>
91+
/// <returns>A <see cref="RenderedComponent{TComponent}"/> that provides access to the rendered component.</returns>
8092
public RenderedComponent<TComponent> RenderComponent<TComponent>(ComponentParameterCollection parameters)
8193
where TComponent : IComponent
8294
{
@@ -86,14 +98,26 @@ public RenderedComponent<TComponent> RenderComponent<TComponent>(ComponentParame
8698
return Render(renderFragment, id => activator.CreateRenderedComponent<TComponent>(id));
8799
}
88100

89-
/// <inheritdoc/>
101+
/// <summary>
102+
/// Notifies the renderer that an event has occurred.
103+
/// </summary>
104+
/// <param name="eventHandlerId">The <see cref="RenderTreeFrame.AttributeEventHandlerId"/> value from the original event attribute.</param>
105+
/// <param name="fieldInfo">Information that the renderer can use to update the state of the existing render tree to match the UI.</param>
106+
/// <param name="eventArgs">Arguments to be passed to the event handler.</param>
107+
/// <returns>A <see cref="Task"/> which will complete once all asynchronous processing related to the event has completed.</returns>
90108
public new Task DispatchEventAsync(
91109
ulong eventHandlerId,
92110
EventFieldInfo fieldInfo,
93111
EventArgs eventArgs) => DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs, ignoreUnknownEventHandlers: false);
94112

95-
/// <exception cref="ObjectDisposedException"></exception>
96-
/// <inheritdoc/>
113+
/// <summary>
114+
/// Notifies the renderer that an event has occurred.
115+
/// </summary>
116+
/// <param name="eventHandlerId">The <see cref="RenderTreeFrame.AttributeEventHandlerId"/> value from the original event attribute.</param>
117+
/// <param name="fieldInfo">Information that the renderer can use to update the state of the existing render tree to match the UI.</param>
118+
/// <param name="eventArgs">Arguments to be passed to the event handler.</param>
119+
/// <param name="ignoreUnknownEventHandlers">Set to true to ignore the <see cref="UnknownEventHandlerIdException"/>.</param>
120+
/// <returns>A <see cref="Task"/> which will complete once all asynchronous processing related to the event has completed.</returns>
97121
public new Task DispatchEventAsync(
98122
ulong eventHandlerId,
99123
EventFieldInfo fieldInfo,
@@ -142,7 +166,11 @@ public RenderedComponent<TComponent> RenderComponent<TComponent>(ComponentParame
142166
}
143167
}
144168

145-
/// <inheritdoc/>
169+
/// <summary>
170+
/// Performs a depth-first search for the first <typeparamref name="TComponent"/> child component of the <paramref name="parentComponent"/>.
171+
/// </summary>
172+
/// <typeparam name="TComponent">Type of component to find.</typeparam>
173+
/// <param name="parentComponent">Parent component to search.</param>
146174
public RenderedComponent<TComponent> FindComponent<TComponent>(RenderedFragment parentComponent)
147175
where TComponent : IComponent
148176
{
@@ -152,12 +180,18 @@ public RenderedComponent<TComponent> FindComponent<TComponent>(RenderedFragment
152180
: throw new ComponentNotFoundException(typeof(TComponent));
153181
}
154182

155-
/// <inheritdoc/>
183+
/// <summary>
184+
/// Performs a depth-first search for all <typeparamref name="TComponent"/> child components of the <paramref name="parentComponent"/>.
185+
/// </summary>
186+
/// <typeparam name="TComponent">Type of components to find.</typeparam>
187+
/// <param name="parentComponent">Parent component to search.</param>
156188
public IReadOnlyList<RenderedComponent<TComponent>> FindComponents<TComponent>(RenderedFragment parentComponent)
157189
where TComponent : IComponent
158190
=> FindComponents<TComponent>(parentComponent, int.MaxValue);
159191

160-
/// <inheritdoc />
192+
/// <summary>
193+
/// Disposes all components rendered by the <see cref="BunitRenderer" />.
194+
/// </summary>
161195
public void DisposeComponents()
162196
{
163197
ObjectDisposedException.ThrowIf(disposed, this);
@@ -225,7 +259,7 @@ internal Task SetDirectParametersAsync(RenderedFragment renderedComponent, Param
225259

226260
return result;
227261

228-
static void SetDirectParametersViaComponentState(TestRenderer renderer, int componentId, in ParameterView parameters)
262+
static void SetDirectParametersViaComponentState(BunitRenderer renderer, int componentId, in ParameterView parameters)
229263
{
230264
var componentState = renderer.GetComponentState(componentId);
231265
CallSetDirectParameters(componentState, parameters);
@@ -534,8 +568,10 @@ private ArrayRange<RenderTreeFrame> GetOrLoadRenderTreeFrame(RenderTreeFrameDict
534568
}
535569

536570
/// <inheritdoc/>
537-
protected override void HandleException([NotNull] Exception exception)
571+
protected override void HandleException(Exception exception)
538572
{
573+
ArgumentNullException.ThrowIfNull(exception);
574+
539575
if (disposed)
540576
return;
541577

0 commit comments

Comments
 (0)