-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathFluentJsonElementExtensions.cs
More file actions
156 lines (128 loc) · 5.04 KB
/
FluentJsonElementExtensions.cs
File metadata and controls
156 lines (128 loc) · 5.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Text.Json;
using BlushingPenguin.JsonPath;
using FluentAssertions;
using FluentAssertions.Execution;
using JetBrains.Annotations;
namespace TestBuildingBlocks;
public static class FluentJsonElementExtensions
{
private const string ComponentSchemaPrefix = "#/components/schemas/";
public static JsonElementAssertions Should(this JsonElement source)
{
return new JsonElementAssertions(source);
}
[CustomAssertion]
public static SchemaReferenceIdContainer ShouldBeSchemaReferenceId(this JsonElement source, string value)
{
string schemaReferenceId = source.GetSchemaReferenceId();
schemaReferenceId.Should().Be(value);
return new SchemaReferenceIdContainer(value);
}
[CustomAssertion]
public static string GetSchemaReferenceId(this JsonElement source)
{
source.ValueKind.Should().Be(JsonValueKind.String);
string? jsonElementValue = source.GetString();
jsonElementValue.Should().StartWith(ComponentSchemaPrefix);
return jsonElementValue[ComponentSchemaPrefix.Length..];
}
[CustomAssertion]
public static void WithSchemaReferenceId(this JsonElement subject, [InstantHandle] Action<string> continuation)
{
string schemaReferenceId = subject.GetSchemaReferenceId();
continuation(schemaReferenceId);
}
public sealed class SchemaReferenceIdContainer
{
public string SchemaReferenceId { get; }
internal SchemaReferenceIdContainer(string schemaReferenceId)
{
SchemaReferenceId = schemaReferenceId;
}
}
public sealed class JsonElementAssertions : JsonElementAssertions<JsonElementAssertions>
{
internal JsonElementAssertions(JsonElement subject)
: base(subject)
{
}
}
public class JsonElementAssertions<TAssertions>
where TAssertions : JsonElementAssertions<TAssertions>
{
private readonly JsonElement _subject;
protected JsonElementAssertions(JsonElement subject)
{
_subject = subject;
}
public void ContainProperty(string propertyName)
{
string json = _subject.ToString();
string escapedJson = json.Replace("{", "{{").Replace("}", "}}");
Execute.Assertion.ForCondition(_subject.TryGetProperty(propertyName, out _))
.FailWith($"Expected JSON element '{escapedJson}' to contain a property named '{propertyName}'.");
}
public JsonElement ContainPath(string jsonPath)
{
Func<JsonElement> elementSelector = () => _subject.SelectToken(jsonPath, true)!.Value;
return elementSelector.Should().NotThrow().Subject;
}
public void NotContainPath(string jsonPath)
{
JsonElement? pathToken = _subject.SelectToken(jsonPath);
pathToken.Should().BeNull();
}
public void Be(object? value)
{
if (value == null)
{
_subject.ValueKind.Should().Be(JsonValueKind.Null);
}
else if (value is bool boolValue)
{
_subject.ValueKind.Should().Be(boolValue ? JsonValueKind.True : JsonValueKind.False);
}
else if (value is int intValue)
{
_subject.ValueKind.Should().Be(JsonValueKind.Number);
_subject.GetInt32().Should().Be(intValue);
}
else if (value is double doubleValue)
{
_subject.ValueKind.Should().Be(JsonValueKind.Number);
_subject.GetDouble().Should().Be(doubleValue);
}
else if (value is string stringValue)
{
_subject.ValueKind.Should().Be(JsonValueKind.String);
_subject.GetString().Should().Be(stringValue);
}
else
{
throw new NotSupportedException($"Unknown object of type '{value.GetType()}'.");
}
}
public void HaveProperty(string jsonPath, object? propertyValue)
{
_subject.Should().ContainPath(jsonPath).With(element => element.Should().Be(propertyValue));
}
public void ContainArrayElement(string value)
{
_subject.ValueKind.Should().Be(JsonValueKind.Array);
string?[] stringValues = _subject.EnumerateArray().Where(element => element.ValueKind == JsonValueKind.String)
.Select(element => element.GetString()).ToArray();
stringValues.Should().Contain(value);
}
public void NotContainArrayElement(string value)
{
_subject.ValueKind.Should().Be(JsonValueKind.Array);
string?[] stringValues = _subject.EnumerateArray().Where(element => element.ValueKind == JsonValueKind.String)
.Select(element => element.GetString()).ToArray();
stringValues.Should().NotContain(value);
}
public void BeJson(string json)
{
_subject.ToString().Should().BeJson(json);
}
}
}