OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.test.asset_test; | 5 library barback.test.asset_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:isolate'; |
10 | 11 |
11 import 'package:barback/barback.dart'; | 12 import 'package:barback/barback.dart'; |
| 13 import 'package:barback/src/internal_asset.dart'; |
12 import 'package:path/path.dart' as pathos; | 14 import 'package:path/path.dart' as pathos; |
13 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
14 | 16 |
15 import 'utils.dart'; | 17 import 'utils.dart'; |
16 | 18 |
17 /// The contents of the test binary file. | 19 /// The contents of the test binary file. |
18 final binaryContents = [0, 1, 2, 3, 4]; | 20 final binaryContents = [0, 1, 2, 3, 4]; |
19 | 21 |
20 main() { | 22 main() { |
21 initConfig(); | 23 initConfig(); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 }); | 210 }); |
209 }); | 211 }); |
210 | 212 |
211 group("file asset", () { | 213 group("file asset", () { |
212 test("shows the file path", () { | 214 test("shows the file path", () { |
213 var asset = new Asset.fromPath(id, "path.txt"); | 215 var asset = new Asset.fromPath(id, "path.txt"); |
214 expect(asset.toString(), equals('File "path.txt"')); | 216 expect(asset.toString(), equals('File "path.txt"')); |
215 }); | 217 }); |
216 }); | 218 }); |
217 }); | 219 }); |
| 220 |
| 221 group("across isolates", () { |
| 222 getBytesFromIsolate(Asset asset) { |
| 223 var port = new ReceivePort(); |
| 224 return Isolate.spawn(_getAssetBytes, { |
| 225 'asset': serializeAsset(asset), |
| 226 'replyTo': port.sendPort |
| 227 }).then((_) => port.first); |
| 228 } |
| 229 |
| 230 test("gets the UTF-8-encoded string for a string asset", () { |
| 231 var asset = new Asset.fromString(id, "çøñ†éℵ™"); |
| 232 expect(getBytesFromIsolate(asset), |
| 233 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
| 234 }); |
| 235 |
| 236 test("gets the raw bytes for a byte asset", () { |
| 237 var asset = new Asset.fromBytes(id, binaryContents); |
| 238 expect(getBytesFromIsolate(asset), |
| 239 completion(equals(binaryContents))); |
| 240 }); |
| 241 |
| 242 test("gets the raw bytes for a binary file", () { |
| 243 var asset = new Asset.fromPath(id, binaryFilePath); |
| 244 expect(getBytesFromIsolate(asset), |
| 245 completion(equals(binaryContents))); |
| 246 }); |
| 247 |
| 248 test("gets the raw bytes for a text file", () { |
| 249 var asset = new Asset.fromPath(id, textFilePath); |
| 250 expect(getBytesFromIsolate(asset), |
| 251 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
| 252 }); |
| 253 |
| 254 test("gets the raw bytes for a stream", () { |
| 255 var asset = new Asset.fromStream(id, |
| 256 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
| 257 expect(getBytesFromIsolate(asset), |
| 258 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
| 259 }); |
| 260 }); |
218 } | 261 } |
| 262 |
| 263 void _getAssetBytes(message) { |
| 264 var asset = deserializeAsset(message['asset']); |
| 265 var builder = asset.read().fold(new BytesBuilder(), |
| 266 (builder, chunk) => builder..add(chunk)); |
| 267 builder.then((builder) => message['replyTo'].send(builder.takeBytes())); |
| 268 } |
OLD | NEW |