#nullable enable using System.Reflection; using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using Robust.Shared.Configuration; namespace Content.IntegrationTests.Fixtures.Attributes; /// /// Ensures the given CVar, on the given side (or both), is the given value. /// Attribute version of , and stores the old value the same way. /// /// This only works with fixtures. /// The side to set the CVar on, or both. /// The type the CVar is defined on. /// The name of the static field defining the CVar. /// The value to set the CVar to. /// /// /// [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. /// } /// /// /// [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}"); } }