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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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).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.
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 });
66
nweiz 2013/07/08 22:50:46 Extra newline.
Bob Nystrom 2013/07/08 23:26:04 Done.
32 }); 67 });
33 68
34 group("Asset.fromString", () { 69 group("Asset.fromString", () {
35 test("returns an asset with the given ID", () { 70 test("returns an asset with the given ID", () {
36 var asset = new Asset.fromString(id, "content"); 71 var asset = new Asset.fromString(id, "content");
37 expect(asset.id, equals(id)); 72 expect(asset.id, equals(id));
38 }); 73 });
39 }); 74 });
75
76 group("read()", () {
77 test("gets the UTF-8-encoded string for a string asset", () {
78 var asset = new Asset.fromString(id, "çøñ†éℵ™");
79 expect(asset.read().toList(),
80 completion(equals([encodeUtf8("çøñ†éℵ™")])));
81 });
82
83 test("gets the raw bytes for a byte asset", () {
84 var asset = new Asset.fromBytes(id, binaryContents);
85 expect(asset.read().toList(),
86 completion(equals([binaryContents])));
87 });
88
89 test("gets the raw bytes for a binary file", () {
90 var asset = new Asset.fromPath(id, binaryFilePath);
91 expect(asset.read().toList(),
92 completion(equals([binaryContents])));
93 });
94
95 test("gets the raw bytes for a text file", () {
96 var asset = new Asset.fromPath(id, textFilePath);
97 expect(asset.read().toList(),
98 completion(equals([encodeUtf8("çøñ†éℵ™")])));
99 });
100 });
101
102 group("readAsString()", () {
103 group("byte asset", () {
104 test("defaults to UTF-8 if encoding is omitted", () {
105 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™"));
106 expect(asset.readAsString(),
107 completion(equals("çøñ†éℵ™")));
108 });
109
110 test("supports UTF-8", () {
111 var asset = new Asset.fromBytes(id, encodeUtf8("çøñ†éℵ™"));
112 expect(asset.readAsString(encoding: Encoding.UTF_8),
113 completion(equals("çøñ†éℵ™")));
114 });
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.
115 });
116
117 group("string asset", () {
118 test("gets the string", () {
119 var asset = new Asset.fromString(id, "contents");
120 expect(asset.readAsString(),
121 completion(equals("contents")));
122 });
123
124 test("ignores the encoding", () {
125 var asset = new Asset.fromString(id, "contents");
126 expect(asset.readAsString(encoding: Encoding.ISO_8859_1),
127 completion(equals("contents")));
128 });
129 });
130
131 group("file asset", () {
132 test("defaults to UTF-8 if encoding is omitted", () {
133 var asset = new Asset.fromPath(id, textFilePath);
134 expect(asset.readAsString(),
135 completion(equals("çøñ†éℵ™")));
136 });
137
138 test("uses the given encoding", () {
139 var asset = new Asset.fromPath(id, utf32FilePath);
140 expect(asset.readAsString(encoding: Encoding.fromName("UTF-32")),
141 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
142 });
143 });
144 });
145
146 group("toString()", () {
147 group("byte asset", () {
148 test("shows the list of bytes in hex", () {
149 var asset = new Asset.fromBytes(id,
150 [0, 1, 2, 4, 8, 16, 32, 64, 128, 255]);
151 expect(asset.toString(), equals(
152 "Bytes [00 01 02 04 08 10 20 40 80 ff]"));
153 });
154
155 test("truncates the middle of there are more than ten bytes", () {
156 var asset = new Asset.fromBytes(id,
157 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
158 expect(asset.toString(), equals(
159 "Bytes [01 02 03 04 05 ... 0a 0b 0c 0d 0e]"));
160 });
161 });
162
163 group("string asset", () {
164 test("shows the contents", () {
165 var asset = new Asset.fromString(id, "contents");
166 expect(asset.toString(), equals(
167 'String "contents"'));
168 });
169
170 test("truncates the middle of there are more than 40 characters", () {
171 var asset = new Asset.fromString(id,
172 "this is a fairly long string asset content that gets shortened");
173 expect(asset.toString(), equals(
174 'String "this is a fairly lon ... that gets shortened"'));
175 });
176 });
177
178 group("file asset", () {
179 test("shows the file path", () {
180 var asset = new Asset.fromPath(id, "path.txt");
181 expect(asset.toString(), equals('File "path.txt"'));
182 });
183 });
184 });
40 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698