Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address some STA warnings #119

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
<NoWarn>$(NoWarn);IDE0079;S1135;CA1510;CA1511;CA1512;CA1513;CA1863</NoWarn>
<NoWarn>$(NoWarn);IDE0079;S1135;CA1510;CA1511;CA1512;CA1513;CA1863;S1939;S1133</NoWarn>
<NoWarn Condition="'$(Configuration)'=='Release'">$(NoWarn);NETSDK1138</NoWarn>
<WarningsAsErrors>$(WarningsAsErrors);NU1601;NU1603;NU1605;NU1608;NU1701;MSB3644</WarningsAsErrors>
<ContinuousIntegrationBuild Condition="'$(CI)'!=''">true</ContinuousIntegrationBuild>
Expand Down
32 changes: 16 additions & 16 deletions src/MockHttp/Extensions/IRespondsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,6 @@ public static TResult Respond<TResult>(this IResponds<TResult> responds, Action<
return responds.RespondUsing(new RequestSpecificResponseBuilder(with));
}

/// <summary>
/// Specifies a strategy that returns the response for a request.
/// </summary>
/// <param name="responds"></param>
internal static TResult RespondUsing<TStrategy, TResult>(this IResponds<TResult> responds)
where TStrategy : IResponseStrategy, new()
where TResult : IResponseResult
{
if (responds is null)
{
throw new ArgumentNullException(nameof(responds));
}

return responds.RespondUsing(new TStrategy());
}

/// <summary>
/// Configures a response via the response builder API.
/// </summary>
Expand Down Expand Up @@ -121,6 +105,22 @@ public static TResult Respond<TResult>(this IResponds<TResult> responds, Action<
return responds.Respond((ctx, _, builder) => with(ctx, builder));
}

/// <summary>
/// Specifies a strategy that returns the response for a request.
/// </summary>
/// <param name="responds"></param>
internal static TResult RespondUsing<TStrategy, TResult>(this IResponds<TResult> responds)
where TStrategy : IResponseStrategy, new()
where TResult : IResponseResult
{
if (responds is null)
{
throw new ArgumentNullException(nameof(responds));
}

return responds.RespondUsing(new TStrategy());
}

private sealed class RequestSpecificResponseBuilder : IResponseStrategy
{
private readonly Action<MockHttpRequestContext, CancellationToken, IResponseBuilder> _with;
Expand Down
2 changes: 2 additions & 0 deletions src/MockHttp/HttpMockException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ internal HttpMockException(string message, Exception innerException) : base(mess
/// <inheritdoc />
[ExcludeFromCodeCoverage]
#if NET8_0_OR_GREATER
#pragma warning disable CA1041
[Obsolete(DiagnosticId = "SYSLIB0051")]
#pragma warning restore CA1041
#endif
protected HttpMockException(SerializationInfo info, StreamingContext context)
: base(info, context)
Expand Down
55 changes: 42 additions & 13 deletions src/MockHttp/IsSent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ namespace MockHttp;
public sealed class IsSent
{
private readonly Func<int, bool> _evaluator;
private readonly int _min;
private readonly int _max;
private readonly string _name;
private readonly string _message;

private IsSent(Func<int, bool> evaluator, int min, int max, string name, string message)
private IsSent(Func<int, bool> evaluator, string name, string message)
{
_evaluator = evaluator ?? throw new ArgumentNullException(nameof(evaluator));
_min = min;
_max = max;
_name = name;
_message = message;
}
Expand All @@ -32,15 +28,23 @@ public static IsSent AtLeast(int callCount)
throw new ArgumentOutOfRangeException(nameof(callCount));
}

return new IsSent(i => i >= callCount, callCount, int.MaxValue, $"{nameof(AtLeast)}({callCount})", $"Expected request to have been sent at least {callCount} time(s){{0}}, but was sent only {{1}} time(s).");
return new IsSent(
i => i >= callCount,
$"{nameof(AtLeast)}({callCount})",
$"Expected request to have been sent at least {callCount} time(s){{0}}, but was sent only {{1}} time(s)."
);
}

/// <summary>
/// Specifies that the mocked request should be sent one time at minimum.
/// </summary>
public static IsSent AtLeastOnce()
{
return new IsSent(i => i >= 1, 1, int.MaxValue, nameof(AtLeastOnce), "Expected request to have been sent at least once{0}, but it was not.");
return new IsSent(
i => i >= 1,
nameof(AtLeastOnce),
"Expected request to have been sent at least once{0}, but it was not."
);
}

/// <summary>
Expand All @@ -53,15 +57,23 @@ public static IsSent AtMost(int callCount)
throw new ArgumentOutOfRangeException(nameof(callCount));
}

return new IsSent(i => i <= callCount, 0, callCount, $"{nameof(AtMost)}({callCount})", $"Expected request to have been sent at most {callCount} time(s){{0}}, but was actually sent {{1}} time(s).");
return new IsSent(
i => i <= callCount,
$"{nameof(AtMost)}({callCount})",
$"Expected request to have been sent at most {callCount} time(s){{0}}, but was actually sent {{1}} time(s)."
);
}

/// <summary>
/// Specifies that the mocked request should be sent one time at maximum.
/// </summary>
public static IsSent AtMostOnce()
{
return new IsSent(i => i <= 1, 0, 1, nameof(AtMostOnce), "Expected request to have been sent at most once{0}, but was actually sent {1} time(s).");
return new IsSent(
i => i <= 1,
nameof(AtMostOnce),
"Expected request to have been sent at most once{0}, but was actually sent {1} time(s)."
);
}

/// <summary>
Expand All @@ -74,23 +86,35 @@ public static IsSent Exactly(int callCount)
throw new ArgumentOutOfRangeException(nameof(callCount));
}

return new IsSent(i => i == callCount, callCount, callCount, $"{nameof(Exactly)}({callCount})", $"Expected request to have been sent exactly {callCount} time(s){{0}}, but was actually sent {{1}} time(s).");
return new IsSent(
i => i == callCount,
$"{nameof(Exactly)}({callCount})",
$"Expected request to have been sent exactly {callCount} time(s){{0}}, but was actually sent {{1}} time(s)."
);
}

/// <summary>
/// Specifies that the mocked request should not be sent.
/// </summary>
public static IsSent Never()
{
return new IsSent(i => i == 0, 0, 0, nameof(Never), "Expected request to have never been sent{0}, but was actually sent {1} time(s).");
return new IsSent(
i => i == 0,
nameof(Never),
"Expected request to have never been sent{0}, but was actually sent {1} time(s)."
);
}

/// <summary>
/// Specifies that the mocked request should be sent exactly one time.
/// </summary>
public static IsSent Once()
{
return new IsSent(i => i == 1, 1, 1, nameof(Once), "Expected request to have been sent exactly once{0}, but was actually sent {1} time(s).");
return new IsSent(
i => i == 1,
nameof(Once),
"Expected request to have been sent exactly once{0}, but was actually sent {1} time(s)."
);
}

internal bool Verify(int callCount)
Expand All @@ -100,7 +124,12 @@ internal bool Verify(int callCount)

internal string GetErrorMessage(int actualCount, string becauseMessage)
{
return string.Format(CultureInfo.CurrentCulture, _message, becauseMessage, actualCount);
return string.Format(
CultureInfo.CurrentCulture,
_message,
becauseMessage,
actualCount
);
}

/// <inheritdoc />
Expand Down
1 change: 1 addition & 0 deletions test/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<NoWarn>$(NoWarn);NU1902;NU1903</NoWarn>
<NetTestSdkVersion>17.11.1</NetTestSdkVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
Expand Down
Loading