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 import 'dart:isolate'; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 test("returns an asset with the given ID", () { | 78 test("returns an asset with the given ID", () { |
79 var asset = new Asset.fromStream(id, | 79 var asset = new Asset.fromStream(id, |
80 new Stream.fromFuture(new Future.value([104, 101, 108, 108, 111]))); | 80 new Stream.fromFuture(new Future.value([104, 101, 108, 108, 111]))); |
81 expect(asset.id, equals(id)); | 81 expect(asset.id, equals(id)); |
82 }); | 82 }); |
83 }); | 83 }); |
84 | 84 |
85 group("read()", () { | 85 group("read()", () { |
86 test("gets the UTF-8-encoded string for a string asset", () { | 86 test("gets the UTF-8-encoded string for a string asset", () { |
87 var asset = new Asset.fromString(id, "çøñ†éℵ™"); | 87 var asset = new Asset.fromString(id, "çøñ†éℵ™"); |
88 expect(asset.read().toList(), | 88 expect( |
89 completion(equals([UTF8.encode("çøñ†éℵ™")]))); | 89 asset.read().toList(), completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
90 }); | 90 }); |
91 | 91 |
92 test("gets the raw bytes for a byte asset", () { | 92 test("gets the raw bytes for a byte asset", () { |
93 var asset = new Asset.fromBytes(id, binaryContents); | 93 var asset = new Asset.fromBytes(id, binaryContents); |
94 expect(asset.read().toList(), | 94 expect(asset.read().toList(), completion(equals([binaryContents]))); |
95 completion(equals([binaryContents]))); | |
96 }); | 95 }); |
97 | 96 |
98 test("gets the raw bytes for a binary file", () { | 97 test("gets the raw bytes for a binary file", () { |
99 var asset = new Asset.fromPath(id, binaryFilePath); | 98 var asset = new Asset.fromPath(id, binaryFilePath); |
100 expect(asset.read().toList(), | 99 expect(asset.read().toList(), completion(equals([binaryContents]))); |
101 completion(equals([binaryContents]))); | |
102 }); | 100 }); |
103 | 101 |
104 test("gets the raw bytes for a text file", () { | 102 test("gets the raw bytes for a text file", () { |
105 var asset = new Asset.fromPath(id, textFilePath); | 103 var asset = new Asset.fromPath(id, textFilePath); |
106 expect(asset.read().toList(), | 104 expect( |
107 completion(equals([UTF8.encode("çøñ†éℵ™")]))); | 105 asset.read().toList(), completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
108 }); | 106 }); |
109 | 107 |
110 test("gets the raw bytes for a stream", () { | 108 test("gets the raw bytes for a stream", () { |
111 var asset = new Asset.fromStream(id, | 109 var asset = new Asset.fromStream( |
112 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); | 110 id, new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
113 expect(asset.read().toList(), | 111 expect( |
114 completion(equals([UTF8.encode("çøñ†éℵ™")]))); | 112 asset.read().toList(), completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
115 }); | 113 }); |
116 }); | 114 }); |
117 | 115 |
118 group("readAsString()", () { | 116 group("readAsString()", () { |
119 group("byte asset", () { | 117 group("byte asset", () { |
120 test("defaults to UTF-8 if encoding is omitted", () { | 118 test("defaults to UTF-8 if encoding is omitted", () { |
121 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); | 119 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
122 expect(asset.readAsString(), | 120 expect(asset.readAsString(), completion(equals("çøñ†éℵ™"))); |
123 completion(equals("çøñ†éℵ™"))); | |
124 }); | 121 }); |
125 | 122 |
126 test("supports UTF-8", () { | 123 test("supports UTF-8", () { |
127 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); | 124 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
128 expect(asset.readAsString(encoding: UTF8), | 125 expect( |
129 completion(equals("çøñ†éℵ™"))); | 126 asset.readAsString(encoding: UTF8), completion(equals("çøñ†éℵ™"))); |
130 }); | 127 }); |
131 | 128 |
132 // TODO(rnystrom): Test other encodings once #6284 is fixed. | 129 // TODO(rnystrom): Test other encodings once #6284 is fixed. |
133 }); | 130 }); |
134 | 131 |
135 group("string asset", () { | 132 group("string asset", () { |
136 test("gets the string", () { | 133 test("gets the string", () { |
137 var asset = new Asset.fromString(id, "contents"); | 134 var asset = new Asset.fromString(id, "contents"); |
138 expect(asset.readAsString(), | 135 expect(asset.readAsString(), completion(equals("contents"))); |
139 completion(equals("contents"))); | |
140 }); | 136 }); |
141 | 137 |
142 test("ignores the encoding", () { | 138 test("ignores the encoding", () { |
143 var asset = new Asset.fromString(id, "contents"); | 139 var asset = new Asset.fromString(id, "contents"); |
144 expect(asset.readAsString(encoding: LATIN1), | 140 expect(asset.readAsString(encoding: LATIN1), |
145 completion(equals("contents"))); | 141 completion(equals("contents"))); |
146 }); | 142 }); |
147 }); | 143 }); |
148 | 144 |
149 group("file asset", () { | 145 group("file asset", () { |
150 test("defaults to UTF-8 if encoding is omitted", () { | 146 test("defaults to UTF-8 if encoding is omitted", () { |
151 var asset = new Asset.fromPath(id, textFilePath); | 147 var asset = new Asset.fromPath(id, textFilePath); |
152 expect(asset.readAsString(), | 148 expect(asset.readAsString(), completion(equals("çøñ†éℵ™"))); |
153 completion(equals("çøñ†éℵ™"))); | |
154 }); | 149 }); |
155 }); | 150 }); |
156 | 151 |
157 group("stream asset", () { | 152 group("stream asset", () { |
158 test("defaults to UTF-8 if encoding is omitted", () { | 153 test("defaults to UTF-8 if encoding is omitted", () { |
159 var asset = new Asset.fromStream(id, | 154 var asset = new Asset.fromStream(id, |
160 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); | 155 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
161 expect(asset.readAsString(), | 156 expect(asset.readAsString(), completion(equals("çøñ†éℵ™"))); |
162 completion(equals("çøñ†éℵ™"))); | |
163 }); | 157 }); |
164 | 158 |
165 test("supports UTF-8", () { | 159 test("supports UTF-8", () { |
166 var asset = new Asset.fromStream(id, | 160 var asset = new Asset.fromStream(id, |
167 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); | 161 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
168 expect(asset.readAsString(encoding: UTF8), | 162 expect( |
169 completion(equals("çøñ†éℵ™"))); | 163 asset.readAsString(encoding: UTF8), completion(equals("çøñ†éℵ™"))); |
170 }); | 164 }); |
171 | 165 |
172 test("supports ISO-8859-1", () { | 166 test("supports ISO-8859-1", () { |
173 var future = new Future.value(LATIN1.encode("blåbærgrød")); | 167 var future = new Future.value(LATIN1.encode("blåbærgrød")); |
174 var asset = new Asset.fromStream(id, new Stream.fromFuture(future)); | 168 var asset = new Asset.fromStream(id, new Stream.fromFuture(future)); |
175 expect(asset.readAsString(encoding: LATIN1), | 169 expect(asset.readAsString(encoding: LATIN1), |
176 completion(equals("blåbærgrød"))); | 170 completion(equals("blåbærgrød"))); |
177 }); | 171 }); |
178 }); | 172 }); |
179 }); | 173 }); |
180 | 174 |
181 group("toString()", () { | 175 group("toString()", () { |
182 group("byte asset", () { | 176 group("byte asset", () { |
183 test("shows the list of bytes in hex", () { | 177 test("shows the list of bytes in hex", () { |
184 var asset = new Asset.fromBytes(id, | 178 var asset = |
185 [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]); | 179 new Asset.fromBytes(id, [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]); |
186 expect(asset.toString(), equals( | 180 expect( |
187 "Bytes [00 01 02 04 08 10 20 40 80 ff]")); | 181 asset.toString(), equals("Bytes [00 01 02 04 08 10 20 40 80 ff]")); |
188 }); | 182 }); |
189 | 183 |
190 test("truncates the middle of there are more than ten bytes", () { | 184 test("truncates the middle of there are more than ten bytes", () { |
191 var asset = new Asset.fromBytes(id, | 185 var asset = new Asset.fromBytes( |
192 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); | 186 id, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); |
193 expect(asset.toString(), equals( | 187 expect(asset.toString(), |
194 "Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]")); | 188 equals("Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]")); |
195 }); | 189 }); |
196 }); | 190 }); |
197 | 191 |
198 group("string asset", () { | 192 group("string asset", () { |
199 test("shows the contents", () { | 193 test("shows the contents", () { |
200 var asset = new Asset.fromString(id, "contents"); | 194 var asset = new Asset.fromString(id, "contents"); |
201 expect(asset.toString(), equals( | 195 expect(asset.toString(), equals('String "contents"')); |
202 'String "contents"')); | |
203 }); | 196 }); |
204 | 197 |
205 test("truncates the middle of there are more than 40 characters", () { | 198 test("truncates the middle of there are more than 40 characters", () { |
206 var asset = new Asset.fromString(id, | 199 var asset = new Asset.fromString(id, |
207 "this is a fairly long string asset content that gets shortened"); | 200 "this is a fairly long string asset content that gets shortened"); |
208 expect(asset.toString(), equals( | 201 expect(asset.toString(), |
209 'String "this is a fairly lon ... that gets shortened"')); | 202 equals('String "this is a fairly lon ... that gets shortened"')); |
210 }); | 203 }); |
211 }); | 204 }); |
212 | 205 |
213 group("file asset", () { | 206 group("file asset", () { |
214 test("shows the file path", () { | 207 test("shows the file path", () { |
215 var asset = new Asset.fromPath(id, "path.txt"); | 208 var asset = new Asset.fromPath(id, "path.txt"); |
216 expect(asset.toString(), equals('File "path.txt"')); | 209 expect(asset.toString(), equals('File "path.txt"')); |
217 }); | 210 }); |
218 }); | 211 }); |
219 }); | 212 }); |
220 | 213 |
221 group("across isolates", () { | 214 group("across isolates", () { |
222 getBytesFromIsolate(Asset asset) { | 215 getBytesFromIsolate(Asset asset) { |
223 var port = new ReceivePort(); | 216 var port = new ReceivePort(); |
224 return Isolate.spawn(_getAssetBytes, { | 217 return Isolate.spawn(_getAssetBytes, { |
225 'asset': serializeAsset(asset), | 218 'asset': serializeAsset(asset), |
226 'replyTo': port.sendPort | 219 'replyTo': port.sendPort |
227 }).then((_) => port.first); | 220 }).then((_) => port.first); |
228 } | 221 } |
229 | 222 |
230 test("gets the UTF-8-encoded string for a string asset", () { | 223 test("gets the UTF-8-encoded string for a string asset", () { |
231 var asset = new Asset.fromString(id, "çøñ†éℵ™"); | 224 var asset = new Asset.fromString(id, "çøñ†éℵ™"); |
232 expect(getBytesFromIsolate(asset), | 225 expect(getBytesFromIsolate(asset), |
233 completion(equals(UTF8.encode("çøñ†éℵ™")))); | 226 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
234 }); | 227 }); |
235 | 228 |
236 test("gets the raw bytes for a byte asset", () { | 229 test("gets the raw bytes for a byte asset", () { |
237 var asset = new Asset.fromBytes(id, binaryContents); | 230 var asset = new Asset.fromBytes(id, binaryContents); |
238 expect(getBytesFromIsolate(asset), | 231 expect(getBytesFromIsolate(asset), completion(equals(binaryContents))); |
239 completion(equals(binaryContents))); | |
240 }); | 232 }); |
241 | 233 |
242 test("gets the raw bytes for a binary file", () { | 234 test("gets the raw bytes for a binary file", () { |
243 var asset = new Asset.fromPath(id, binaryFilePath); | 235 var asset = new Asset.fromPath(id, binaryFilePath); |
244 expect(getBytesFromIsolate(asset), | 236 expect(getBytesFromIsolate(asset), completion(equals(binaryContents))); |
245 completion(equals(binaryContents))); | |
246 }); | 237 }); |
247 | 238 |
248 test("gets the raw bytes for a text file", () { | 239 test("gets the raw bytes for a text file", () { |
249 var asset = new Asset.fromPath(id, textFilePath); | 240 var asset = new Asset.fromPath(id, textFilePath); |
250 expect(getBytesFromIsolate(asset), | 241 expect(getBytesFromIsolate(asset), |
251 completion(equals(UTF8.encode("çøñ†éℵ™")))); | 242 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
252 }); | 243 }); |
253 | 244 |
254 test("gets the raw bytes for a stream", () { | 245 test("gets the raw bytes for a stream", () { |
255 var asset = new Asset.fromStream(id, | 246 var asset = new Asset.fromStream( |
256 new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); | 247 id, new Stream.fromFuture(new Future.value(UTF8.encode("çøñ†éℵ™")))); |
257 expect(getBytesFromIsolate(asset), | 248 expect(getBytesFromIsolate(asset), |
258 completion(equals(UTF8.encode("çøñ†éℵ™")))); | 249 completion(equals(UTF8.encode("çøñ†éℵ™")))); |
259 }); | 250 }); |
260 }); | 251 }); |
261 } | 252 } |
262 | 253 |
263 void _getAssetBytes(message) { | 254 void _getAssetBytes(message) { |
264 var asset = deserializeAsset(message['asset']); | 255 var asset = deserializeAsset(message['asset']); |
265 var builder = asset.read().fold(new BytesBuilder(), | 256 var builder = asset |
266 (builder, chunk) => builder..add(chunk)); | 257 .read() |
| 258 .fold(new BytesBuilder(), (builder, chunk) => builder..add(chunk)); |
267 builder.then((builder) => message['replyTo'].send(builder.takeBytes())); | 259 builder.then((builder) => message['replyTo'].send(builder.takeBytes())); |
268 } | 260 } |
OLD | NEW |