Skip to content

Commit 0908052

Browse files
committed
Code cleanup
1 parent 7c11263 commit 0908052

38 files changed

Lines changed: 335 additions & 162 deletions

src/bunit.core/Asserting/ActualExpectedAssertException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace Bunit.Asserting
45
{
56
/// <summary>
67
/// Represents a generic assert exception used when an actual result does not match an expected result.
78
/// </summary>
9+
[Serializable]
810
public class ActualExpectedAssertException : Exception
911
{
1012
/// <summary>
@@ -20,6 +22,9 @@ public ActualExpectedAssertException(string actual, string expected, string actu
2022
{
2123
}
2224

25+
/// <inheritdoc/>
26+
protected ActualExpectedAssertException(SerializationInfo info, StreamingContext context) : base(info, context) { }
27+
2328
private static string CreateMessage(string actual, string expected, string actualText, string expectedText, string message)
2429
{
2530
return $"{message}{Environment.NewLine}{actualText}: {actual}{Environment.NewLine}{expectedText}: {expected}";

src/bunit.core/ComponentParameterCollection.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Bunit
1010
/// <summary>
1111
/// A collection for <see cref="ComponentParameter" />
1212
/// </summary>
13-
public class ComponentParameterCollection : ICollection<ComponentParameter>, IReadOnlyCollection<ComponentParameter>, IEnumerable<ComponentParameter>
13+
public class ComponentParameterCollection : ICollection<ComponentParameter>, IReadOnlyCollection<ComponentParameter>
1414
{
1515
private static readonly MethodInfo CreateTemplateWrapperMethod = GetCreateTemplateWrapperMethod();
1616
private static readonly Type CascadingValueType = typeof(CascadingValue<>);
@@ -207,11 +207,7 @@ void AddAttributes(RenderTreeBuilder builder)
207207
/// <inheritdoc/>
208208
public IEnumerator<ComponentParameter> GetEnumerator()
209209
{
210-
if (_parameters is null)
211-
{
212-
yield break;
213-
}
214-
else
210+
if (_parameters is not null)
215211
{
216212
for (int i = 0; i < _parameters.Count; i++)
217213
{
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace Bunit.Extensions.WaitForHelpers
45
{
56
/// <summary>
67
/// Represents an exception thrown when the <see cref="WaitForHelper"/> does not complete successfully.
78
/// </summary>
8-
public class WaitForFailedException : Exception
9+
[Serializable]
10+
public sealed class WaitForFailedException : Exception
911
{
1012
/// <summary>
1113
/// Creates an instance of the <see cref="WaitForFailedException"/>.
1214
/// </summary>
1315
public WaitForFailedException(string? errorMessage, Exception? innerException = null) : base(errorMessage ?? string.Empty, innerException)
1416
{
1517
}
18+
19+
private WaitForFailedException(SerializationInfo info, StreamingContext context) : base(info, context) { }
1620
}
1721
}

src/bunit.core/Extensions/WaitForHelpers/WaitForHelper.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,21 @@ private void OnTimeout(object? state)
131131
/// </summary>
132132
public void Dispose()
133133
{
134-
if (_isDisposed)
134+
Dispose(true);
135+
GC.SuppressFinalize(this);
136+
}
137+
138+
/// <summary>
139+
/// Disposes of the wait task and related logic.
140+
/// </summary>
141+
/// <remarks>
142+
/// The disposing parameter should be false when called from a finalizer, and true when called from the
143+
/// <see cref="Dispose()"/> method. In other words, it is true when deterministically called and false when non-deterministically called.
144+
/// </remarks>
145+
/// <param name="disposing">Set to true if called from <see cref="Dispose()"/>, false if called from a finalizer.f</param>
146+
protected virtual void Dispose(bool disposing)
147+
{
148+
if (_isDisposed || !disposing)
135149
return;
136150

137151
lock (_completionSouce)

src/bunit.core/IRenderedComponentBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Bunit
66
/// Represents a rendered component-under-test.
77
/// </summary>
88
/// <typeparam name="TComponent">The type of the component under test</typeparam>
9-
public interface IRenderedComponentBase<TComponent> : IRenderedFragmentBase where TComponent : IComponent
9+
public interface IRenderedComponentBase<out TComponent> : IRenderedFragmentBase where TComponent : IComponent
1010
{
1111
/// <summary>
1212
/// Gets the component under test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace Bunit.RazorTesting
5+
{
6+
/// <summary>
7+
/// Represents an missing or invalid Blazor parameter on a Blazor component.
8+
/// </summary>
9+
[Serializable]
10+
public sealed class ParameterException : ArgumentException
11+
{
12+
/// <summary>
13+
/// Creates an instance of the <see cref="ParameterException"/> class.
14+
/// </summary>
15+
/// <param name="messsage">Validation message.</param>
16+
/// <param name="parameterName">Name of the Blazor parameter.</param>
17+
public ParameterException(string messsage, string parameterName)
18+
: base(messsage, parameterName)
19+
{ }
20+
21+
private ParameterException(SerializationInfo serializationInfo, StreamingContext streamingContext)
22+
: base(serializationInfo, streamingContext)
23+
{ }
24+
}
25+
26+
}

src/bunit.core/RazorTesting/FixtureBase.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.Serialization;
34
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Components;
56

@@ -59,16 +60,15 @@ public override Task SetParametersAsync(ParameterView parameters)
5960
}
6061

6162
/// <inheritdoc/>
62-
[SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "Validating component parameters")]
6363
public override void Validate()
6464
{
6565
base.Validate();
6666
if (ChildContent is null)
67-
throw new ArgumentException($"No '{nameof(ChildContent)}' specified in the {GetType().Name} component.", nameof(ChildContent));
67+
throw new ParameterException($"No '{nameof(ChildContent)}' specified in the {GetType().Name} component.", nameof(ChildContent));
6868
if (Test is null && TestAsync is null)
69-
throw new ArgumentException($"No test action provided via the '{nameof(Test)}' or '{nameof(TestAsync)}' parameters to the {GetType().Name} component.", nameof(Test));
69+
throw new ParameterException($"No test action provided via the '{nameof(Test)}' or '{nameof(TestAsync)}' parameters to the {GetType().Name} component.", nameof(Test));
7070
if (Test is not null && TestAsync is not null)
71-
throw new ArgumentException($"Only one of the '{nameof(Test)}' or '{nameof(TestAsync)}' actions can be provided to the {GetType().Name} component at the same time.", nameof(Test));
71+
throw new ParameterException($"Only one of the '{nameof(Test)}' or '{nameof(TestAsync)}' actions can be provided to the {GetType().Name} component at the same time.", nameof(Test));
7272
}
7373

7474
/// <inheritdoc/>
@@ -88,4 +88,5 @@ protected virtual async Task Run(TFixture self)
8888
await TryRunAsync(TestAsync, self).ConfigureAwait(false);
8989
}
9090
}
91+
9192
}

src/bunit.core/RazorTesting/FragmentBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public abstract class FragmentBase : IComponent
1515
[Parameter] public RenderFragment ChildContent { get; set; } = default!;
1616

1717
/// <inheritdoc />
18-
public void Attach(RenderHandle renderHandle) { }
18+
public void Attach(RenderHandle renderHandle)
19+
{
20+
// Since this component just captures a render fragment for testing,
21+
// the renderHandler is not used for anything in this component.
22+
}
1923

2024
/// <inheritdoc />
2125
public virtual Task SetParametersAsync(ParameterView parameters)

src/bunit.core/RazorTesting/RazorTestBase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public virtual Task SetParametersAsync(ParameterView parameters)
7272
public virtual void Validate() { }
7373

7474
/// <inheritdoc/>
75-
void IComponent.Attach(RenderHandle renderHandle) { }
75+
void IComponent.Attach(RenderHandle renderHandle)
76+
{
77+
// Since this component just captures a render fragments and test settings for testing,
78+
// the renderHandler is not used for anything in this component.
79+
}
7680

7781
/// <summary>
7882
/// Implements the logic for running the test.
@@ -92,11 +96,11 @@ protected static void TryRun<T>(Action<T> action, T input)
9296
/// <summary>
9397
/// Try to run the <see cref="Func{T, Task}"/>.
9498
/// </summary>
95-
protected static async Task TryRunAsync<T>(Func<T, Task> action, T input)
99+
protected static Task TryRunAsync<T>(Func<T, Task> action, T input)
96100
{
97101
if (action is null)
98102
throw new ArgumentNullException(nameof(action));
99-
await action(input).ConfigureAwait(false);
103+
return action(input);
100104
}
101105
}
102106
}

src/bunit.core/Rendering/ComponentDisposedException.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace Bunit.Rendering
45
{
56
/// <summary>
67
/// Represents an exception that is thrown when a <see cref="Bunit.IRenderedFragmentBase"/>'s
78
/// properties is accessed after the underlying component has been dispsoed by the renderer.
89
/// </summary>
9-
public class ComponentDisposedException : Exception
10+
[Serializable]
11+
public sealed class ComponentDisposedException : Exception
1012
{
1113
/// <summary>
1214
/// Creates an instance of the <see cref="ComponentDisposedException"/>.
@@ -16,5 +18,8 @@ public ComponentDisposedException(int componentId)
1618
: base($"The component has been removed from the render tree by the renderer and is no longer available for inspection. ComponentId = {componentId}.")
1719
{
1820
}
21+
22+
private ComponentDisposedException(SerializationInfo serializationInfo, StreamingContext streamingContext)
23+
: base(serializationInfo, streamingContext) { }
1924
}
2025
}

0 commit comments

Comments
 (0)