Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Things that should be "auto-generated" are between AUTO START and | 5 // Things that should be "auto-generated" are between AUTO START and |
| 6 // AUTO END (or just AUTO if it's a single line). | 6 // AUTO END (or just AUTO if it's a single line). |
| 7 | 7 |
| 8 #library("MintMakerTest"); | 8 #library("MintMakerTest"); |
| 9 #import("dart:isolate"); | 9 #import("dart:isolate"); |
| 10 | 10 #import('../../../lib/unittest/unittest.dart'); |
| 11 | 11 |
| 12 class Mint { | 12 class Mint { |
| 13 Mint() : registry_ = new Map<SendPort, Purse>() { | 13 Mint() : registry_ = new Map<SendPort, Purse>() { |
| 14 // AUTO START | 14 // AUTO START |
| 15 ReceivePort mintPort = new ReceivePort(); | 15 ReceivePort mintPort = new ReceivePort(); |
| 16 port = mintPort.toSendPort(); | 16 port = mintPort.toSendPort(); |
| 17 serveMint(mintPort); | 17 serveMint(mintPort); |
| 18 // AUTO END | 18 // AUTO END |
| 19 } | 19 } |
| 20 | 20 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 port.call(null).then((var message) { | 182 port.call(null).then((var message) { |
| 183 SendPort mint = message[0]; | 183 SendPort mint = message[0]; |
| 184 handleMint(new MintWrapper(mint)); | 184 handleMint(new MintWrapper(mint)); |
| 185 }); | 185 }); |
| 186 }); | 186 }); |
| 187 } | 187 } |
| 188 | 188 |
| 189 Future<SendPort> port_; | 189 Future<SendPort> port_; |
| 190 } | 190 } |
| 191 | 191 |
| 192 _checkBalance(PurseWrapper wrapper, expected) { | |
| 193 wrapper.queryBalance(later1((int balance) { | |
| 194 Expect.equals(expected, balance); | |
|
Bob Nystrom
2012/04/20 22:31:36
expect(balance).equals(expected);
?
Siggi Cherem (dart-lang)
2012/04/21 00:03:43
Done.
| |
| 195 })); | |
| 196 } | |
| 192 | 197 |
| 193 class MintMakerTest { | 198 main() { |
| 194 static void testMain() { | 199 test("creating purse, deposit, and query balance", () { |
| 195 MintMakerWrapper mintMaker = new MintMakerWrapper(); | 200 MintMakerWrapper mintMaker = new MintMakerWrapper(); |
| 196 mintMaker.makeMint((MintWrapper mint) { | 201 mintMaker.makeMint(later1((MintWrapper mint) { |
| 197 mint.createPurse(100, (PurseWrapper purse) { | 202 mint.createPurse(100, later1((PurseWrapper purse) { |
| 198 purse.queryBalance((int balance) { | 203 _checkBalance(purse, 100); |
| 199 Expect.equals(100, balance); | 204 purse.sproutPurse(later1((PurseWrapper sprouted) { |
| 200 }); | 205 _checkBalance(sprouted, 0); |
| 201 purse.sproutPurse((PurseWrapper sprouted) { | 206 _checkBalance(purse, 100); |
| 202 sprouted.queryBalance((int balance) { | 207 |
| 203 Expect.equals(0, balance); | |
| 204 }); | |
| 205 sprouted.deposit(purse, 5); | 208 sprouted.deposit(purse, 5); |
| 206 sprouted.queryBalance((int balance) { | 209 _checkBalance(sprouted, 0 + 5); |
| 207 Expect.equals(0 + 5, balance); | 210 _checkBalance(purse, 100 - 5); |
| 208 }); | 211 |
| 209 purse.queryBalance((int balance) { | |
| 210 Expect.equals(100 - 5, balance); | |
| 211 }); | |
| 212 sprouted.deposit(purse, 42); | 212 sprouted.deposit(purse, 42); |
| 213 sprouted.queryBalance((int balance) { | 213 _checkBalance(sprouted, 0 + 5 + 42); |
| 214 Expect.equals(0 + 5 + 42, balance); | 214 _checkBalance(purse, 100 - 5 - 42); |
| 215 }); | 215 })); |
| 216 purse.queryBalance((int balance) { | 216 })); |
| 217 Expect.equals(100 - 5 - 42, balance); | 217 })); |
| 218 }); | 218 }); |
| 219 }); | |
| 220 }); | |
| 221 }); | |
| 222 } | |
| 223 | 219 |
| 224 /* This is an attempt to show how the above code could look like if we had | 220 /* This is an attempt to show how the above code could look like if we had |
| 225 * better language support for asynchronous messages (deferred/asynccall). | 221 * better language support for asynchronous messages (deferred/asynccall). |
| 226 * The static helper methods like createPurse and queryBalance would also | 222 * The static helper methods like createPurse and queryBalance would also |
| 227 * have to be marked async. | 223 * have to be marked async. |
| 228 | 224 |
| 229 void run(port) { | 225 void run(port) { |
| 230 MintMakerWrapper mintMaker = spawnMintMaker(); | 226 MintMakerWrapper mintMaker = spawnMintMaker(); |
| 231 deferred { | 227 deferred { |
| 232 MintWrapper mint = asynccall mintMaker.createMint(); | 228 MintWrapper mint = asynccall mintMaker.createMint(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 260 | 256 |
| 261 sprouted...deposit(purse, 5); | 257 sprouted...deposit(purse, 5); |
| 262 Expect.equals(0 + 5, sprouted.queryBalance()); | 258 Expect.equals(0 + 5, sprouted.queryBalance()); |
| 263 Expect.equals(100 - 5, purse.queryBalance()); | 259 Expect.equals(100 - 5, purse.queryBalance()); |
| 264 | 260 |
| 265 sprouted...deposit(purse, 42); | 261 sprouted...deposit(purse, 42); |
| 266 Expect.equals(0 + 5 + 42, sprouted.queryBalance()); | 262 Expect.equals(0 + 5 + 42, sprouted.queryBalance()); |
| 267 Expect.equals(100 - 5 - 42, purse.queryBalance()); | 263 Expect.equals(100 - 5 - 42, purse.queryBalance()); |
| 268 } | 264 } |
| 269 */ | 265 */ |
| 270 | |
| 271 } | 266 } |
| 272 | |
| 273 main() { | |
| 274 MintMakerTest.testMain(); | |
| 275 } | |
| OLD | NEW |