Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add tests for ByteArrayExtensions #38

Merged
merged 4 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
namespace MoreDotNet.Tests.Extensions.Common.ByteArrayExtensions
{
using System.Linq;
using System.Text;
using MoreDotNet.Extensions.Common;
using Xunit;

public class GetStringTests
{
[Fact]
public void GetString_NullBuffer_ShouldReturnEmptyString()
{
byte[] buffer = null;
var result = buffer.GetString();

Assert.Equal(string.Empty, result);
}

[Fact]
public void GetString_ZeroLengthBuffer_ShouldReturnEmptyString()
{
var buffer = new byte[] { };
var result = buffer.GetString();

Assert.Equal(string.Empty, result);
}

[Fact]
public void GetString_UTF8_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.UTF8, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_Unicode_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.Unicode, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_BigEndianUnicode_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.BigEndianUnicode, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF32_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.UTF32, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF32BigEndian_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(new UTF32Encoding(true, true), "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF7_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.UTF7, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_ANSI_Specified_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.Default, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_NoEncodingSpecified_ShouldReturnProperString()
{
var buffer = new byte[] { 77, 111, 114, 101, 32, 68, 111, 116, 32, 78, 101, 116 };
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

[Fact]
public void GetString_UTF16WithLeadingZeros_ShouldReturnProperString()
{
var buffer = new byte[] { 0xff, 0xfe, 0, 0, 77, 0, 111, 0, 97, 0, 114, 0, 101, 0, 32, 0, 68, 0, 111, 0, 116, 0, 32, 0, 78, 0, 101, 0, 116, 0 };
var result = buffer.GetString();

Assert.Equal(result, "\0Moare Dot Net");
}

[Fact]
public void GetString_ASCII_ShouldReturnProperString()
{
var buffer = GetBytesWithPreamble(Encoding.ASCII, "More Dot Net");
var result = buffer.GetString();

Assert.Equal(result, "More Dot Net");
}

private static byte[] GetBytesWithPreamble(Encoding encoding, string data) => encoding.GetPreamble().Concat(encoding.GetBytes(data)).ToArray();
}
}
1 change: 1 addition & 0 deletions Source/MoreDotNet.Test/MoreDotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<Compile Include="Extensions\Common\BooleanExtensions\WhenTrueTests.cs" />
<Compile Include="Extensions\Collections\CollectionExtensions\AddRangeTests.cs" />
<Compile Include="Extensions\Collections\CollectionExtensions\IsNullOrEmptyTests.cs" />
<Compile Include="Extensions\Common\ByteArrayExtensions\GetStringTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToHexStringTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToRgbStringTests.cs" />
<Compile Include="Extensions\Common\ConvertibleExtensions\ToOrDefaultTests.cs" />
Expand Down
16 changes: 11 additions & 5 deletions Source/MoreDotNet/Extensions/Common/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ FE FF UTF-16 big endian
{
encoding = Encoding.UTF8;
}
else if (buffer[0] == 0xfe && buffer[1] == 0xff)

// In addition to preamble check the length to help UTF16 with leading zeros be recognized properly as UTF32 is 4 bytes fixed with and UTF 16 is 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typos:
fixed with -> fixed width
UTF16 -> UTF-16
UTF 16 -> UTF-16
UTF32 -> UTF-32

Also UTF-16 is has variable width of 2 or 4 bytes. It's good to update the comment. 😸

else if (buffer[0] == 0xff && buffer[1] == 0xfe && buffer[2] == 0 && buffer[3] == 0 && buffer.Length % 4 == 0)
{
encoding = Encoding.UTF32;
}
else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
encoding = new UTF32Encoding(true, true);
}
else if (buffer[0] == 0xff && buffer[1] == 0xfe)
{
encoding = Encoding.Unicode;
}
else if (buffer[0] == 0xfe && buffer[1] == 0xff)
{
encoding = Encoding.BigEndianUnicode; // utf-16be
}
else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
encoding = Encoding.UTF32;
}
else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
{
encoding = Encoding.UTF7;
Expand Down