| 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..c2ecefc8d9d62e91c48d924280bc14a8777ed176 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,110 @@ 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("çøñ†éℵ™")));
|
| + });
|
| + });
|
| + });
|
| +
|
| + 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"'));
|
| + });
|
| + });
|
| + });
|
| }
|
|
|