AutoInitShutdownAttribute.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Diagnostics;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using Xunit.Sdk;
  5. namespace UnitTests;
  6. /// <summary>
  7. /// Enables test functions annotated with the [AutoInitShutdown] attribute to
  8. /// automatically call Application.Init at start of the test and Application.Shutdown after the
  9. /// test exits.
  10. /// This is necessary because a) Application is a singleton and Init/Shutdown must be called
  11. /// as a pair, and b) all unit test functions should be atomic..
  12. /// </summary>
  13. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  14. public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
  15. {
  16. /// <summary>
  17. /// Initializes a [AutoInitShutdown] attribute, which determines if/how Application.Init and Application.Shutdown
  18. /// are automatically called Before/After a test runs.
  19. /// </summary>
  20. /// <param name="autoInit">If true, Application.Init will be called Before the test runs.</param>
  21. /// <param name="consoleDriverType">
  22. /// Determines which IConsoleDriver (FakeDriver, WindowsDriver, CursesDriver, NetDriver)
  23. /// will be used when Application.Init is called. If null FakeDriver will be used. Only valid if
  24. /// <paramref name="autoInit"/> is true.
  25. /// </param>
  26. /// <param name="useFakeClipboard">
  27. /// If true, will force the use of <see cref="FakeDriver.FakeClipboard"/>. Only valid if
  28. /// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  29. /// </param>
  30. /// <param name="fakeClipboardAlwaysThrowsNotSupportedException">
  31. /// Only valid if <paramref name="autoInit"/> is true. Only
  32. /// valid if <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  33. /// </param>
  34. /// <param name="fakeClipboardIsSupportedAlwaysTrue">
  35. /// Only valid if <paramref name="autoInit"/> is true. Only valid if
  36. /// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  37. /// </param>
  38. /// <param name="verifyShutdown">If true and <see cref="Application.Initialized"/> is true, the test will fail.</param>
  39. public AutoInitShutdownAttribute (
  40. bool autoInit = true,
  41. Type consoleDriverType = null,
  42. bool useFakeClipboard = true,
  43. bool fakeClipboardAlwaysThrowsNotSupportedException = false,
  44. bool fakeClipboardIsSupportedAlwaysTrue = false,
  45. bool verifyShutdown = false
  46. )
  47. {
  48. AutoInit = autoInit;
  49. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  50. _driverType = consoleDriverType ?? typeof (FakeDriver);
  51. FakeDriver.FakeBehaviors.UseFakeClipboard = useFakeClipboard;
  52. FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException =
  53. fakeClipboardAlwaysThrowsNotSupportedException;
  54. FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
  55. _verifyShutdown = verifyShutdown;
  56. }
  57. private readonly bool _verifyShutdown;
  58. private readonly Type _driverType;
  59. public override void After (MethodInfo methodUnderTest)
  60. {
  61. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  62. // Turn off diagnostic flags in case some test left them on
  63. View.Diagnostics = ViewDiagnosticFlags.Off;
  64. if (AutoInit)
  65. {
  66. // try
  67. {
  68. if (!_verifyShutdown)
  69. {
  70. Application.ResetState (ignoreDisposed: true);
  71. }
  72. Application.Shutdown ();
  73. #if DEBUG_IDISPOSABLE
  74. if (View.Instances.Count == 0)
  75. {
  76. Assert.Empty (View.Instances);
  77. }
  78. else
  79. {
  80. View.Instances.Clear ();
  81. }
  82. #endif
  83. }
  84. //catch (Exception e)
  85. //{
  86. // Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}");
  87. //}
  88. //finally
  89. {
  90. #if DEBUG_IDISPOSABLE
  91. View.Instances.Clear ();
  92. Application.ResetState (true);
  93. #endif
  94. }
  95. }
  96. Debug.Assert (!CM.IsEnabled, "This test left ConfigurationManager enabled!");
  97. // Force the ConfigurationManager to reset to its hardcoded defaults
  98. CM.Disable(true);
  99. }
  100. public override void Before (MethodInfo methodUnderTest)
  101. {
  102. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  103. // Disable & force the ConfigurationManager to reset to its hardcoded defaults
  104. CM.Disable (true);
  105. //Debug.Assert(!CM.IsEnabled, "Some other test left ConfigurationManager enabled.");
  106. if (AutoInit)
  107. {
  108. #if DEBUG_IDISPOSABLE
  109. View.EnableDebugIDisposableAsserts = true;
  110. // Clear out any lingering Responder instances from previous tests
  111. if (View.Instances.Count == 0)
  112. {
  113. Assert.Empty (View.Instances);
  114. }
  115. else
  116. {
  117. View.Instances.Clear ();
  118. }
  119. #endif
  120. Application.Init ((IConsoleDriver)Activator.CreateInstance (_driverType));
  121. }
  122. }
  123. private bool AutoInit { get; }
  124. }