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' show Encoding, UTF8, LATIN1; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:utf'; | |
11 | 10 |
12 import 'package:barback/barback.dart'; | 11 import 'package:barback/barback.dart'; |
13 import 'package:path/path.dart' as pathos; | 12 import 'package:path/path.dart' as pathos; |
14 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
15 | 14 |
16 import 'utils.dart'; | 15 import 'utils.dart'; |
17 | 16 |
18 /// The contents of the test binary file. | 17 /// The contents of the test binary file. |
19 final binaryContents = [0, 1, 2, 3, 4]; | 18 final binaryContents = [0, 1, 2, 3, 4]; |
20 | 19 |
21 main() { | 20 main() { |
22 initConfig(); | 21 initConfig(); |
23 | 22 |
24 Directory tempDir; | 23 Directory tempDir; |
25 String binaryFilePath; | 24 String binaryFilePath; |
26 String textFilePath; | 25 String textFilePath; |
27 String utf32FilePath; | 26 String latin1FilePath; |
28 | 27 |
29 setUp(() { | 28 setUp(() { |
30 // Create a temp file we can use for assets. | 29 // Create a temp file we can use for assets. |
31 tempDir = new Directory("").createTempSync(); | 30 tempDir = new Directory("").createTempSync(); |
32 binaryFilePath = pathos.join(tempDir.path, "file.bin"); | 31 binaryFilePath = pathos.join(tempDir.path, "file.bin"); |
33 new File(binaryFilePath).writeAsBytesSync(binaryContents); | 32 new File(binaryFilePath).writeAsBytesSync(binaryContents); |
34 | 33 |
35 textFilePath = pathos.join(tempDir.path, "file.txt"); | 34 textFilePath = pathos.join(tempDir.path, "file.txt"); |
36 new File(textFilePath).writeAsStringSync("çøñ†éℵ™"); | 35 new File(textFilePath).writeAsStringSync("çøñ†éℵ™"); |
37 | 36 |
38 utf32FilePath = pathos.join(tempDir.path, "file.utf32"); | 37 latin1FilePath = pathos.join(tempDir.path, "file.latin1"); |
39 new File(utf32FilePath).writeAsBytesSync(encodeUtf32("çøñ†éℵ™")); | 38 new File(latin1FilePath).writeAsBytesSync(LATIN1.encode("blåbærgrød")); |
40 }); | 39 }); |
41 | 40 |
42 tearDown(() { | 41 tearDown(() { |
43 if (tempDir != null) tempDir.deleteSync(recursive: true); | 42 if (tempDir != null) tempDir.deleteSync(recursive: true); |
44 }); | 43 }); |
45 | 44 |
46 var id = new AssetId.parse("package|path/to/asset.txt"); | 45 var id = new AssetId.parse("package|path/to/asset.txt"); |
47 | 46 |
48 group("Asset.fromBytes", () { | 47 group("Asset.fromBytes", () { |
49 test("returns an asset with the given ID", () { | 48 test("returns an asset with the given ID", () { |
(...skipping 20 matching lines...) Expand all Loading... |
70 test("returns an asset with the given ID", () { | 69 test("returns an asset with the given ID", () { |
71 var asset = new Asset.fromString(id, "content"); | 70 var asset = new Asset.fromString(id, "content"); |
72 expect(asset.id, equals(id)); | 71 expect(asset.id, equals(id)); |
73 }); | 72 }); |
74 }); | 73 }); |
75 | 74 |
76 group("read()", () { | 75 group("read()", () { |
77 test("gets the UTF-8-encoded string for a string asset", () { | 76 test("gets the UTF-8-encoded string for a string asset", () { |
78 var asset = new Asset.fromString(id, "çøñ†éℵ™"); | 77 var asset = new Asset.fromString(id, "çøñ†éℵ™"); |
79 expect(asset.read().toList(), | 78 expect(asset.read().toList(), |
80 completion(equals([encodeUtf8("çøñ†éℵ™")]))); | 79 completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
81 }); | 80 }); |
82 | 81 |
83 test("gets the raw bytes for a byte asset", () { | 82 test("gets the raw bytes for a byte asset", () { |
84 var asset = new Asset.fromBytes(id, binaryContents); | 83 var asset = new Asset.fromBytes(id, binaryContents); |
85 expect(asset.read().toList(), | 84 expect(asset.read().toList(), |
86 completion(equals([binaryContents]))); | 85 completion(equals([binaryContents]))); |
87 }); | 86 }); |
88 | 87 |
89 test("gets the raw bytes for a binary file", () { | 88 test("gets the raw bytes for a binary file", () { |
90 var asset = new Asset.fromPath(id, binaryFilePath); | 89 var asset = new Asset.fromPath(id, binaryFilePath); |
91 expect(asset.read().toList(), | 90 expect(asset.read().toList(), |
92 completion(equals([binaryContents]))); | 91 completion(equals([binaryContents]))); |
93 }); | 92 }); |
94 | 93 |
95 test("gets the raw bytes for a text file", () { | 94 test("gets the raw bytes for a text file", () { |
96 var asset = new Asset.fromPath(id, textFilePath); | 95 var asset = new Asset.fromPath(id, textFilePath); |
97 expect(asset.read().toList(), | 96 expect(asset.read().toList(), |
98 completion(equals([encodeUtf8("çøñ†éℵ™")]))); | 97 completion(equals([UTF8.encode("çøñ†éℵ™")]))); |
99 }); | 98 }); |
100 }); | 99 }); |
101 | 100 |
102 group("readAsString()", () { | 101 group("readAsString()", () { |
103 group("byte asset", () { | 102 group("byte asset", () { |
104 test("defaults to UTF-8 if encoding is omitted", () { | 103 test("defaults to UTF-8 if encoding is omitted", () { |
105 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™")); | 104 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
106 expect(asset.readAsString(), | 105 expect(asset.readAsString(), |
107 completion(equals("çøñ†éℵ™"))); | 106 completion(equals("çøñ†éℵ™"))); |
108 }); | 107 }); |
109 | 108 |
110 test("supports UTF-8", () { | 109 test("supports UTF-8", () { |
111 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™")); | 110 var asset = new Asset.fromBytes(id, UTF8.encode("çøñ†éℵ™")); |
112 expect(asset.readAsString(encoding: UTF8), | 111 expect(asset.readAsString(encoding: UTF8), |
113 completion(equals("çøñ†éℵ™"))); | 112 completion(equals("çøñ†éℵ™"))); |
114 }); | 113 }); |
115 | 114 |
116 // TODO(rnystrom): Test other encodings once #6284 is fixed. | 115 // TODO(rnystrom): Test other encodings once #6284 is fixed. |
117 }); | 116 }); |
118 | 117 |
119 group("string asset", () { | 118 group("string asset", () { |
120 test("gets the string", () { | 119 test("gets the string", () { |
121 var asset = new Asset.fromString(id, "contents"); | 120 var asset = new Asset.fromString(id, "contents"); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 }); | 171 }); |
173 | 172 |
174 group("file asset", () { | 173 group("file asset", () { |
175 test("shows the file path", () { | 174 test("shows the file path", () { |
176 var asset = new Asset.fromPath(id, "path.txt"); | 175 var asset = new Asset.fromPath(id, "path.txt"); |
177 expect(asset.toString(), equals('File "path.txt"')); | 176 expect(asset.toString(), equals('File "path.txt"')); |
178 }); | 177 }); |
179 }); | 178 }); |
180 }); | 179 }); |
181 } | 180 } |
OLD | NEW |