-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4502b2
commit 04c19f0
Showing
11 changed files
with
222 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ο»Ώglobal using System.Reflection; | ||
|
||
global using Xunit; | ||
global using Xunit.Abstractions; | ||
global using Xunit.Sdk; | ||
|
||
global using Songhay.Extensions; | ||
global using Songhay.Tests.Orderers; | ||
|
||
[assembly: CollectionBehavior(DisableTestParallelization = true)] | ||
[assembly: TestCaseOrderer(TestCaseOrderer.TypeName, ordererAssemblyName: TestCaseOrderer.AssemblyName)] |
47 changes: 47 additions & 0 deletions
47
SonghayCore.OrderedTests/Orderers/OrderedTestBaseFailureTests.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,47 @@ | ||
namespace Songhay.Tests.Orderers; | ||
|
||
public class OrderedTestBaseFailureTests : OrderedTestBase | ||
{ | ||
static readonly List<string> MethodCalls = new(); | ||
|
||
static int _currentValue = 32; | ||
|
||
public OrderedTestBaseFailureTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 0, reason: "Subtract 8")] | ||
public void ShouldSubtract8() | ||
{ | ||
AssertNoXUnitException(); | ||
MethodCalls.Add(nameof(ShouldSubtract8)); | ||
|
||
const int wrongValue = 42; | ||
|
||
_currentValue -= wrongValue; | ||
|
||
Assert.Equal(24, _currentValue); | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 1, reason: "Subtract 4")] | ||
public void ShouldSubtract4() | ||
{ | ||
Assert.True(XUnitExceptionHasOccurred); | ||
|
||
MethodCalls.Add(nameof(ShouldSubtract4)); | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 2, reason: "Subtract 4 again")] | ||
public void ShouldSubtract4Again() | ||
{ | ||
Assert.True(XUnitExceptionHasOccurred); | ||
|
||
MethodCalls.Add(nameof(ShouldSubtract4Again)); | ||
|
||
_testOutputHelper.WriteLine("method calls:"); | ||
MethodCalls.ForEachInEnumerable(m => _testOutputHelper.WriteLine(m)); | ||
} | ||
|
||
readonly ITestOutputHelper _testOutputHelper; | ||
} |
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,54 @@ | ||
using Songhay.Extensions; | ||
|
||
namespace Songhay.Tests.Orderers; | ||
|
||
public class OrderedTestBaseTests : OrderedTestBase | ||
{ | ||
const int ExpectedResult = 16; | ||
|
||
static readonly List<string> MethodCalls = new(); | ||
|
||
static int _currentValue = 32; | ||
|
||
public OrderedTestBaseTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
AssertNoXUnitException(); | ||
|
||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 0, reason: "Subtract 8")] | ||
public void ShouldSubtract8() | ||
{ | ||
_currentValue -= 8; | ||
|
||
Assert.Equal(24, _currentValue); | ||
|
||
MethodCalls.Add(nameof(ShouldSubtract8)); | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 1, reason: "Subtract 4")] | ||
public void ShouldSubtract4() | ||
{ | ||
_currentValue -= 4; | ||
|
||
Assert.Equal(20, _currentValue); | ||
|
||
MethodCalls.Add(nameof(ShouldSubtract4)); | ||
} | ||
|
||
[Fact, TestOrder(ordinal: 2, reason: "Subtract 4 again")] | ||
public void ShouldSubtract4Again() | ||
{ | ||
_currentValue -= 4; | ||
|
||
Assert.Equal(ExpectedResult, _currentValue); | ||
|
||
MethodCalls.Add(nameof(ShouldSubtract4Again)); | ||
|
||
_testOutputHelper.WriteLine("method calls:"); | ||
MethodCalls.ForEachInEnumerable(m => _testOutputHelper.WriteLine(m)); | ||
} | ||
|
||
readonly ITestOutputHelper _testOutputHelper; | ||
} |
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,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>Songhay.Tests</RootNamespace> | ||
|
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\SonghayCore.xUnit\Orderers\OrderedTestBase.cs" Link="Orderers\OrderedTestBase.cs"/> | ||
<Compile Include="..\SonghayCore.xUnit\Orderers\TestCaseOrderer.cs" Link="Orderers\TestCaseOrderer.cs"/> | ||
<Compile Include="..\SonghayCore.xUnit\Orderers\TestOrderAttribute.cs" zLink="Orderers\TestOrderAttribute.cs"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/> | ||
<PackageReference Include="xunit" Version="2.4.1"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SonghayCore\SonghayCore.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -1,16 +1,7 @@ | ||
ο»Ώglobal using System.Diagnostics; | ||
global using Songhay.Extensions; | ||
global using Songhay.Tests.Orderers; | ||
global using System.Reflection; | ||
global using System.Text.Json; | ||
global using Xunit; | ||
global using Xunit.Abstractions; | ||
global using Xunit.Sdk; | ||
|
||
[assembly: CollectionBehavior(DisableTestParallelization = true)] | ||
[assembly: TestCaseOrderer(TestCaseOrderer.TypeName, ordererAssemblyName: "SonghayCore.Tests")] | ||
/* | ||
FUNKYKB: π§ Note that when the real `SonghayCore.xUnit` package is in use | ||
the `orderAssemblyName` value should be `TestCaseOrderer.AssemblyName` | ||
instead of this project, linking to `SonghayCore.xUnit` external files. | ||
*/ |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
ο»Ώnamespace Songhay.Tests.Orderers | ||
ο»Ώnamespace Songhay.Tests.Orderers; | ||
|
||
/// <summary> | ||
/// Provides ordered test assertions. | ||
/// </summary> | ||
/// <remarks> | ||
/// For more detail, see βHow to Order xUnit Tests and Collectionsβ by Tom DuPont | ||
/// [http://www.tomdupont.net/2016/04/how-to-order-xunit-tests-and-collections.html] | ||
/// </remarks> | ||
[TestCaseOrderer(TestCaseOrderer.TypeName, TestCaseOrderer.AssemblyName)] | ||
public abstract class OrderedTestBase | ||
{ | ||
/// <summary> | ||
/// Provides ordered test assertions. | ||
/// Initializes a new instance of the <see cref="OrderedTestBase"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// For more detail, see βHow to Order xUnit Tests and Collectionsβ by Tom DuPont | ||
/// [http://www.tomdupont.net/2016/04/how-to-order-xunit-tests-and-collections.html] | ||
/// </remarks> | ||
[TestCaseOrderer(TestCaseOrderer.TypeName, TestCaseOrderer.AssemblyName)] | ||
public abstract class OrderedTestBase | ||
protected OrderedTestBase() | ||
{ | ||
/// <summary> | ||
/// The expected ordinal of the current test. | ||
/// </summary> | ||
protected static int TestOrdinal; | ||
|
||
protected void AssertTestName(string testName) | ||
AppDomain.CurrentDomain.FirstChanceException += (_, e) => | ||
{ | ||
var type = GetType(); | ||
var queue = TestCaseOrderer.QueuedTests[type.FullName!]; | ||
var result = queue.TryDequeue(out string? dequeuedName); | ||
Assert.True(result); | ||
Assert.Equal(testName, dequeuedName); | ||
} | ||
if (XUnitExceptionHasOccurred || !IsXunitException(e.Exception)) return; | ||
XUnitExceptionHasOccurred = true; | ||
_lastException = e.Exception; | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Returns <c>true</c> when an XUnit assertion exception has occurred. | ||
/// </summary> | ||
protected static bool XUnitExceptionHasOccurred { get; private set; } | ||
|
||
/// <summary> | ||
/// Asserts there are no exceptions of type <c>xUnit.Sdk.*</c> | ||
/// to prevent ordered tests from running. | ||
/// </summary> | ||
/// <remarks> | ||
/// See https://github.com/xunit/xunit/issues/856 | ||
/// </remarks> | ||
protected static void AssertNoXUnitException() => | ||
Assert.False(XUnitExceptionHasOccurred, | ||
$"Assertion Failed: An exception has occurred under test [message: `{_lastException?.Message}`]."); | ||
|
||
static Exception? _lastException; | ||
|
||
/// <summary> | ||
/// Determines whether the specified <see cref="Exception"/> is from an xUnit assertion. | ||
/// </summary> | ||
/// <param name="ex">The ex.</param> | ||
/// <returns> | ||
/// <c>true</c> if it is a xUnit <see cref="Exception"/>; otherwise, <c>false</c>. | ||
/// </returns> | ||
/// <remarks> | ||
/// <see cref="AppDomain.FirstChanceException"/> will detect ALL exceptions, | ||
/// including those NOT on the current path of execution! | ||
/// | ||
/// This means a huge amount of exceptions can pass through <see cref="AppDomain.FirstChanceException"/>. | ||
/// </remarks> | ||
static bool IsXunitException(Exception? ex) => | ||
ex?.GetType().FullName?.ToLowerInvariant().StartsWith("xunit.sdk") == true; | ||
} |
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