2
0

TimeSpanExtensionsTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public void AsTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
  19. {
  20. var timeSpan = TimeSpan.FromSeconds(-1);
  21. Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
  22. }
  23. [TestMethod]
  24. public void AsTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
  25. {
  26. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  27. Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
  28. }
  29. [TestMethod]
  30. public void AsTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
  31. {
  32. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  33. try
  34. {
  35. timeSpan.AsTimeout();
  36. }
  37. catch (ArgumentOutOfRangeException ex)
  38. {
  39. Assert.IsNull(ex.InnerException);
  40. ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
  41. Assert.AreEqual(nameof(timeSpan), ex.ParamName);
  42. }
  43. }
  44. [TestMethod]
  45. public void EnsureValidTimeout_ValidTimeSpan_DoesNotThrow()
  46. {
  47. var timeSpan = TimeSpan.FromSeconds(5);
  48. timeSpan.EnsureValidTimeout();
  49. }
  50. [TestMethod]
  51. public void EnsureValidTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
  52. {
  53. var timeSpan = TimeSpan.FromSeconds(-1);
  54. Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
  55. }
  56. [TestMethod]
  57. public void EnsureValidTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
  58. {
  59. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  60. Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
  61. }
  62. [TestMethod]
  63. public void EnsureValidTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
  64. {
  65. var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
  66. try
  67. {
  68. timeSpan.EnsureValidTimeout();
  69. }
  70. catch (ArgumentOutOfRangeException ex)
  71. {
  72. Assert.IsNull(ex.InnerException);
  73. ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
  74. Assert.AreEqual(nameof(timeSpan), ex.ParamName);
  75. }
  76. }
  77. }
  78. }