Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

StringExtensions Unit Tests #13657

Merged
merged 3 commits into from
May 11, 2023
Merged
Changes from all 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,57 @@
using System;
using Xunit;

namespace OrchardCore.Modules
{
public class StringExtensionsTests
{
[Theory]
[InlineData(new byte[0], "")]
[InlineData(new byte[]{ 10, 20, 30 }, "0A141E")]
public void ToHexString_ReturnsCorrectHexString(byte[] bytes, string expected)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public void ToHexString_ReturnsCorrectHexString(byte[] bytes, string expected)
[InlineData(new byte[0], "")]
public void ToHexString_ReturnsCorrectHexString(byte[] bytes, string expected)

{
// Arrange

// Act
var result = bytes.ToHexString();

// Assert
Assert.Equal(expected, result);
}

[Theory]
[InlineData("414243", new byte[] { 0x41, 0x42, 0x43 })]
[InlineData("48656C6C6F20576F726C64", new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 })]
[InlineData("", new byte[0])]
public void ToByteArray_ShouldConvertToByteArray(string hex, byte[] expected)
{
// Arrange

// Act
var result = hex.ToByteArray();

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ToByteArray_ShouldThrowException_WhenHexContainsOddNumberOfCharacters()
{
// Arrange
var hex = "41424";

// Act & Assert
Assert.Throws<FormatException>(() => hex.ToByteArray());
}

[Fact]
public void ToByteArray_ShouldThrowException_WhenHexIsNotValid()
{
// Arrange
var hex = "GHIJK";

// Act & Assert
Assert.Throws<FormatException>(() => hex.ToByteArray());
}
}
}