-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
JsonElementAssertions.cs
152 lines (134 loc) · 6.77 KB
/
JsonElementAssertions.cs
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
using System.Diagnostics;
using System.Text.Json;
using FluentAssertions.Execution;
namespace FluentAssertions.Json;
/// <summary>
/// Contains a number of methods to assert that an <see cref="JsonElement" /> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class JsonElementAssertions
{
public JsonElement Subject { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonElementAssertions" /> class.
/// </summary>
/// <param name="subject">The subject.</param>
public JsonElementAssertions(JsonElement subject)
{
Subject = subject;
}
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> has the specified <see cref="JsonValueKind"/>.
/// </summary>
/// <param name="valueKind">The JSON string.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> HaveValueKind(JsonValueKind valueKind, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == valueKind)
.FailWith("Expected {context:JSON} to have value kind {0}{reason}, but found {1}.", valueKind, Subject.ValueKind);
return new(this);
}
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> is the JSON null node.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> BeNull(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == JsonValueKind.Null)
.FailWith("Expected {context:JSON} to be a JSON null{reason}, but found {0}.", Subject);
return new(this);
}
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> is the JSON string node.
/// </summary>
/// <param name="value">The value of the JSON string node.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> BeString(string value, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == JsonValueKind.String && Subject.GetString() == value)
.FailWith("Expected {context:JSON} to be a JSON string {0}{reason}, but found {1}.", value, Subject);
return new(this);
}
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> is the JSON number node.
/// </summary>
/// <param name="value">The value of the JSON number node.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> BeNumber(decimal value, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == JsonValueKind.Number && Subject.GetDecimal() == value)
.FailWith("Expected {context:JSON} to be a JSON string {0}{reason}, but found {1}.", value, Subject);
return new(this);
}
/// <inheritdoc cref="BeNumber(decimal, string, object[])"/>
public AndConstraint<JsonElementAssertions> BeNumber(long value, string because = "", params object[] becauseArgs)
=> BeNumber((decimal)value, because, becauseArgs);
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> is the JSON true node.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> BeTrue(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == JsonValueKind.True)
.FailWith("Expected {context:JSON} to be a JSON true{reason}, but found {0}.", Subject);
return new(this);
}
/// <summary>
/// Asserts that the current <see cref="JsonElement"/> is the JSON false node.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public AndConstraint<JsonElementAssertions> BeFalse(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject.ValueKind == JsonValueKind.False)
.FailWith("Expected {context:JSON} to be JSON false{reason}, but found {0}.", Subject);
return new(this);
}
}