| 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 #import('../../../lib/unittest/unittest.dart'); | 10 |
| 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(expectAsync1((int balance) { | |
| 194 expect(balance).equals(expected); | |
| 195 })); | |
| 196 } | |
| 197 | 192 |
| 198 main() { | 193 class MintMakerTest { |
| 199 test("creating purse, deposit, and query balance", () { | 194 static void testMain() { |
| 200 MintMakerWrapper mintMaker = new MintMakerWrapper(); | 195 MintMakerWrapper mintMaker = new MintMakerWrapper(); |
| 201 mintMaker.makeMint(expectAsync1((MintWrapper mint) { | 196 mintMaker.makeMint((MintWrapper mint) { |
| 202 mint.createPurse(100, expectAsync1((PurseWrapper purse) { | 197 mint.createPurse(100, (PurseWrapper purse) { |
| 203 _checkBalance(purse, 100); | 198 purse.queryBalance((int balance) { |
| 204 purse.sproutPurse(expectAsync1((PurseWrapper sprouted) { | 199 Expect.equals(100, balance); |
| 205 _checkBalance(sprouted, 0); | 200 }); |
| 206 _checkBalance(purse, 100); | 201 purse.sproutPurse((PurseWrapper sprouted) { |
| 207 | 202 sprouted.queryBalance((int balance) { |
| 203 Expect.equals(0, balance); |
| 204 }); |
| 208 sprouted.deposit(purse, 5); | 205 sprouted.deposit(purse, 5); |
| 209 _checkBalance(sprouted, 0 + 5); | 206 sprouted.queryBalance((int balance) { |
| 210 _checkBalance(purse, 100 - 5); | 207 Expect.equals(0 + 5, balance); |
| 211 | 208 }); |
| 209 purse.queryBalance((int balance) { |
| 210 Expect.equals(100 - 5, balance); |
| 211 }); |
| 212 sprouted.deposit(purse, 42); | 212 sprouted.deposit(purse, 42); |
| 213 _checkBalance(sprouted, 0 + 5 + 42); | 213 sprouted.queryBalance((int balance) { |
| 214 _checkBalance(purse, 100 - 5 - 42); | 214 Expect.equals(0 + 5 + 42, balance); |
| 215 })); | 215 }); |
| 216 })); | 216 purse.queryBalance((int balance) { |
| 217 })); | 217 Expect.equals(100 - 5 - 42, balance); |
| 218 }); | 218 }); |
| 219 }); |
| 220 }); |
| 221 }); |
| 222 } |
| 219 | 223 |
| 220 /* This is an attempt to show how the above code could look like if we had | 224 /* This is an attempt to show how the above code could look like if we had |
| 221 * better language support for asynchronous messages (deferred/asynccall). | 225 * better language support for asynchronous messages (deferred/asynccall). |
| 222 * The static helper methods like createPurse and queryBalance would also | 226 * The static helper methods like createPurse and queryBalance would also |
| 223 * have to be marked async. | 227 * have to be marked async. |
| 224 | 228 |
| 225 void run(port) { | 229 void run(port) { |
| 226 MintMakerWrapper mintMaker = spawnMintMaker(); | 230 MintMakerWrapper mintMaker = spawnMintMaker(); |
| 227 deferred { | 231 deferred { |
| 228 MintWrapper mint = asynccall mintMaker.createMint(); | 232 MintWrapper mint = asynccall mintMaker.createMint(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 256 | 260 |
| 257 sprouted...deposit(purse, 5); | 261 sprouted...deposit(purse, 5); |
| 258 Expect.equals(0 + 5, sprouted.queryBalance()); | 262 Expect.equals(0 + 5, sprouted.queryBalance()); |
| 259 Expect.equals(100 - 5, purse.queryBalance()); | 263 Expect.equals(100 - 5, purse.queryBalance()); |
| 260 | 264 |
| 261 sprouted...deposit(purse, 42); | 265 sprouted...deposit(purse, 42); |
| 262 Expect.equals(0 + 5 + 42, sprouted.queryBalance()); | 266 Expect.equals(0 + 5 + 42, sprouted.queryBalance()); |
| 263 Expect.equals(100 - 5 - 42, purse.queryBalance()); | 267 Expect.equals(100 - 5 - 42, purse.queryBalance()); |
| 264 } | 268 } |
| 265 */ | 269 */ |
| 270 |
| 266 } | 271 } |
| 272 |
| 273 main() { |
| 274 MintMakerTest.testMain(); |
| 275 } |
| OLD | NEW |