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

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

Issue 934413002: Replace the existing unittest APIs with the new runner infrastructure. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 10 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
« no previous file with comments | « lib/src/test_environment.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library unittest.throws_matcher; 5 library unittest.throws_matcher;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:matcher/matcher.dart' hide fail, expect; 9 import 'package:matcher/matcher.dart' hide fail, expect;
10 10
11 import '../unittest.dart'; 11 import 'expect.dart';
12 import 'invoker.dart';
13 import 'utils.dart';
12 14
13 /// This can be used to match two kinds of objects: 15 /// This can be used to match two kinds of objects:
14 /// 16 ///
15 /// * A [Function] that throws an exception when called. The function cannot 17 /// * A [Function] that throws an exception when called. The function cannot
16 /// take any arguments. If you want to test that a function expecting 18 /// take any arguments. If you want to test that a function expecting
17 /// arguments throws, wrap it in another zero-argument function that calls 19 /// arguments throws, wrap it in another zero-argument function that calls
18 /// the one you want to test. 20 /// the one you want to test.
19 /// 21 ///
20 /// * A [Future] that completes with an exception. Note that this creates an 22 /// * A [Future] that completes with an exception. Note that this creates an
21 /// asynchronous expectation. The call to `expect()` that includes this will 23 /// asynchronous expectation. The call to `expect()` that includes this will
(...skipping 19 matching lines...) Expand all
41 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher)); 43 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher));
42 44
43 class Throws extends Matcher { 45 class Throws extends Matcher {
44 final Matcher _matcher; 46 final Matcher _matcher;
45 47
46 const Throws([Matcher matcher]) : this._matcher = matcher; 48 const Throws([Matcher matcher]) : this._matcher = matcher;
47 49
48 bool matches(item, Map matchState) { 50 bool matches(item, Map matchState) {
49 if (item is! Function && item is! Future) return false; 51 if (item is! Function && item is! Future) return false;
50 if (item is Future) { 52 if (item is Future) {
51 var done = expectAsync((fn) => fn()); 53 Invoker.current.addOutstandingCallback();
52
53 // Queue up an asynchronous expectation that validates when the future 54 // Queue up an asynchronous expectation that validates when the future
54 // completes. 55 // completes.
55 item.then((value) { 56 item.then((value) {
56 done(() { 57 fail("Expected future to fail, but succeeded with '$value'.");
57 fail("Expected future to fail, but succeeded with '$value'.");
58 });
59 }, onError: (error, trace) { 58 }, onError: (error, trace) {
60 done(() { 59 if (_matcher == null) return;
61 if (_matcher == null) return; 60
62 var reason; 61 var reason;
63 if (trace != null) { 62 if (trace != null) {
64 var stackTrace = trace.toString(); 63 var stackTrace = terseChain(trace).toString();
65 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}"; 64 stackTrace = " ${stackTrace.replaceAll("\n", "\n ")}";
66 reason = "Actual exception trace:\n$stackTrace"; 65 reason = "Actual exception trace:\n$stackTrace";
67 } 66 }
68 expect(error, _matcher, reason: reason); 67
69 }); 68 // Re-run [expect] to get the proper formatting.
70 }); 69 expect(() => throw error, this, reason: reason);
70 }).then((_) => Invoker.current.removeOutstandingCallback());
71 // It hasn't failed yet. 71 // It hasn't failed yet.
72 return true; 72 return true;
73 } 73 }
74 74
75 try { 75 try {
76 item(); 76 item();
77 return false; 77 return false;
78 } catch (e, s) { 78 } catch (e, s) {
79 if (_matcher == null || _matcher.matches(e, matchState)) { 79 if (_matcher == null || _matcher.matches(e, matchState)) {
80 return true; 80 return true;
(...skipping 22 matching lines...) Expand all
103 mismatchDescription 103 mismatchDescription
104 .add('threw ') 104 .add('threw ')
105 .addDescriptionOf(matchState['exception']); 105 .addDescriptionOf(matchState['exception']);
106 if (verbose) { 106 if (verbose) {
107 mismatchDescription.add(' at ').add(matchState['stack'].toString()); 107 mismatchDescription.add(' at ').add(matchState['stack'].toString());
108 } 108 }
109 return mismatchDescription; 109 return mismatchDescription;
110 } 110 }
111 } 111 }
112 } 112 }
OLDNEW
« no previous file with comments | « lib/src/test_environment.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698