AsyncTests.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public partial class AsyncTests
  12. {
  13. public void AssertThrows<E>(Action a)
  14. where E : Exception
  15. {
  16. Assert.Throws<E>(a);
  17. }
  18. [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
  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. }