Files
ss14-wl/Content.IntegrationTests/Fixtures/Attributes/EnsureCVarAttribute.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

75 lines
2.7 KiB
C#

#nullable enable
using System.Reflection;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using Robust.Shared.Configuration;
namespace Content.IntegrationTests.Fixtures.Attributes;
/// <summary>
/// Ensures the given CVar, on the given side (or both), is the given value.
/// Attribute version of <see cref="GameTest.OverrideCVar{T}"/>, and stores the old value the same way.
/// </summary>
/// <remarks>This only works with <see cref="GameTest"/> fixtures.</remarks>
/// <param name="side">The side to set the CVar on, or both.</param>
/// <param name="definitionType">The type the CVar is defined on.</param>
/// <param name="fieldName">The name of the static field defining the CVar.</param>
/// <param name="value">The value to set the CVar to.</param>
/// <example>
/// <code>
/// [Test]
/// [EnsureCVar(Side.Server, typeof(CCVars), nameof(CCVars.FlavorText), true)]
/// public async Task MyTest()
/// {
/// // CVar is set for you inside the test, and automatically un-set on teardown.
/// }
/// </code>
/// </example>
/// <seealso cref="GameTest"/>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class EnsureCVarAttribute(Side side, Type definitionType, string fieldName, object value) : Attribute, IGameTestModifier, IApplyToTest
{
public const string ClientEnsuredCVarsProperty = "ClientEnsuredCVars";
public const string ServerEnsuredCVarsProperty = "ServerEnsuredCVars";
Task IGameTestModifier.ApplyToTest(GameTest test)
{
var cvar = LookupCVar();
test.PreTestAddOverride(side, cvar.Name, value);
return Task.CompletedTask;
}
private CVarDef LookupCVar()
{
var field = definitionType.GetField(fieldName, BindingFlags.Static | BindingFlags.Public);
if (field is null)
throw new ArgumentException($"Couldn't find a public, static field named {fieldName} on {definitionType}");
var obj = field.GetValue(null);
if (obj is not CVarDef cvar)
{
throw new ArgumentException(
$"Expected a CVar definition on {definitionType}.{fieldName}, but it was a {obj?.GetType().FullName ?? "null"}");
}
if (value.GetType() != cvar.DefaultValue.GetType())
throw new NotSupportedException($"Cannot set {cvar.Name} to {value}, it's the wrong type.");
return cvar;
}
void IApplyToTest.ApplyToTest(Test test)
{
var cvar = LookupCVar();
if ((side & Side.Client) != 0)
test.Properties.Add(ClientEnsuredCVarsProperty, $"{cvar.Name} = {value}");
if ((side & Side.Server) != 0)
test.Properties.Add(ServerEnsuredCVarsProperty, $"{cvar.Name} = {value}");
}
}