Skip to content

Commit 20a85eb

Browse files
build(deps): Bump SonarAnalyzer.CSharp from 9.0.0.68202 to 9.4.0.72892 (#1121)
* build(deps): Bump SonarAnalyzer.CSharp from 9.0.0.68202 to 9.4.0.72892 Bumps [SonarAnalyzer.CSharp](https://github.com/SonarSource/sonar-dotnet) from 9.0.0.68202 to 9.4.0.72892. - [Release notes](https://github.com/SonarSource/sonar-dotnet/releases) - [Commits](SonarSource/sonar-dotnet@9.0.0.68202...9.4.0.72892) --- updated-dependencies: - dependency-name: SonarAnalyzer.CSharp dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * refactor: align with latest coding rules --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Egil Hansen <egil@assimilated.dk>
1 parent f5b0e50 commit 20a85eb

9 files changed

Lines changed: 122 additions & 117 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<!-- Shared code analyzers used for all projects in the solution -->
5151
<ItemGroup Label="Code Analyzers">
5252
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
53-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.0.0.68202" PrivateAssets="All" />
53+
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.4.0.72892" PrivateAssets="All" />
5454
</ItemGroup>
5555

5656
<ItemGroup Label="Implicit usings"

src/bunit.core/ComponentParameterCollection.cs

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

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

src/bunit.core/ComponentParameterCollectionBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ private static bool HasPublicParameterProperty(string parameterName)
480480
var type = typeof(TComponent);
481481
var property = type.GetProperty(parameterName);
482482

483-
return property != null && property.GetCustomAttributes(inherit: true).Any(a => a is ParameterAttribute);
483+
return property is not null && Array.Exists(
484+
property.GetCustomAttributes(inherit: true),
485+
a => a is ParameterAttribute);
484486
}
485487

486488
private static bool IsConcreteGenericOf(Type type, Type openGeneric)

src/bunit.core/Rendering/RootRenderTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public bool TryAdd<TComponent>(Action<ComponentParameterCollectionBuilder<TCompo
4545
where TComponent : IComponent
4646
{
4747
var componentType = typeof(TComponent);
48-
if (registrations.Any(x => x.ComponentType == componentType))
48+
if (registrations.Exists(x => x.ComponentType == componentType))
4949
return false;
5050

5151
var registration = new RootRenderTreeRegistration(componentType, CreateRenderFragmentBuilder(parameterBuilder));

src/bunit.web/Asserting/DiffAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static IDiff ShouldHaveSingleChange(this IEnumerable<IDiff> diffs)
2929
expectedText: "Expected changes",
3030
message: "There were more than one change");
3131

32-
return diffsArray.First();
32+
return diffsArray[0];
3333
}
3434

3535
/// <summary>
Lines changed: 108 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,145 @@
11
using System.Text;
22

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
412
{
513
/// <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.
921
/// </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))
1225
{
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)
2542
{
26-
Invocation = invocation;
43+
sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}({GetArguments(invocation)})"));
2744
}
28-
29-
private JSRuntimeUnhandledInvocationException(SerializationInfo serializationInfo, StreamingContext streamingContext)
30-
: base(serializationInfo, streamingContext)
45+
else
3146
{
47+
sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}<{GetGenericInvocationArguments(invocation)}>({GetArguments(invocation)})"));
3248
}
3349

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();
3953

54+
if (IsImportModuleInvocation(invocation))
55+
{
56+
sb.AppendLine(FormattableString.Invariant($" SetupModule({GetArguments(invocation, includeIdentifier: false)})"));
57+
}
58+
else
59+
{
4060
if (invocation.IsVoidResultInvocation)
4161
{
42-
sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}({GetArguments(invocation)})"));
62+
sb.AppendLine(FormattableString.Invariant($" SetupVoid({GetArguments(invocation)})"));
4363
}
4464
else
4565
{
46-
sb.AppendLine(FormattableString.Invariant($" {invocation.InvocationMethodName}<{GetGenericInvocationArguments(invocation)}>({GetArguments(invocation)})"));
66+
sb.AppendLine(FormattableString.Invariant($" Setup<{GetReturnTypeName(invocation.ResultType)}>({GetArguments(invocation)})"));
4767
}
4868

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())
5870
{
71+
sb.AppendLine("or the following, to match any arguments:");
5972
if (invocation.IsVoidResultInvocation)
60-
{
61-
sb.AppendLine(FormattableString.Invariant($" SetupVoid({GetArguments(invocation)})"));
62-
}
73+
sb.AppendLine(FormattableString.Invariant($" SetupVoid(\"{invocation.Identifier}\", _ => true)"));
6374
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)"));
7676
}
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();
8477
}
8578

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
10589
{
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+
};
111104

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));
119112

120-
#if NET5_0_OR_GREATER
121-
private static bool IsImportModuleInvocation(JSRuntimeInvocation invocation)
113+
return string.Join(", ", genericArgs);
114+
}
115+
else
122116
{
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);
126118
}
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+
}
127128
#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;
131132
#endif
132133

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)
134139
{
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}\"");
143141
}
142+
143+
return string.Join(", ", args);
144144
}
145145
}

src/bunit.web/TestDoubles/Authorization/FakeAuthorizationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object? re
6767
AuthorizationResult result;
6868

6969
var requirementsArray = requirements.ToArray();
70-
if (requirementsArray.All(p => p is DenyAnonymousAuthorizationRequirement))
70+
if (Array.TrueForAll(requirementsArray, p => p is DenyAnonymousAuthorizationRequirement))
7171
{
7272
result = currentState == AuthorizationState.Authorized
7373
? AuthorizationResult.Success()
7474
: AuthorizationResult.Failed();
7575
}
76-
else if (requirementsArray.All(p => p is RolesAuthorizationRequirement))
76+
else if (Array.TrueForAll(requirementsArray, p => p is RolesAuthorizationRequirement))
7777
{
7878
result = VerifyRequiredRoles(requirementsArray);
7979
}

tests/bunit.web.tests/EventDispatchExtensions/EventDispatchExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected void VerifyEventRaisesCorrectly(MethodInfo helper, TEventArgs expected
2727
var spy = CreateTriggerSpy(ElementName, eventName);
2828
var evtArg = new TEventArgs();
2929

30-
if (helper.GetParameters().Any(p => p.ParameterType == EventArgsType))
30+
if (Array.Exists(helper.GetParameters(), p => p.ParameterType == EventArgsType))
3131
{
3232
// Matches methods like: public static void Xxxx(this IElement element, TEventArgs args)
3333
spy.Trigger(element =>

tests/bunit.web.tests/EventDispatchExtensions/KeyboardEventDispatchExtensionsTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ namespace Bunit;
22

33
public class KeyboardEventDispatchExtensionsTest : EventDispatchExtensionsTest<KeyboardEventArgs>
44
{
5-
public static IEnumerable<object[]> Helpers { get; } = GetEventHelperMethods(typeof(KeyboardEventDispatchExtensions), x => x.GetParameters().All(p => p.ParameterType != typeof(Key)));
5+
public static IEnumerable<object[]> Helpers { get; }
6+
= GetEventHelperMethods(
7+
typeof(KeyboardEventDispatchExtensions),
8+
x => Array.TrueForAll(x.GetParameters(), p => p.ParameterType != typeof(Key)));
69

710
protected override string ElementName => "input";
811

0 commit comments

Comments
 (0)