-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJsonComparer.cs
More file actions
136 lines (125 loc) · 5.16 KB
/
JsonComparer.cs
File metadata and controls
136 lines (125 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Socolin.TestUtils.JsonComparer.Color;
using Socolin.TestUtils.JsonComparer.Comparers;
using Socolin.TestUtils.JsonComparer.Errors;
using Socolin.TestUtils.JsonComparer.Handlers;
using Socolin.TestUtils.JsonComparer.Utils;
namespace Socolin.TestUtils.JsonComparer;
[PublicAPI]
public interface IJsonComparer
{
IList<IJsonCompareError<JToken>> Compare(string expectedJson, string actualJson, JsonComparisonOptions? options = null);
IList<IJsonCompareError<JToken>> Compare(JToken? expected, JToken? actual, JsonComparisonOptions? options = null);
IEnumerable<IJsonCompareError<JToken>> Compare(JToken? expected, JToken? actual, string path, JsonComparisonOptions? options = null);
}
public class JsonComparer : IJsonComparer
{
private readonly IJsonObjectComparer _jsonObjectComparer;
private readonly IJsonArrayComparer _jsonArrayComparer;
private readonly IJsonValueComparer _jsonValueComparer;
private readonly IJsonSpecialHandler _jsonSpecialHandler;
private readonly IJsonDeserializer _jsonDeserializer;
public static JsonComparer GetDefault(
Action<string, JToken>? captureHandler = null,
bool useColor = false,
JsonComparerColorOptions? colorOptions = null,
RegexAliasesContainer? regexAliasesContainer = null
)
{
return new JsonComparer(
new JsonObjectComparer(),
new JsonArrayComparer(),
new JsonValueComparer(),
new JsonSpecialHandler(captureHandler, new JsonObjectPartialComparer(), new PartialArrayHandler(), regexAliasesContainer),
new JsonDeserializerWithNiceError(colorOptions ?? (useColor ? JsonComparerColorOptions.DefaultColored : JsonComparerColorOptions.Default))
);
}
public JsonComparer(
IJsonObjectComparer jsonObjectComparer,
IJsonArrayComparer jsonArrayComparer,
IJsonValueComparer jsonValueComparer,
IJsonSpecialHandler jsonSpecialHandler,
IJsonDeserializer jsonDeserializer
)
{
_jsonObjectComparer = jsonObjectComparer;
_jsonArrayComparer = jsonArrayComparer;
_jsonValueComparer = jsonValueComparer;
_jsonSpecialHandler = jsonSpecialHandler;
_jsonDeserializer = jsonDeserializer;
}
public IList<IJsonCompareError<JToken>> Compare(string expectedJson, string actualJson, JsonComparisonOptions? options = null)
{
var expected = _jsonDeserializer.Deserialize<JToken>(expectedJson,
new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
});
var actual = _jsonDeserializer.Deserialize<JToken>(actualJson,
new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
});
return Compare(expected, actual, options);
}
public IList<IJsonCompareError<JToken>> Compare(JToken? expected, JToken? actual, JsonComparisonOptions? options = null)
{
return Compare(expected, actual, "", options).ToList();
}
public IEnumerable<IJsonCompareError<JToken>> Compare(JToken? expected, JToken? actual, string path, JsonComparisonOptions? options = null)
{
if (expected == null && actual == null)
yield break;
if (expected == null)
{
yield return new InvalidTypeJsonCompareError("", expected, actual);
yield break;
}
if (actual == null)
{
yield return new InvalidTypeJsonCompareError("", expected, actual);
yield break;
}
var (captureSucceeded, captureErrors) = _jsonSpecialHandler.HandleSpecialObject(expected, actual, path, this, options);
if (captureSucceeded)
yield break;
if (captureErrors?.Count > 0)
{
foreach (var error in captureErrors)
yield return error;
yield break;
}
if (expected.Type != actual.Type)
{
yield return new InvalidTypeJsonCompareError(path, expected, actual);
yield break;
}
IEnumerable<IJsonCompareError<JToken>> errors;
switch (actual.Type)
{
case JTokenType.Object:
errors = _jsonObjectComparer.Compare((JObject)expected, (JObject)actual, this, path, options);
break;
case JTokenType.Array:
errors = _jsonArrayComparer.Compare((JArray)expected, (JArray)actual, this, path, options);
break;
case JTokenType.Integer:
case JTokenType.Float:
case JTokenType.String:
case JTokenType.Boolean:
case JTokenType.Null:
case JTokenType.Undefined:
case JTokenType.Date:
errors = _jsonValueComparer.Compare((JValue)expected, (JValue)actual, path, options);
break;
default:
throw new ArgumentOutOfRangeException(nameof(actual.Type), actual.Type, @"Cannot compare this type");
}
foreach (var jsonCompareError in errors)
{
yield return jsonCompareError;
}
}
}