Dart DocumentationmatcherThrows

Throws class

class Throws extends BaseMatcher {
 final Matcher _matcher;

 const Throws([Matcher matcher]) :
   this._matcher = matcher;

 bool matches(item, MatchState matchState) {
   if (item is! Function && item is! Future) return false;
   if (item is Future) {
     var done = wrapAsync((fn) => fn());

     // Queue up an asynchronous expectation that validates when the future
     // completes.
     item.then((value) {
       done(() => expect(false, isTrue, reason:
           "Expected future to fail, but succeeded with '$value'."));
     }, onError: (e) {
       done(() {
         if (_matcher == null) return;
         var reason;
         if (e.stackTrace != null) {
           var stackTrace = e.stackTrace.toString();
           stackTrace = "  ${stackTrace.replaceAll("\n", "\n  ")}";
           reason = "Actual exception trace:\n$stackTrace";
         }
         expect(e.error, _matcher, reason: reason);
       });
     });
     // It hasn't failed yet.
     return true;
   }

   try {
     item();
     return false;
   } catch (e, s) {
     if (_matcher == null ||_matcher.matches(e, matchState)) {
       return true;
     } else {
       matchState.state = {
           'exception' :e,
           'stack': s
       };
       return false;
     }
   }
 }

 Description describe(Description description) {
   if (_matcher == null) {
     return description.add("throws an exception");
   } else {
     return description.add('throws an exception which matches ').
         addDescriptionOf(_matcher);
   }
 }

 Description describeMismatch(item, Description mismatchDescription,
                              MatchState matchState,
                              bool verbose) {
   if (item is! Function && item is! Future) {
     return mismatchDescription.add(' not a Function or Future');
   } else if (_matcher == null ||  matchState.state == null) {
     return mismatchDescription.add(' no exception');
   } else {
     mismatchDescription.
         add(' exception ').addDescriptionOf(matchState.state['exception']);
     if (verbose) {
         mismatchDescription.add(' at ').
         add(matchState.state['stack'].toString());
     }
      mismatchDescription.add(' does not match ').addDescriptionOf(_matcher);
      return mismatchDescription;
   }
 }
}

Extends

BaseMatcher > Throws

Constructors

const Throws([Matcher matcher]) #

const Throws([Matcher matcher]) :
 this._matcher = matcher;

Methods

Description describe(Description description) #

Creates a textual description of a matcher, by appending to mismatchDescription.

docs inherited from BaseMatcher
Description describe(Description description) {
 if (_matcher == null) {
   return description.add("throws an exception");
 } else {
   return description.add('throws an exception which matches ').
       addDescriptionOf(_matcher);
 }
}

Description describeMismatch(item, Description mismatchDescription, MatchState matchState, bool verbose) #

Generates a description of the matcher failed for a particular item, by appending the description to mismatchDescription. It does not check whether the item fails the match, as it is only called after a failed match. There may be additional info about the mismatch in matchState.

docs inherited from BaseMatcher
Description describeMismatch(item, Description mismatchDescription,
                            MatchState matchState,
                            bool verbose) {
 if (item is! Function && item is! Future) {
   return mismatchDescription.add(' not a Function or Future');
 } else if (_matcher == null ||  matchState.state == null) {
   return mismatchDescription.add(' no exception');
 } else {
   mismatchDescription.
       add(' exception ').addDescriptionOf(matchState.state['exception']);
   if (verbose) {
       mismatchDescription.add(' at ').
       add(matchState.state['stack'].toString());
   }
    mismatchDescription.add(' does not match ').addDescriptionOf(_matcher);
    return mismatchDescription;
 }
}

bool matches(item, MatchState matchState) #

Tests the matcher against a given item and return true if the match succeeds; false otherwise. matchState may be used to return additional info for the use of describeMismatch.

docs inherited from BaseMatcher
bool matches(item, MatchState matchState) {
 if (item is! Function && item is! Future) return false;
 if (item is Future) {
   var done = wrapAsync((fn) => fn());

   // Queue up an asynchronous expectation that validates when the future
   // completes.
   item.then((value) {
     done(() => expect(false, isTrue, reason:
         "Expected future to fail, but succeeded with '$value'."));
   }, onError: (e) {
     done(() {
       if (_matcher == null) return;
       var reason;
       if (e.stackTrace != null) {
         var stackTrace = e.stackTrace.toString();
         stackTrace = "  ${stackTrace.replaceAll("\n", "\n  ")}";
         reason = "Actual exception trace:\n$stackTrace";
       }
       expect(e.error, _matcher, reason: reason);
     });
   });
   // It hasn't failed yet.
   return true;
 }

 try {
   item();
   return false;
 } catch (e, s) {
   if (_matcher == null ||_matcher.matches(e, matchState)) {
     return true;
   } else {
     matchState.state = {
         'exception' :e,
         'stack': s
     };
     return false;
   }
 }
}