2
0

SetupFakeApplicationAttribute.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using JetBrains.Annotations;
  5. using TerminalGuiFluentTesting;
  6. using Xunit.Sdk;
  7. namespace UnitTests;
  8. /// <summary>
  9. /// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new
  10. /// FakeDriver(). The driver is set up with 80 rows and 25 columns.
  11. /// </summary>
  12. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  13. public class SetupFakeApplicationAttribute : BeforeAfterTestAttribute
  14. {
  15. private IDisposable? _appDispose = null!;
  16. public override void Before (MethodInfo methodUnderTest)
  17. {
  18. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  19. _appDispose?.Dispose ();
  20. var appFactory = new FakeApplicationFactory ();
  21. _appDispose = appFactory.SetupFakeApplication ();
  22. base.Before (methodUnderTest);
  23. }
  24. public override void After (MethodInfo methodUnderTest)
  25. {
  26. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  27. _appDispose?.Dispose ();
  28. _appDispose = null;
  29. // TODO: This is troublesome; it seems to cause tests to hang when enabled, but shouldn't have any impact.
  30. // TODO: Uncomment after investigation.
  31. //ApplicationImpl.SetInstance (null);
  32. base.After (methodUnderTest);
  33. }
  34. /// <summary>
  35. /// Runs a single iteration of the main loop (layout, draw, run timed events etc.)
  36. /// </summary>
  37. public static void RunIteration () { ((ApplicationImpl)ApplicationImpl.Instance).Coordinator?.RunIteration (); }
  38. }