SetupFakeApplicationAttribute.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. var appFactory = new FakeApplicationFactory ();
  20. _appDispose = appFactory.SetupFakeApplication ();
  21. base.Before (methodUnderTest);
  22. }
  23. public override void After (MethodInfo methodUnderTest)
  24. {
  25. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  26. _appDispose?.Dispose ();
  27. _appDispose = null;
  28. // TODO: This is troublesome; it seems to cause tests to hang when enabled, but shouldn't have any impact.
  29. // TODO: Uncomment after investigation.
  30. //ApplicationImpl.SetInstance (null);
  31. base.After (methodUnderTest);
  32. }
  33. /// <summary>
  34. /// Runs a single iteration of the main loop (layout, draw, run timed events etc.)
  35. /// </summary>
  36. public static void RunIteration () { ((ApplicationImpl)ApplicationImpl.Instance).Coordinator?.RunIteration (); }
  37. }