mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 00:25:06 +01:00
Add more unitests for CSharpFormatter utils (#938)
* Add unit-tests for CSharpFormatter * . * t
This commit is contained in:
@@ -10,17 +10,17 @@ public class BytesEncodingUtilsTests
|
||||
[Fact]
|
||||
public void TryGetEncoding_UTF32()
|
||||
{
|
||||
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out Encoding encoding);
|
||||
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out var encoding);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
|
||||
encoding?.CodePage.Should().Be(Encoding.UTF32.CodePage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetEncoding_Invalid()
|
||||
{
|
||||
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out Encoding encoding);
|
||||
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out var encoding);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
|
||||
105
test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs
Normal file
105
test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using FluentAssertions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Util;
|
||||
|
||||
public class CSharpFormatterTests
|
||||
{
|
||||
[Fact]
|
||||
public void ConvertToAnonymousObjectDefinition_ShouldReturn_ValidValue_WhenJsonBodyIsValidJsonString()
|
||||
{
|
||||
// Arrange
|
||||
var jsonBody = new { Key1 = "value1", Key2 = 42, F = 1.2 };
|
||||
var expectedOutput = "new\r\n {\r\n Key1 = \"value1\",\r\n Key2 = 42,\r\n F = 1.2\r\n }";
|
||||
|
||||
// Act
|
||||
var result = CSharpFormatter.ConvertToAnonymousObjectDefinition(jsonBody);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToCSharpStringLiteral_ShouldReturn_ValidValue_WhenStringIsNotNull()
|
||||
{
|
||||
// Arrange
|
||||
var inputString = "test string";
|
||||
var expectedOutput = "\"test string\"";
|
||||
|
||||
// Act
|
||||
var result = CSharpFormatter.ToCSharpStringLiteral(inputString);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToCSharpStringLiteral_ShouldReturn_ValidValue_WhenStringContainsNewLineCharacters()
|
||||
{
|
||||
// Arrange
|
||||
var inputString = "line1\nline2\nline3";
|
||||
var expectedOutput = "@\"line1\nline2\nline3\"";
|
||||
|
||||
// Action
|
||||
var result = CSharpFormatter.ToCSharpStringLiteral(inputString);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FormatPropertyName_ShouldReturn_ValidPropertyName_WhenPropertyNameIsNotReserved()
|
||||
{
|
||||
// Arrange
|
||||
var propertyName = "propertyname";
|
||||
var expectedOutput = "propertyname";
|
||||
|
||||
// Action
|
||||
var result = CSharpFormatter.FormatPropertyName(propertyName);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FormatPropertyName_ShouldReturn_ValidPropertyName_WhenPropertyNameIsReserved()
|
||||
{
|
||||
// Arrange
|
||||
var propertyName = "class";
|
||||
var expectedOutput = "@class";
|
||||
|
||||
// Action
|
||||
var result = CSharpFormatter.FormatPropertyName(propertyName);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvertJsonToAnonymousObjectDefinition_ShouldReturn_ValidObject_WhenJsonInputIsValid()
|
||||
{
|
||||
// Arrange
|
||||
var jObject = new JObject
|
||||
(
|
||||
new JProperty("Name", "John Smith"),
|
||||
new JProperty("Age", 25.1f),
|
||||
new JProperty("Gender", "Male"),
|
||||
new JProperty("address", new JObject
|
||||
(
|
||||
new JProperty("Street", "123 Main St"),
|
||||
new JProperty("City", "Anytown"),
|
||||
new JProperty("State", "CA"),
|
||||
new JProperty("Zip", "90001")
|
||||
)
|
||||
));
|
||||
var expectedOutput = "new\r\n{\r\n Name = \"John Smith\",\r\n Age = 25.1,\r\n Gender = \"Male\",\r\n address = new\r\n {\r\n Street = \"123 Main St\",\r\n City = \"Anytown\",\r\n State = \"CA\",\r\n Zip = \"90001\"\r\n }\r\n}";
|
||||
|
||||
// Action
|
||||
var result = CSharpFormatter.ConvertJsonToAnonymousObjectDefinition(jObject);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedOutput);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user