Skip to content

Commit

Permalink
refactor: Use protected ctor instead of having field protected
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Jul 14, 2023
1 parent ce0067c commit 37fec4d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
34 changes: 27 additions & 7 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TestRenderer : Renderer, ITestRenderer
private static readonly FieldInfo IsBatchInProgressField = RendererType.GetField("_isBatchInProgress", BindingFlags.Instance | BindingFlags.NonPublic)!;
private static readonly MethodInfo GetRequiredComponentStateMethod = RendererType.GetMethod("GetRequiredComponentState", BindingFlags.Instance | BindingFlags.NonPublic)!;

private readonly SynchronizationContext? usersSyncContext = SynchronizationContext.Current;
private readonly object renderTreeUpdateLock = new();
private readonly Dictionary<int, IRenderedFragmentBase> renderedComponents = new();
private readonly List<RootComponent> rootComponents = new();
Expand All @@ -23,11 +24,6 @@ public class TestRenderer : Renderer, ITestRenderer
private TaskCompletionSource<Exception> unhandledExceptionTsc = new(TaskCreationOptions.RunContinuationsAsynchronously);
private Exception? capturedUnhandledException;

/// <summary>
/// The users <see cref="SynchronizationContext"/> if any.
/// </summary>
protected SynchronizationContext? UsersSyncContext = SynchronizationContext.Current;

private bool IsBatchInProgress
{
#pragma warning disable S1144 // Unused private types or members should be removed
Expand All @@ -48,6 +44,16 @@ private bool IsBatchInProgress
internal int RenderCount { get; private set; }

#if NETSTANDARD
/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
protected TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services,
ILoggerFactory loggerFactory, SynchronizationContext? context)
: this(renderedComponentActivator, services, loggerFactory)
{
usersSyncContext = context;

}
/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
Expand All @@ -58,6 +64,20 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
activator = renderedComponentActivator;
}
#elif NET5_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
protected TestRenderer(
IRenderedComponentActivator renderedComponentActivator,
TestServiceProvider services,
ILoggerFactory loggerFactory,
SynchronizationContext? context)
: this(renderedComponentActivator, services, loggerFactory)
{
usersSyncContext = context;
}


/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
Expand Down Expand Up @@ -339,7 +359,7 @@ void LoadChangesIntoRenderEvent(int componentId)

void InvokeApplyRenderEvent()
{
if (UsersSyncContext is not null && UsersSyncContext != SynchronizationContext.Current)
if (usersSyncContext is not null && usersSyncContext != SynchronizationContext.Current)
{
// The users' sync context, typically one established by
// xUnit or another testing framework is used to update any
Expand All @@ -354,7 +374,7 @@ void InvokeApplyRenderEvent()
// CPU cache updates not happening immediately).
//
// There is no guarantee a caller/test framework has set a sync context.
UsersSyncContext.Send(_ => ApplyRenderEvent(renderEvent), null);
usersSyncContext.Send(_ => ApplyRenderEvent(renderEvent), null);
}
else
{
Expand Down
5 changes: 1 addition & 4 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,7 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
if (expected is null)
throw new ArgumentNullException(nameof(expected));

using var detachedRenderer = new MarkupMatchesRenderer(
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
actual.Services.GetRequiredService<TestServiceProvider>(),
actual.Services.GetRequiredService<ILoggerFactory>());
var detachedRenderer = actual.Services.GetRequiredService<MarkupMatchesRenderer>();

var renderedFragment = (IRenderedFragment)detachedRenderer.RenderFragment(expected);
MarkupMatches(actual, renderedFragment, userMessage);
Expand Down
14 changes: 1 addition & 13 deletions src/bunit.web/Rendering/Internal/MarkupMatchesRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,7 @@ internal sealed class MarkupMatchesRenderer : TestRenderer
/// Initializes a new instance of the <see cref="MarkupMatchesRenderer"/> class.
/// </summary>
public MarkupMatchesRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
: base(renderedComponentActivator, services, loggerFactory)
: base(renderedComponentActivator, services, loggerFactory, (SynchronizationContext?)null)
{
UsersSyncContext = null;
}

#if NET5_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="MarkupMatchesRenderer"/> class.
/// </summary>
public MarkupMatchesRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
: base(renderedComponentActivator, services, loggerFactory, componentActivator)
{
UsersSyncContext = null;
}
#endif
}

0 comments on commit 37fec4d

Please sign in to comment.