Files
tbd-station-14/Content.IntegrationTests/Tests/Toolshed/ToolshedTest.cs
Pieter-Jan Briers a6c9c36b68 Dependency update / fixes / skrungle bungle (#23745)
* Give .props files 2-space indents.

* Move to Central Package Management.

Allows us to store NuGet package versions all in one place. Yay!

* Update NuGet packages and fix code for changes.

Notable:

Changes to ILVerify.
Npgsql doesn't need hacks for inet anymore, now we need hacks to make the old code work with this new reality.
NUnit's analyzers are already complaining and I didn't even update it to 4.x yet.
TerraFX changed to GetLastSystemError so error handling had to be changed.
Buncha APIs have more NRT annotations.

* Remove dotnet-eng NuGet package source.

I genuinely don't know what this was for, and Central Package Management starts throwing warnings about it, so YEET.

* Remove Robust.Physics project.

Never used.

* Remove erroneous NVorbis reference.

Should be VorbisPizza and otherwise wasn't used.

* Sandbox fixes

* Remove unused unit test package references.

Castle.Core and NUnit.ConsoleRunner.

* Update NUnit to 4.0.1

This requires replacing all the old assertion methods because they removed them 🥲

* Oh so that's what dotnet-eng was used for. Yeah ok that makes sense.

* Add Robust.Analyzers.Test

* Update submodule

* commit to re-run CI
2024-01-12 23:22:01 +01:00

171 lines
4.4 KiB
C#

#nullable enable
using System.Collections.Generic;
using Content.IntegrationTests.Pair;
using Content.Server.Administration.Managers;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Toolshed;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Toolshed.Syntax;
using Robust.UnitTesting;
namespace Content.IntegrationTests.Tests.Toolshed;
[TestFixture]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public abstract class ToolshedTest : IInvocationContext
{
protected TestPair Pair = default!;
protected virtual bool Connected => false;
protected virtual bool AssertOnUnexpectedError => true;
protected RobustIntegrationTest.ServerIntegrationInstance Server = default!;
protected RobustIntegrationTest.ClientIntegrationInstance? Client = null;
public ToolshedManager Toolshed { get; private set; } = default!;
public ToolshedEnvironment Environment => Toolshed.DefaultEnvironment;
protected IAdminManager AdminManager = default!;
protected IInvocationContext? InvocationContext = null;
[TearDown]
public async Task TearDownInternal()
{
await Pair.CleanReturnAsync();
await TearDown();
}
protected virtual async Task TearDown()
{
Assert.That(_expectedErrors, Is.Empty);
ClearErrors();
}
[SetUp]
public virtual async Task Setup()
{
Pair = await PoolManager.GetServerClient(new PoolSettings {Connected = Connected});
Server = Pair.Server;
if (Connected)
{
Client = Pair.Client;
await Client.WaitIdleAsync();
}
await Server.WaitIdleAsync();
Toolshed = Server.ResolveDependency<ToolshedManager>();
AdminManager = Server.ResolveDependency<IAdminManager>();
}
protected bool InvokeCommand(string command, out object? result, ICommonSession? session = null)
{
return Toolshed.InvokeCommand(this, command, null, out result);
}
protected T InvokeCommand<T>(string command)
{
InvokeCommand(command, out var res);
Assert.That(res, Is.AssignableTo<T>());
return (T) res!;
}
protected void ParseCommand(string command, Type? inputType = null, Type? expectedType = null, bool once = false)
{
var parser = new ParserContext(command, Toolshed);
var success = CommandRun.TryParse(false, parser, inputType, expectedType, once, out _, out _, out var error);
if (error is not null)
ReportError(error);
if (error is null)
Assert.That(success, $"Parse failed despite no error being reported. Parsed {command}");
}
public bool CheckInvokable(CommandSpec command, out IConError? error)
{
if (InvocationContext is not null)
{
return InvocationContext.CheckInvokable(command, out error);
}
error = null;
return true;
}
protected ICommonSession? InvocationSession { get; set; }
public NetUserId? User => Session?.UserId;
public ICommonSession? Session
{
get
{
if (InvocationContext is not null)
{
return InvocationContext.Session;
}
return InvocationSession;
}
}
public void WriteLine(string line)
{
return;
}
private Queue<Type> _expectedErrors = new();
private List<IConError> _errors = new();
public void ReportError(IConError err)
{
if (_expectedErrors.Count == 0)
{
if (AssertOnUnexpectedError)
{
Assert.Fail($"Got an error, {err.GetType()}, when none was expected.\n{err.Describe()}");
}
goto done;
}
var ty = _expectedErrors.Dequeue();
if (AssertOnUnexpectedError)
{
Assert.That(
err.GetType().IsAssignableTo(ty),
$"The error {err.GetType()} wasn't assignable to the expected type {ty}.\n{err.Describe()}"
);
}
done:
_errors.Add(err);
}
public IEnumerable<IConError> GetErrors()
{
return _errors;
}
public void ClearErrors()
{
_errors.Clear();
}
public Dictionary<string, object?> Variables { get; } = new();
protected void ExpectError(Type err)
{
_expectedErrors.Enqueue(err);
}
protected void ExpectError<T>()
{
_expectedErrors.Enqueue(typeof(T));
}
}