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

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

Issue 10441104: New expectation functions plus convert old tests to use these. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 /**
6 * This is the main assertion function, and can be used in two ways:
7 *
8 * expect(value, matcher, reason)
Bob Nystrom 2012/05/30 23:23:51 Do five spaces here and elsewhere. The first one i
9 *
10 * or:
11 *
12 * expect(BooleanConditon, reason)
13 *
14 * The reason is optional and in the first case typically not
15 * supplied, as a reason can be generated from the matcher.
16 *
17 * In the first form expect() asserts that the value [arg1] satisfies
18 * the matcher [arg2] for the reason [arg3]. In the second form expect()
Bob Nystrom 2012/05/30 23:23:51 Do we use the second form anywhere? If not, we cou
gram 2012/06/01 17:33:15 Done.
19 * asserts that the Boolean condition [arg1] is true for reason [arg2].
20 *
21 * If the assertion fails, then the default behavior is to throw an
22 * ExpectException, but this behavior can be changed by calling
Bob Nystrom 2012/05/30 23:23:51 Put square brackets around ExpectException, config
gram 2012/06/01 17:33:15 Done.
23 * configureExpectHandler and providing an alternative handler that
24 * implements the IFailureHandler interface.
25 *
26 * expect is a 3rd generation assertion mechanism, drawing
Bob Nystrom 2012/05/30 23:23:51 `expect()`
gram 2012/06/01 17:33:15 Done.
27 * inspiration from Hamcrest and Ladislav Thon's matcher
Bob Nystrom 2012/05/30 23:23:51 Make these markdown links: [Hamcrest][] and Ladis
gram 2012/06/01 17:33:15 Done.
Bob Nystrom 2012/06/01 18:22:22 Almost done. :) The markdown syntax is very specif
28 * library.
29 *
30 * See http://en.wikipedia.org/wiki/Hamcrest
31 * http://http://code.google.com/p/hamcrest/
32 * https://github.com/Ladicek/dart-matchers
33 */
34
35 void expect(arg1, [arg2=null, String arg3='']) {
36 if (arg2 is IMatcher) {
37 _assertMatch(arg1, arg2, arg3);
38 } else {
39 _assertBool(arg1, arg2);
40 }
41 }
42
43 // _assertMatch handles expect() calls that use a matcher.
Bob Nystrom 2012/05/30 23:23:51 This comment is probably not necessary.
gram 2012/06/01 17:33:15 Done.
44 // It just checks if the matcher matches, and if not, builds up
45 // an error message that then calls the failure handler.
46 void _assertMatch(actual, IMatcher matcher, String reason) {
47 if (!matcher.matches(actual)) {
48 // Make sure we have a failure handler and formatter configured
49 configureExpectHandler(_assertFailureHandler);
50 configureExpectFormatter(_assertErrorFormatter);
51 _assertFailureHandler.failMatch(actual, matcher, reason);
52 }
53 }
54
55 // assertBool handles expect() calls that use a Boolean condition.
56 void _assertBool(assertion, [String reason=null]) {
Bob Nystrom 2012/05/30 23:23:51 Type annotate assertion.
gram 2012/06/01 17:33:15 Done.
57 if (!assertion) {
58 if (reason == null) {
59 reason = 'Assertion failed';
60 }
61 // Make sure we have a failure handler configured
62 configureExpectHandler(_assertFailureHandler);
63 _assertFailureHandler.fail(reason);
64 }
65 }
66
67 // wrapMatcher takes an argument and returns a equivalent matcher.
Bob Nystrom 2012/05/30 23:23:51 "Takes an argument and returns an equivalent match
gram 2012/06/01 17:33:15 Done.
68 // If the argument is already a matcher this does nothing, else it
69 // generates a equals matcher for the argument.
Bob Nystrom 2012/05/30 23:23:51 "a equals" -> "an equals"
gram 2012/06/01 17:33:15 Done.
70 IMatcher wrapMatcher(x) => ((x is IMatcher) ? x : equals(x));
Bob Nystrom 2012/05/30 23:23:51 What is this used for?
gram 2012/06/01 17:33:15 It allows matchers that themselves expect matcher
71
72 //-----------------------------------------------------------
73 // The reference to the failure handler for failed asserts
Bob Nystrom 2012/05/30 23:23:51 This comment doesn't add much.
gram 2012/06/01 17:33:15 Done.
74 IFailureHandler _assertFailureHandler = null;
75
76 // The default failure handler that throws ExpectExceptions
77 class DefaultFailureHandler implements IFailureHandler {
78 void fail(String reason) {
79 throw new ExpectException(reason);
80 }
81 void failMatch(actual, IMatcher matcher, String reason) {
82 fail(_assertErrorFormatter.format(actual, matcher, reason));
83 }
84 }
85
86 /**
87 * Changes or resets to the default the failure handler for expect()
88 * [handler] is a reference to the new handler (that must implement
89 * IFailureHandler); if this is omitted or null then the failure handler
Bob Nystrom 2012/05/30 23:23:51 Remove "(that must implement IFailureHandler)" Tha
gram 2012/06/01 17:33:15 Done.
90 * is reset to the default, which throws ExpectExceptions on expect()
Bob Nystrom 2012/05/30 23:23:51 Square brackets around [ExpectException]s and [exp
gram 2012/06/01 17:33:15 Done.
91 * assertion failures.
92 */
93 void configureExpectHandler([IFailureHandler handler = null]) {
Bob Nystrom 2012/05/30 23:23:51 Why make this optional?
gram 2012/06/01 17:33:15 For the reset to default case.
94 if (handler == null) {
95 handler = new DefaultFailureHandler();
96 }
97 _assertFailureHandler = handler;
98 }
99
100 //------------------------------------------------------------------
101 // The reference to the error message formatter for failed asserts
102 IErrorFormatter _assertErrorFormatter = null;
Bob Nystrom 2012/05/30 23:23:51 This comment doesn't add much.
gram 2012/06/01 17:33:15 Done.
103
104 // The default error formatter implementation
105 class DefaultErrorFormatter implements IErrorFormatter {
106 String format(actual, IMatcher matcher, String reason) {
107 var description = new StringDescription();
108 description.append(reason).
109 append('\nExpected: ').
Bob Nystrom 2012/05/30 23:23:51 Indent these +4.
gram 2012/06/01 17:33:15 Done.
110 appendDescriptionOf(matcher).
111 append('\n but: ');
112 matcher.describeMismatch(actual, description);
113 description.append('\n');
114 return description.toString();
115 }
116 }
117
118 /**
119 * Changes or resets to default the failure message formatter for expect().
120 * [formatter] is a reference to the new formatter (that must implement
Bob Nystrom 2012/05/30 23:23:51 Remove parenthetical.
gram 2012/06/01 17:33:15 Done.
121 * IFailureFormatter); if this is omitted or null then the failure formatter
122 * is reset to the default.
123 */
124 void configureExpectFormatter([IErrorFormatter formatter = null]) {
125 if (formatter == null) {
126 formatter = new DefaultErrorFormatter();
127 }
128 _assertErrorFormatter = formatter;
129 }
130
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698