AsyncTests.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 FluentAssertions;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public partial class AsyncTests
  13. {
  14. #pragma warning disable xUnit1013 // Public method should be marked as test
  15. public void AssertThrows<E>(Action a)
  16. where E : Exception
  17. {
  18. Assert.Throws<E>(a);
  19. }
  20. [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
  21. public Task AssertThrows<E>(Func<Task> func)
  22. where E : Exception
  23. {
  24. return Assert.ThrowsAsync<E>(func);
  25. }
  26. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  27. where E : Exception
  28. {
  29. var hasFailed = false;
  30. try
  31. {
  32. a();
  33. }
  34. catch (E e)
  35. {
  36. Assert.True(assert(e));
  37. hasFailed = true;
  38. }
  39. if (!hasFailed)
  40. {
  41. Assert.True(false);
  42. }
  43. }
  44. public void NoNext<T>(IAsyncEnumerator<T> e)
  45. {
  46. Assert.False(e.MoveNextAsync().Result);
  47. }
  48. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  49. {
  50. Assert.True(e.MoveNextAsync().Result);
  51. Assert.Equal(value, e.Current);
  52. }
  53. public async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  54. {
  55. var en1 = enumerable.GetAsyncEnumerator();
  56. var en2 = enumerable.GetAsyncEnumerator();
  57. Assert.Equal(en1.GetType(), en2.GetType());
  58. await en1.DisposeAsync();
  59. await en2.DisposeAsync();
  60. var e1t = enumerable.ToList();
  61. var e2t = enumerable.ToList();
  62. await Task.WhenAll(e1t, e2t);
  63. var e1Result = e1t.Result;
  64. var e2Result = e2t.Result;
  65. e1Result.ShouldAllBeEquivalentTo(e2Result);
  66. }
  67. #pragma warning restore xUnit1013 // Public method should be marked as test
  68. }
  69. }