SetupFakeDriverAttribute.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using TerminalGuiFluentTesting;
  4. using Xunit.Sdk;
  5. namespace UnitTests;
  6. /// <summary>
  7. /// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new
  8. /// FakeDriver(). The driver is set up with 25 rows and columns.
  9. /// </summary>
  10. /// <remarks>
  11. /// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly.
  12. /// On After, sets Configuration.Locations to ConfigLocations.All.
  13. /// </remarks>
  14. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  15. public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
  16. {
  17. public override void After (MethodInfo methodUnderTest)
  18. {
  19. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  20. // Turn off diagnostic flags in case some test left them on
  21. View.Diagnostics = ViewDiagnosticFlags.Off;
  22. Application.ResetState (true);
  23. Assert.Null (Application.Driver);
  24. Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
  25. base.After (methodUnderTest);
  26. }
  27. public override void Before (MethodInfo methodUnderTest)
  28. {
  29. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  30. Application.ResetState (true);
  31. Assert.Null (Application.Driver);
  32. var ff = new FakeDriverFactory ();
  33. var driver = ff.Create ();
  34. Application.Driver = driver;
  35. driver.SetBufferSize (25, 25);
  36. Assert.Equal (new (0, 0, 25, 25), Application.Screen);
  37. // Ensures subscribing events, at least for the SizeChanged event
  38. Application.SubscribeDriverEvents ();
  39. base.Before (methodUnderTest);
  40. }
  41. }