Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Unified Diff: pkg/barback/test/asset_test.dart

Issue 18854007: Binary assets and more unit tests for Asset. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/barback/test/asset_test.dart
diff --git a/pkg/barback/test/asset_test.dart b/pkg/barback/test/asset_test.dart
index fb8b1624a5d21fa1aece3c25e7cd69b7c1c0c93e..8df6e237ff485120e1605fe37a627263678305fa 100644
--- a/pkg/barback/test/asset_test.dart
+++ b/pkg/barback/test/asset_test.dart
@@ -6,17 +6,51 @@ library barback.test.asset_test;
import 'dart:async';
import 'dart:io';
+import 'dart:utf';
import 'package:barback/barback.dart';
+import 'package:pathos/path.dart' as pathos;
import 'package:unittest/unittest.dart';
import 'utils.dart';
+/// The contents of the test binary file.
+final binaryContents = [0, 1, 2, 3, 4];
+
main() {
initConfig();
+ Directory tempDir;
+ String binaryFilePath;
+ String textFilePath;
+ String utf32FilePath;
+
+ setUp(() {
+ // Create a temp file we can use for assets.
+ tempDir = new Directory("").createTempSync();
+ binaryFilePath = pathos.join(tempDir.path, "file.bin");
+ new File(binaryFilePath).writeAsBytesSync(binaryContents);
+
+ textFilePath = pathos.join(tempDir.path, "file.txt");
+ new File(textFilePath).writeAsStringSync("çøñ†éℵ™");
+
+ utf32FilePath = pathos.join(tempDir.path, "file.utf32");
+ new File(utf32FilePath).writeAsBytesSync(encodeUtf32("çøñ†éℵ™"));
+ });
+
+ tearDown(() {
+ if (tempDir != null) tempDir.deleteSync(recursive: true);
+ });
+
var id = new AssetId.parse("package|path/to/asset.txt");
+ group("Asset.fromBytes", () {
+ test("returns an asset with the given ID", () {
+ var asset = new Asset.fromBytes(id, [1]);
+ expect(asset.id, equals(id));
+ });
+ });
+
group("Asset.fromFile", () {
test("returns an asset with the given ID", () {
var asset = new Asset.fromFile(id, new File("asset.txt"));
@@ -37,4 +71,119 @@ main() {
expect(asset.id, equals(id));
});
});
+
+ group("read()", () {
+ test("gets the UTF-8-encoded string for a string asset", () {
+ var asset = new Asset.fromString(id, "çøñ†éℵ™");
+ expect(asset.read().toList(),
+ completion(equals([encodeUtf8("çøñ†éℵ™")])));
+ });
+
+ test("gets the raw bytes for a byte asset", () {
+ var asset = new Asset.fromBytes(id, binaryContents);
+ expect(asset.read().toList(),
+ completion(equals([binaryContents])));
+ });
+
+ test("gets the raw bytes for a binary file", () {
+ var asset = new Asset.fromPath(id, binaryFilePath);
+ expect(asset.read().toList(),
+ completion(equals([binaryContents])));
+ });
+
+ test("gets the raw bytes for a text file", () {
+ var asset = new Asset.fromPath(id, textFilePath);
+ expect(asset.read().toList(),
+ completion(equals([encodeUtf8("çøñ†éℵ™")])));
+ });
+ });
+
+ group("readAsString()", () {
+ group("byte asset", () {
+ test("defaults to UTF-8 if encoding is omitted", () {
+ var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™"));
+ expect(asset.readAsString(),
+ completion(equals("çøñ†éℵ™")));
+ });
+
+ test("supports UTF-8", () {
+ var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™"));
+ expect(asset.readAsString(encoding: Encoding.UTF_8),
+ completion(equals("çøñ†éℵ™")));
+ });
+
+ // TODO(rnystrom): Test other encodings once #6284 is fixed.
+ });
+
+ group("string asset", () {
+ test("gets the string", () {
+ var asset = new Asset.fromString(id, "contents");
+ expect(asset.readAsString(),
+ completion(equals("contents")));
+ });
+
+ test("ignores the encoding", () {
+ var asset = new Asset.fromString(id, "contents");
+ expect(asset.readAsString(encoding: Encoding.ISO_8859_1),
+ completion(equals("contents")));
+ });
+ });
+
+ group("file asset", () {
+ test("defaults to UTF-8 if encoding is omitted", () {
+ var asset = new Asset.fromPath(id, textFilePath);
+ expect(asset.readAsString(),
+ completion(equals("çøñ†éℵ™")));
+ });
+
+ // 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.
+ /*
+ test("uses the given encoding", () {
+ var asset = new Asset.fromPath(id, utf32FilePath);
+ expect(asset.readAsString(encoding: Encoding.fromName("UTF-32")),
+ completion(equals("çøñ†éℵ™")));
+ });
+ */
+ });
+ });
+
+ group("toString()", () {
+ group("byte asset", () {
+ test("shows the list of bytes in hex", () {
+ var asset = new Asset.fromBytes(id,
+ [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]);
+ expect(asset.toString(), equals(
+ "Bytes [00 01 02 04 08 10 20 40 80 ff]"));
+ });
+
+ test("truncates the middle of there are more than ten bytes", () {
+ var asset = new Asset.fromBytes(id,
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
+ expect(asset.toString(), equals(
+ "Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]"));
+ });
+ });
+
+ group("string asset", () {
+ test("shows the contents", () {
+ var asset = new Asset.fromString(id, "contents");
+ expect(asset.toString(), equals(
+ 'String "contents"'));
+ });
+
+ test("truncates the middle of there are more than 40 characters", () {
+ var asset = new Asset.fromString(id,
+ "this is a fairly long string asset content that gets shortened");
+ expect(asset.toString(), equals(
+ 'String "this is a fairly lon ... that gets shortened"'));
+ });
+ });
+
+ group("file asset", () {
+ test("shows the file path", () {
+ var asset = new Asset.fromPath(id, "path.txt");
+ expect(asset.toString(), equals('File "path.txt"'));
+ });
+ });
+ });
}

Powered by Google App Engine
This is Rietveld 408576698