Skip to content

Commit 494c2fc

Browse files
committed
refactor: use modern language features, clean up whitespace
commit d258ec40de0f32a993f929497652a14d45a82514 Author: Egil Hansen <egil@assimilated.dk> Date: Thu Jul 7 16:28:24 2022 +0000 refactor: whitespace commit 9dbd56b2a38284b6e068e0e0a7d30ab5eaa79579 Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 22:49:05 2022 +1000 Update RootRenderTreeTest.cs commit 2d9426d7ec604ac7284ec5f2a5a4653221c9e35c Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:44:45 2022 +1000 redundant type in lambda commit 7b2e0f6a70299b1f27b4fb58c2fccc5c7233104a Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:25:56 2022 +1000 redundant parents commit 780088986dbb95ec62315125c7bef557d485884d Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:17:38 2022 +1000 redundant string interpolation commit 44ea5f7d9e25a81ccce0de2203074c20c04c281a Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:07:08 2022 +1000 use variable discards commit 5abf38d14b26775fcb1d6bfbdeb935837dfb7152 Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:06:36 2022 +1000 remove redundant params commit 7a7af54045a46e923378b02ff4fbfd350aab2315 Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:04:56 2022 +1000 use pattern matching commit e59857bed055205b7231348435df613a60484317 Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:04:28 2022 +1000 use negation pattern commit ca36cf90a371f91024fa9807b64ee88328d11976 Author: Simon Cropp <simon.cropp@gmail.com> Date: Mon Jul 4 11:03:43 2022 +1000 null coalescing
1 parent c45ec7a commit 494c2fc

59 files changed

Lines changed: 626 additions & 630 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benchmark/bunit.benchmarks/Benchmark.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Bunit;
66
[MemoryDiagnoser]
77
public class Benchmark : BenchmarkBase
88
{
9-
[Benchmark]
10-
public IRenderedComponentBase<Counter> RenderCounter()
11-
{
12-
return RenderComponent<Counter>();
13-
}
9+
[Benchmark]
10+
public IRenderedComponentBase<Counter> RenderCounter()
11+
{
12+
return RenderComponent<Counter>();
13+
}
1414
}

benchmark/bunit.benchmarks/BenchmarkBase.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@ namespace Bunit;
88

99
public abstract class BenchmarkBase
1010
{
11-
private static readonly ComponentParameterCollection EmptyParameter = new();
12-
private readonly ServiceCollection services = new();
13-
14-
protected TestRenderer Renderer { get; private set; } = default!;
15-
16-
[GlobalSetup]
17-
public void Setup()
18-
{
19-
RegisterServices(services);
20-
21-
var serviceProvider = services.BuildServiceProvider();
22-
Renderer = new TestRenderer(
23-
new RenderedComponentActivator(serviceProvider),
24-
new TestServiceProvider(services),
25-
new NullLoggerFactory());
26-
}
27-
28-
[GlobalCleanup]
29-
public void Cleanup()
30-
{
31-
InternalCleanup();
32-
Renderer.Dispose();
33-
}
34-
35-
protected IRenderedComponentBase<TComponent> RenderComponent<TComponent>()
36-
where TComponent : IComponent =>
37-
Renderer.RenderComponent<TComponent>(EmptyParameter);
38-
39-
protected virtual void InternalCleanup()
40-
{
41-
}
42-
43-
protected virtual void RegisterServices(IServiceCollection serviceCollection)
44-
{
45-
services.AddSingleton<BunitHtmlParser>();
46-
}
11+
private static readonly ComponentParameterCollection EmptyParameter = new();
12+
private readonly ServiceCollection services = new();
13+
14+
protected TestRenderer Renderer { get; private set; } = default!;
15+
16+
[GlobalSetup]
17+
public void Setup()
18+
{
19+
RegisterServices(services);
20+
21+
var serviceProvider = services.BuildServiceProvider();
22+
Renderer = new TestRenderer(
23+
new RenderedComponentActivator(serviceProvider),
24+
new TestServiceProvider(services),
25+
new NullLoggerFactory());
26+
}
27+
28+
[GlobalCleanup]
29+
public void Cleanup()
30+
{
31+
InternalCleanup();
32+
Renderer.Dispose();
33+
}
34+
35+
protected IRenderedComponentBase<TComponent> RenderComponent<TComponent>()
36+
where TComponent : IComponent =>
37+
Renderer.RenderComponent<TComponent>(EmptyParameter);
38+
39+
protected virtual void InternalCleanup()
40+
{
41+
}
42+
43+
protected virtual void RegisterServices(IServiceCollection serviceCollection)
44+
{
45+
services.AddSingleton<BunitHtmlParser>();
46+
}
4747
}

