-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathJSRuntimeInvocation.cs
More file actions
135 lines (115 loc) · 3.9 KB
/
Copy pathJSRuntimeInvocation.cs
File metadata and controls
135 lines (115 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
namespace Bunit;
/// <summary>
/// Represents an invocation of JavaScript via the JSRuntime Mock.
/// </summary>
public readonly struct JSRuntimeInvocation : IEquatable<JSRuntimeInvocation>
{
/// <summary>
/// Gets the identifier used in the invocation.
/// </summary>
public string Identifier { get; }
/// <summary>
/// Gets the cancellation token used in the invocation, if any.
/// </summary>
public CancellationToken? CancellationToken { get; }
/// <summary>
/// Gets the arguments used in the invocation.
/// </summary>
public IReadOnlyList<object?> Arguments { get; }
/// <summary>
/// Gets whether the invocation has a <c>void</c> return type.
/// </summary>
public bool IsVoidResultInvocation { get; }
/// <summary>
/// Gets the result type of the invocation. If <see cref="IsVoidResultInvocation"/> then
/// this will be of type <see cref="object"/>.
/// </summary>
public Type ResultType { get; }
/// <summary>
/// Gets the name of the method that initiated the invocation, e.g. <c>InvokeAsync</c> or <c>Invoke</c>.
/// </summary>
public string InvocationMethodName { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JSRuntimeInvocation"/> struct.
/// </summary>
public JSRuntimeInvocation(string identifier, Type resultType, string invocationMethodName)
: this(identifier, null, Array.Empty<object?>(), resultType, invocationMethodName)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="JSRuntimeInvocation"/> struct.
/// </summary>
public JSRuntimeInvocation(string identifier, object?[] args, Type resultType, string invocationMethodName)
: this(identifier, null, args, resultType, invocationMethodName)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="JSRuntimeInvocation"/> struct.
/// </summary>
public JSRuntimeInvocation(
string identifier,
CancellationToken? cancellationToken,
object?[]? args,
Type resultType,
string invocationMethodName)
{
Identifier = identifier;
CancellationToken = cancellationToken;
Arguments = args ?? Array.Empty<object?>();
ResultType = resultType;
InvocationMethodName = invocationMethodName;
IsVoidResultInvocation = resultType == typeof(Microsoft.JSInterop.Infrastructure.IJSVoidResult);
}
/// <inheritdoc/>
public bool Equals(JSRuntimeInvocation other)
=> Identifier.Equals(other.Identifier, StringComparison.Ordinal)
&& CancellationToken == other.CancellationToken
&& ArgumentsEqual(Arguments, other.Arguments)
&& ResultType == other.ResultType
&& InvocationMethodName.Equals(other.InvocationMethodName, StringComparison.Ordinal);
/// <inheritdoc/>
public override bool Equals(object? obj) => obj is JSRuntimeInvocation other && Equals(other);
/// <inheritdoc/>
public override int GetHashCode()
{
var hash = default(HashCode);
hash.Add(Identifier);
hash.Add(CancellationToken);
hash.Add(ResultType);
hash.Add(InvocationMethodName);
for (var i = 0; i < Arguments.Count; i++)
{
hash.Add(Arguments[i]);
}
return hash.ToHashCode();
}
/// <summary>
/// Verify whether <paramref name="left"/> and <paramref name="right"/>
/// <see cref="JSRuntimeInvocation"/> is equal.
/// </summary>
public static bool operator ==(JSRuntimeInvocation left, JSRuntimeInvocation right) => left.Equals(right);
/// <summary>
/// Verify whether <paramref name="left"/> and <paramref name="right"/>
/// <see cref="JSRuntimeInvocation"/> is not equal.
/// </summary>
public static bool operator !=(JSRuntimeInvocation left, JSRuntimeInvocation right) => !(left == right);
private static bool ArgumentsEqual(IReadOnlyList<object?> left, IReadOnlyList<object?> right)
{
if (left.Count != right.Count)
return false;
for (var i = 0; i < left.Count; i++)
{
var l = left[i];
var r = right[i];
if (l is null)
{
if (r is object)
return false;
}
else
{
if (!l.Equals(right[i]))
return false;
}
}
return true;
}
}