Dart API ReferenceunittestCallMatcher

CallMatcher Class

A CallMatcher is a special matcher used to match method calls (i.e. a method name and set of arguments). It is not a Matcher like the unit test Matcher, but instead represents a method name and a collection of Matchers, one per argument, that will be applied to the parameters to decide if the method call is a match.

Constructors

Code new CallMatcher([name, arg0 = _noArg, arg1 = _noArg, arg2 = _noArg, arg3 = _noArg, arg4 = _noArg, arg5 = _noArg, arg6 = _noArg, arg7 = _noArg, arg8 = _noArg, arg9 = _noArg]) #

Constructor for CallMatcher. name can be null to match anything, or a literal String, a predicate Function, or a Matcher. The various arguments can be scalar values or Matchers.

CallMatcher([name,
            arg0 = _noArg,
            arg1 = _noArg,
            arg2 = _noArg,
            arg3 = _noArg,
            arg4 = _noArg,
            arg5 = _noArg,
            arg6 = _noArg,
            arg7 = _noArg,
            arg8 = _noArg,
            arg9 = _noArg]) {
  if (name == null) {
    nameFilter = anything;
  } else {
    nameFilter = wrapMatcher(name);
  }
  argMatchers = new List<Matcher>();
  if (arg0 == _noArg) return;
  argMatchers.add(wrapMatcher(arg0));
  if (arg1 == _noArg) return;
  argMatchers.add(wrapMatcher(arg1));
  if (arg2 == _noArg) return;
  argMatchers.add(wrapMatcher(arg2));
  if (arg3 == _noArg) return;
  argMatchers.add(wrapMatcher(arg3));
  if (arg4 == _noArg) return;
  argMatchers.add(wrapMatcher(arg4));
  if (arg5 == _noArg) return;
  argMatchers.add(wrapMatcher(arg5));
  if (arg6 == _noArg) return;
  argMatchers.add(wrapMatcher(arg6));
  if (arg7 == _noArg) return;
  argMatchers.add(wrapMatcher(arg7));
  if (arg8 == _noArg) return;
  argMatchers.add(wrapMatcher(arg8));
  if (arg9 == _noArg) return;
  argMatchers.add(wrapMatcher(arg9));
}

Methods

Code bool matches(String method, List arguments) #

Given a method name and list of arguments, return true if it matches this [CallMatcher.

bool matches(String method, List arguments) {
  var matchState = new MatchState();
  if (!nameFilter.matches(method, matchState)) {
    return false;
  }
  if (arguments.length < argMatchers.length) {
    throw new Exception("Less arguments than matchers for $method.");
  }
  for (var i = 0; i < argMatchers.length; i++) {
    if (!argMatchers[i].matches(arguments[i], matchState)) {
      return false;
    }
  }
  return true;
}

Code String toString() #

We keep our behavior specifications in a Map, which is keyed by the CallMatcher. To make the keys unique and to get a descriptive value for the CallMatcher we have this override of toString().

String toString() {
  Description d = new StringDescription();
  d.addDescriptionOf(nameFilter);
  // If the nameFilter was a simple string - i.e. just a method name -
  // strip the quotes to make this more natural in appearance.
  if (d.toString()[0] == "'") {
    d.replace(d.toString().substring(1, d.toString().length - 1));
  }
  d.add('(');
  for (var i = 0; i < argMatchers.length; i++) {
    if (i > 0) d.add(', ');
    d.addDescriptionOf(argMatchers[i]);
  }
  d.add(')');
  return d.toString();
}

Fields

Code List<Matcher> argMatchers #

List<Matcher> argMatchers;

Code Matcher nameFilter #

Matcher nameFilter;