Chromium Code Reviews| 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..0a81c7d6807dd2be0c025a601e217f621caedaad 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).writeAsStringSync("çøñ†éℵ™"); |
|
nweiz
2013/07/08 22:50:46
It doesn't look like you're actually writing this
Bob Nystrom
2013/07/08 23:26:04
Eek crap. Copy/paste bug.
|
| + }); |
| + |
| + 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")); |
| @@ -29,6 +63,7 @@ main() { |
| var asset = new Asset.fromPath(id, "asset.txt"); |
| expect(asset.id, equals(id)); |
| }); |
| + |
|
nweiz
2013/07/08 22:50:46
Extra newline.
Bob Nystrom
2013/07/08 23:26:04
Done.
|
| }); |
| group("Asset.fromString", () { |
| @@ -37,4 +72,114 @@ 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("çøñ†éℵ™"))); |
| + }); |
|
nweiz
2013/07/08 22:50:46
Add a TODO to test that this supports other encodi
Bob Nystrom
2013/07/08 23:26:04
Done.
|
| + }); |
| + |
| + 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("çøñ†éℵ™"))); |
| + }); |
| + |
| + test("uses the given encoding", () { |
| + var asset = new Asset.fromPath(id, utf32FilePath); |
| + expect(asset.readAsString(encoding: Encoding.fromName("UTF-32")), |
| + completion(equals("çøñ†éℵ™"))); |
|
nweiz
2013/07/08 22:50:46
Since it looks like utf32FilePath isn't being writ
Bob Nystrom
2013/07/08 23:26:04
Two bugs collide! My bug outputs the file in UTF-8
|
| + }); |
| + }); |
| + }); |
| + |
| + 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"')); |
| + }); |
| + }); |
| + }); |
| } |