Skip to content

Commit

Permalink
Merge pull request #444 from andreniggemann/feat/nunitTestTools
Browse files Browse the repository at this point in the history
Port NUnit Test Tools to release 8
  • Loading branch information
1nf0rmagician authored Sep 5, 2024
2 parents 853b625 + c790958 commit aae4b8a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 26 deletions.
7 changes: 7 additions & 0 deletions MORYX-Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moryx.Runtime.Endpoints.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moryx.Runtime.Endpoints.IntegrationTests", "src\Tests\Moryx.Runtime.Endpoints.IntegrationTests\Moryx.Runtime.Endpoints.IntegrationTests.csproj", "{4FFB98A7-9A4C-476F-8BCC-C19B7F757BF8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moryx.TestTools.NUnit", "src\Moryx.TestTools.NUnit\Moryx.TestTools.NUnit.csproj", "{6FF878E0-AF61-4C3A-9B9C-71C35A949E51}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moryx.TestTools.IntegrationTest", "src\Moryx.TestTools.IntegrationTest\Moryx.TestTools.IntegrationTest.csproj", "{C949164C-0345-4893-9E4C-A79BC1F93F85}"
EndProject
Global
Expand Down Expand Up @@ -298,6 +300,10 @@ Global
{4FFB98A7-9A4C-476F-8BCC-C19B7F757BF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FFB98A7-9A4C-476F-8BCC-C19B7F757BF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FFB98A7-9A4C-476F-8BCC-C19B7F757BF8}.Release|Any CPU.Build.0 = Release|Any CPU
{6FF878E0-AF61-4C3A-9B9C-71C35A949E51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6FF878E0-AF61-4C3A-9B9C-71C35A949E51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FF878E0-AF61-4C3A-9B9C-71C35A949E51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FF878E0-AF61-4C3A-9B9C-71C35A949E51}.Release|Any CPU.Build.0 = Release|Any CPU
{C949164C-0345-4893-9E4C-A79BC1F93F85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C949164C-0345-4893-9E4C-A79BC1F93F85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C949164C-0345-4893-9E4C-A79BC1F93F85}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -346,6 +352,7 @@ Global
{FEB3BA44-2CD9-445A-ABF2-C92378C443F7} = {0A466330-6ED6-4861-9C94-31B1949CDDB9}
{7792C4E0-6D07-42C9-AC29-BAB76836FC11} = {0A466330-6ED6-4861-9C94-31B1949CDDB9}
{4FFB98A7-9A4C-476F-8BCC-C19B7F757BF8} = {8517D209-5BC1-47BD-A7C7-9CF9ADD9F5B6}
{6FF878E0-AF61-4C3A-9B9C-71C35A949E51} = {953AAE25-26C8-4A28-AB08-61BAFE41B22F}
{C949164C-0345-4893-9E4C-A79BC1F93F85} = {953AAE25-26C8-4A28-AB08-61BAFE41B22F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
54 changes: 54 additions & 0 deletions src/Moryx.TestTools.NUnit/MAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.Internal;
using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Moryx.TestTools.NUnit
{
public abstract class MAssert
{
public static void That(bool condition, string? message = null, [CallerArgumentExpression(nameof(condition))] string? predicateExpression = null)
{
That(condition, Is.True, message, predicateExpression);
}
public static void That(Func<bool> predicate, string? message = null, [CallerArgumentExpression(nameof(predicate))]string? predicateExpression = null)
{
That(predicate, Is.True, message, predicateExpression);
}

public static void That<T>(T actual, IResolveConstraint constraint, string? message = null, [CallerArgumentExpression(nameof(actual))] string? predicateExpression = null)
{
if (message != null)
{
message = $"{message}\nExpression: {predicateExpression}";
}
else
{
message = predicateExpression;
}
Assert.That(actual, constraint, message);
}

public static void That<T>(Func<T> actualExpression, Constraint constraint, string? message = null, [CallerArgumentExpression(nameof(actualExpression))] string? predicateExpression = null)
{
if (message != null)
{
message = $"{message}\nExpression: {predicateExpression}";
}
else
{
message = predicateExpression;
}
int fails = TestExecutionContext.CurrentContext.CurrentResult.PendingFailures;
T value = default(T)!;
Assert.That<T>(() => value = actualExpression(), new ThrowsNothingConstraint(), $"{message}\nExpected {constraint.Description} and");
if (TestExecutionContext.CurrentContext.CurrentResult.PendingFailures > fails) return; // TODO: Check if we there could be multithreading issues and whether or not we care
Assert.That(value, constraint, message);
}
}

}
13 changes: 13 additions & 0 deletions src/Moryx.TestTools.NUnit/Moryx.TestTools.NUnit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/Tests/Moryx.Tests/Moryx.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Moryx.TestTools.NUnit\Moryx.TestTools.NUnit.csproj" />
<ProjectReference Include="..\..\Moryx.TestTools.UnitTest\Moryx.TestTools.UnitTest.csproj" />
<ProjectReference Include="..\..\Moryx\Moryx.csproj" />
</ItemGroup>

Expand Down
65 changes: 39 additions & 26 deletions src/Tests/Moryx.Tests/Workplans/TransitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Collections.Generic;
using System.Linq;
using Moryx.TestTools.NUnit;
using Moryx.Workplans;
using Moryx.Workplans.Transitions;
using NUnit.Framework;
Expand Down Expand Up @@ -56,14 +58,15 @@ public void SplitTransition()
// Act
trans.Initialize();
_inputs[0].Add(_token);

// Assert
Assert.AreEqual(0, _inputs[0].Tokens.Count());
Assert.IsTrue(_outputs.All(o => o.Tokens.Count() == 1));
Assert.IsInstanceOf<SplitToken>(_outputs[0].Tokens.First());
Assert.IsInstanceOf<SplitToken>(_outputs[1].Tokens.First());
Assert.AreEqual(_token, ((SplitToken)_outputs[0].Tokens.First()).Original);
Assert.AreEqual(_token, ((SplitToken)_outputs[1].Tokens.First()).Original);
Assert.Multiple(() =>
{
MAssert.That(_inputs[0].Tokens, Is.Empty);
MAssert.That(_outputs.Select(o => o.Tokens), Has.All.Count.EqualTo(1));
MAssert.That(() => ((SplitToken)_outputs[0].Tokens.First()).Original, Is.EqualTo(_token));
MAssert.That(() => ((SplitToken)_outputs[1].Tokens.First()).Original, Is.EqualTo(_token));
});
}

[Test]
Expand All @@ -83,11 +86,13 @@ public void JoinTransition()
trans.Initialize();
_inputs[0].Add(split1);
_inputs[1].Add(split2);

// Assert
Assert.IsTrue(_inputs.All(i => !i.Tokens.Any()));
Assert.AreEqual(1, _outputs[0].Tokens.Count());
Assert.AreEqual(_token, _outputs[0].Tokens.First());
Assert.Multiple(() =>
{
MAssert.That(_inputs.All(i => !i.Tokens.Any()));
MAssert.That(_outputs[0].Tokens.Count(), Is.EqualTo(1), "The split token should be joined into one");
MAssert.That(_outputs[0].Tokens.First(), Is.EqualTo(_token));
});
}

