mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:08:28 +02:00
Add more unitests for CSharpFormatter utils (#938)
* Add unit-tests for CSharpFormatter * . * t
This commit is contained in:
@@ -92,6 +92,7 @@ internal static class CSharpFormatter
|
|||||||
"while"
|
"while"
|
||||||
});
|
});
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private const string Null = "null";
|
private const string Null = "null";
|
||||||
|
|
||||||
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
|
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
|
||||||
@@ -104,7 +105,7 @@ internal static class CSharpFormatter
|
|||||||
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
|
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
|
public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
|
||||||
{
|
{
|
||||||
return token switch
|
return token switch
|
||||||
{
|
{
|
||||||
@@ -127,7 +128,7 @@ internal static class CSharpFormatter
|
|||||||
_ => $"UNHANDLED_CASE: {token}"
|
_ => $"UNHANDLED_CASE: {token}"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ToCSharpStringLiteral(string? value)
|
public static string ToCSharpStringLiteral(string? value)
|
||||||
{
|
{
|
||||||
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
||||||
@@ -139,7 +140,7 @@ internal static class CSharpFormatter
|
|||||||
return $"\"{escapedValue}\"";
|
return $"\"{escapedValue}\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string FormatPropertyName(string propertyName)
|
public static string FormatPropertyName(string propertyName)
|
||||||
{
|
{
|
||||||
return CSharpReservedKeywords.Contains(propertyName) ? "@" + propertyName : propertyName;
|
return CSharpReservedKeywords.Contains(propertyName) ? "@" + propertyName : propertyName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ public class BytesEncodingUtilsTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void TryGetEncoding_UTF32()
|
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
|
// Assert
|
||||||
result.Should().BeTrue();
|
result.Should().BeTrue();
|
||||||
encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
|
encoding?.CodePage.Should().Be(Encoding.UTF32.CodePage);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void TryGetEncoding_Invalid()
|
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
|
// Assert
|
||||||
result.Should().BeFalse();
|
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