TestRespondersDisposedAttribute.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. base.After (methodUnderTest);
  21. #if DEBUG_IDISPOSABLE
  22. View.DebugIDisposable = true;
  23. Assert.Empty (View.Instances);
  24. #endif
  25. }
  26. public override void Before (MethodInfo methodUnderTest)
  27. {
  28. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  29. base.Before (methodUnderTest);
  30. #if DEBUG_IDISPOSABLE
  31. View.DebugIDisposable = true;
  32. // Clear out any lingering Responder instances from previous tests
  33. View.Instances.Clear ();
  34. Assert.Empty (View.Instances);
  35. #endif
  36. }
  37. }