AsyncTests.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #if !NO_TPL
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public partial class AsyncTests
  13. {
  14. public void AssertThrows<E>(Action a)
  15. where E : Exception
  16. {
  17. Assert.Throws<E>(a);
  18. }
  19. public Task AssertThrows<E>(Func<Task> func)
  20. where E : Exception
  21. {
  22. return Assert.ThrowsAsync<E>(func);
  23. }
  24. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  25. where E : Exception
  26. {
  27. var hasFailed = false;
  28. try
  29. {
  30. a();
  31. }
  32. catch (E e)
  33. {
  34. Assert.True(assert(e));
  35. hasFailed = true;
  36. }
  37. if (!hasFailed)
  38. {
  39. Assert.True(false);
  40. }
  41. }
  42. public void NoNext<T>(IAsyncEnumerator<T> e)
  43. {
  44. Assert.False(e.MoveNext().Result);
  45. }
  46. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  47. {
  48. Assert.True(e.MoveNext().Result);
  49. Assert.Equal(value, e.Current);
  50. }
  51. }
  52. }
  53. #endif