[TestCase(0, Description = "Place only one split token on the first input")]
Expand All @@ -108,9 +113,12 @@ public void IncompleteJoinTransition(int index)
_inputs[index].Add(split);

// Assert
Assert.AreEqual(1, _inputs[index].Tokens.Count());
Assert.AreEqual(0, _inputs[(index + 1) % 2].Tokens.Count());
Assert.AreEqual(0, _outputs[0].Tokens.Count());
Assert.Multiple(() =>
{
MAssert.That(_inputs[index].Tokens, Has.Count.EqualTo(1));
MAssert.That(_inputs[(index + 1) % 2].Tokens, Is.Empty);
MAssert.That(_outputs[0].Tokens, Is.Empty);
});
}

[Test]
Expand Down Expand Up @@ -138,10 +146,12 @@ public void SubWorkplanTransition()
_inputs[0].Add(_token);

// Assert
Assert.AreEqual(0, _inputs[0].Tokens.Count());
Assert.AreEqual(_token, _outputs[0].Tokens.First());
Assert.AreEqual(2, triggered.Count);
Assert.IsTrue(triggered.All(t => t is DummyTransition));
Assert.Multiple(() => {
MAssert.That(_inputs[0].Tokens, Is.Empty);
MAssert.That(() => _outputs[0].Tokens.First(), Is.EqualTo(_token));
MAssert.That(triggered.Count, Is.EqualTo(2));
MAssert.That(triggered, Has.All.InstanceOf<DummyTransition>());
});
}

[Test]
Expand Down Expand Up @@ -171,14 +181,17 @@ public void SubworkplanPause()
trans.Resume();

// Assert
Assert.AreEqual(0, _inputs[0].Tokens.Count());
Assert.AreEqual(_token, _outputs[0].Tokens.First());
Assert.AreEqual(1, triggered.Count);
Assert.IsInstanceOf<WorkplanSnapshot>(state);
var snapshot = (WorkplanSnapshot)state;
Assert.AreEqual(1, snapshot.Holders.Length);
var stepId = workplan.Steps.First(s => s is PausableStep).Id;
Assert.AreEqual(stepId, snapshot.Holders[0].HolderId);
Assert.Multiple(() =>
{
MAssert.That(_inputs[0].Tokens, Is.Empty);
MAssert.That(_outputs[0].Tokens.First(), Is.EqualTo(_token));
MAssert.That(triggered, Has.Count.EqualTo(1));
MAssert.That(state, Is.InstanceOf<WorkplanSnapshot>());
var snapshot = (WorkplanSnapshot)state;
MAssert.That(snapshot.Holders, Has.Length.EqualTo(1));
var stepId = workplan.Steps.First(s => s is PausableStep).Id;
MAssert.That(snapshot.Holders[0].HolderId, Is.EqualTo(stepId));
});
}
}
}

0 comments on commit aae4b8a

Please sign in to comment.