Skip to content

Commit

Permalink
Add tests for "Akka.TestKit.Xunit2"
Browse files Browse the repository at this point in the history
  • Loading branch information
milang committed Jan 8, 2025
1 parent 7baa641 commit a141a33
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Akka.sln
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClusterToolsExample.Seed",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClusterToolsExample.Node", "examples\Cluster\ClusterTools\ClusterToolsExample.Node\ClusterToolsExample.Node.csproj", "{337A85B5-4A7C-4883-8634-46E7E52A765F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.TestKit.Xunit2.Tests", "contrib\testkits\Akka.TestKit.Xunit2.Tests\Akka.TestKit.Xunit2.Tests.csproj", "{95017C99-E960-44E5-83AD-BF21461DF06F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1343,6 +1345,18 @@ Global
{337A85B5-4A7C-4883-8634-46E7E52A765F}.Release|x64.Build.0 = Release|Any CPU
{337A85B5-4A7C-4883-8634-46E7E52A765F}.Release|x86.ActiveCfg = Release|Any CPU
{337A85B5-4A7C-4883-8634-46E7E52A765F}.Release|x86.Build.0 = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|x64.ActiveCfg = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|x64.Build.0 = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|x86.ActiveCfg = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Debug|x86.Build.0 = Debug|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|Any CPU.Build.0 = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|x64.ActiveCfg = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|x64.Build.0 = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|x86.ActiveCfg = Release|Any CPU
{95017C99-E960-44E5-83AD-BF21461DF06F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1469,6 +1483,7 @@ Global
{88D7D845-2F50-4D37-9026-B0A8353D0E8D} = {7735F35A-E7B7-44DE-B6FB-C770B53EB69C}
{ED00E6F4-2B5C-4F16-ADE4-45E4A73C17B8} = {7735F35A-E7B7-44DE-B6FB-C770B53EB69C}
{337A85B5-4A7C-4883-8634-46E7E52A765F} = {7735F35A-E7B7-44DE-B6FB-C770B53EB69C}
{95017C99-E960-44E5-83AD-BF21461DF06F} = {7625FD95-4B2C-4A5B-BDD5-94B1493FAC8E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {03AD8E21-7507-4E68-A4E9-F4A7E7273164}
Expand Down
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>
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
}
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);
}
}

0 comments on commit a141a33

Please sign in to comment.