|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Docfx.Common; |
| 5 | +using FluentAssertions; |
| 6 | + |
| 7 | +namespace docfx.Tests; |
| 8 | + |
| 9 | +public partial class JsonSerializationEncoderTest |
| 10 | +{ |
| 11 | + [Theory] |
| 12 | + [InlineData("abcdefghighlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")] |
| 13 | + [InlineData("0123456789")] |
| 14 | + [InlineData("\0\a\b\t\n\v\f\r\e")] |
| 15 | + [InlineData("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")] |
| 16 | + [InlineData("①②③")] // NonAscii chars (Enclosed Alphanumerics) |
| 17 | + [InlineData("123")] // NonAscii chars (Full-width digits) |
| 18 | + [InlineData("äöü")] // Umlaut |
| 19 | + [InlineData("漢字")] // Kanji |
| 20 | + public void JsonEncoderTest(string data) |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var model = data; |
| 24 | + |
| 25 | + //Act |
| 26 | + var systemTextJsonResult = SystemTextJsonUtility.Serialize(model); |
| 27 | + var newtonsoftJsonResult = NewtonsoftJsonUtility.Serialize(model); |
| 28 | + |
| 29 | + // Assert |
| 30 | + // Compare serialized result text with `StringComparer.OrdinalIgnoreCase` |
| 31 | + // - SystemTextJson escape chars with capital case(`\u001B`) |
| 32 | + // - NewtonsoftJson escape chars with lower case (`\u001b`) |
| 33 | + ((object)systemTextJsonResult).Should().Be(newtonsoftJsonResult, StringComparer.OrdinalIgnoreCase); // Currently StringAssertions don't expose overload that accepts StringComparer. (See: https://github.com/fluentassertions/fluentassertions/issues/2720) |
| 34 | + } |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [InlineData(" ", @"\u3000")] // Full-Width space (Excaped by global block list (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#global-block-list)) |
| 38 | + [InlineData("𠮟", @"\uD842\uDF9F")] // Kanji (that use Surrogate Pair) |
| 39 | + [InlineData("📄", @"\uD83D\uDCC4")] // Emoji |
| 40 | + [InlineData("👁🗨", @"\uD83D\uDC41\uD83D\uDDE8")] // Emoji (with ZWJ (ZERO WIDTH JOINER)) |
| 41 | + public void JsonEncoderTest_NoCompatibility(string data, string expected) |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + var model = data; |
| 45 | + |
| 46 | + //Act |
| 47 | + var systemTextJsonResult = SystemTextJsonUtility.Serialize(model); |
| 48 | + var newtonsoftJsonResult = NewtonsoftJsonUtility.Serialize(model); |
| 49 | + |
| 50 | + // Assert |
| 51 | + systemTextJsonResult.Should().NotBe(newtonsoftJsonResult); |
| 52 | + |
| 53 | + systemTextJsonResult.Should().Contain(expected); |
| 54 | + newtonsoftJsonResult.Should().Contain(data); |
| 55 | + } |
| 56 | +} |
0 commit comments