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

Side by Side Diff: mojo/public/dart/third_party/test/lib/src/frontend/expect.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) 2015, 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.expect;
6
7 import 'package:matcher/matcher.dart';
8
9 import '../backend/closed_exception.dart';
10 import '../backend/invoker.dart';
11
12 /// An exception thrown when a test assertion fails.
13 class TestFailure {
14 final String message;
15
16 TestFailure(this.message);
17
18 String toString() => message;
19 }
20
21 /// The type used for functions that can be used to build up error reports
22 /// upon failures in [expect].
23 typedef String ErrorFormatter(
24 actual, Matcher matcher, String reason, Map matchState, bool verbose);
25
26 /// Assert that [actual] matches [matcher].
27 ///
28 /// This is the main assertion function. [reason] is optional and is typically
29 /// not supplied, as a reason is generated from [matcher]; if [reason]
30 /// is included it is appended to the reason generated by the matcher.
31 ///
32 /// [matcher] can be a value in which case it will be wrapped in an
33 /// [equals] matcher.
34 ///
35 /// If the assertion fails a [TestFailure] is thrown.
36 ///
37 /// In some cases extra diagnostic info can be produced on failure (for
38 /// example, stack traces on mismatched exceptions). To enable these,
39 /// [verbose] should be specified as `true`.
40 void expect(actual, matcher,
41 {String reason, bool verbose: false, ErrorFormatter formatter}) {
42 if (Invoker.current == null) {
43 throw new StateError("expect() may only be called within a test.");
44 }
45
46 if (Invoker.current.closed) throw new ClosedException();
47
48 matcher = wrapMatcher(matcher);
49 var matchState = {};
50 try {
51 if (matcher.matches(actual, matchState)) return;
52 } catch (e, trace) {
53 if (reason == null) {
54 reason = '${(e is String) ? e : e.toString()} at $trace';
55 }
56 }
57 if (formatter == null) formatter = _defaultFailFormatter;
58 fail(formatter(actual, matcher, reason, matchState, verbose));
59 }
60
61 /// Convenience method for throwing a new [TestFailure] with the provided
62 /// [message].
63 void fail(String message) => throw new TestFailure(message);
64
65 // The default error formatter.
66 String _defaultFailFormatter(
67 actual, Matcher matcher, String reason, Map matchState, bool verbose) {
68 var description = new StringDescription();
69 description.add('Expected: ').addDescriptionOf(matcher).add('\n');
70 description.add(' Actual: ').addDescriptionOf(actual).add('\n');
71
72 var mismatchDescription = new StringDescription();
73 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose);
74
75 if (mismatchDescription.length > 0) {
76 description.add(' Which: ${mismatchDescription}\n');
77 }
78 if (reason != null) description.add(reason).add('\n');
79 return description.toString();
80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698