Skip to content

Commit

Permalink
Safer formatting of assertion messages (akkadotnet#159)
Browse files Browse the repository at this point in the history
* Bump NUnit.Analyzers from 4.4.0 to 4.5.0

Bumps [NUnit.Analyzers](https://github.com/nunit/nunit.analyzers) from 4.4.0 to 4.5.0.
- [Release notes](https://github.com/nunit/nunit.analyzers/releases)
- [Changelog](https://github.com/nunit/nunit.analyzers/blob/master/CHANGES.md)
- [Commits](nunit/nunit.analyzers@4.4.0...4.5.0)

---
updated-dependencies:
- dependency-name: NUnit.Analyzers
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump Akka.TestKit from 1.5.32 to 1.5.33

Bumps [Akka.TestKit](https://github.com/akkadotnet/akka.net) from 1.5.32 to 1.5.33.
- [Release notes](https://github.com/akkadotnet/akka.net/releases)
- [Changelog](https://github.com/akkadotnet/akka.net/blob/dev/RELEASE_NOTES.md)
- [Commits](akkadotnet/akka.net@1.5.32...1.5.33)

---
updated-dependencies:
- dependency-name: Akka.TestKit
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump NUnit from 4.2.2 to 4.3.2

Bumps [NUnit](https://github.com/nunit/nunit) from 4.2.2 to 4.3.2.
- [Release notes](https://github.com/nunit/nunit/releases)
- [Changelog](https://github.com/nunit/nunit/blob/main/CHANGES.md)
- [Commits](nunit/nunit@4.2.2...4.3.2)

---
updated-dependencies:
- dependency-name: NUnit
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Switch to .NET SDK 9 and C# 13

* Use "AssertBase.ConvertMessageWithArgs" to format assertion messages

* Test that assertion messages are used as-is when no arguments are specified

* Run tests using .NET 8 (.NET 6 runtime is no longer available on Linux)

* Add release notes entry for 1.5.33

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
milang and dependabot[bot] authored Jan 6, 2025
1 parent 30a266e commit 6a295a5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 35 deletions.
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/checkout@v4.1.1
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/checkout@v4.1.1
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);
}
}
}

0 comments on commit 6a295a5

Please sign in to comment.