TestHelpers.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using NUnit.Framework;
  5. namespace Unity.Cloud.Collaborate.Tests
  6. {
  7. public static class TestHelpers
  8. {
  9. public const string TestDirectory = "SomePathName/";
  10. static readonly TaskFactory k_MyTaskFactory = new
  11. TaskFactory(CancellationToken.None,
  12. TaskCreationOptions.None,
  13. TaskContinuationOptions.None,
  14. TaskScheduler.Default);
  15. public static TResult RunSync<TResult>(Func<Task<TResult>> func)
  16. {
  17. return k_MyTaskFactory
  18. .StartNew(func)
  19. .Unwrap()
  20. .GetAwaiter()
  21. .GetResult();
  22. }
  23. public static void RunSync(Func<Task> func)
  24. {
  25. k_MyTaskFactory
  26. .StartNew(func)
  27. .Unwrap()
  28. .GetAwaiter()
  29. .GetResult();
  30. }
  31. public static void ThrowsAsync<T>(Func<Task> asyncDelegate) where T : Exception
  32. {
  33. Assert.Throws<T>(() => RunSync(asyncDelegate));
  34. }
  35. public static void ShouldBe<T>(this T expr1, T value, string msg = "")
  36. {
  37. if (!expr1.Equals(value))
  38. throw new InvalidOperationException($"Test expected {value}, but found : {expr1}. [{msg}]");
  39. }
  40. public static void ShouldBe(this object expr1, object value, string msg = "")
  41. {
  42. if (expr1 != value)
  43. throw new InvalidOperationException($"Test expected {value}, but found : {expr1}. [{msg}]");
  44. }
  45. public static void ShouldBeNull(object obj, string msg = "")
  46. {
  47. if (obj != null)
  48. throw new InvalidOperationException($"Test expected null value, but found : {obj}. [{msg}]" );
  49. }
  50. }
  51. }