ToolshedTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using Content.IntegrationTests.Pair;
  4. using Content.Server.Administration.Managers;
  5. using Robust.Shared.Network;
  6. using Robust.Shared.Player;
  7. using Robust.Shared.Toolshed;
  8. using Robust.Shared.Toolshed.Errors;
  9. using Robust.Shared.Toolshed.Syntax;
  10. using Robust.UnitTesting;
  11. namespace Content.IntegrationTests.Tests.Toolshed;
  12. [TestFixture]
  13. [FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
  14. public abstract class ToolshedTest : IInvocationContext
  15. {
  16. protected TestPair Pair = default!;
  17. protected virtual bool Connected => false;
  18. protected virtual bool AssertOnUnexpectedError => true;
  19. protected RobustIntegrationTest.ServerIntegrationInstance Server = default!;
  20. protected RobustIntegrationTest.ClientIntegrationInstance? Client = null;
  21. public ToolshedManager Toolshed { get; private set; } = default!;
  22. public ToolshedEnvironment Environment => Toolshed.DefaultEnvironment;
  23. protected IAdminManager AdminManager = default!;
  24. protected IInvocationContext? InvocationContext = null;
  25. [TearDown]
  26. public async Task TearDownInternal()
  27. {
  28. await Pair.CleanReturnAsync();
  29. await TearDown();
  30. }
  31. protected virtual Task TearDown()
  32. {
  33. Assert.That(_expectedErrors, Is.Empty);
  34. ClearErrors();
  35. return Task.CompletedTask;
  36. }
  37. [SetUp]
  38. public virtual async Task Setup()
  39. {
  40. Pair = await PoolManager.GetServerClient(new PoolSettings { Connected = Connected });
  41. Server = Pair.Server;
  42. if (Connected)
  43. {
  44. Client = Pair.Client;
  45. await Client.WaitIdleAsync();
  46. }
  47. await Server.WaitIdleAsync();
  48. Toolshed = Server.ResolveDependency<ToolshedManager>();
  49. AdminManager = Server.ResolveDependency<IAdminManager>();
  50. }
  51. protected bool InvokeCommand(string command, out object? result, ICommonSession? session = null)
  52. {
  53. return Toolshed.InvokeCommand(this, command, null, out result);
  54. }
  55. protected T InvokeCommand<T>(string command)
  56. {
  57. InvokeCommand(command, out var res);
  58. Assert.That(res, Is.AssignableTo<T>());
  59. return (T) res!;
  60. }
  61. protected void ParseCommand(string command, Type? inputType = null, Type? expectedType = null)
  62. {
  63. var parser = new ParserContext(command, Toolshed);
  64. var success = CommandRun.TryParse(parser, inputType, expectedType, out _);
  65. if (parser.Error is not null)
  66. ReportError(parser.Error);
  67. if (parser.Error is null)
  68. Assert.That(success, $"Parse failed despite no error being reported. Parsed {command}");
  69. }
  70. public bool CheckInvokable(CommandSpec command, out IConError? error)
  71. {
  72. if (InvocationContext is not null)
  73. {
  74. return InvocationContext.CheckInvokable(command, out error);
  75. }
  76. error = null;
  77. return true;
  78. }
  79. protected ICommonSession? InvocationSession { get; set; }
  80. public NetUserId? User => Session?.UserId;
  81. public ICommonSession? Session
  82. {
  83. get
  84. {
  85. if (InvocationContext is not null)
  86. {
  87. return InvocationContext.Session;
  88. }
  89. return InvocationSession;
  90. }
  91. }
  92. public void WriteLine(string line)
  93. {
  94. return;
  95. }
  96. private Queue<Type> _expectedErrors = new();
  97. private List<IConError> _errors = new();
  98. public void ReportError(IConError err)
  99. {
  100. if (_expectedErrors.Count == 0)
  101. {
  102. if (AssertOnUnexpectedError)
  103. {
  104. Assert.Fail($"Got an error, {err.GetType()}, when none was expected.\n{err.Describe()}");
  105. }
  106. goto done;
  107. }
  108. var ty = _expectedErrors.Dequeue();
  109. if (AssertOnUnexpectedError)
  110. {
  111. Assert.That(
  112. err.GetType().IsAssignableTo(ty),
  113. $"The error {err.GetType()} wasn't assignable to the expected type {ty}.\n{err.Describe()}"
  114. );
  115. }
  116. done:
  117. _errors.Add(err);
  118. }
  119. public IEnumerable<IConError> GetErrors()
  120. {
  121. return _errors;
  122. }
  123. public bool HasErrors => _errors.Count > 0;
  124. public void ClearErrors()
  125. {
  126. _errors.Clear();
  127. }
  128. public object? ReadVar(string name)
  129. {
  130. return Variables.GetValueOrDefault(name);
  131. }
  132. public void WriteVar(string name, object? value)
  133. {
  134. Variables[name] = value;
  135. }
  136. public IEnumerable<string> GetVars()
  137. {
  138. return Variables.Keys;
  139. }
  140. public Dictionary<string, object?> Variables { get; } = new();
  141. protected void ExpectError(Type err)
  142. {
  143. _expectedErrors.Enqueue(err);
  144. }
  145. protected void ExpectError<T>()
  146. {
  147. _expectedErrors.Enqueue(typeof(T));
  148. }
  149. }