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

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

Issue 869053002: unittest: refactor ahead of matcher changes (Closed) Base URL: https://github.com/dart-lang/unittest.git@master
Patch Set: no longer requires matcher changes Created 5 years, 11 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) 2012, the Dart project authors. Please see the AUTHORS file
nweiz 2015/01/26 22:44:09 Use the current year for copyright headers, even f
kevmoo 2015/01/27 00:11:29 Done.
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 unittest.expect;
6
7 import 'package:matcher/matcher.dart';
8
9 /// The objects thrown by the default failure handler.
nweiz 2015/01/26 22:44:09 You removed any concept of a "failure handler". I
kevmoo 2015/01/27 00:11:30 Done.
10 class TestFailure extends Error {
nweiz 2015/01/26 22:44:09 I don't think this should extend Error. It's not a
kevmoo 2015/01/27 00:11:29 Done.
11 final String message;
12
13 TestFailure(this.message);
14
15 String toString() => message;
16 }
17
18 /// The used for functions that can be used to build up error reports upon
19 /// failures in [expect].
nweiz 2015/01/26 22:44:09 "The used" -> "The type used" It seems very stran
kevmoo 2015/01/27 00:11:29 Done.
20 typedef String ErrorFormatter(
21 actual, Matcher matcher, String reason, Map matchState, bool verbose);
22
23 /// Assert that [actual] matches [matcher].
24 ///
25 /// This is the main assertion function. [reason] is optional and is typically
26 /// not supplied, as a reason is generated from the matcher; if [reason]
nweiz 2015/01/26 22:44:09 "the matcher" -> "[matcher]"
kevmoo 2015/01/27 00:11:29 Done.
27 /// is included it is appended to the reason generated by the matcher.
28 ///
29 /// [matcher] can be a value in which case it will be wrapped in an
30 /// [equals] matcher.
31 ///
32 /// If the assertion fails a [TestFailure] is thrown.
33 ///
34 /// In some cases extra diagnostic info can be produced on failure (for
35 /// example, stack traces on mismatched exceptions). To enable these,
36 /// [verbose] should be specified as true;
nweiz 2015/01/26 22:44:09 "specified as true;" -> "`true`.".
kevmoo 2015/01/27 00:11:29 Done.
37 void expect(actual, matcher,
38 {String reason, bool verbose: false, ErrorFormatter formatter}) {
39 matcher = wrapMatcher(matcher);
40 var matchState = {};
41 try {
42 if (matcher.matches(actual, matchState)) return;
43 } catch (e, trace) {
44 if (reason == null) {
45 reason = '${(e is String) ? e : e.toString()} at $trace';
46 }
47 }
48 if (formatter == null) formatter = _defaultFailFormatter;
49 fail(formatter(actual, matcher, reason, matchState, verbose));
50 }
51
52 /// Convenience method for throwing a new [TestFailure] with the provided
53 /// [message].
54 void fail(String message) => throw new TestFailure(message);
55
56 // The default error formatter implementation.
nweiz 2015/01/26 22:44:09 "formatter implementation." -> "formatter."
kevmoo 2015/01/27 00:11:29 Done.
57 String _defaultFailFormatter(
58 actual, Matcher matcher, String reason, Map matchState, bool verbose) {
59 var description = new StringDescription();
60 description.add('Expected: ').addDescriptionOf(matcher).add('\n');
61 description.add(' Actual: ').addDescriptionOf(actual).add('\n');
62
63 var mismatchDescription = new StringDescription();
64 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose);
65
66 if (mismatchDescription.length > 0) {
67 description.add(' Which: ${mismatchDescription}\n');
68 }
69 if (reason != null) description.add(reason).add('\n');
70 return description.toString();
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698