Skip to content

Commit 26be467

Browse files
committed
Added tests for anglesharp extensions, JsRuntimeInvocation and ComponentParameter
1 parent 4925628 commit 26be467

13 files changed

Lines changed: 317 additions & 20 deletions

sample/tests/Assembly.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]

src/Extensions/AngleSharpExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
194194
yield return node;
195195
}
196196

197-
198197
/// <summary>
199198
/// Gets the <see cref="TestHtmlParser"/> stored in the <paramref name="node"/>s
200199
/// owning context, if one is available.

src/Mocking/JSInterop/JsRuntimeInvocation.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,53 @@ namespace Egil.RazorComponents.Testing
2727
/// <summary>
2828
/// Creates an instance of the <see cref="JsRuntimeInvocation"/>.
2929
/// </summary>
30-
public JsRuntimeInvocation(string identifier, CancellationToken? cancellationToken, object[] args)
30+
public JsRuntimeInvocation(string identifier, CancellationToken cancellationToken, object[] args)
3131
{
3232
Identifier = identifier;
33-
CancellationToken = cancellationToken ?? CancellationToken.None;
33+
CancellationToken = cancellationToken;
3434
Arguments = args;
3535
}
3636

3737
/// <inheritdoc/>
38-
public bool Equals(JsRuntimeInvocation other) => Identifier.Equals(other.Identifier, StringComparison.Ordinal) && CancellationToken == other.CancellationToken && Arguments == other.Arguments;
38+
public bool Equals(JsRuntimeInvocation other)
39+
=> Identifier.Equals(other.Identifier, StringComparison.Ordinal)
40+
&& CancellationToken == other.CancellationToken
41+
&& ArgumentsEqual(Arguments, other.Arguments);
3942

4043
/// <inheritdoc/>
4144
public override bool Equals(object obj) => obj is JsRuntimeInvocation other && Equals(other);
4245

4346
/// <inheritdoc/>
44-
public override int GetHashCode() => (Identifier, CancellationToken, Arguments).GetHashCode();
47+
public override int GetHashCode()
48+
{
49+
var hash = new HashCode();
50+
hash.Add(Identifier);
51+
hash.Add(CancellationToken);
52+
53+
for (int i = 0; i < Arguments.Count; i++)
54+
{
55+
hash.Add(Arguments[i]);
56+
}
57+
58+
return hash.ToHashCode();
59+
}
4560

4661
/// <inheritdoc/>
4762
public static bool operator ==(JsRuntimeInvocation left, JsRuntimeInvocation right) => left.Equals(right);
4863

4964
/// <inheritdoc/>
5065
public static bool operator !=(JsRuntimeInvocation left, JsRuntimeInvocation right) => !(left == right);
66+
67+
private static bool ArgumentsEqual(IReadOnlyList<object> left, IReadOnlyList<object> right)
68+
{
69+
if (left.Count != right.Count) return false;
70+
71+
for (int i = 0; i < left.Count; i++)
72+
{
73+
if (!left[i].Equals(right[i])) return false;
74+
}
75+
76+
return true;
77+
}
5178
}
5279
}

src/Rendering/ComponentParameter.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private ComponentParameter(string? name, object? value, bool isCascadingValue)
3434
if (isCascadingValue && value is null)
3535
throw new ArgumentNullException(nameof(value), "Cascading values cannot be set to null");
3636

37-
if(!isCascadingValue && name is null)
37+
if (!isCascadingValue && name is null)
3838
throw new ArgumentNullException(nameof(name), "A parameters name cannot be set to null");
3939

4040
Name = name;
@@ -47,46 +47,45 @@ private ComponentParameter(string? name, object? value, bool isCascadingValue)
4747
/// </summary>
4848
/// <param name="name">Name of the parameter to pass to the component</param>
4949
/// <param name="value">Value or null to pass the component</param>
50-
public static ComponentParameter CreateParameter(string name, object? value) => new ComponentParameter(name, value, false);
50+
public static ComponentParameter CreateParameter(string name, object? value)
51+
=> new ComponentParameter(name, value, false);
5152

5253
/// <summary>
5354
/// Create a Cascading Value parameter for a component under test.
5455
/// </summary>
5556
/// <param name="name">A optional name for the cascading value</param>
5657
/// <param name="value">The cascading value</param>
57-
public static ComponentParameter CreateCascadingValue(string? name, object value) => new ComponentParameter(name, value, true);
58+
public static ComponentParameter CreateCascadingValue(string? name, object value)
59+
=> new ComponentParameter(name, value, true);
5860

5961
/// <summary>
6062
/// Create a parameter for a component under test.
6163
/// </summary>
6264
/// <param name="input">A name/value pair for the parameter</param>
63-
public static implicit operator ComponentParameter((string name, object? value) input) => CreateParameter(input.name, input.value);
65+
public static implicit operator ComponentParameter((string name, object? value) input)
66+
=> CreateParameter(input.name, input.value);
6467

6568
/// <summary>
6669
/// Create a parameter or cascading value for a component under test.
6770
/// </summary>
6871
/// <param name="input">A name/value/isCascadingValue triple for the parameter</param>
69-
public static implicit operator ComponentParameter((string? name, object? value, bool isCascadingValue) input) => new ComponentParameter(input.name, input.value, input.isCascadingValue);
72+
public static implicit operator ComponentParameter((string? name, object? value, bool isCascadingValue) input)
73+
=> new ComponentParameter(input.name, input.value, input.isCascadingValue);
7074

