Files
ss14-wl/Content.IntegrationTests/Fixtures/Attributes/RunOnSideAttribute.cs
T
Moony 9adb10d791 GameTest part 1 (#43182)
* Ports much of GameTest, minus all the WIP stuff.

* remark.

* EnsureCVar now adds test properties.

* Some cleanup and functionality.

* Ignore broken test. Needs fixed eventually. Also explicit context config.

* TrackingIssue attribute.

* oops

* Pair config attribute.

* Remove SystemAttribute in favor of using the EntitySystemManager dependency collection.

* Ensure idleness.

* More tests for tests.

* More specific failure catching tests.

* Reverse attribute resolution order so suite-wide attributes happen first.

* Get rid of AffectedProperties again because I need to refactor PoolSettings for that.

* Poke

* Final cleanup pass.

* Update Content.IntegrationTests/Fixtures/Attributes/RunOnSideAttribute.cs

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>

* Update Content.IntegrationTests/Fixtures/Attributes/Side.cs

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>

* Update Content.IntegrationTests/NUnit/Utilities/ITestExtensions.cs

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>

* Fixes.

* shut up nunit.

* Make TrackingIssue a bit strict on purpose, so people don't put junk here.

* Update Content.IntegrationTests/Tests/GameTestTests/DisconnectedDependencyTest.cs

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>

* Address.

---------

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
2026-03-27 19:08:47 +00:00

80 lines
2.4 KiB
C#

#nullable enable
using Content.IntegrationTests.NUnit.Utilities;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
namespace Content.IntegrationTests.Fixtures.Attributes;
/// <summary>
/// Ensures a test method runs on the given side (client or server, not neither nor both).
/// </summary>
/// <remarks>
/// This only works for <see cref="GameTest"/> fixtures.
/// </remarks>
/// <seealso cref="GameTest"/>
[AttributeUsage(AttributeTargets.Method)]
public sealed class RunOnSideAttribute : Attribute, IWrapTestMethod, IImplyFixture, IApplyToTest
{
public const string RunOnSideProperty = "RanOnSide";
/// <summary>
/// Which side to run the inner test code on, if not the test thread.
/// </summary>
public Side RunOnSide { get; }
public RunOnSideAttribute(Side side)
{
RunOnSide = side;
if (side is not Side.Client and not Side.Server)
throw new NotSupportedException("Test run-on-side can only be the client or server, not both or neither.");
}
TestCommand ICommandWrapper.Wrap(TestCommand command)
{
return new SidedTestCommand(command, RunOnSide);
}
private sealed class SidedTestCommand : DelegatingTestCommand
{
private readonly Side _side;
public SidedTestCommand(TestCommand inner, Side side) : base(inner)
{
_side = side;
}
public override TestResult Execute(TestExecutionContext context)
{
innerCommand.Test.EnsureFixtureIsGameTest(typeof(RunOnSideAttribute), out var gt);
if (_side is not Side.Client and not Side.Server)
throw new NotSupportedException($"Sided tests need to specify a specific side. {Test}");
if (_side is Side.Client)
{
gt.Client.WaitAssertion(() =>
{
context.CurrentResult = innerCommand.Execute(context);
})
.Wait();
}
else
{
gt.Server.WaitAssertion(() =>
{
context.CurrentResult = innerCommand.Execute(context);
})
.Wait();
}
return context.CurrentResult;
}
}
public void ApplyToTest(Test test)
{
test.Properties.Add(RunOnSideProperty, RunOnSide.ToString());
}
}