Dart API ReferenceunittestThrows

Throws class

class Throws extends BaseMatcher {
  final Matcher _matcher;

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

  bool matches(item, MatchState matchState) {
    if (item is Future) {
      // Queue up an asynchronous expectation that validates when the future
      // completes.
      item.onComplete(expectAsync1((future) {
        if (future.hasValue) {
          expect(false, isTrue,
              "Expected future to fail, but succeeded with '${future.value}'.");
        } else if (_matcher != null) {
          var reason;
          if (future.stackTrace != null) {
            var stackTrace = future.stackTrace.toString();
            stackTrace = "  ${stackTrace.replaceAll("\n", "\n  ")}";
            reason = "Actual exception trace:\n$stackTrace";
          }
          expect(future.exception, _matcher, 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 (_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 (_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 Future) {
    // Queue up an asynchronous expectation that validates when the future
    // completes.
    item.onComplete(expectAsync1((future) {
      if (future.hasValue) {
        expect(false, isTrue,
            "Expected future to fail, but succeeded with '${future.value}'.");
      } else if (_matcher != null) {
        var reason;
        if (future.stackTrace != null) {
          var stackTrace = future.stackTrace.toString();
          stackTrace = "  ${stackTrace.replaceAll("\n", "\n  ")}";
          reason = "Actual exception trace:\n$stackTrace";
        }
        expect(future.exception, _matcher, 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;
    }
  }
}