benchmark/bunit.benchmarks/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
using BenchmarkDotNet.Running;
44
using Bunit;
55

6-
BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).RunAll();
6+
BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).RunAll();

src/AngleSharpWrappers/IElementFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace AngleSharpWrappers
66
/// Represents an <see cref="IElement"/> factory, used by a <see cref="Wrapper{TElement}"/>.
77
/// </summary>
88
public interface IElementFactory<out TElement> where TElement : class, INode
9-
{
10-
/// <summary>
11-
/// A method that returns the element to wrap.
12-
/// </summary>
13-
/// <returns></returns>
14-
TElement GetElement();
15-
}
9+
{
10+
/// <summary>
11+
/// A method that returns the element to wrap.
12+
/// </summary>
13+
/// <returns></returns>
14+
TElement GetElement();
15+
}
1616
}

src/AngleSharpWrappers/IWrapper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace AngleSharpWrappers
66
/// Represents a wrapper around an <typeparamref name="TElement"/>.
77
/// </summary>
88
public interface IWrapper<out TElement> where TElement : class, INode
9-
{
10-
/// <summary>
11-
/// Gets the wrapped element.
12-
/// </summary>
13-
TElement WrappedElement { get; }
14-
}
9+
{
10+
/// <summary>
11+
/// Gets the wrapped element.
12+
/// </summary>
13+
TElement WrappedElement { get; }
14+
}
1515
}

src/AngleSharpWrappers/Wrapper.cs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,58 @@ namespace AngleSharpWrappers
88
/// Represents a wrapper <see cref="IElement"/>.
99
/// </summary>
1010
public abstract class Wrapper<TElement> : IWrapper<TElement> where TElement : class, INode
11-
{
12-
private readonly IElementFactory<TElement> elementFactory;
13-
14-
/// <summary>
15-
/// Gets the wrapped element.
16-
/// </summary>
17-
[DebuggerHidden]
18-
public TElement WrappedElement => elementFactory.GetElement();
19-
20-
/// <summary>
21-
/// Creates an instance of the <see cref="Wrapper{T}"/> class.
22-
/// </summary>
23-
protected Wrapper(IElementFactory<TElement> elementFactory)
24-
{
25-
if (elementFactory is null) throw new ArgumentNullException(nameof(elementFactory));
26-
27-
this.elementFactory = elementFactory;
28-
}
29-
/// <inheritdoc/>
30-
31-
public override bool Equals(object obj) => WrappedElement.Equals(obj);
32-
/// <inheritdoc/>
33-
34-
public override int GetHashCode() => WrappedElement.GetHashCode();
35-
/// <inheritdoc/>
36-
37-
public static bool operator ==(Wrapper<TElement> x, TElement y)
38-
{
39-
if (x is null) return y is null;
40-
return x.WrappedElement == y;
41-
}
42-
/// <inheritdoc/>
43-
44-
public static bool operator !=(Wrapper<TElement> x, TElement y)
45-
{
46-
return !(x == y);
47-
}
48-
/// <inheritdoc/>
49-
50-
public static bool operator ==(TElement x, Wrapper<TElement> y)
51-
{
52-
if (y is null) return x is null;
53-
return x == y.WrappedElement;
54-
}
55-
/// <inheritdoc/>
56-
57-
public static bool operator !=(TElement x, Wrapper<TElement> y)
58-
{
59-
return !(x == y);
60-
}
61-
}
11+
{
12+
private readonly IElementFactory<TElement> elementFactory;
13+
14+
/// <summary>
15+
/// Gets the wrapped element.
16+
/// </summary>
17+
[DebuggerHidden]
18+
public TElement WrappedElement => elementFactory.GetElement();
19+
20+
/// <summary>
21+
/// Creates an instance of the <see cref="Wrapper{T}"/> class.
22+
/// </summary>
23+
protected Wrapper(IElementFactory<TElement> elementFactory)
24+
{
25+
if (elementFactory is null)
26+
throw new ArgumentNullException(nameof(elementFactory));
27+
28+
this.elementFactory = elementFactory;
29+
}
30+
/// <inheritdoc/>
31+
32+
public override bool Equals(object obj) => WrappedElement.Equals(obj);
33+
/// <inheritdoc/>
34+
35+
public override int GetHashCode() => WrappedElement.GetHashCode();
36+
/// <inheritdoc/>
37+
38+
public static bool operator ==(Wrapper<TElement> x, TElement y)
39+
{
40+
if (x is null)
41+
return y is null;
42+
return x.WrappedElement == y;
43+
}
44+
/// <inheritdoc/>
45+
46+
public static bool operator !=(Wrapper<TElement> x, TElement y)
47+
{
48+
return !(x == y);
49+
}
50+
/// <inheritdoc/>
51+
52+
public static bool operator ==(TElement x, Wrapper<TElement> y)
53+
{
54+
if (y is null)
55+
return x is null;
56+
return x == y.WrappedElement;
57+
}
58+
/// <inheritdoc/>
59+
60+
public static bool operator !=(TElement x, Wrapper<TElement> y)
61+
{
62+
return !(x == y);
63+
}
64+
}
6265
}