7175
/// <inheritdoc/>
72-
public bool Equals(ComponentParameter other) => Name == other.Name && Value == other.Value && IsCascadingValue == other.IsCascadingValue;
76+
public bool Equals(ComponentParameter other)
77+
=> string.Equals(Name, other.Name, StringComparison.Ordinal) && Value == other.Value && IsCascadingValue == other.IsCascadingValue;
7378

7479
/// <inheritdoc/>
7580
public override bool Equals(object obj) => obj is ComponentParameter other && Equals(other);
7681

7782
/// <inheritdoc/>
78-
public override int GetHashCode() => (Name, Value, IsCascadingValue).GetHashCode();
83+
public override int GetHashCode() => HashCode.Combine(Name, Value, IsCascadingValue);
7984

8085
/// <inheritdoc/>
81-
public static bool operator ==(ComponentParameter left, ComponentParameter right)
82-
{
83-
return left.Equals(right);
84-
}
86+
public static bool operator ==(ComponentParameter left, ComponentParameter right) => left.Equals(right);
8587

8688
/// <inheritdoc/>
87-
public static bool operator !=(ComponentParameter left, ComponentParameter right)
88-
{
89-
return !(left == right);
90-
}
89+
public static bool operator !=(ComponentParameter left, ComponentParameter right) => !(left == right);
9190
}
9291
}

template/Razor.Components.Testing.Library.Template.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ This library's goal is to make it easy to write comprehensive, stable unit tests
2626
<ItemGroup>
2727
<Content Include="template\**\*" Exclude="template\**\bin\**;template\**\obj\**;template\**\.vs\**" />
2828
<Compile Remove="**\*" />
29+
<Compile Remove="template\obj\**" />
30+
<Content Remove="template\obj\**" />
31+
<EmbeddedResource Remove="template\obj\**" />
32+
<None Remove="template\obj\**" />
2933
</ItemGroup>
3034

3135
</Project>

tests/Assembly.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]

tests/Egil.RazorComponents.Testing.Library.Tests.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<Compile Remove="TestResults\**" />
11+
<Content Remove="TestResults\**" />
12+
<EmbeddedResource Remove="TestResults\**" />
13+
<None Remove="TestResults\**" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
1021
<PackageReference Include="DeepEqual" Version="2.0.0" />
1122
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
1223
<PackageReference Include="Moq" Version="4.13.1" />

tests/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
44
[assembly: SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "<Pending>")]
55
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "<Pending>")]
6+
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "In tests its ok to catch the general exception type")]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Shouldly;
8+
using Xunit;
9+
10+
namespace Egil.RazorComponents.Testing.Mocking.JSInterop
11+
{
12+
public class JsRuntimeInvocationTest
13+
{
14+
public static IEnumerable<object[]> GetEqualsTestData()
15+
{
16+
var token = new CancellationToken(true);
17+
var args = new object[] { 1, "baz" };
18+
19+
var i1 = new JsRuntimeInvocation("foo", token, args);
20+
var i2 = new JsRuntimeInvocation("foo", token, args);
21+
var i3 = new JsRuntimeInvocation("bar", token, args);
22+
var i4 = new JsRuntimeInvocation("foo", CancellationToken.None, args);
23+
var i5 = new JsRuntimeInvocation("foo", token, Array.Empty<object>());
24+
var i6 = new JsRuntimeInvocation("foo", token, new object[] { 2, "woop" });
25+
26+
yield return new object[] { i1, i1, true };
27+
yield return new object[] { i1, i2, true };
28+
yield return new object[] { i1, i3, false };
29+
yield return new object[] { i1, i4, false };
30+
yield return new object[] { i1, i5, false };
31+
yield return new object[] { i1, i6, false };
32+
}
33+
34+
[Theory(DisplayName = "Equals operator works as expected")]
35+
[MemberData(nameof(GetEqualsTestData))]
36+
public void Test002(JsRuntimeInvocation left, JsRuntimeInvocation right, bool expectedResult)
37+
{
38+
left.Equals(right).ShouldBe(expectedResult);
39+
right.Equals(left).ShouldBe(expectedResult);
40+
(left == right).ShouldBe(expectedResult);
41+
(left != right).ShouldNotBe(expectedResult);
42+
left.Equals((object)right).ShouldBe(expectedResult);
43+
right.Equals((object)left).ShouldBe(expectedResult);
44+
}
45+
46+
[Fact(DisplayName = "Equals operator works as expected with non compatible types")]
47+
public void Test003()
48+
{
49+
new JsRuntimeInvocation().Equals(new object()).ShouldBeFalse();
50+
}
51+
52+
[Theory(DisplayName = "GetHashCode returns same result for equal JsRuntimeInvocations")]
53+
[MemberData(nameof(GetEqualsTestData))]
54+
public void Test004(JsRuntimeInvocation left, JsRuntimeInvocation right, bool expectedResult)
55+
{
56+
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
57+
}
58+
}
59+
}
File renamed without changes.

0 commit comments

Comments
 (0)