AssertEx.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using Xunit;
  5. using System;
  6. namespace Tests
  7. {
  8. internal class AssertEx
  9. {
  10. internal static void Throws<T>(Action action)
  11. where T : Exception
  12. {
  13. Throws<T>(action, _ => true);
  14. }
  15. internal static void Throws<T>(Action action, Func<T, bool> assert)
  16. where T : Exception
  17. {
  18. var failed = false;
  19. try
  20. {
  21. action();
  22. }
  23. catch (T ex)
  24. {
  25. Assert.True(assert(ex));
  26. failed = true;
  27. }
  28. Assert.True(failed);
  29. }
  30. internal static void SucceedOrFailProper(Action action)
  31. {
  32. try
  33. {
  34. action();
  35. }
  36. catch (AggregateException ex)
  37. {
  38. var inner = ex.Flatten().InnerException;
  39. // TODO: proper assert; unfortunately there's not always a good call stack
  40. }
  41. }
  42. }
  43. }