From 11f4c47851a6a49bbc4455a65951780ed11e4afe Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 19 May 2023 16:14:26 +0200 Subject: [PATCH] Add more unitests for CSharpFormatter utils (#938) * Add unit-tests for CSharpFormatter * . * t --- src/WireMock.Net/Util/CSharpFormatter.cs | 7 +- .../Util/BytesEncodingUtilsTests.cs | 6 +- .../Util/CSharpFormatterTests.cs | 105 ++++++++++++++++++ 3 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs diff --git a/src/WireMock.Net/Util/CSharpFormatter.cs b/src/WireMock.Net/Util/CSharpFormatter.cs index 66cf248f..f8fd8065 100644 --- a/src/WireMock.Net/Util/CSharpFormatter.cs +++ b/src/WireMock.Net/Util/CSharpFormatter.cs @@ -92,6 +92,7 @@ internal static class CSharpFormatter "while" }); #endregion + private const string Null = "null"; public static object ConvertToAnonymousObjectDefinition(object jsonBody) @@ -104,7 +105,7 @@ internal static class CSharpFormatter 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 { @@ -127,7 +128,7 @@ internal static class CSharpFormatter _ => $"UNHANDLED_CASE: {token}" }; } - + public static string ToCSharpStringLiteral(string? value) { var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty; @@ -139,7 +140,7 @@ internal static class CSharpFormatter return $"\"{escapedValue}\""; } - private static string FormatPropertyName(string propertyName) + public static string FormatPropertyName(string propertyName) { return CSharpReservedKeywords.Contains(propertyName) ? "@" + propertyName : propertyName; } diff --git a/test/WireMock.Net.Tests/Util/BytesEncodingUtilsTests.cs b/test/WireMock.Net.Tests/Util/BytesEncodingUtilsTests.cs index 5a945fde..64c09154 100644 --- a/test/WireMock.Net.Tests/Util/BytesEncodingUtilsTests.cs +++ b/test/WireMock.Net.Tests/Util/BytesEncodingUtilsTests.cs @@ -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(); diff --git a/test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs b/test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs new file mode 100644 index 00000000..d58b031e --- /dev/null +++ b/test/WireMock.Net.Tests/Util/CSharpFormatterTests.cs @@ -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); + } +} \ No newline at end of file