Skip to content

Commit

Permalink
Merge pull request #28 from jongleur1983/master
Browse files Browse the repository at this point in the history
#13 unit tests for StreamExtensions
  • Loading branch information
Teodor92 authored Oct 26, 2016
2 parents 6cc9bd4 + 35b443d commit d003ca6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace MoreDotNet.Tests.Extensions.Common.StreamExtensions
{
using System;
using System.IO;
using MoreDotNet.Extensions.Common;
using Xunit;

public class ToByteArrayTests
{
[Fact]
public void ToByteArray_OnNullInput_ShouldThrowArgumentNullException()
{
Stream input = null;
Assert.Throws<ArgumentNullException>(() => input.ToByteArray());
}

[Theory]
[InlineData(new byte[0])]
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })]
[InlineData(new byte[] { 0, 255, 4, 123 })]
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })]
public void ToByteArray_OnStream_ShouldReturnCorrectByteArray(byte[] expectedByteArray)
{
var stream = new MemoryStream(expectedByteArray);
Assert.Equal(expectedByteArray, stream.ToByteArray());
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
[InlineData(10000)]
[InlineData(100000)]
[InlineData(1000000)]
[InlineData(10000000)]
public void ToByteArray_OnStreamsOfReallyBigSize_ShouldReturnCorrectByteArray(int length)
{
var array = new byte[length];
for (int i = 0; i < length; i++)
{
array[i] = (byte)(i % 256);
}

var stream = new MemoryStream(array);
Assert.Equal(array, stream.ToByteArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace MoreDotNet.Tests.Extensions.Common.StreamExtensions
{
using System;
using System.IO;
using MoreDotNet.Extensions.Common;
using Xunit;

public class ToStreamTests
{
[Fact]
public void ToStream_OnNullInput_ShouldThrowArgumentNullException()
{
byte[] input = null;
Assert.Throws<ArgumentNullException>(() => input.ToStream());
}

[Theory]
[InlineData(new byte[0])]
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })]
[InlineData(new byte[] { 0, 255, 4, 123 })]
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })]
public void ToStream_OnByteArray_ReturnsCorrectStream(byte[] inputByteArray)
{
var expectedStream = new MemoryStream(inputByteArray);
var actualStream = inputByteArray.ToStream();

var expectedBuffer = new byte[16];
int expected;
var actualBuffer = new byte[16];

while ((expected = expectedStream.Read(expectedBuffer, 0, expectedBuffer.Length)) > 0)
{
var actual = actualStream.Read(actualBuffer, 0, actualBuffer.Length);
Assert.Equal(expected, actual);
}
}
}
}
2 changes: 2 additions & 0 deletions Source/MoreDotNet.Test/MoreDotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
<Compile Include="Extensions\Common\ObjectExtensions\IsNotTests.cs" />
<Compile Include="Extensions\Common\ObjectExtensions\ToDictionaryTests.cs" />
<Compile Include="Extensions\Common\ObjectExtensions\IsTests.cs" />
<Compile Include="Extensions\Common\StreamExtensions\ToByteArrayTests.cs" />
<Compile Include="Extensions\Common\StreamExtensions\ToStreamTests.cs" />
<Compile Include="Extensions\Common\StringExtensions\CapitalizeTests.cs" />
<Compile Include="Extensions\Common\StringExtensions\IsLikeTests.cs" />
<Compile Include="Extensions\Common\StringExtensions\NthIndexOfTests.cs" />
Expand Down

0 comments on commit d003ca6

Please # to comment.