AssertEx.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. namespace Tests
  4. {
  5. internal class AssertEx
  6. {
  7. internal static void Throws<T>(Action action)
  8. where T : Exception
  9. {
  10. Throws<T>(action, _ => true);
  11. }
  12. internal static void Throws<T>(Action action, Func<T, bool> assert)
  13. where T : Exception
  14. {
  15. var failed = false;
  16. try
  17. {
  18. action();
  19. }
  20. catch (T ex)
  21. {
  22. Assert.IsTrue(assert(ex));
  23. failed = true;
  24. }
  25. Assert.IsTrue(failed);
  26. }
  27. internal static void SucceedOrFailProper(Action action)
  28. {
  29. try
  30. {
  31. action();
  32. }
  33. catch (AggregateException ex)
  34. {
  35. var inner = ex.Flatten().InnerException;
  36. // TODO: proper assert; unfortunately there's not always a good call stack
  37. }
  38. }
  39. }
  40. }