count method

AssertableJson count(
  1. dynamic key, [
  2. int? length
])
inherited

Verifies the count of elements at a specific key or at the root level.

Usage patterns:

  • count(expectedCount) - Verifies the root level JSON has the specified count
  • count(key, expectedCount) - Verifies the element at key has the specified count

This method supports dot notation for nested property access, and can be used with both objects and arrays.

// Check root JSON object has 3 properties
json.count(3);

// Check 'items' array has 5 elements
json.count('items', 5);

// Check nested array using dot notation
json.count('user.permissions', 3);

Returns this instance for method chaining.

Implementation

AssertableJson count(dynamic key, [int? length]) {
  if (length == null) {
    expect(this.length(), equals(key),
        reason: 'Root level does not have the expected size');
    return this as AssertableJson;
  }

  expect(this.length(key), equals(length),
      reason: 'Property [$key] does not have the expected size');
  interactsWith(key);
  return this as AssertableJson;
}