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

Safer formatting of assertion messages #159

Merged
merged 8 commits into from
Jan 6, 2025
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
10 changes: 5 additions & 5 deletions .github/workflows/pr_validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
windows-latest:
name: windows-latest
runs-on: windows-latest

steps:
- name: "Checkout"
uses: actions/[email protected]
Expand All @@ -57,11 +57,11 @@ jobs:

- name: "dotnet pack"
run: dotnet pack -c Release -o ./bin/nuget

ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest

steps:
- name: "Checkout"
uses: actions/[email protected]
Expand All @@ -81,11 +81,11 @@ jobs:

- name: "dotnet build"
run: dotnet build -c Release

# .NET Framework tests can't run reliably on Linux, so we only do .NET Core
- name: "dotnet test"
shell: bash
run: dotnet test -c Release -f net6.0
run: dotnet test -c Release -f net8.0

- name: "dotnet pack"
run: dotnet pack -c Release -o ./bin/nuget
26 changes: 10 additions & 16 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,23 @@
<Description>TestKit for writing tests for Akka.NET using NUnit.</Description>
<PackageTags>akka;actors;actor model;Akka;concurrency;testkit;nunit</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageIcon>akka.png</PackageIcon>
<!-- https://github.com/NuGet/Home/wiki/Packaging-Icon-within-the-nupkg -->
<PackageIcon>akka.png</PackageIcon><!-- https://github.com/NuGet/Home/wiki/Packaging-Icon-within-the-nupkg -->
<PackageProjectUrl>https://github.com/AkkaNetContrib/Akka.TestKit.Nunit</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- https://devblogs.microsoft.com/nuget/add-a-readme-to-your-nuget-package/ -->
<PackageReadmeFile>README.md</PackageReadmeFile><!-- https://devblogs.microsoft.com/nuget/add-a-readme-to-your-nuget-package/ -->
<NoWarn>$(NoWarn);CS1591</NoWarn>
<LangVersion>10.0</LangVersion>
<ContinuousIntegrationBuild Condition=" '$(Configuration)' == 'Release' ">true</ContinuousIntegrationBuild>
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#continuousintegrationbuild -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<!-- https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/nuget#symbol-packages -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#publishrepositoryurl -->
<LangVersion>13.0</LangVersion>
<ContinuousIntegrationBuild Condition=" '$(Configuration)' == 'Release' ">true</ContinuousIntegrationBuild><!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#continuousintegrationbuild -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder><!-- https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/nuget#symbol-packages -->
<EmbedUntrackedSources>true</EmbedUntrackedSources><!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
<PublishRepositoryUrl>true</PublishRepositoryUrl><!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#publishrepositoryurl -->
</PropertyGroup>
<PropertyGroup>
<AkkaTestKitVersion>1.5.32</AkkaTestKitVersion>
<AkkaTestKitVersion>1.5.33</AkkaTestKitVersion>
<MicrosoftNetTestSdkVersion>17.12.0</MicrosoftNetTestSdkVersion>
<NUnit3Version>3.14.0</NUnit3Version>
<NUnit4Version>4.2.2</NUnit4Version>
<NUnitAnalyzersVersion>4.4.0</NUnitAnalyzersVersion>
<NUnit4Version>4.3.2</NUnit4Version>
<NUnitAnalyzersVersion>4.5.0</NUnitAnalyzersVersion>
<NUnitTestAdapterVersion>4.6.0</NUnitTestAdapterVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#### 1.5.33 January 4th 2025 ####
- Safer formatting of assertion messages
- Bump `NUnit` to [4.3.2](https://github.com/nunit/nunit/releases/tag/4.3.2)
- Bump `NUnit.Analyzers` to [4.5.0](https://github.com/nunit/nunit.analyzers/releases/tag/4.5.0)
- Bump `Akka.TestKit` to [1.5.33](https://github.com/akkadotnet/akka.net/releases/tag/1.5.33)

#### 1.5.32 December 20th 2024 ####
- Bump `Akka.TestKit` to [1.5.32](https://github.com/akkadotnet/akka.net/releases/tag/1.5.32)
- Bump `NUnit3TestAdapter` to [4.6.0](https://github.com/nunit/nunit3-vs-adapter/releases/tag/V4.6.0)
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"rollForward": "latestFeature",
"version": "6.0.428"
"version": "9.0.101"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
<TargetFrameworks>net8.0;net462</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 26 additions & 6 deletions src/Akka.TestKit.NUnit.Tests/AssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ namespace Akka.TestKit.NUnit.Tests
[Parallelizable(ParallelScope.All)]
public class AssertionsTests : TestKit
{
private readonly NUnitAssertions _assertions;

public AssertionsTests()
{
_assertions = new NUnitAssertions();
}
private readonly NUnitAssertions _assertions = new();

[Test]
public void Fail_should_throw()
Expand Down Expand Up @@ -72,5 +67,30 @@ public void AssertEqualWithComparer_should_succeed_on_equal()
{
_assertions.AssertEqual(42, 4711, (x, y) => true);
}

[Test]
public void Assert_should_not_format_message_when_no_arguments_are_specified()
{
const string testMessage = "{Value} with different format placeholders {0}";

Assert.Multiple(() =>
{
Assert.That(
code: () => _assertions.Fail(testMessage),
constraint: Throws.Exception.TypeOf<AssertionException>().And.Message.Contains(testMessage));
Assert.That(
code: () => _assertions.AssertTrue(false, testMessage),
constraint: Throws.Exception.TypeOf<AssertionException>().And.Message.Contains(testMessage));
Assert.That(
code: () => _assertions.AssertFalse(true, testMessage),
constraint: Throws.Exception.TypeOf<AssertionException>().And.Message.Contains(testMessage));
Assert.That(
code: () => _assertions.AssertEqual(4, 2, testMessage),
constraint: Throws.Exception.TypeOf<AssertionException>().And.Message.Contains(testMessage));
Assert.That(
code: () => _assertions.AssertEqual(4, 2, (_, _) => false, testMessage),
constraint: Throws.Exception.TypeOf<AssertionException>().And.Message.Contains(testMessage));
});
}
}
}
22 changes: 16 additions & 6 deletions src/Akka.TestKit.NUnit/NUnitAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,40 @@ namespace Akka.TestKit.NUnit
/// </summary>
public class NUnitAssertions : ITestKitAssertions
{

public void Fail(string format = "", params object[] args)
{
Assert.Fail(string.Format(format, args));
Assert.Fail(NUnitAssertBase.ConvertMessageWithArgs(format, args));
}

public void AssertTrue(bool condition, string format = "", params object[] args)
{
Assert.That(condition, Is.True, string.Format(format, args));
Assert.That(condition, Is.True, NUnitAssertBase.ConvertMessageWithArgs(format, args));
}

public void AssertFalse(bool condition, string format = "", params object[] args)
{
Assert.That(condition, Is.False, string.Format(format, args));
Assert.That(condition, Is.False, NUnitAssertBase.ConvertMessageWithArgs(format, args));
}

public void AssertEqual<T>(T expected, T actual, string format = "", params object[] args)
{
Assert.That(actual, Is.EqualTo(expected), string.Format(format, args));
Assert.That(actual, Is.EqualTo(expected), NUnitAssertBase.ConvertMessageWithArgs(format, args));
}

public void AssertEqual<T>(T expected, T actual, Func<T, T, bool> comparer, string format = "", params object[] args)
{
Assert.That(actual, Is.EqualTo(expected).Using<T>(comparer), string.Format(format, args));
Assert.That(actual, Is.EqualTo(expected).Using<T>(comparer), NUnitAssertBase.ConvertMessageWithArgs(format, args));
}

/// <remarks>
/// This class only exists to allow us to call <c>NUnit.Framework.AssertBase.ConvertMessageWithArgs</c>.
/// As of NUnit 4.3.1, this method is declared <c>protected</c> and thus cannot be called directly.
/// See https://github.com/nunit/nunit/blob/4.3.1/src/NUnitFramework/framework/AssertBase.cs#L10-L14.
/// </remarks>
private sealed class NUnitAssertBase : AssertBase
{
public new static string ConvertMessageWithArgs(string message, object[] args) =>
AssertBase.ConvertMessageWithArgs(message, args);
}
}
}
Loading