AsyncTests.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_TPL
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. namespace Tests
  8. {
  9. [TestClass]
  10. public partial class AsyncTests
  11. {
  12. public void AssertThrows<E>(Action a)
  13. where E : Exception
  14. {
  15. var hasFailed = false;
  16. try
  17. {
  18. a();
  19. }
  20. catch (E)
  21. {
  22. hasFailed = true;
  23. }
  24. if (!hasFailed)
  25. {
  26. Assert.Fail();
  27. }
  28. }
  29. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  30. where E : Exception
  31. {
  32. var hasFailed = false;
  33. try
  34. {
  35. a();
  36. }
  37. catch (E e)
  38. {
  39. Assert.IsTrue(assert(e));
  40. hasFailed = true;
  41. }
  42. if (!hasFailed)
  43. {
  44. Assert.Fail();
  45. }
  46. }
  47. public void NoNext<T>(IAsyncEnumerator<T> e)
  48. {
  49. Assert.IsFalse(e.MoveNext().Result);
  50. }
  51. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  52. {
  53. Assert.IsTrue(e.MoveNext().Result);
  54. Assert.AreEqual(value, e.Current);
  55. }
  56. }
  57. }
  58. #endif