TimeSpanExtensionsTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Tests.Common;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. public class TimeSpanExtensionsTest
  9. {
  10. [TestMethod]
  11. public void AsTimeout_ValidTimeSpan_ReturnsExpectedMilliseconds()
  12. {
  13. var timeSpan = TimeSpan.FromSeconds(10);
  14. var timeout = timeSpan.AsTimeout();
  15. Assert.AreEqual(10000, timeout);
  16. }
  17. [TestMethod]
  18. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  19. public void AsTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
  20. {
  21. var timeSpan = TimeSpan.FromSeconds(-1);
  22. timeSpan.AsTimeout();
  23. }
  24. [TestMethod]
  25. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  26. public void AsTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
  27. {
  28. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  29. timeSpan.AsTimeout();
  30. }
  31. [TestMethod]
  32. public void AsTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
  33. {
  34. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  35. try
  36. {
  37. timeSpan.AsTimeout();
  38. }
  39. catch (ArgumentOutOfRangeException ex)
  40. {
  41. Assert.IsNull(ex.InnerException);
  42. ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
  43. Assert.AreEqual(nameof(timeSpan), ex.ParamName);
  44. }
  45. }
  46. [TestMethod]
  47. public void EnsureValidTimeout_ValidTimeSpan_DoesNotThrow()
  48. {
  49. var timeSpan = TimeSpan.FromSeconds(5);
  50. timeSpan.EnsureValidTimeout();
  51. }
  52. [TestMethod]
  53. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  54. public void EnsureValidTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
  55. {
  56. var timeSpan = TimeSpan.FromSeconds(-1);
  57. timeSpan.EnsureValidTimeout();
  58. }
  59. [TestMethod]
  60. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  61. public void EnsureValidTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
  62. {
  63. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  64. timeSpan.EnsureValidTimeout();
  65. }
  66. [TestMethod]
  67. public void EnsureValidTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
  68. {
  69. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  70. try
  71. {
  72. timeSpan.EnsureValidTimeout();
  73. }
  74. catch (ArgumentOutOfRangeException ex)
  75. {
  76. Assert.IsNull(ex.InnerException);
  77. ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
  78. Assert.AreEqual(nameof(timeSpan), ex.ParamName);
  79. }
  80. }
  81. }
  82. }