AsyncTests.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
  20. public Task AssertThrows<E>(Func<Task> func)
  21. where E : Exception
  22. {
  23. return Assert.ThrowsAsync<E>(func);
  24. }
  25. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  26. where E : Exception
  27. {
  28. var hasFailed = false;
  29. try
  30. {
  31. a();
  32. }
  33. catch (E e)
  34. {
  35. Assert.True(assert(e));
  36. hasFailed = true;
  37. }
  38. if (!hasFailed)
  39. {
  40. Assert.True(false);
  41. }
  42. }
  43. public void NoNext<T>(IAsyncEnumerator<T> e)
  44. {
  45. Assert.False(e.MoveNext().Result);
  46. }
  47. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  48. {
  49. Assert.True(e.MoveNext().Result);
  50. Assert.Equal(value, e.Current);
  51. }
  52. }
  53. }
  54. #endif