2
0

ExtensionsTest_Take_Count.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. [SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
  9. public class ExtensionsTest_Take_Count
  10. {
  11. private Random _random;
  12. [TestInitialize]
  13. public void Init()
  14. {
  15. _random = new Random();
  16. }
  17. [TestMethod]
  18. public void ShouldThrowArgumentNullExceptionWhenValueIsNull()
  19. {
  20. const byte[] value = null;
  21. const int count = 0;
  22. try
  23. {
  24. Extensions.Take(value, count);
  25. Assert.Fail();
  26. }
  27. catch (ArgumentNullException ex)
  28. {
  29. Assert.IsNull(ex.InnerException);
  30. Assert.AreEqual("value", ex.ParamName);
  31. }
  32. }
  33. [TestMethod]
  34. public void ShouldReturnEmptyByteArrayWhenCountIsZero()
  35. {
  36. var value = CreateBuffer(16);
  37. const int count = 0;
  38. var actual = Extensions.Take(value, count);
  39. Assert.IsNotNull(actual);
  40. Assert.AreEqual(0, actual.Length);
  41. }
  42. [TestMethod]
  43. public void ShouldReturnValueWhenCountIsEqualToLengthOfValue()
  44. {
  45. var value = CreateBuffer(16);
  46. var count = value.Length;
  47. var actual = Extensions.Take(value, count);
  48. Assert.IsNotNull(actual);
  49. Assert.AreSame(value, actual);
  50. }
  51. [TestMethod]
  52. public void ShouldReturnLeadingBytesWhenCountIsLessThanLengthOfValue()
  53. {
  54. var value = CreateBuffer(16);
  55. const int count = 5;
  56. var actual = Extensions.Take(value, count);
  57. Assert.IsNotNull(actual);
  58. Assert.AreEqual(count, actual.Length);
  59. Assert.AreEqual(value[0], actual[0]);
  60. Assert.AreEqual(value[1], actual[1]);
  61. Assert.AreEqual(value[2], actual[2]);
  62. Assert.AreEqual(value[3], actual[3]);
  63. Assert.AreEqual(value[4], actual[4]);
  64. }
  65. [TestMethod]
  66. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  67. {
  68. var value = CreateBuffer(16);
  69. var count = value.Length + 1;
  70. try
  71. {
  72. Extensions.Take(value, count);
  73. Assert.Fail();
  74. }
  75. catch (ArgumentException)
  76. {
  77. }
  78. }
  79. private byte[] CreateBuffer(int length)
  80. {
  81. var buffer = new byte[length];
  82. _random.NextBytes(buffer);
  83. return buffer;
  84. }
  85. }
  86. }