forked from graphql-dotnet/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShouldBeExtensions.cs
More file actions
58 lines (53 loc) · 2.05 KB
/
ShouldBeExtensions.cs
File metadata and controls
58 lines (53 loc) · 2.05 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
using Newtonsoft.Json.Linq;
using Shouldly;
namespace Samples.Server.Tests
{
internal static class ShouldBeExtensions
{
/// <summary>
/// Compares two strings after normalizing any encoding differences first.
/// </summary>
/// <param name="actual">Actual.</param>
/// <param name="expected">Expected.</param>
/// <param name="ignoreExtensions">
/// Pass true to ignore the `extensions` path of the JSON when comparing for equality.
/// </param>
public static void ShouldBeEquivalentJson(this string actual, string expected, bool ignoreExtensions)
{
if (ignoreExtensions)
{
if (actual.StartsWith('['))
{
var json = JArray.Parse(actual);
foreach (var item in json)
{
if (item is JObject obj)
{
obj.Remove("extensions");
}
}
actual = json.ToString(Newtonsoft.Json.Formatting.None);
}
else
{
var json = JObject.Parse(actual);
json.Remove("extensions");
actual = json.ToString(Newtonsoft.Json.Formatting.None);
}
}
actual.ShouldBeEquivalentJson(expected);
}
/// <summary>
/// Compares two strings after normalizing any encoding differences first.
/// </summary>
/// <param name="actualJson">Actual.</param>
/// <param name="expectedJson">Expected.</param>
public static void ShouldBeEquivalentJson(this string actualJson, string expectedJson)
{
expectedJson = expectedJson.NormalizeJson();
actualJson = actualJson.NormalizeJson();
actualJson.ShouldBe(expectedJson);
}
private static string NormalizeJson(this string json) => json.Replace("'", @"\u0027").Replace("\"", @"\u0022");
}
}