AssertEx.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. _ = ex.Flatten().InnerException;
  39. // TODO: proper assert; unfortunately there's not always a good call stack
  40. }
  41. }
  42. }
  43. }