src/AngleSharpWrappers/WrapperFactoryExtensions.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ namespace AngleSharpWrappers
77
/// Extensions for <see cref="Wrapper{TElement}" />.
88
/// </summary>
99
public static class WrappedElementExtensions
10-
{
11-
/// <summary>
12-
/// Unwraps a wrapped AngleSharp object, if it has been wrapped.
13-
/// </summary>
14-
public static TElement Unwrap<TElement>(this TElement wrappedObject) where TElement : class, INode
15-
=> wrappedObject is IWrapper<TElement> wrapper ? wrapper.WrappedElement : wrappedObject;
10+
{
11+
/// <summary>
12+
/// Unwraps a wrapped AngleSharp object, if it has been wrapped.
13+
/// </summary>
14+
public static TElement Unwrap<TElement>(this TElement wrappedObject) where TElement : class, INode
15+
=> wrappedObject is IWrapper<TElement> wrapper ? wrapper.WrappedElement : wrappedObject;
1616

17-
/// <summary>
18-
/// Unwraps a enumerable of wrapped AngleSharp object, if they have been wrapped.
19-
/// </summary>
20-
public static IEnumerable<TElement> Unwrap<TElement>(this IEnumerable<TElement> wrappedObjects) where TElement : class, INode
21-
{
22-
if(wrappedObjects is null) yield break;
17+
/// <summary>
18+
/// Unwraps a enumerable of wrapped AngleSharp object, if they have been wrapped.
19+
/// </summary>
20+
public static IEnumerable<TElement> Unwrap<TElement>(this IEnumerable<TElement> wrappedObjects) where TElement : class, INode
21+
{
22+
if (wrappedObjects is null)
23+
yield break;
2324

24-
foreach(var node in wrappedObjects)
25-
{
26-
yield return node.Unwrap();
27-
}
28-
}
29-
}
25+
foreach (var node in wrappedObjects)
26+
{
27+
yield return node.Unwrap();
28+
}
29+
}
30+
}
3031
}

