OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library barback.test.asset_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
| 10 import 'dart:isolate'; |
| 11 |
| 12 import 'package:barback/barback.dart'; |
| 13 import 'package:barback/src/asset/internal_asset.dart'; |
| 14 import 'package:path/path.dart' as pathos; |
| 15 import 'package:unittest/unittest.dart'; |
| 16 |
| 17 import 'utils.dart'; |
| 18 |
| 19 /// The contents of the test binary file. |
| 20 final binaryContents = [0, 1, 2, 3, 4]; |
| 21 |
| 22 main() { |
| 23 initConfig(); |
| 24 |
| 25 Directory tempDir; |
| 26 String binaryFilePath; |
| 27 String textFilePath; |
| 28 String latin1FilePath; |
| 29 |
| 30 setUp(() { |
| 31 // Create a temp file we can use for assets. |
| 32 tempDir = Directory.systemTemp.createTempSync('barback_asset_test_'); |
| 33 binaryFilePath = pathos.join(tempDir.path, "file.bin"); |
| 34 new File(binaryFilePath).writeAsBytesSync(binaryContents); |
| 35 |
| 36 textFilePath = pathos.join(tempDir.path, "file.txt"); |
| 37 new File(textFilePath).writeAsStringSync("çøñ†éℵ™"); |
| 38 |
| 39 latin1FilePath = pathos.join(tempDir.path, "file.latin1"); |
| 40 new File(latin1FilePath).writeAsBytesSync(LATIN1.encode("blåbærgrød")); |
| 41 }); |
| 42 |
| 43 tearDown(() { |
| 44 if (tempDir != null) tempDir.deleteSync(recursive: true); |
| 45 }); |
| 46 |
| 47 var id = new AssetId.parse("package|path/to/asset.txt"); |
| 48 |
| 49 group("Asset.fromBytes", () { |
| 50 test("returns an asset with the given ID", () { |
| 51 var asset = new Asset.fromBytes(id, [1]); |
| 52 expect(asset.id, equals(id)); |
| 53 }); |
| 54 }); |
| 55 |
| 56 group("Asset.fromFile", () { |
| 57 test("returns an asset with the given ID", () { |
| 58 var asset = new Asset.fromFile(id, new File("asset.txt")); |
| 59 expect(asset.id, equals(id)); |
| 60 }); |
| 61 }); |
| 62 |
| 63 group("Asset.fromPath", () { |
| 64 test("returns an asset with the given ID", () { |
| 65 var asset = new Asset.fromPath(id, "asset.txt"); |
| 66 expect(asset.id, equals(id)); |
| 67 }); |
| 68 }); |
| 69 |
| 70 group("Asset.fromString", () { |
| 71 test("returns an asset with the given ID", () { |
| 72 var asset = new Asset.fromString(id, "content"); |
| 73 expect(asset.id, equals(id)); |
| 74 }); |
| 75 }); |
| 76 |
| 77 group("Asset.fromStream", () { |
| 78 test("returns an asset with the given ID", () { |
| 79 var asset = new Asset.fromStream(id, |
| 80 new Stream.fromFuture(new Future.value([104, 101, 108, 108, 111]))); |
| 81 expect(asset.id, equals(id)); |
| 82 }); |
| 83 }); |
| 84 |
| 85 group("read()", () { |
| 86 test("gets the UTF-8-encoded string for a string asset", () { |
| 87 var asset = new Asset.fromString(id, "çøñ†éℵ™"); |
| 88 expect(asset.read().toList(), |
| 89 completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
| 90 }); |
| 91 |
| 92 test("gets the raw bytes for a byte asset", () { |
| 93 var asset = new Asset.fromBytes(id, binaryContents); |
| 94 expect(asset.read().toList(), |
| 95 completion(equals([binaryContents]))); |
| 96 }); |
| 97 |
| 98 test("gets the raw bytes for a binary file", () { |
| 99 var asset = new Asset.fromPath(id, binaryFilePath); |
| 100 expect(asset.read().toList(), |
| 101 completion(equals([binaryContents]))); |
| 102 }); |
| 103 |
| 104 test("gets the raw bytes for a text file", () { |
| 105 var asset = new Asset.fromPath(id, textFilePath); |
| 106 expect(asset.read().toList(), |
| 107 completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
| 108 }); |
| 109 |
| 110 test("gets the raw bytes for a stream", () { |
| 111 var asset = new Asset.fromStream(id, |
| 112 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
| 113 expect(asset.read().toList(), |
| 114 completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
| 115 }); |
| 116 }); |
| 117 |
| 118 group("readAsString()", () { |
| 119 group("byte asset", () { |
| 120 test("defaults to UTF-8 if encoding is omitted", () { |
| 121 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
| 122 expect(asset.readAsString(), |
| 123 completion(equals("çøñ†éℵ™"))); |
| 124 }); |
| 125 |
| 126 test("supports UTF-8", () { |
| 127 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
| 128 expect(asset.readAsString(encoding: UTF8), |
| 129 completion(equals("çøñ†éℵ™"))); |
| 130 }); |
| 131 |
| 132 // TODO(rnystrom): Test other encodings once #6284 is fixed. |
| 133 }); |
| 134 |
| 135 group("string asset", () { |
| 136 test("gets the string", () { |
| 137 var asset = new Asset.fromString(id, "contents"); |
| 138 expect(asset.readAsString(), |
| 139 completion(equals("contents"))); |
| 140 }); |
| 141 |
| 142 test("ignores the encoding", () { |
| 143 var asset = new Asset.fromString(id, "contents"); |
| 144 expect(asset.readAsString(encoding: LATIN1), |
| 145 completion(equals("contents"))); |
| 146 }); |
| 147 }); |
| 148 |
| 149 group("file asset", () { |
| 150 test("defaults to UTF-8 if encoding is omitted", () { |
| 151 var asset = new Asset.fromPath(id, textFilePath); |
| 152 expect(asset.readAsString(), |
| 153 completion(equals("çøñ†éℵ™"))); |
| 154 }); |
| 155 }); |
| 156 |
| 157 group("stream asset", () { |
| 158 test("defaults to UTF-8 if encoding is omitted", () { |
| 159 var asset = new Asset.fromStream(id, |
| 160 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
| 161 expect(asset.readAsString(), |
| 162 completion(equals("çøñ†éℵ™"))); |
| 163 }); |
| 164 |
| 165 test("supports UTF-8", () { |
| 166 var asset = new Asset.fromStream(id, |
| 167 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
| 168 expect(asset.readAsString(encoding: UTF8), |
| 169 completion(equals("çøñ†éℵ™"))); |
| 170 }); |
| 171 |
| 172 test("supports ISO-8859-1", () { |
| 173 var future = new Future.value(LATIN1.encode("blåbærgrød")); |
| 174 var asset = new Asset.fromStream(id, new Stream.fromFuture(future)); |
| 175 expect(asset.readAsString(encoding: LATIN1), |
| 176 completion(equals("blåbærgrød"))); |
| 177 }); |
| 178 }); |
| 179 }); |
| 180 |
| 181 group("toString()", () { |
| 182 group("byte asset", () { |
| 183 test("shows the list of bytes in hex", () { |
| 184 var asset = new Asset.fromBytes(id, |
| 185 [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]); |
| 186 expect(asset.toString(), equals( |
| 187 "Bytes [00 01 02 04 08 10 20 40 80 ff]")); |
| 188 }); |
| 189 |
| 190 test("truncates the middle of there are more than ten bytes", () { |
| 191 var asset = new Asset.fromBytes(id, |
| 192 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); |
| 193 expect(asset.toString(), equals( |
| 194 "Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]")); |
| 195 }); |
| 196 }); |
| 197 |
| 198 group("string asset", () { |
| 199 test("shows the contents", () { |
| 200 var asset = new Asset.fromString(id, "contents"); |
| 201 expect(asset.toString(), equals( |
| 202 'String "contents"')); |
| 203 }); |
| 204 |
| 205 test("truncates the middle of there are more than 40 characters", () { |
| 206 var asset = new Asset.fromString(id, |
| 207 "this is a fairly long string asset content that gets shortened"); |
| 208 expect(asset.toString(), equals( |
| 209 'String "this is a fairly lon ... that gets shortened"')); |
| 210 }); |
| 211 }); |
| 212 |
| 213 group("file asset", () { |
| 214 test("shows the file path", () { |
| 215 var asset = new Asset.fromPath(id, "path.txt"); |
| 216 expect(asset.toString(), equals('File "path.txt"')); |
| 217 }); |
| 218 }); |
| 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 }); |
| 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 |