-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Safer formatting of Xunit assertion messages (#7446)
* Simplify "AkkaEqualException" + format message only when needed * Use safe message formatting in all "XunitAssertions" methods * Add tests for "Akka.TestKit.Xunit2" * Update expected API shape for Xunit2 * Do not shorten delay by epsilon (test failures)
- Loading branch information
Showing
10 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/contrib/testkits/Akka.TestKit.Xunit2.Tests/Akka.TestKit.Xunit2.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="../../../xunitSettings.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(NetFrameworkTestVersion);$(NetTestVersion)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> | ||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" /> | ||
<ProjectReference Include="../Akka.TestKit.Xunit2/Akka.TestKit.Xunit2.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
33 changes: 33 additions & 0 deletions
33
src/contrib/testkits/Akka.TestKit.Xunit2.Tests/Internals/AkkaEqualExceptionSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="AkkaEqualException.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2025 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using Akka.TestKit.Xunit2.Internals; | ||
using Xunit; | ||
|
||
namespace Akka.TestKit.Xunit2.Tests.Internals; | ||
|
||
public static class AkkaEqualExceptionSpec | ||
{ | ||
#if NETFRAMEWORK | ||
[Fact] | ||
public static void Constructor_deserializes_message() | ||
{ | ||
var originalException = new AkkaEqualException("Test message"); | ||
|
||
AkkaEqualException deserializedException; | ||
using (var memoryStream = new System.IO.MemoryStream()) | ||
{ | ||
var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | ||
formatter.Serialize(memoryStream, originalException); | ||
memoryStream.Seek(0, System.IO.SeekOrigin.Begin); | ||
deserializedException = (AkkaEqualException)formatter.Deserialize(memoryStream); | ||
} | ||
|
||
Assert.Equal(originalException.Message, deserializedException.Message); | ||
} | ||
#endif | ||
} |
81 changes: 81 additions & 0 deletions
81
src/contrib/testkits/Akka.TestKit.Xunit2.Tests/XunitAssertionsSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="XunitAssertionsTests.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2025 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Akka.TestKit.Xunit2.Tests; | ||
|
||
public class XunitAssertionsSpec | ||
{ | ||
private readonly XunitAssertions _assertions = new(); | ||
|
||
[Fact] | ||
public void Assert_does_not_format_message_when_no_arguments_are_specified() | ||
{ | ||
const string testMessage = "{Value} with different format placeholders {0}"; | ||
|
||
var exception = Assert.ThrowsAny<XunitException>(() => _assertions.Fail(testMessage)); | ||
Assert.Contains(testMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertTrue(false, testMessage)); | ||
Assert.Contains(testMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertFalse(true, testMessage)); | ||
Assert.Contains(testMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, testMessage)); | ||
Assert.Contains(testMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, (_, _) => false, testMessage)); | ||
Assert.Contains(testMessage, exception.Message); | ||
} | ||
|
||
[Fact] | ||
public void Assert_formats_message_when_arguments_are_specified() | ||
{ | ||
const string testMessage = "Meaning: {0}"; | ||
const string expectedMessage = "Meaning: 42"; | ||
|
||
var exception = Assert.ThrowsAny<XunitException>(() => _assertions.Fail(testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertTrue(false, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertFalse(true, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, (_, _) => false, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
} | ||
|
||
[Fact] | ||
public void Assert_catches_format_exceptions() | ||
{ | ||
const string testMessage = "Meaning: {0} {1}"; | ||
const string expectedMessage = "Could not string.Format"; | ||
|
||
var exception = Assert.ThrowsAny<XunitException>(() => _assertions.Fail(testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertTrue(false, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertFalse(true, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
|
||
exception = Assert.ThrowsAny<XunitException>(() => _assertions.AssertEqual(4, 2, (_, _) => false, testMessage, 42)); | ||
Assert.Contains(expectedMessage, exception.Message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters