Chromium Code Reviews| 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:io'; | 8 import 'dart:io'; |
| 9 import 'dart:utf'; | |
| 9 | 10 |
| 10 import 'package:barback/barback.dart'; | 11 import 'package:barback/barback.dart'; |
| 12 import 'package:pathos/path.dart' as pathos; | |
| 11 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 12 | 14 |
| 13 import 'utils.dart'; | 15 import 'utils.dart'; |
| 14 | 16 |
| 17 /// The contents of the test binary file. | |
| 18 final binaryContents = [0, 1, 2, 3, 4]; | |
| 19 | |
| 15 main() { | 20 main() { |
| 16 initConfig(); | 21 initConfig(); |
| 17 | 22 |
| 23 Directory tempDir; | |
| 24 String binaryFilePath; | |
| 25 String textFilePath; | |
| 26 String utf32FilePath; | |
| 27 | |
| 28 setUp(() { | |
| 29 // Create a temp file we can use for assets. | |
| 30 tempDir = new Directory("").createTempSync(); | |
| 31 binaryFilePath = pathos.join(tempDir.path, "file.bin"); | |
| 32 new File(binaryFilePath).writeAsBytesSync(binaryContents); | |
| 33 | |
| 34 textFilePath = pathos.join(tempDir.path, "file.txt"); | |
| 35 new File(textFilePath).writeAsStringSync("çøñ†éℵ™"); | |
| 36 | |
| 37 utf32FilePath = pathos.join(tempDir.path, "file.utf32"); | |
| 38 new File(utf32FilePath).writeAsBytesSync(encodeUtf32("çøñ†éℵ™")); | |
| 39 }); | |
| 40 | |
| 41 tearDown(() { | |
| 42 if (tempDir != null) tempDir.deleteSync(recursive: true); | |
| 43 }); | |
| 44 | |
| 18 var id = new AssetId.parse("package|path/to/asset.txt"); | 45 var id = new AssetId.parse("package|path/to/asset.txt"); |
| 19 | 46 |
| 47 group("Asset.fromBytes", () { | |
| 48 test("returns an asset with the given ID", () { | |
| 49 var asset = new Asset.fromBytes(id, [1]); | |
| 50 expect(asset.id, equals(id)); | |
| 51 }); | |
| 52 }); | |
| 53 | |
| 20 group("Asset.fromFile", () { | 54 group("Asset.fromFile", () { |
| 21 test("returns an asset with the given ID", () { | 55 test("returns an asset with the given ID", () { |
| 22 var asset = new Asset.fromFile(id, new File("asset.txt")); | 56 var asset = new Asset.fromFile(id, new File("asset.txt")); |
| 23 expect(asset.id, equals(id)); | 57 expect(asset.id, equals(id)); |
| 24 }); | 58 }); |
| 25 }); | 59 }); |
| 26 | 60 |
| 27 group("Asset.fromPath", () { | 61 group("Asset.fromPath", () { |
| 28 test("returns an asset with the given ID", () { | 62 test("returns an asset with the given ID", () { |
| 29 var asset = new Asset.fromPath(id, "asset.txt"); | 63 var asset = new Asset.fromPath(id, "asset.txt"); |
| 30 expect(asset.id, equals(id)); | 64 expect(asset.id, equals(id)); |
| 31 }); | 65 }); |
| 32 }); | 66 }); |
| 33 | 67 |
| 34 group("Asset.fromString", () { | 68 group("Asset.fromString", () { |
| 35 test("returns an asset with the given ID", () { | 69 test("returns an asset with the given ID", () { |
| 36 var asset = new Asset.fromString(id, "content"); | 70 var asset = new Asset.fromString(id, "content"); |
| 37 expect(asset.id, equals(id)); | 71 expect(asset.id, equals(id)); |
| 38 }); | 72 }); |
| 39 }); | 73 }); |
| 74 | |
| 75 group("read()", () { | |
| 76 test("gets the UTF-8-encoded string for a string asset", () { | |
| 77 var asset = new Asset.fromString(id, "çøñ†éℵ™"); | |
| 78 expect(asset.read().toList(), | |
| 79 completion(equals([encodeUtf8("çøñ†éℵ™")]))); | |
| 80 }); | |
| 81 | |
| 82 test("gets the raw bytes for a byte asset", () { | |
| 83 var asset = new Asset.fromBytes(id, binaryContents); | |
| 84 expect(asset.read().toList(), | |
| 85 completion(equals([binaryContents]))); | |
| 86 }); | |
| 87 | |
| 88 test("gets the raw bytes for a binary file", () { | |
| 89 var asset = new Asset.fromPath(id, binaryFilePath); | |
| 90 expect(asset.read().toList(), | |
| 91 completion(equals([binaryContents]))); | |
| 92 }); | |
| 93 | |
| 94 test("gets the raw bytes for a text file", () { | |
| 95 var asset = new Asset.fromPath(id, textFilePath); | |
| 96 expect(asset.read().toList(), | |
| 97 completion(equals([encodeUtf8("çøñ†éℵ™")]))); | |
| 98 }); | |
| 99 }); | |
| 100 | |
| 101 group("readAsString()", () { | |
| 102 group("byte asset", () { | |
| 103 test("defaults to UTF-8 if encoding is omitted", () { | |
| 104 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™")); | |
| 105 expect(asset.readAsString(), | |
| 106 completion(equals("çøñ†éℵ™"))); | |
| 107 }); | |
| 108 | |
| 109 test("supports UTF-8", () { | |
| 110 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™")); | |
| 111 expect(asset.readAsString(encoding: Encoding.UTF_8), | |
| 112 completion(equals("çøñ†éℵ™"))); | |
| 113 }); | |
| 114 | |
| 115 // TODO(rnystrom): Test other encodings once #6284 is fixed. | |
| 116 }); | |
| 117 | |
| 118 group("string asset", () { | |
| 119 test("gets the string", () { | |
| 120 var asset = new Asset.fromString(id, "contents"); | |
| 121 expect(asset.readAsString(), | |
| 122 completion(equals("contents"))); | |
| 123 }); | |
| 124 | |
| 125 test("ignores the encoding", () { | |
| 126 var asset = new Asset.fromString(id, "contents"); | |
| 127 expect(asset.readAsString(encoding: Encoding.ISO_8859_1), | |
| 128 completion(equals("contents"))); | |
| 129 }); | |
| 130 }); | |
| 131 | |
| 132 group("file asset", () { | |
| 133 test("defaults to UTF-8 if encoding is omitted", () { | |
| 134 var asset = new Asset.fromPath(id, textFilePath); | |
| 135 expect(asset.readAsString(), | |
| 136 completion(equals("çøñ†éℵ™"))); | |
| 137 }); | |
| 138 | |
| 139 // TODO(rnystrom): Disabled until #11744 is fixed. | |
|
nweiz
2013/07/09 02:00:01
I'd rather just remove this entirely. I'm not a fa
Bob Nystrom
2013/07/09 16:26:03
Done.
| |
| 140 /* | |
| 141 test("uses the given encoding", () { | |
| 142 var asset = new Asset.fromPath(id, utf32FilePath); | |
| 143 expect(asset.readAsString(encoding: Encoding.fromName("UTF-32")), | |
| 144 completion(equals("çøñ†éℵ™"))); | |
| 145 }); | |
| 146 */ | |
| 147 }); | |
| 148 }); | |
| 149 | |
| 150 group("toString()", () { | |
| 151 group("byte asset", () { | |
| 152 test("shows the list of bytes in hex", () { | |
| 153 var asset = new Asset.fromBytes(id, | |
| 154 [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]); | |
| 155 expect(asset.toString(), equals( | |
| 156 "Bytes [00 01 02 04 08 10 20 40 80 ff]")); | |
| 157 }); | |
| 158 | |
| 159 test("truncates the middle of there are more than ten bytes", () { | |
| 160 var asset = new Asset.fromBytes(id, | |
| 161 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); | |
| 162 expect(asset.toString(), equals( | |
| 163 "Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]")); | |
| 164 }); | |
| 165 }); | |
| 166 | |
| 167 group("string asset", () { | |
| 168 test("shows the contents", () { | |
| 169 var asset = new Asset.fromString(id, "contents"); | |
| 170 expect(asset.toString(), equals( | |
| 171 'String "contents"')); | |
| 172 }); | |
| 173 | |
| 174 test("truncates the middle of there are more than 40 characters", () { | |
| 175 var asset = new Asset.fromString(id, | |
| 176 "this is a fairly long string asset content that gets shortened"); | |
| 177 expect(asset.toString(), equals( | |
| 178 'String "this is a fairly lon ... that gets shortened"')); | |
| 179 }); | |
| 180 }); | |
| 181 | |
| 182 group("file asset", () { | |
| 183 test("shows the file path", () { | |
| 184 var asset = new Asset.fromPath(id, "path.txt"); | |
| 185 expect(asset.toString(), equals('File "path.txt"')); | |
| 186 }); | |
| 187 }); | |
| 188 }); | |
| 40 } | 189 } |
| OLD | NEW |