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

Add more unitests for CSharpFormatter utils #938

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/WireMock.Net/Util/CSharpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ internal static class CSharpFormatter
"while"
});
#endregion

private const string Null = "null";

public static object ConvertToAnonymousObjectDefinition(object jsonBody)
Expand All @@ -104,7 +105,7 @@ public static object ConvertToAnonymousObjectDefinition(object jsonBody)
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
{
Expand All @@ -127,7 +128,7 @@ private static string ConvertJsonToAnonymousObjectDefinition(JToken token, int i
_ => $"UNHANDLED_CASE: {token}"
};
}

public static string ToCSharpStringLiteral(string? value)
{
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
Expand All @@ -139,7 +140,7 @@ public static string ToCSharpStringLiteral(string? value)
return $"\"{escapedValue}\"";
}

private static string FormatPropertyName(string propertyName)
public static string FormatPropertyName(string propertyName)
{
return CSharpReservedKeywords.Contains(propertyName) ? "@" + propertyName : propertyName;
}
Expand Down
6 changes: 3 additions & 3 deletions test/WireMock.Net.Tests/Util/BytesEncodingUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
105 changes: 105 additions & 0 deletions test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}