|
1 | 1 | using System.Text; |
2 | 2 |
|
3 | | -namespace Bunit |
| 3 | +namespace Bunit; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Exception use to indicate that an invocation was |
| 7 | +/// received by the <see cref="BunitJSInterop"/> running in <see cref="JSRuntimeMode.Strict"/> mode, |
| 8 | +/// which didn't contain a matching invocation handler. |
| 9 | +/// </summary> |
| 10 | +[Serializable] |
| 11 | +public sealed class JSRuntimeUnhandledInvocationException : Exception |
4 | 12 | { |
5 | 13 | /// <summary> |
6 | | - /// Exception use to indicate that an invocation was |
7 | | - /// received by the <see cref="BunitJSInterop"/> running in <see cref="JSRuntimeMode.Strict"/> mode, |
8 | | - /// which didn't contain a matching invocation handler. |
| 14 | + /// Gets the unplanned invocation. |
| 15 | + /// </summary> |
| 16 | + public JSRuntimeInvocation Invocation { get; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="JSRuntimeUnhandledInvocationException"/> class |
| 20 | + /// with the provided <see cref="Invocation"/> attached. |
9 | 21 | /// </summary> |
10 | | - [Serializable] |
11 | | - public sealed class JSRuntimeUnhandledInvocationException : Exception |
| 22 | + /// <param name="invocation">The unplanned invocation.</param> |
| 23 | + public JSRuntimeUnhandledInvocationException(JSRuntimeInvocation invocation) |
| 24 | + : base(CreateErrorMessage(invocation)) |
12 | 25 | { |
13 | | - /// <summary> |
14 | | - /// Gets the unplanned invocation. |
15 | | - /// </summary> |
16 | | - public JSRuntimeInvocation Invocation { get; } |
17 | | - |
18 | | - /// <summary> |
19 | | - /// Initializes a new instance of the <see cref="JSRuntimeUnhandledInvocationException"/> class |
20 | | - /// with the provided <see cref="Invocation"/> attached. |
21 | | - /// </summary> |
22 | | - /// <param name="invocation">The unplanned invocation.</param> |
23 | | - public JSRuntimeUnhandledInvocationException(JSRuntimeInvocation invocation) |
24 | | - : base(CreateErrorMessage(invocation)) |
| 26 | + Invocation = invocation; |
| 27 | + } |
| 28 | + |
| 29 | + private JSRuntimeUnhandledInvocationException(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| 30 | + : base(serializationInfo, streamingContext) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + [SuppressMessage("Minor Code Smell", "S6618:\"string.Create\" should be used instead of \"FormattableString\"", Justification = "string.Create not supported in all TFs")] |
| 35 | + private static string CreateErrorMessage(JSRuntimeInvocation invocation) |
| 36 | + { |
| 37 | + var sb = new StringBuilder(); |
| 38 | + sb.AppendLine("bUnit's JSInterop has not been configured to handle the call:"); |
| 39 | + sb.AppendLine(); |
| 40 | + |
| 41 | + if (invocation.IsVoidResultInvocation) |
25 | 42 | { |
26 | | - Invocation = invocation; |
| 43 | + sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}({GetArguments(invocation)})")); |
27 | 44 | } |
28 | | - |
29 | | - private JSRuntimeUnhandledInvocationException(SerializationInfo serializationInfo, StreamingContext streamingContext) |
30 | | - : base(serializationInfo, streamingContext) |
| 45 | + else |
31 | 46 | { |
| 47 | + sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}<{GetGenericInvocationArguments(invocation)}>({GetArguments(invocation)})")); |
32 | 48 | } |
33 | 49 |
|
34 | | - private static string CreateErrorMessage(JSRuntimeInvocation invocation) |
35 | | - { |
36 | | - var sb = new StringBuilder(); |
37 | | - sb.AppendLine("bUnit's JSInterop has not been configured to handle the call:"); |
38 | | - sb.AppendLine(); |
| 50 | + sb.AppendLine(); |
| 51 | + sb.AppendLine("Configure bUnit's JSInterop to handle the call with following:"); |
| 52 | + sb.AppendLine(); |
39 | 53 |
|
| 54 | + if (IsImportModuleInvocation(invocation)) |
| 55 | + { |
| 56 | + sb.AppendLine(FormattableString.Invariant($" SetupModule({GetArguments(invocation, includeIdentifier: false)})")); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
40 | 60 | if (invocation.IsVoidResultInvocation) |
41 | 61 | { |
42 | | - sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}({GetArguments(invocation)})")); |
| 62 | + sb.AppendLine(FormattableString.Invariant($" SetupVoid({GetArguments(invocation)})")); |
43 | 63 | } |
44 | 64 | else |
45 | 65 | { |
46 | | - sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}<{GetGenericInvocationArguments(invocation)}>({GetArguments(invocation)})")); |
| 66 | + sb.AppendLine(FormattableString.Invariant($" Setup<{GetReturnTypeName(invocation.ResultType)}>({GetArguments(invocation)})")); |
47 | 67 | } |
48 | 68 |
|
49 | | - sb.AppendLine(); |
50 | | - sb.AppendLine("Configure bUnit's JSInterop to handle the call with following:"); |
51 | | - sb.AppendLine(); |
52 | | - |
53 | | - if (IsImportModuleInvocation(invocation)) |
54 | | - { |
55 | | - sb.AppendLine(FormattableString.Invariant($" SetupModule({GetArguments(invocation, includeIdentifier: false)})")); |
56 | | - } |
57 | | - else |
| 69 | + if (invocation.Arguments.Any()) |
58 | 70 | { |
| 71 | + sb.AppendLine("or the following, to match any arguments:"); |
59 | 72 | if (invocation.IsVoidResultInvocation) |
60 | | - { |
61 | | - sb.AppendLine(FormattableString.Invariant($" SetupVoid({GetArguments(invocation)})")); |
62 | | - } |
| 73 | + sb.AppendLine(FormattableString.Invariant($" SetupVoid(\"{invocation.Identifier}\", _ => true)")); |
63 | 74 | else |
64 | | - { |
65 | | - sb.AppendLine(FormattableString.Invariant($" Setup<{GetReturnTypeName(invocation.ResultType)}>({GetArguments(invocation)})")); |
66 | | - } |
67 | | - |
68 | | - if (invocation.Arguments.Any()) |
69 | | - { |
70 | | - sb.AppendLine("or the following, to match any arguments:"); |
71 | | - if (invocation.IsVoidResultInvocation) |
72 | | - sb.AppendLine(FormattableString.Invariant($" SetupVoid(\"{invocation.Identifier}\", _ => true)")); |
73 | | - else |
74 | | - sb.AppendLine(FormattableString.Invariant($" Setup<{GetReturnTypeName(invocation.ResultType)}>(\"{invocation.Identifier}\", _ => true)")); |
75 | | - } |
| 75 | + sb.AppendLine(FormattableString.Invariant($" Setup<{GetReturnTypeName(invocation.ResultType)}>(\"{invocation.Identifier}\", _ => true)")); |
76 | 76 | } |
77 | | - |
78 | | - sb.AppendLine(); |
79 | | - sb.AppendLine("The setup methods are available on an instance of the BunitJSInterop or"); |
80 | | - sb.AppendLine("BunitJSModuleInterop type. The standard BunitJSInterop is available"); |
81 | | - sb.AppendLine("through the TestContext.JSInterop property, and a BunitJSModuleInterop"); |
82 | | - sb.AppendLine("instance is returned from calling SetupModule on a BunitJSInterop instance."); |
83 | | - return sb.ToString(); |
84 | 77 | } |
85 | 78 |
|
86 | | - private static string GetReturnTypeName(Type resultType) |
87 | | - => resultType switch |
88 | | - { |
89 | | - Type { FullName: "System.Boolean" } => "bool", |
90 | | - Type { FullName: "System.Byte" } => "byte", |
91 | | - Type { FullName: "System.Char" } => "char", |
92 | | - Type { FullName: "System.Double" } => "double", |
93 | | - Type { FullName: "System.Int16" } => "short", |
94 | | - Type { FullName: "System.Int32" } => "int", |
95 | | - Type { FullName: "System.Int64" } => "long", |
96 | | - Type { FullName: "System.SByte" } => "sbyte", |
97 | | - Type { FullName: "System.Single" } => "float", |
98 | | - Type { FullName: "System.UInt16" } => "ushort", |
99 | | - Type { FullName: "System.UInt32" } => "uint", |
100 | | - Type { FullName: "System.UInt64" } => "ulong", |
101 | | - Type x => x.Name |
102 | | - }; |
103 | | - |
104 | | - private static string GetGenericInvocationArguments(JSRuntimeInvocation invocation) |
| 79 | + sb.AppendLine(); |
| 80 | + sb.AppendLine("The setup methods are available on an instance of the BunitJSInterop or"); |
| 81 | + sb.AppendLine("BunitJSModuleInterop type. The standard BunitJSInterop is available"); |
| 82 | + sb.AppendLine("through the TestContext.JSInterop property, and a BunitJSModuleInterop"); |
| 83 | + sb.AppendLine("instance is returned from calling SetupModule on a BunitJSInterop instance."); |
| 84 | + return sb.ToString(); |
| 85 | + } |
| 86 | + |
| 87 | + private static string GetReturnTypeName(Type resultType) |
| 88 | + => resultType switch |
105 | 89 | { |
106 | | - if (invocation.InvocationMethodName.Equals("InvokeUnmarshalled", StringComparison.Ordinal)) |
107 | | - { |
108 | | - var genericArgs = invocation.Arguments |
109 | | - .Select(x => x is not null ? GetReturnTypeName(x.GetType()) : "?") |
110 | | - .Append(GetReturnTypeName(invocation.ResultType)); |
| 90 | + Type { FullName: "System.Boolean" } => "bool", |
| 91 | + Type { FullName: "System.Byte" } => "byte", |
| 92 | + Type { FullName: "System.Char" } => "char", |
| 93 | + Type { FullName: "System.Double" } => "double", |
| 94 | + Type { FullName: "System.Int16" } => "short", |
| 95 | + Type { FullName: "System.Int32" } => "int", |
| 96 | + Type { FullName: "System.Int64" } => "long", |
| 97 | + Type { FullName: "System.SByte" } => "sbyte", |
| 98 | + Type { FullName: "System.Single" } => "float", |
| 99 | + Type { FullName: "System.UInt16" } => "ushort", |
| 100 | + Type { FullName: "System.UInt32" } => "uint", |
| 101 | + Type { FullName: "System.UInt64" } => "ulong", |
| 102 | + Type x => x.Name |
| 103 | + }; |
111 | 104 |
|
112 | | - return string.Join(", ", genericArgs); |
113 | | - } |
114 | | - else |
115 | | - { |
116 | | - return GetReturnTypeName(invocation.ResultType); |
117 | | - } |
118 | | - } |
| 105 | + private static string GetGenericInvocationArguments(JSRuntimeInvocation invocation) |
| 106 | + { |
| 107 | + if (invocation.InvocationMethodName.Equals("InvokeUnmarshalled", StringComparison.Ordinal)) |
| 108 | + { |
| 109 | + var genericArgs = invocation.Arguments |
| 110 | + .Select(x => x is not null ? GetReturnTypeName(x.GetType()) : "?") |
| 111 | + .Append(GetReturnTypeName(invocation.ResultType)); |
119 | 112 |
|
120 | | -#if NET5_0_OR_GREATER |
121 | | - private static bool IsImportModuleInvocation(JSRuntimeInvocation invocation) |
| 113 | + return string.Join(", ", genericArgs); |
| 114 | + } |
| 115 | + else |
122 | 116 | { |
123 | | - const string DefaultImportIdentifier = "import"; |
124 | | - return string.Equals(invocation.Identifier, DefaultImportIdentifier, StringComparison.Ordinal) |
125 | | - && typeof(IJSObjectReference).IsAssignableFrom(invocation.ResultType); |
| 117 | + return GetReturnTypeName(invocation.ResultType); |
126 | 118 | } |
| 119 | + } |
| 120 | + |
| 121 | +#if NET5_0_OR_GREATER |
| 122 | + private static bool IsImportModuleInvocation(JSRuntimeInvocation invocation) |
| 123 | + { |
| 124 | + const string DefaultImportIdentifier = "import"; |
| 125 | + return string.Equals(invocation.Identifier, DefaultImportIdentifier, StringComparison.Ordinal) |
| 126 | + && typeof(IJSObjectReference).IsAssignableFrom(invocation.ResultType); |
| 127 | + } |
127 | 128 | #else |
128 | | - [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Method to allow compatibility with netstandard2.1")] |
129 | | - private static bool IsImportModuleInvocation(JSRuntimeInvocation runtimeInvocation) |
130 | | - => false; |
| 129 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Method to allow compatibility with netstandard2.1")] |
| 130 | + private static bool IsImportModuleInvocation(JSRuntimeInvocation runtimeInvocation) |
| 131 | + => false; |
131 | 132 | #endif |
132 | 133 |
|
133 | | - private static string GetArguments(JSRuntimeInvocation invocation, bool includeIdentifier = true) |
| 134 | + private static string GetArguments(JSRuntimeInvocation invocation, bool includeIdentifier = true) |
| 135 | + { |
| 136 | + var args = invocation.Arguments |
| 137 | + .Select(x => x is string s ? $"\"{s}\"" : x?.ToString() ?? "null"); |
| 138 | + if (includeIdentifier) |
134 | 139 | { |
135 | | - var args = invocation.Arguments |
136 | | - .Select(x => x is string s ? $"\"{s}\"" : x?.ToString() ?? "null"); |
137 | | - if (includeIdentifier) |
138 | | - { |
139 | | - args = args.Prepend($"\"{invocation.Identifier}\""); |
140 | | - } |
141 | | - |
142 | | - return string.Join(", ", args); |
| 140 | + args = args.Prepend($"\"{invocation.Identifier}\""); |
143 | 141 | } |
| 142 | + |
| 143 | + return string.Join(", ", args); |
144 | 144 | } |
145 | 145 | } |
0 commit comments