TestRespondersDisposedAttribute.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 [TestRespondersDisposed] attribute to ensure all Views are disposed.
  8. /// </summary>
  9. /// <remarks>
  10. /// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly.
  11. /// On After, sets Configuration.Locations to ConfigLocations.All.
  12. /// </remarks>
  13. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  14. public class TestRespondersDisposedAttribute : BeforeAfterTestAttribute
  15. {
  16. public TestRespondersDisposedAttribute () { CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US"); }
  17. public override void After (MethodInfo methodUnderTest)
  18. {
  19. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  20. Debug.Assert (!CM.IsEnabled, "This test left ConfigurationManager enabled!");
  21. base.After (methodUnderTest);
  22. #if DEBUG_IDISPOSABLE
  23. Assert.True (View.EnableDebugIDisposableAsserts);
  24. Assert.Empty (View.Instances);
  25. #endif
  26. }
  27. public override void Before (MethodInfo methodUnderTest)
  28. {
  29. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  30. base.Before (methodUnderTest);
  31. #if DEBUG_IDISPOSABLE
  32. View.EnableDebugIDisposableAsserts = true;
  33. // Clear out any lingering Responder instances from previous tests
  34. View.Instances.Clear ();
  35. Assert.Empty (View.Instances);
  36. #endif
  37. }
  38. }