-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from M-Zuber/master
Add tests for ByteArrayExtensions
- Loading branch information
Showing
3 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
Source/MoreDotNet.Test/Extensions/Common/ByteArrayExtensions/GetStringTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters