Class JsonShapeAssertions
- Namespace
- JsonAssertions.TUnit
- Assembly
- JsonAssertions.TUnit.dll
Fluent TUnit entry points for asserting the shape of the value at a dot-separated
JSON path: that it is a JSON array of a given length (or non-empty, or empty), or that it
is of a given JsonValueKind. Each method is generated into a TUnit assertion
chain via the [GenerateAssertion] source generator and delegates to
JsonPath and JsonShape in the framework-agnostic core. Both a
JSON string and a JsonElement are accepted as the asserted
value.
public static class JsonShapeAssertions
- Inheritance
-
JsonShapeAssertions
- Inherited Members
Remarks
The methods return TUnit.Assertions.Core.AssertionResult so a failure surfaces either where the path resolution stopped, or the shape that was found in place of the expected one (via JsonFailureMessage).
Methods
HasEmptyJsonArray(string, string)
Asserts the value at the dot-separated path is a JSON array
with no elements.
[GenerateAssertion]
public static AssertionResult HasEmptyJsonArray(this string json, string path)
Parameters
jsonstringThe JSON document text.
pathstringA dot-separated property path, for example
user.roles.
Returns
- AssertionResult
HasEmptyJsonArray(JsonElement, string)
Asserts the value at the dot-separated path is a JSON array
with no elements.
[GenerateAssertion]
public static AssertionResult HasEmptyJsonArray(this JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA dot-separated property path, for example
user.roles.
Returns
- AssertionResult
HasJsonArrayLength(string, string, int)
Asserts the value at the dot-separated path is a JSON array
with exactly expectedLength elements.
[GenerateAssertion]
public static AssertionResult HasJsonArrayLength(this string json, string path, int expectedLength)
Parameters
jsonstringThe JSON document text.
pathstringA dot-separated property path, for example
user.roles.expectedLengthintThe expected array length.
Returns
- AssertionResult
HasJsonArrayLength(JsonElement, string, int)
Asserts the value at the dot-separated path is a JSON array
with exactly expectedLength elements.
[GenerateAssertion]
public static AssertionResult HasJsonArrayLength(this JsonElement element, string path, int expectedLength)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA dot-separated property path, for example
user.roles.expectedLengthintThe expected array length.
Returns
- AssertionResult
HasJsonBoolean(string, string)
Asserts the value at path is a JSON boolean (either
true or false). JSON's true and
false are distinct JsonValueKinds, so this is the
discoverable form of "this field is a boolean, either value" that
HasJsonValueKind alone cannot express.
[GenerateAssertion]
public static AssertionResult HasJsonBoolean(this string json, string path)
Parameters
jsonstringThe JSON document text.
pathstringA path of dot-separated property names and zero-based bracket indices, for example
user.active.
Returns
- AssertionResult
HasJsonBoolean(JsonElement, string)
[GenerateAssertion]
public static AssertionResult HasJsonBoolean(this JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA path of dot-separated property names and zero-based bracket indices, for example
user.active.
Returns
- AssertionResult
HasJsonValueKind(string, string, JsonValueKind)
Asserts the value at the dot-separated path is of the given
JsonValueKind.
[GenerateAssertion]
public static AssertionResult HasJsonValueKind(this string json, string path, JsonValueKind expectedKind)
Parameters
jsonstringThe JSON document text.
pathstringA dot-separated property path, for example
user.meta.expectedKindJsonValueKindThe expected JSON value kind.
Returns
- AssertionResult
HasJsonValueKind(JsonElement, string, JsonValueKind)
Asserts the value at the dot-separated path is of the given
JsonValueKind.
[GenerateAssertion]
public static AssertionResult HasJsonValueKind(this JsonElement element, string path, JsonValueKind expectedKind)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA dot-separated property path, for example
user.meta.expectedKindJsonValueKindThe expected JSON value kind.
Returns
- AssertionResult
HasNonEmptyJsonArray(string, string)
Asserts the value at the dot-separated path is a JSON array
with at least one element.
[GenerateAssertion]
public static AssertionResult HasNonEmptyJsonArray(this string json, string path)
Parameters
jsonstringThe JSON document text.
pathstringA dot-separated property path, for example
user.roles.
Returns
- AssertionResult
HasNonEmptyJsonArray(JsonElement, string)
Asserts the value at the dot-separated path is a JSON array
with at least one element.
[GenerateAssertion]
public static AssertionResult HasNonEmptyJsonArray(this JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA dot-separated property path, for example
user.roles.
Returns
- AssertionResult
HasNonEmptyJsonString(string, string)
Asserts the value at path is a non-empty JSON string.
[GenerateAssertion]
public static AssertionResult HasNonEmptyJsonString(this string json, string path)
Parameters
jsonstringThe JSON document text.
pathstringA path of dot-separated property names and zero-based bracket indices, for example
user.name.
Returns
- AssertionResult
HasNonEmptyJsonString(JsonElement, string)
Asserts the value at path is a non-empty JSON string.
[GenerateAssertion]
public static AssertionResult HasNonEmptyJsonString(this JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA path of dot-separated property names and zero-based bracket indices, for example
user.name.
Returns
- AssertionResult