Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: pkg/matcher/lib/src/throws_matcher.dart

Issue 438473002: pkg/matcher: refactored code around data-structure vs execution-based matchers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/matcher/lib/src/operator_matchers.dart ('k') | pkg/matcher/lib/src/throws_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library matcher.throws_matcher;
6
7 import 'dart:async';
8
9 import 'expect.dart';
10 import 'interfaces.dart';
11 import 'util.dart';
12
13 /// This can be used to match two kinds of objects:
14 ///
15 /// * A [Function] that throws an exception when called. The function cannot
16 /// take any arguments. If you want to test that a function expecting
17 /// arguments throws, wrap it in another zero-argument function that calls
18 /// the one you want to test.
19 ///
20 /// * A [Future] that completes with an exception. Note that this creates an
21 /// asynchronous expectation. The call to `expect()` that includes this will
22 /// return immediately and execution will continue. Later, when the future
23 /// completes, the actual expectation will run.
24 const Matcher throws = const Throws();
25
26 /// This can be used to match two kinds of objects:
27 ///
28 /// * A [Function] that throws an exception when called. The function cannot
29 /// take any arguments. If you want to test that a function expecting
30 /// arguments throws, wrap it in another zero-argument function that calls
31 /// the one you want to test.
32 ///
33 /// * A [Future] that completes with an exception. Note that this creates an
34 /// asynchronous expectation. The call to `expect()` that includes this will
35 /// return immediately and execution will continue. Later, when the future
36 /// completes, the actual expectation will run.
37 ///
38 /// In both cases, when an exception is thrown, this will test that the exceptio n
39 /// object matches [matcher]. If [matcher] is not an instance of [Matcher], it
40 /// will implicitly be treated as `equals(matcher)`.
41 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher));
42
43 class Throws extends Matcher {
44 final Matcher _matcher;
45
46 const Throws([Matcher matcher]) : this._matcher = matcher;
47
48 bool matches(item, Map matchState) {
49 if (item is! Function && item is! Future) return false;
50 if (item is Future) {
51 var done = wrapAsync((fn) => fn());
52
53 // Queue up an asynchronous expectation that validates when the future
54 // completes.
55 item.then((value) {
56 done(() {
57 fail("Expected future to fail, but succeeded with '$value'.");
58 });
59 }, onError: (error, trace) {
60 done(() {
61 if (_matcher == null) return;
62 var reason;
63 if (trace != null) {
64 var stackTrace = trace.toString();
65 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}";
66 reason = "Actual exception trace:\n$stackTrace";
67 }
68 expect(error, _matcher, reason: reason);
69 });
70 });
71 // It hasn't failed yet.
72 return true;
73 }
74
75 try {
76 item();
77 return false;
78 } catch (e, s) {
79 if (_matcher == null || _matcher.matches(e, matchState)) {
80 return true;
81 } else {
82 addStateInfo(matchState, {'exception': e, 'stack': s});
83 return false;
84 }
85 }
86 }
87
88 Description describe(Description description) {
89 if (_matcher == null) {
90 return description.add("throws");
91 } else {
92 return description.add('throws ').addDescriptionOf(_matcher);
93 }
94 }
95
96 Description describeMismatch(item, Description mismatchDescription,
97 Map matchState,
98 bool verbose) {
99 if (item is! Function && item is! Future) {
100 return mismatchDescription.add('is not a Function or Future');
101 } else if (_matcher == null || matchState['exception'] == null) {
102 return mismatchDescription.add('did not throw');
103 } else {
104 mismatchDescription. add('threw ').
105 addDescriptionOf(matchState['exception']);
106 if (verbose) {
107 mismatchDescription.add(' at ').add(matchState['stack'].toString());
108 }
109 return mismatchDescription;
110 }
111 }
112 }
113
OLDNEW
« no previous file with comments | « pkg/matcher/lib/src/operator_matchers.dart ('k') | pkg/matcher/lib/src/throws_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698