AssertableJson class
A powerful JSON testing utility that provides fluent assertions for JSON structures.
The AssertableJson class combines multiple mixins to provide a rich set of testing capabilities:
TappableMixinfor chaining operationsConditionableMixinfor conditional assertionsConditionMixinfor numeric validationsMatchingMixinfor pattern matchingHasMixinfor property verification
Core Features:
- Property existence checking
- Type validation
- Numeric comparisons
- Pattern matching
- Nested object navigation
- Array validation
Basic Usage:
final json = AssertableJson({
'name': 'John',
'age': 30,
'scores': [85, 90, 95]
});
json
.has('name')
.whereType<String>('name')
.where('name', 'John')
.has('age')
.isGreaterThan('age', 25)
.count('scores', 3);
Nested Objects:
final json = AssertableJson({
'user': {
'profile': {
'email': 'john@example.com'
}
}
});
json.hasNested('user.profile.email');
Conditional Testing:
json.when(isAdmin, (json) {
json.has('adminPrivileges');
});
Array Validation:
json.has('items', 3, (items) {
items.each((item) {
item.has('id').has('name');
});
});
Numeric Assertions:
json
.isGreaterThan('age', 18)
.isLessThan('score', 100)
.isBetween('rating', 1, 5);
Pattern Matching:
json
.whereType<String>('email')
.whereContains('email', '@')
.whereIn('status', ['active', 'pending']);
Schema Validation:
json.matchesSchema({
'id': int,
'name': String,
'active': bool
});
Property Interaction Tracking:
The class automatically tracks which properties have been accessed during
testing. Use verifyInteracted to ensure all properties have been checked:
json
.has('name')
.has('age')
.verifyInteracted(); // Fails if any properties weren't checked
See also:
- AssertableJsonBase for core functionality
HasMixinfor property verification methodsConditionMixinfor numeric assertionsMatchingMixinfor pattern matching capabilities
- Inheritance
-
- Object
- AssertableJsonBase
- AssertableJson
Constructors
- AssertableJson(dynamic jsonData)
-
Constructs an AssertableJson instance with the provided
jsonData.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- json ↔ dynamic
-
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
assertEmpty(
[String? path]) → void -
Asserts that the value at the specified
pathis empty.inherited -
count(
dynamic key, [int? length]) → AssertableJson -
Verifies the count of elements at a specific key or at the root level.
inherited
-
countBetween(
dynamic key, dynamic min, dynamic max) → AssertableJson -
Verifies that the count of elements at
keyis betweenminandmax, inclusive.inherited -
dd(
) → void -
Prints the JSON data in a human-readable format and stops execution.
inherited
-
each(
dynamic callback(AssertableJson)) → AssertableJson -
Iterates over the JSON data, either an array or an object, and calls the provided
callbackfunction for each element.inherited -
ensureSorted(
Map< String, dynamic> value) → void -
Ensures all nested maps within the given
valuehave their entries sorted by key.inherited -
equals(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keyequalsvalue.inherited -
etc(
) → AssertableJson -
Marks all properties in the JSON object as accessed.
inherited
-
exists(
String path) → bool -
Checks if the specified
pathexists within the JSON data.inherited -
first(
dynamic callback(AssertableJson)) → AssertableJson -
Scopes the current AssertableJson instance to the first element of the JSON data.
inherited
-
get<
T> (String path) → T? -
Gets the value at the specified
pathand casts it to the specified typeT.inherited -
getList<
T> (String path) → List< T> -
Gets a list of items at the specified
pathand casts them to the specified typeT.inherited -
getMap<
T> (String path) → Map< String, T> -
inherited
-
getRequired<
T> (String path) → T -
Gets the value at the specified
pathand casts it to the specified typeT.inherited -
has(
dynamic key, [dynamic arg1, AssertableJsonCallback? arg2]) → AssertableJson -
Verifies the existence of a
keyand optionally its content.inherited -
hasAll(
dynamic keys) → AssertableJson -
Verifies the existence of all specified
keys.inherited -
hasAny(
dynamic keys) → AssertableJson -
Verifies that at least one of the specified
keysexists.inherited -
hasNested(
String path) → AssertableJson -
Verifies the existence of a nested property at
path.inherited -
hasValues(
List values) → AssertableJson -
Verifies that all specified
valuesexist in the JSON object.inherited -
interactsWith(
String key) → void -
Records an interaction with the specified JSON property
key.inherited -
isBetween(
String key, num min, num max) → AssertableJson -
Asserts that the numeric value at
keyis betweenminandmaxinclusive.inherited -
isDivisibleBy(
String key, num divisor) → AssertableJson -
Asserts that the numeric value at
keyis divisible bydivisor.inherited -
isGreaterOrEqual(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keyis greater than or equal tovalue.inherited -
isGreaterThan(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keyis greater thanvalue.inherited -
isLessOrEqual(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keyis less than or equal tovalue.inherited -
isLessThan(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keyis less thanvalue.inherited -
isMultipleOf(
String key, num factor) → AssertableJson -
Asserts that the numeric value at
keyis a multiple offactor.inherited -
isNegative(
String key) → AssertableJson -
Asserts that the numeric value at
keyis negative (less than zero).inherited -
isPositive(
String key) → AssertableJson -
Asserts that the numeric value at
keyis positive (greater than zero).inherited -
keys(
[String? path]) → List< String> -
Gets a list of all keys at the specified
pathin the JSON data.inherited -
length(
[String? path]) → int -
Gets the length of the JSON data at the specified
path.inherited -
matchesSchema(
Map< String, Type> schema) → AssertableJson -
Validates that the JSON structure matches a given
schema.inherited -
missing(
String key) → AssertableJson -
Verifies that the specified
keyis missing from the JSON.inherited -
missingAll(
dynamic keys) → AssertableJson -
Verifies that all specified
keysare missing from the JSON.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notEquals(
String key, num value) → AssertableJson -
Asserts that the numeric value at
keydoes not equalvalue.inherited -
printR(
{int indent = 2}) → void -
Prints the JSON data in a human-readable format.
inherited
-
scope(
String key, dynamic callback(AssertableJson)) → AssertableJson -
Scopes the current AssertableJson instance to the specified
keyand calls the providedcallbackfunction with the scoped instance.inherited -
tap(
dynamic callback(AssertableJson)) → AssertableJson -
Executes the provided
callbackfunction on the current AssertableJson instance and returns the instance to enable method chaining.inherited -
toString(
) → String -
A string representation of this object.
inherited
-
unless(
bool condition, dynamic callback(AssertableJson)) → AssertableJson -
Executes the
callbackon this AssertableJson instance when theconditionis false.inherited -
values<
T> ([String? path]) → List< T> -
Gets a list of all the values at the specified
pathwithin the JSON data.inherited -
verifyInteracted(
) → void -
Verifies that all properties in the JSON object have been accessed.
inherited
-
when(
bool condition, dynamic callback(AssertableJson)) → AssertableJson -
Executes the
callbackon this AssertableJson instance when theconditionis true.inherited -
where(
String key, dynamic expected) → AssertableJson -
Asserts that a property matches an expected value or satisfies a condition.
inherited
-
whereAll(
Map< String, dynamic> bindings) → AssertableJson -
Asserts that multiple properties match their expected values.
inherited
-
whereAllType<
T> (List< String> keys) → AssertableJson -
Asserts that multiple properties are all of type
T.inherited -
whereContains(
String key, dynamic expected) → AssertableJson -
Asserts that a property contains an expected value.
inherited
-
whereIn(
String key, List values) → AssertableJson -
Asserts that a property's value is one of the provided
values.inherited -
whereNot(
String key, dynamic unexpected) → AssertableJson -
Asserts that a property does not match an unexpected value.
inherited
-
whereNotIn(
String key, List values) → AssertableJson -
Asserts that a property's value is not one of the provided
values.inherited -
whereType<
T> ([String? key]) → AssertableJson -
Asserts that a property is of a specific type
T.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited