matches method

bool matches(
  1. dynamic actual,
  2. dynamic expected, {
  3. bool exactMaps = false,
})

Check the map keys and values determined by the definition.

Implementation

bool matches(dynamic actual, dynamic expected, {bool exactMaps = false}) {
  if (actual == null && expected == null) {
    return true;
  }

  /// if data is MockDataCallback do not need to match;
  if (expected is MockDataCallback) return true;
  if (expected is Matcher) {
    /// Check the match here to bypass the fallthrough strict equality check
    /// at the end.
    if (!expected.matches(actual)) {
      return false;
    }
  } else if (actual is Map && expected is Map) {
    // If exactMap is true, ensure that actual and expected have the same length.
    if (exactMaps && actual.length != expected.length) {
      return false;
    }

    for (final key in expected.keys.toList()) {
      if (!actual.containsKey(key)) {
        return false;
      } else if (expected[key] is Matcher) {
        // Check matcher for the configured request.
        if (!expected[key].matches(actual[key])) {
          return false;
        }
      } else if (expected[key] != actual[key]) {
        // Exact match unless map.
        if (expected[key] is Map && actual[key] is Map) {
          if (!matches(actual[key], expected[key], exactMaps: exactMaps)) {
            // Allow maps to use matchers.
            return false;
          }
        } else if (expected[key].toString() != actual[key].toString()) {
          // If some other kind of object like list then rely on `toString`
          // to provide comparison value.
          return false;
        }
      }
    }

    // If exactMap is true, check that there are no keys in actual that are not in expected.
    if (exactMaps && actual.keys.any((key) => !expected.containsKey(key))) {
      return false;
    }
  } else if (actual is List && expected is List) {
    for (var index in Iterable.generate(actual.length)) {
      if (!matches(actual[index], expected[index])) {
        return false;
      }
    }
  } else if (actual is Set && expected is Set) {
    final exactMatch = !matches(actual.containsAll(expected), false);

    if (exactMatch) {
      return true;
    }

    for (var index in Iterable.generate(actual.length)) {
      if (!matches(actual.elementAt(index), expected.elementAt(index))) {
        return false;
      }
    }
  } else if (actual != expected) {
    // Fall back to original check.
    return false;
  }

  return true;
}