-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. 😸