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.
class CallMatcher { Matcher nameFilter; List<Matcher> argMatchers; /** * 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 * [Matcher]s. */ 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)); } /** * 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(); } /** * 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; } var numArgs = (arguments == null) ? 0 : arguments.length; if (numArgs < 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; } }
Constructors
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)); }
Properties
Methods
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; } var numArgs = (arguments == null) ? 0 : arguments.length; if (numArgs < 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; }
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(); }