src/bunit.core/ComponentParameterCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void AddAttributes(RenderTreeBuilder builder)
118118
foreach (var pgroup in parameters.Where(x => !x.IsCascadingValue).GroupBy(x => x.Name, StringComparer.Ordinal))
119119
{
120120
var group = pgroup.ToArray();
121-
var groupObject = group.FirstOrDefault(x => !(x.Value is null)).Value;
121+
var groupObject = group.FirstOrDefault(x => x.Value is not null).Value;
122122

123123
if (group.Length == 1)
124124
{

src/bunit.core/ComponentParameterCollectionBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ public ComponentParameterCollectionBuilder<TComponent> Bind<TValue>(
342342

343343
if (isCascading)
344344
throw new ArgumentException("Using Bind with a cascading parameter is not allowed.", parameterName);
345-
345+
346346
if (changedAction is null)
347-
throw new ArgumentNullException(nameof(changedAction));
347+
throw new ArgumentNullException(nameof(changedAction));
348348

349349
var changedName = $"{parameterName}Changed";
350350
var expressionName = $"{parameterName}Expression";
@@ -360,7 +360,7 @@ public ComponentParameterCollectionBuilder<TComponent> Bind<TValue>(
360360
AddParameter(changedName, EventCallback.Factory.Create(changedAction.Target!, changedAction));
361361

362362
return !HasPublicParameterProperty(expressionName)
363-
? this
363+
? this
364364
: AddParameter(expressionName, valueExpression ?? (() => initialValue));
365365

366366
static void AssertBindTargetIsCorrect(string parameterName, Expression<Func<TComponent, TValue>> parameterSelector)
@@ -380,8 +380,8 @@ static void AssertBindTargetIsCorrect(string parameterName, Expression<Func<TCom
380380
$"Try {selectorExpression.Replace(parameterName, possibleSelector, StringComparison.Ordinal)} instead.");
381381
}
382382
}
383-
384-
static string TrimEnd(string source, string value)
383+
384+
static string TrimEnd(string source, string value)
385385
=> source.EndsWith(value, StringComparison.Ordinal)
386386
? source.Remove(source.LastIndexOf(value, StringComparison.Ordinal))
387387
: source;
@@ -431,7 +431,7 @@ private static (string Name, string? CascadingValueName, bool IsCascading) GetPa
431431
if (parameterSelector is null)
432432
throw new ArgumentNullException(nameof(parameterSelector));
433433

434-
if (!(parameterSelector.Body is MemberExpression memberExpression) || !(memberExpression.Member is PropertyInfo propInfoCandidate))
434+
if (parameterSelector.Body is not MemberExpression { Member: PropertyInfo propInfoCandidate })
435435
throw new ArgumentException($"The parameter selector '{parameterSelector}' does not resolve to a public property on the component '{typeof(TComponent)}'.", nameof(parameterSelector));
436436

437437
var propertyInfo = propInfoCandidate.DeclaringType != TComponentType
@@ -474,20 +474,20 @@ private static RenderFragment GetRenderFragment<TChildComponent>(Action<Componen
474474
var childBuilder = new ComponentParameterCollectionBuilder<TChildComponent>(childParameterBuilder);
475475
return childBuilder.Build().ToRenderFragment<TChildComponent>();
476476
}
477-
477+
478478
private static bool HasPublicParameterProperty(string parameterName)
479479
{
480480
var type = typeof(TComponent);
481481
var property = type.GetProperty(parameterName);
482482

483483
return property != null && property.GetCustomAttributes(inherit: true).Any(a => a is ParameterAttribute);
484484
}
485-
485+
486486
private static bool IsConcreteGenericOf(Type type, Type openGeneric)
487487
{
488488
if (!type.IsGenericType)
489489
return false;
490490

491491
return type.GetGenericTypeDefinition() == openGeneric;
492492
}
493-
}
493+
}

src/bunit.core/ComponentParameterFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static ComponentParameter RenderFragment(string name, string markup)
189189
public static ComponentParameter RenderFragment<TComponent>(string name, params ComponentParameter[] parameters)
190190
where TComponent : class, IComponent
191191
{
192-
var cpc = new ComponentParameterCollection() { parameters };
192+
var cpc = new ComponentParameterCollection { parameters };
193193
return ComponentParameter.CreateParameter(name, cpc.ToRenderFragment<TComponent>());
194194
}
195195

@@ -217,7 +217,7 @@ public static ComponentParameter Template<TValue>(string name, RenderFragment<TV
217217
/// <returns>The <see cref="ComponentParameter"/>.</returns>
218218
public static ComponentParameter Template<TValue>(string name, Func<TValue, string> markupFactory)
219219
{
220-
return Template<TValue>(name, value => (RenderTreeBuilder builder) => builder.AddMarkupContent(0, markupFactory(value)));
220+
return Template<TValue>(name, value => builder => builder.AddMarkupContent(0, markupFactory(value)));
221221
}
222222

223223
/// <summary>
@@ -235,7 +235,7 @@ public static ComponentParameter Template<TComponent, TValue>(string name, Func<
235235
{
236236
return Template<TValue>(name, value =>
237237
{
238-
var cpc = new ComponentParameterCollection() { parameterCollectionBuilder(value) };
238+
var cpc = new ComponentParameterCollection { parameterCollectionBuilder(value) };
239239
return cpc.ToRenderFragment<TComponent>();
240240
});
241241
}

0 commit comments

Comments
 (0)