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

Side by Side Diff: mojo/public/dart/third_party/test/lib/src/frontend/throws_matcher.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 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
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 test.frontend.throws_matcher;
6
7 import 'dart:async';
8
9 import 'package:matcher/matcher.dart' hide fail, expect;
10
11 import '../backend/invoker.dart';
12 import '../utils.dart';
13 import 'expect.dart';
14
15 /// This can be used to match two kinds of objects:
16 ///
17 /// * A [Function] that throws an exception when called. The function cannot
18 /// take any arguments. If you want to test that a function expecting
19 /// arguments throws, wrap it in another zero-argument function that calls
20 /// the one you want to test.
21 ///
22 /// * A [Future] that completes with an exception. Note that this creates an
23 /// asynchronous expectation. The call to `expect()` that includes this will
24 /// return immediately and execution will continue. Later, when the future
25 /// completes, the actual expectation will run.
26 const Matcher throws = const Throws();
27
28 /// This can be used to match two kinds of objects:
29 ///
30 /// * A [Function] that throws an exception when called. The function cannot
31 /// take any arguments. If you want to test that a function expecting
32 /// arguments throws, wrap it in another zero-argument function that calls
33 /// the one you want to test.
34 ///
35 /// * A [Future] that completes with an exception. Note that this creates an
36 /// asynchronous expectation. The call to `expect()` that includes this will
37 /// return immediately and execution will continue. Later, when the future
38 /// completes, the actual expectation will run.
39 ///
40 /// In both cases, when an exception is thrown, this will test that the
41 /// exception object matches [matcher]. If [matcher] is not an instance of
42 /// [Matcher], it will implicitly be treated as `equals(matcher)`.
43 Matcher throwsA(matcher) => new Throws(wrapMatcher(matcher));
44
45 class Throws extends Matcher {
46 final Matcher _matcher;
47
48 const Throws([Matcher matcher]) : this._matcher = matcher;
49
50 bool matches(item, Map matchState) {
51 if (item is! Function && item is! Future) return false;
52 if (item is Future) {
53 Invoker.current.addOutstandingCallback();
54 // Queue up an asynchronous expectation that validates when the future
55 // completes.
56 item.then((value) {
57 fail("Expected future to fail, but succeeded with '$value'.");
58 }, onError: (error, trace) {
59 if (_matcher == null) return;
60
61 var reason;
62 if (trace != null) {
63 var stackTrace = terseChain(trace,
64 verbose: Invoker.current.metadata.verboseTrace);
65 stackTrace = " ${stackTrace.toString().replaceAll("\n", "\n ")}";
66 reason = "Actual exception trace:\n$stackTrace";
67 }
68
69 // Re-run [expect] to get the proper formatting.
70 expect(() => throw error, this, reason: reason);
71 }).then((_) => Invoker.current.removeOutstandingCallback());
72 // It hasn't failed yet.
73 return true;
74 }
75
76 try {
77 item();
78 return false;
79 } catch (e, s) {
80 if (_matcher == null || _matcher.matches(e, matchState)) {
81 return true;
82 } else {
83 addStateInfo(matchState, {'exception': e, 'stack': s});
84 return false;
85 }
86 }
87 }
88
89 Description describe(Description description) {
90 if (_matcher == null) {
91 return description.add("throws");
92 } else {
93 return description.add('throws ').addDescriptionOf(_matcher);
94 }
95 }
96
97 Description describeMismatch(
98 item, Description mismatchDescription, Map matchState, 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
105 .add('threw ')
106 .addDescriptionOf(matchState['exception']);
107 if (verbose) {
108 mismatchDescription.add(' at ').add(matchState['stack'].toString());
109 }
110 return mismatchDescription;
111 }
112 }
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698