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

Side by Side Diff: tests/corelib/src/FutureTest.dart

Issue 9392029: futures: add more tests for future and completer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: '' Created 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2012, the Dart project authors. Please see the AUTHORS file 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 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 // Tests for Future.immediate
6
5 testImmediate() { 7 testImmediate() {
6 final future = new Future<String>.immediate("42"); 8 final future = new Future<String>.immediate("42");
7 Expect.isTrue(future.isComplete); 9 Expect.isTrue(future.isComplete);
10 Expect.isTrue(future.hasValue);
8 var value = null; 11 var value = null;
9 future.then((x) => value = x); 12 future.then((x) => value = x);
10 Expect.equals("42", value); 13 Expect.equals("42", value);
11 } 14 }
12 15
16 // Tests for getters (value, exception, isComplete, isValue)
17
18 testNeverComplete() {
19 final completer = new Completer<int>();
20 final future = completer.future;
21 Expect.isFalse(future.isComplete);
22 Expect.isFalse(future.hasValue);
23 Expect.throws(() { future.value; });
24 Expect.throws(() { future.exception; });
25 }
26
27 testComplete() {
28 final completer = new Completer<int>();
29 final future = completer.future;
30
31 completer.complete(3);
32
33 Expect.isTrue(future.isComplete);
34 Expect.isTrue(future.hasValue);
35 Expect.equals(3, future.value);
36 Expect.isNull(future.exception);
37 }
38
39 // Tests for [then]
40
41 testCompleteWithHandlerBeforeComplete() {
42 final completer = new Completer<int>();
43 final future = completer.future;
44
45 int before;
46 future.then((int v) { before = v; });
47 Expect.throws(() { future.value; });
48 Expect.isNull(before);
49 completer.complete(3);
50
51 Expect.equals(3, future.value);
52 Expect.equals(3, before);
53 }
54
55 testCompleteWithHandlerAfterComplete() {
56 final completer = new Completer<int>();
57 final future = completer.future;
58
59 int after;
60 completer.complete(3);
61 Expect.equals(3, future.value);
62 Expect.isNull(after);
63
64 future.then((int v) { after = v; });
65
66 Expect.equals(3, future.value);
67 Expect.equals(3, after);
68 }
69
70 testCompleteManyHandlers() {
71 final completer = new Completer<int>();
72 final future = completer.future;
73 int after1;
74 int after2;
75 int after3;
76
77 future.then((int v) { after1 = v; });
78 completer.complete(3);
79 future.then((int v) { after2 = v; });
80 future.then((int v) { after3 = v; });
81
82 Expect.equals(3, future.value);
83 Expect.equals(3, after1);
84 Expect.equals(3, after2);
85 Expect.equals(3, after3);
86 }
87
88 // Tests for [handleException]
89
90 testException() {
91 final completer = new Completer<int>();
92 final future = completer.future;
93 final ex = new Exception();
94 future.then((_) {}); // exception is thrown if we plan to use the value
95 Expect.throws(
96 () { completer.completeException(ex); },
97 check: (e) => e == ex);
98 }
99
100 testExceptionNoListeners() {
101 final completer = new Completer<int>();
102 final future = completer.future;
103 final ex = new Exception();
104 completer.completeException(ex); // future.then is not called, so no exception
105 }
106
107 testExceptionHandler() {
108 final completer = new Completer<int>();
109 final future = completer.future;
110 final ex = new Exception();
111
112 var ex2;
113 future.handleException((e) { ex2 = e; return true; });
114 completer.completeException(ex);
115 Expect.equals(ex, ex2);
116 }
117
118 testExceptionHandlerReturnsTrue() {
119 final completer = new Completer<int>();
120 final future = completer.future;
121 final ex = new Exception();
122
123 bool reached = false;
124 future.handleException((e) { return true; });
125 future.handleException((e) { reached = true; return false; }); // overshadowed
126 completer.completeException(ex);
127 Expect.isFalse(reached);
128 }
129
130 testExceptionHandlerReturnsTrue2() {
131 final completer = new Completer<int>();
132 final future = completer.future;
133 final ex = new Exception();
134
135 bool reached = false;
136 future.handleException((e) { return false; });
137 future.handleException((e) { reached = true; return true; });
138 completer.completeException(ex);
139 Expect.isTrue(reached);
140 }
141
142 testExceptionHandlerReturnsFalse() {
143 final completer = new Completer<int>();
144 final future = completer.future;
145 final ex = new Exception();
146
147 bool reached = false;
148 future.then((_) {}); // ensure exception is thrown...
149 future.handleException((e) { return false; });
150 future.handleException((e) { reached = true; return false; }); // overshadowed
151 Expect.throws(
152 () { completer.completeException(ex); },
153 check: (e) => e == ex);
154 Expect.isTrue(reached);
155 }
156
157 testExceptionHandlerReturnsFalse2() {
158 final completer = new Completer<int>();
159 final future = completer.future;
160 final ex = new Exception();
161
162 bool reached = false;
163 future.handleException((e) { return false; });
164 future.handleException((e) { reached = true; return false; }); // overshadowed
165 completer.completeException(ex); // future.then is not called, so no exception
166 Expect.isTrue(reached);
167 }
168
169 // Tests for Future.transform
170
13 testTransformSuccess() { 171 testTransformSuccess() {
14 final completer = new Completer<String>(); 172 final completer = new Completer<String>();
15 final transformedFuture = completer.future.transform((x) => "** $x **"); 173 final transformedFuture = completer.future.transform((x) => "** $x **");
16 Expect.isFalse(transformedFuture.isComplete); 174 Expect.isFalse(transformedFuture.isComplete);
17 completer.complete("42"); 175 completer.complete("42");
18 Expect.equals("** 42 **", transformedFuture.value); 176 Expect.equals("** 42 **", transformedFuture.value);
19 } 177 }
20 178
21 testTransformFutureFails() { 179 testTransformFutureFails() {
22 final completer = new Completer<String>(); 180 final completer = new Completer<String>();
23 final error = new Exception("Oh no!"); 181 final error = new Exception("Oh no!");
24 final transformedFuture = completer.future.transform((x) { 182 final transformedFuture = completer.future.transform((x) {
25 Expect.fail("transformer shouldn't be called"); 183 Expect.fail("transformer shouldn't be called");
26 }); 184 });
27 Expect.isFalse(transformedFuture.isComplete); 185 Expect.isFalse(transformedFuture.isComplete);
28 completer.completeException(error); 186 completer.completeException(error);
29 Expect.equals(error, transformedFuture.exception); 187 Expect.equals(error, transformedFuture.exception);
30 } 188 }
31 189
32 testTransformTransformerFails() { 190 testTransformTransformerFails() {
33 final completer = new Completer<String>(); 191 final completer = new Completer<String>();
34 final error = new Exception("Oh no!"); 192 final error = new Exception("Oh no!");
35 final transformedFuture = completer.future.transform((x) { throw error; }); 193 final transformedFuture = completer.future.transform((x) { throw error; });
36 Expect.isFalse(transformedFuture.isComplete); 194 Expect.isFalse(transformedFuture.isComplete);
37 completer.complete("42"); 195 completer.complete("42");
38 Expect.equals(error, transformedFuture.exception); 196 Expect.equals(error, transformedFuture.exception);
39 } 197 }
40 198
199 // Tests for Future.chain
200
41 testChainSuccess() { 201 testChainSuccess() {
42 final completerA = new Completer<String>(); 202 final completerA = new Completer<String>();
43 final completerB = new Completer<String>(); 203 final completerB = new Completer<String>();
44 final chainedFuture = completerA.future.chain((x) { 204 final chainedFuture = completerA.future.chain((x) {
45 Expect.equals("42", x); 205 Expect.equals("42", x);
46 return completerB.future; 206 return completerB.future;
47 }); 207 });
48 Expect.isFalse(chainedFuture.isComplete); 208 Expect.isFalse(chainedFuture.isComplete);
49 completerA.complete("42"); 209 completerA.complete("42");
50 Expect.isFalse(chainedFuture.isComplete); 210 Expect.isFalse(chainedFuture.isComplete);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 }); 245 });
86 Expect.isFalse(chainedFuture.isComplete); 246 Expect.isFalse(chainedFuture.isComplete);
87 completerA.complete("42"); 247 completerA.complete("42");
88 Expect.isFalse(chainedFuture.isComplete); 248 Expect.isFalse(chainedFuture.isComplete);
89 completerB.completeException(error); 249 completerB.completeException(error);
90 Expect.equals(error, chainedFuture.exception); 250 Expect.equals(error, chainedFuture.exception);
91 } 251 }
92 252
93 main() { 253 main() {
94 testImmediate(); 254 testImmediate();
255 testNeverComplete();
256 testComplete();
257 testCompleteWithHandlerBeforeComplete();
258 testCompleteWithHandlerAfterComplete();
259 testCompleteManyHandlers();
260 testException();
261 testExceptionHandler();
262 testExceptionHandlerReturnsTrue();
263 testExceptionHandlerReturnsTrue2();
264 testExceptionHandlerReturnsFalse();
265 testExceptionHandlerReturnsFalse2();
95 testTransformSuccess(); 266 testTransformSuccess();
96 testTransformFutureFails(); 267 testTransformFutureFails();
97 testTransformTransformerFails(); 268 testTransformTransformerFails();
98 testChainSuccess(); 269 testChainSuccess();
99 testChainFirstFutureFails(); 270 testChainFirstFutureFails();
100 testChainTransformerFails(); 271 testChainTransformerFails();
101 testChainSecondFutureFails(); 272 testChainSecondFutureFails();
102 } 273 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698