|
| 1 | +using System.Collections.Generic; |
| 2 | +using FluentAssertions; |
| 3 | +using FluentAssertions.Execution; |
| 4 | +using GraphQL.Client.Serializer.Newtonsoft; |
| 5 | +using GraphQL.Client.Serializer.SystemTextJson; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace GraphQL.Client.Serializer.Tests |
| 10 | +{ |
| 11 | + public class ConsistencyTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void MapConvertersShouldBehaveConsistent() |
| 15 | + { |
| 16 | + const string json = @"{ |
| 17 | + ""array"": [ |
| 18 | + ""some stuff"", |
| 19 | + ""something else"" |
| 20 | + ], |
| 21 | + ""string"": ""this is a string"", |
| 22 | + ""boolean"": true, |
| 23 | + ""number"": 1234.567, |
| 24 | + ""nested object"": { |
| 25 | + ""prop1"": false |
| 26 | + }, |
| 27 | + ""arrayOfObjects"": [ |
| 28 | + {""number"": 1234.567}, |
| 29 | + {""number"": 567.8} |
| 30 | + ] |
| 31 | + }"; |
| 32 | + |
| 33 | + var newtonsoftSerializer = new NewtonsoftJsonSerializer(); |
| 34 | + var systemTextJsonSerializer = new SystemTextJsonSerializer(); |
| 35 | + |
| 36 | + var newtonsoftMap = JsonConvert.DeserializeObject<Map>(json, newtonsoftSerializer.JsonSerializerSettings); |
| 37 | + var systemTextJsonMap = System.Text.Json.JsonSerializer.Deserialize<Map>(json, systemTextJsonSerializer.Options); |
| 38 | + |
| 39 | + |
| 40 | + using(new AssertionScope()) |
| 41 | + { |
| 42 | + CompareMaps(newtonsoftMap, systemTextJsonMap); |
| 43 | + } |
| 44 | + |
| 45 | + newtonsoftMap.Should().BeEquivalentTo(systemTextJsonMap, options => options |
| 46 | + .RespectingRuntimeTypes()); |
| 47 | + } |
| 48 | + |
| 49 | + private void CompareMaps(Dictionary<string, object> first, Dictionary<string, object> second) |
| 50 | + { |
| 51 | + foreach (var keyValuePair in first) |
| 52 | + { |
| 53 | + second.Should().ContainKey(keyValuePair.Key); |
| 54 | + second[keyValuePair.Key].Should().BeOfType(keyValuePair.Value.GetType()); |
| 55 | + if(keyValuePair.Value is Dictionary<string, object> map) |
| 56 | + CompareMaps(map, (Dictionary<string, object>)second[keyValuePair.Key]); |
| 57 | + else |
| 58 | + keyValuePair.Value.Should().BeEquivalentTo(second[keyValuePair.Key]); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments