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

Side by Side Diff: tests/standalone/io/file_test.dart

Issue 10392023: Change dart:io to use Future for one-shot operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed a couple of uses in samples. Created 8 years, 7 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Dart test program for testing file I/O. 5 // Dart test program for testing file I/O.
6 6
7 #import("dart:io"); 7 #import("dart:io");
8 #import("dart:isolate"); 8 #import("dart:isolate");
9 9
10 class MyListOfOneElement implements List { 10 class MyListOfOneElement implements List {
11 int _value; 11 int _value;
12 MyListOfOneElement(this._value); 12 MyListOfOneElement(this._value);
13 int get length() => 1; 13 int get length() => 1;
14 operator [](int index) => _value; 14 operator [](int index) => _value;
15 } 15 }
16 16
17 class FileTest { 17 class FileTest {
18 static Directory tempDirectory; 18 static Directory tempDirectory;
19 static int numLiveAsyncTests = 0; 19 static int numLiveAsyncTests = 0;
20 static ReceivePort port;
20 21
21 static void asyncTestStarted() { ++numLiveAsyncTests; } 22 static void asyncTestStarted() { ++numLiveAsyncTests; }
22 static void asyncTestDone(String name) { 23 static void asyncTestDone(String name) {
23 --numLiveAsyncTests; 24 --numLiveAsyncTests;
24 if (numLiveAsyncTests == 0) { 25 if (numLiveAsyncTests == 0) {
25 deleteTempDirectory(); 26 deleteTempDirectory();
27 port.close();
26 } 28 }
27 } 29 }
28 30
29 static void createTempDirectory(Function doNext) { 31 static void createTempDirectory(Function doNext) {
30 tempDirectory = new Directory(''); 32 new Directory('').createTemp().then((temp) {
31 tempDirectory.onError = (e) { 33 tempDirectory = temp;
32 Expect.fail("Failed creating temporary directory"); 34 doNext();
33 }; 35 });
34 tempDirectory.createTemp(doNext);
35 } 36 }
36 37
37 static void deleteTempDirectory() { 38 static void deleteTempDirectory() {
38 tempDirectory.deleteRecursivelySync(); 39 tempDirectory.deleteRecursivelySync();
39 } 40 }
40 41
41 // Test for file read functionality. 42 // Test for file read functionality.
42 static void testReadStream() { 43 static void testReadStream() {
43 // Read a file and check part of it's contents. 44 // Read a file and check part of it's contents.
44 String filename = getFilename("bin/file_test.cc"); 45 String filename = getFilename("bin/file_test.cc");
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 }; 143 };
143 }; 144 };
144 }; 145 };
145 }; 146 };
146 }; 147 };
147 }; 148 };
148 }; 149 };
149 } 150 }
150 151
151 static void testRead() { 152 static void testRead() {
153 ReceivePort port = new ReceivePort();
152 // Read a file and check part of it's contents. 154 // Read a file and check part of it's contents.
153 String filename = getFilename("bin/file_test.cc"); 155 String filename = getFilename("bin/file_test.cc");
154 File file = new File(filename); 156 File file = new File(filename);
155 file.onError = (e) { 157 file.open(FileMode.READ).then((RandomAccessFile file) {
156 Expect.fail("No errors expected : $e");
157 };
158 file.open(FileMode.READ, (RandomAccessFile file) {
159 List<int> buffer = new List<int>(10); 158 List<int> buffer = new List<int>(10);
160 file.readList(buffer, 0, 5, (bytes_read) { 159 file.readList(buffer, 0, 5).then((bytes_read) {
161 Expect.equals(5, bytes_read); 160 Expect.equals(5, bytes_read);
162 file.readList(buffer, 5, 5, (bytes_read) { 161 file.readList(buffer, 5, 5).then((bytes_read) {
163 Expect.equals(5, bytes_read); 162 Expect.equals(5, bytes_read);
164 Expect.equals(47, buffer[0]); // represents '/' in the file. 163 Expect.equals(47, buffer[0]); // represents '/' in the file.
165 Expect.equals(47, buffer[1]); // represents '/' in the file. 164 Expect.equals(47, buffer[1]); // represents '/' in the file.
166 Expect.equals(32, buffer[2]); // represents ' ' in the file. 165 Expect.equals(32, buffer[2]); // represents ' ' in the file.
167 Expect.equals(67, buffer[3]); // represents 'C' in the file. 166 Expect.equals(67, buffer[3]); // represents 'C' in the file.
168 Expect.equals(111, buffer[4]); // represents 'o' in the file. 167 Expect.equals(111, buffer[4]); // represents 'o' in the file.
169 Expect.equals(112, buffer[5]); // represents 'p' in the file. 168 Expect.equals(112, buffer[5]); // represents 'p' in the file.
170 Expect.equals(121, buffer[6]); // represents 'y' in the file. 169 Expect.equals(121, buffer[6]); // represents 'y' in the file.
171 Expect.equals(114, buffer[7]); // represents 'r' in the file. 170 Expect.equals(114, buffer[7]); // represents 'r' in the file.
172 Expect.equals(105, buffer[8]); // represents 'i' in the file. 171 Expect.equals(105, buffer[8]); // represents 'i' in the file.
173 Expect.equals(103, buffer[9]); // represents 'g' in the file. 172 Expect.equals(103, buffer[9]); // represents 'g' in the file.
174 file.close(() => null); 173 file.close().then((ignore) => port.close());
175 }); 174 });
176 }); 175 });
177 }); 176 });
178 } 177 }
179 178
180 static void testReadSync() { 179 static void testReadSync() {
181 // Read a file and check part of it's contents. 180 // Read a file and check part of it's contents.
182 String filename = getFilename("bin/file_test.cc"); 181 String filename = getFilename("bin/file_test.cc");
183 RandomAccessFile file = (new File(filename)).openSync(); 182 RandomAccessFile file = (new File(filename)).openSync();
184 List<int> buffer = new List<int>(42); 183 List<int> buffer = new List<int>(42);
(...skipping 14 matching lines...) Expand all
199 Expect.equals(103, buffer[9]); // represents 'g' in the file. 198 Expect.equals(103, buffer[9]); // represents 'g' in the file.
200 Expect.equals(104, buffer[10]); // represents 'h' in the file. 199 Expect.equals(104, buffer[10]); // represents 'h' in the file.
201 Expect.equals(116, buffer[11]); // represents 't' in the file. 200 Expect.equals(116, buffer[11]); // represents 't' in the file.
202 } 201 }
203 202
204 // Test for file read and write functionality. 203 // Test for file read and write functionality.
205 static void testReadWrite() { 204 static void testReadWrite() {
206 // Read a file. 205 // Read a file.
207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 206 String inFilename = getFilename("tests/vm/data/fixed_length_file");
208 final File file = new File(inFilename); 207 final File file = new File(inFilename);
209 file.onError = (e) { 208 file.open(FileMode.READ).then((openedFile) {
210 Expect.fail("No errors expected : $e");
211 };
212 file.open(FileMode.READ, (RandomAccessFile openedFile) {
213 openedFile.onError = (s) {
214 Expect.fail("No errors expected : $s");
215 };
216 List<int> buffer1 = new List<int>(42); 209 List<int> buffer1 = new List<int>(42);
217 openedFile.readList(buffer1, 0, 42, (bytes_read) { 210 openedFile.readList(buffer1, 0, 42).then((bytes_read) {
218 Expect.equals(42, bytes_read); 211 Expect.equals(42, bytes_read);
219 openedFile.close(() { 212 openedFile.close().then((ignore) {
220 // Write the contents of the file just read into another file. 213 // Write the contents of the file just read into another file.
221 String outFilename = tempDirectory.path + "/out_read_write"; 214 String outFilename = tempDirectory.path + "/out_read_write";
222 final File file2 = new File(outFilename); 215 final File file2 = new File(outFilename);
223 file2.onError = (e) { 216 file2.create().then((ignore) {
224 Expect.fail("No errors expected : $e"); 217 file2.fullPath().then((s) {
225 };
226 file2.create(() {
227 file2.fullPath((s) {
228 Expect.isTrue(new File(s).existsSync()); 218 Expect.isTrue(new File(s).existsSync());
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { 219 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
230 Expect.fail("Not a full path"); 220 Expect.fail("Not a full path");
231 } 221 }
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { 222 file2.open(FileMode.WRITE).then((openedFile2) {
233 openedFile2.onError = (s) { 223 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) {
234 Expect.fail("No errors expected : $s"); 224 openedFile2.close().then((ignore) {
235 };
236 openedFile2.writeList(buffer1, 0, bytes_read);
237 openedFile2.onNoPendingWrites = () {
238 openedFile2.close(() {
239 List<int> buffer2 = new List<int>(bytes_read); 225 List<int> buffer2 = new List<int>(bytes_read);
240 final File file3 = new File(outFilename); 226 final File file3 = new File(outFilename);
241 file3.onError = (e) { 227 file3.open(FileMode.READ).then((openedFile3) {
242 Expect.fail("No errors expected : $e"); 228 openedFile3.readList(buffer2, 0, 42).then((bytes_read) {
243 }; 229 Expect.equals(42, bytes_read);
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { 230 openedFile3.close().then((ignore) {
245 openedFile3.onError = (s) {
246 Expect.fail("No errors expected : $s");
247 };
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) {
249 Expect.equals(42, bytes_read);
250 openedFile3.close(() {
251 // Now compare the two buffers to check if they 231 // Now compare the two buffers to check if they
252 // are identical. 232 // are identical.
253 Expect.equals(buffer1.length, buffer2.length); 233 Expect.equals(buffer1.length, buffer2.length);
254 for (int i = 0; i < buffer1.length; i++) { 234 for (int i = 0; i < buffer1.length; i++) {
255 Expect.equals(buffer1[i], buffer2[i]); 235 Expect.equals(buffer1[i], buffer2[i]);
256 } 236 }
257 // Delete the output file. 237 // Delete the output file.
258 final file4 = file3; 238 final file4 = file3;
259 file4.delete(() { 239 file4.delete().then((ignore) {
260 file4.exists((exists) { 240 file4.exists().then((exists) {
261 Expect.isFalse(exists); 241 Expect.isFalse(exists);
262 asyncTestDone("testReadWrite"); 242 asyncTestDone("testReadWrite");
263 }); 243 });
264 }); 244 });
265 }); 245 });
266 }); 246 });
267 }); 247 });
268 }); 248 });
269 }; 249 });
270 }); 250 });
271 }); 251 });
272 }); 252 });
273 }); 253 });
274 }); 254 });
275 }); 255 });
276 asyncTestStarted(); 256 asyncTestStarted();
277 } 257 }
278 258
279 static void testWriteAppend() { 259 static void testWriteAppend() {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 outStream.close(); 292 outStream.close();
313 outStream.onClosed = () { 293 outStream.onClosed = () {
314 File file2 = new File(filename); 294 File file2 = new File(filename);
315 OutputStream appendingOutput = 295 OutputStream appendingOutput =
316 file2.openOutputStream(FileMode.APPEND); 296 file2.openOutputStream(FileMode.APPEND);
317 appendingOutput.write(buffer); 297 appendingOutput.write(buffer);
318 appendingOutput.onNoPendingWrites = () { 298 appendingOutput.onNoPendingWrites = () {
319 appendingOutput.close(); 299 appendingOutput.close();
320 appendingOutput.onClosed = () { 300 appendingOutput.onClosed = () {
321 File file3 = new File(filename); 301 File file3 = new File(filename);
322 file3.open(FileMode.READ, (RandomAccessFile openedFile) { 302 file3.open(FileMode.READ).then((RandomAccessFile openedFile) {
323 openedFile.length((int length) { 303 openedFile.length().then((int length) {
324 Expect.equals(content.length * 2, length); 304 Expect.equals(content.length * 2, length);
325 openedFile.close(() { 305 openedFile.close().then((ignore) {
326 file3.delete(() { 306 file3.delete().then((ignore) {
327 asyncTestDone("testOutputStreamWriteAppend"); 307 asyncTestDone("testOutputStreamWriteAppend");
328 }); 308 });
329 }); 309 });
330 }); 310 });
331 }); 311 });
332 }; 312 };
333 }; 313 };
334 }; 314 };
335 }; 315 };
336 asyncTestStarted(); 316 asyncTestStarted();
(...skipping 10 matching lines...) Expand all
347 outStream.writeString("abcdABCD"); 327 outStream.writeString("abcdABCD");
348 outStream.writeString("abcdABCD", Encoding.UTF_8); 328 outStream.writeString("abcdABCD", Encoding.UTF_8);
349 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); 329 outStream.writeString("abcdABCD", Encoding.ISO_8859_1);
350 outStream.writeString("abcdABCD", Encoding.ASCII); 330 outStream.writeString("abcdABCD", Encoding.ASCII);
351 outStream.writeString("æøå", Encoding.UTF_8); 331 outStream.writeString("æøå", Encoding.UTF_8);
352 outStream.onNoPendingWrites = () { 332 outStream.onNoPendingWrites = () {
353 outStream.close(); 333 outStream.close();
354 outStream.onClosed = () { 334 outStream.onClosed = () {
355 RandomAccessFile raf = file.openSync(); 335 RandomAccessFile raf = file.openSync();
356 Expect.equals(38, raf.lengthSync()); 336 Expect.equals(38, raf.lengthSync());
337 asyncTestDone("testOutputStreamWriteString");
357 }; 338 };
358 }; 339 };
359 asyncTestStarted(); 340 asyncTestStarted();
360 } 341 }
361 342
362 343
363 static void testReadWriteSync() { 344 static void testReadWriteSync() {
364 // Read a file. 345 // Read a file.
365 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 346 String inFilename = getFilename("tests/vm/data/fixed_length_file");
366 RandomAccessFile file = (new File(inFilename)).openSync(); 347 RandomAccessFile file = (new File(inFilename)).openSync();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 file.createSync(); 385 file.createSync();
405 RandomAccessFile openedFile = file.openSync(); 386 RandomAccessFile openedFile = file.openSync();
406 Expect.equals(-1, openedFile.readByteSync()); 387 Expect.equals(-1, openedFile.readByteSync());
407 openedFile.closeSync(); 388 openedFile.closeSync();
408 file.deleteSync(); 389 file.deleteSync();
409 } 390 }
410 391
411 static void testReadEmptyFile() { 392 static void testReadEmptyFile() {
412 String fileName = tempDirectory.path + "/empty_file"; 393 String fileName = tempDirectory.path + "/empty_file";
413 File file = new File(fileName); 394 File file = new File(fileName);
414 file.onError = (e) {
415 Expect.fail("No errors expected : $e");
416 };
417 asyncTestStarted(); 395 asyncTestStarted();
418 file.create(() { 396 file.create().then((ignore) {
419 file.open(FileMode.READ, (RandomAccessFile openedFile) { 397 file.open(FileMode.READ).then((RandomAccessFile openedFile) {
420 openedFile.readByte((int byte) { 398 var readByteFuture = openedFile.readByte();
399 readByteFuture.then((int byte) {
421 Expect.equals(-1, byte); 400 Expect.equals(-1, byte);
401 asyncTestDone("testReadEmptyFile");
422 }); 402 });
423 openedFile.onError = (e) {
424 Expect.isTrue(e is FileIOException);
425 openedFile.close(() {
426 file.delete(() {
427 asyncTestDone("testReadEmptyFile");
428 });
429 });
430 };
431 }); 403 });
432 }); 404 });
433 } 405 }
434 406
435 // Test for file write of different types of lists. 407 // Test for file write of different types of lists.
436 static void testWriteVariousLists() { 408 static void testWriteVariousLists() {
437 asyncTestStarted(); 409 asyncTestStarted();
438 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; 410 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
439 final File file = new File(fileName); 411 final File file = new File(fileName);
440 file.onError = (e) => Expect.fail("No errors expected : $e"); 412 file.create().then((ignore) {
441 file.create(() { 413 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) {
442 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
443 // Write bytes from 0 to 7. 414 // Write bytes from 0 to 7.
444 openedFile.writeList([0], 0, 1); 415 openedFile.writeList([0], 0, 1);
445 openedFile.writeList(const [1], 0, 1); 416 openedFile.writeList(const [1], 0, 1);
446 openedFile.writeList(new MyListOfOneElement(2), 0, 1); 417 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
447 var x = 12345678901234567890123456789012345678901234567890; 418 var x = 12345678901234567890123456789012345678901234567890;
448 var y = 12345678901234567890123456789012345678901234567893; 419 var y = 12345678901234567890123456789012345678901234567893;
449 openedFile.writeList([y - x], 0, 1); 420 openedFile.writeList([y - x], 0, 1);
450 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. 421 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
451 openedFile.writeList(const [261], 0, 1); 422 openedFile.writeList(const [261], 0, 1);
452 openedFile.writeList(new MyListOfOneElement(262), 0, 1); 423 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
453 x = 12345678901234567890123456789012345678901234567890; 424 x = 12345678901234567890123456789012345678901234567890;
454 y = 12345678901234567890123456789012345678901234568153; 425 y = 12345678901234567890123456789012345678901234568153;
455 openedFile.writeList([y - x], 0, 1); 426 openedFile.writeList([y - x], 0, 1).then((ignore) {
456 427 openedFile.close().then((ignore) {
457 openedFile.onError = (e) => Expect.fail("No errors expected : $e");
458 openedFile.onNoPendingWrites = () {
459 openedFile.close(() {
460 // Check the written bytes. 428 // Check the written bytes.
461 final File file2 = new File(fileName); 429 final File file2 = new File(fileName);
462 var openedFile2 = file2.openSync(); 430 var openedFile2 = file2.openSync();
463 var length = openedFile2.lengthSync(); 431 var length = openedFile2.lengthSync();
464 Expect.equals(8, length); 432 Expect.equals(8, length);
465 List data = new List(length); 433 List data = new List(length);
466 openedFile2.readListSync(data, 0, length); 434 openedFile2.readListSync(data, 0, length);
467 for (var i = 0; i < data.length; i++) { 435 for (var i = 0; i < data.length; i++) {
468 Expect.equals(i, data[i]); 436 Expect.equals(i, data[i]);
469 } 437 }
470 openedFile2.closeSync(); 438 openedFile2.closeSync();
471 file2.deleteSync(); 439 file2.deleteSync();
472 asyncTestDone("testWriteVariousLists"); 440 asyncTestDone("testWriteVariousLists");
473 }); 441 });
474 }; 442 });
475 }); 443 });
476 }); 444 });
477 } 445 }
478 446
479 static void testDirectory() { 447 static void testDirectory() {
480 asyncTestStarted(); 448 asyncTestStarted();
481 449
482 // Port to verify that the test completes. 450 // Port to verify that the test completes.
483 var port = new ReceivePort(); 451 var port = new ReceivePort();
484 port.receive((message, replyTo) { 452 port.receive((message, replyTo) {
485 port.close(); 453 port.close();
486 Expect.equals(1, message); 454 Expect.equals(1, message);
487 asyncTestDone("testDirectory"); 455 asyncTestDone("testDirectory");
488 }); 456 });
489 457
490 var tempDir = tempDirectory.path; 458 var tempDir = tempDirectory.path;
491 var file = new File("${tempDir}/testDirectory"); 459 var file = new File("${tempDir}/testDirectory");
492 var errors = 0; 460 var errors = 0;
493 file.directory((d) => Expect.fail("non-existing file")); 461 var dirFuture = file.directory();
494 file.onError = (e) { 462 dirFuture.then((d) => Expect.fail("non-existing file"));
495 file.onError = (e) => Expect.fail("no error expected"); 463 dirFuture.handleException((e) {
496 file.create(() { 464 file.create().then((ignore) {
497 file.directory((Directory d) { 465 file.directory().then((Directory d) {
498 d.onError = (s) => Expect.fail("no error expected"); 466 d.exists().then((exists) {
499 d.exists((exists) {
500 Expect.isTrue(exists); 467 Expect.isTrue(exists);
501 Expect.isTrue(d.path.endsWith(tempDir)); 468 Expect.isTrue(d.path.endsWith(tempDir));
502 file.delete(() { 469 file.delete().then((ignore) {
503 var file_dir = new File("."); 470 var fileDir = new File(".");
504 file_dir.directory((d) => Expect.fail("non-existing file")); 471 var dirFuture2 = fileDir.directory();
505 file_dir.onError = (e) { 472 dirFuture2.then((d) => Expect.fail("non-existing file"));
506 var file_dir = new File(tempDir); 473 dirFuture2.handleException((e) {
507 file_dir.directory((d) => Expect.fail("non-existing file")); 474 var fileDir = new File(tempDir);
508 file_dir.onError = (e) => port.toSendPort().send(1); 475 var dirFuture3 = fileDir.directory();
509 }; 476 dirFuture3.then((d) => Expect.fail("non-existing file"));
477 dirFuture3.handleException((e) {
478 port.toSendPort().send(1);
479 return true;
480 });
481 return true;
482 });
510 }); 483 });
511 }); 484 });
512 }); 485 });
513 }); 486 });
514 }; 487 return true;
488 });
515 } 489 }
516 490
517 static void testDirectorySync() { 491 static void testDirectorySync() {
518 var tempDir = tempDirectory.path; 492 var tempDir = tempDirectory.path;
519 var file = new File("${tempDir}/testDirectorySync"); 493 var file = new File("${tempDir}/testDirectorySync");
520 // Non-existing file should throw exception. 494 // Non-existing file should throw exception.
521 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); 495 Expect.throws(file.directorySync, (e) { return e is FileIOException; });
522 file.createSync(); 496 file.createSync();
523 // Check that the path of the returned directory is the temp directory. 497 // Check that the path of the returned directory is the temp directory.
524 Directory d = file.directorySync(); 498 Directory d = file.directorySync();
525 Expect.isTrue(d.existsSync()); 499 Expect.isTrue(d.existsSync());
526 Expect.isTrue(d.path.endsWith(tempDir)); 500 Expect.isTrue(d.path.endsWith(tempDir));
527 file.deleteSync(); 501 file.deleteSync();
528 // Directories should throw exception. 502 // Directories should throw exception.
529 var file_dir = new File("."); 503 var file_dir = new File(".");
530 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 504 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
531 file_dir = new File(tempDir); 505 file_dir = new File(tempDir);
532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 506 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
533 } 507 }
534 508
535 // Test for file length functionality. 509 // Test for file length functionality.
536 static void testLength() { 510 static void testLength() {
511 var port = new ReceivePort();
537 String filename = getFilename("tests/vm/data/fixed_length_file"); 512 String filename = getFilename("tests/vm/data/fixed_length_file");
538 File file = new File(filename); 513 File file = new File(filename);
539 RandomAccessFile openedFile = file.openSync(); 514 RandomAccessFile openedFile = file.openSync();
540 openedFile.onError = (e) => Expect.fail("No errors expected"); 515 openedFile.length().then((length) {
541 file.onError = (e) => Expect.fail("No errors expected");
542 openedFile.length((length) {
543 Expect.equals(42, length); 516 Expect.equals(42, length);
544 openedFile.close(() => null); 517 openedFile.close().then((ignore) => port.close());
545 }); 518 });
546 file.length((length) { 519 file.length().then((length) {
547 Expect.equals(42, length); 520 Expect.equals(42, length);
548 }); 521 });
549 } 522 }
550 523
551 static void testLengthSync() { 524 static void testLengthSync() {
552 String filename = getFilename("tests/vm/data/fixed_length_file"); 525 String filename = getFilename("tests/vm/data/fixed_length_file");
553 File file = new File(filename); 526 File file = new File(filename);
554 RandomAccessFile openedFile = file.openSync(); 527 RandomAccessFile openedFile = file.openSync();
555 Expect.equals(42, file.lengthSync()); 528 Expect.equals(42, file.lengthSync());
556 Expect.equals(42, openedFile.lengthSync()); 529 Expect.equals(42, openedFile.lengthSync());
557 openedFile.closeSync(); 530 openedFile.closeSync();
558 } 531 }
559 532
560 // Test for file position functionality. 533 // Test for file position functionality.
561 static void testPosition() { 534 static void testPosition() {
535 var port = new ReceivePort();
562 String filename = getFilename("tests/vm/data/fixed_length_file"); 536 String filename = getFilename("tests/vm/data/fixed_length_file");
563 RandomAccessFile input = (new File(filename)).openSync(); 537 RandomAccessFile input = (new File(filename)).openSync();
564 input.onError = (e) => Expect.fail("No errors expected"); 538 input.position().then((position) {
565 input.position((position) {
566 Expect.equals(0, position); 539 Expect.equals(0, position);
567 List<int> buffer = new List<int>(100); 540 List<int> buffer = new List<int>(100);
568 input.readList(buffer, 0, 12, (bytes_read) { 541 input.readList(buffer, 0, 12).then((bytes_read) {
569 input.position((position) { 542 input.position().then((position) {
570 Expect.equals(12, position); 543 Expect.equals(12, position);
571 input.readList(buffer, 12, 6, (bytes_read) { 544 input.readList(buffer, 12, 6).then((bytes_read) {
572 input.position((position) { 545 input.position().then((position) {
573 Expect.equals(18, position); 546 Expect.equals(18, position);
574 input.setPosition(8, () { 547 input.setPosition(8).then((ignore) {
575 input.position((position) { 548 input.position().then((position) {
576 Expect.equals(8, position); 549 Expect.equals(8, position);
577 input.close(() => null); 550 input.close().then((ignore) => port.close());
578 }); 551 });
579 }); 552 });
580 }); 553 });
581 }); 554 });
582 }); 555 });
583 }); 556 });
584 }); 557 });
585 } 558 }
586 559
587 static void testPositionSync() { 560 static void testPositionSync() {
588 String filename = getFilename("tests/vm/data/fixed_length_file"); 561 String filename = getFilename("tests/vm/data/fixed_length_file");
589 RandomAccessFile input = (new File(filename)).openSync(); 562 RandomAccessFile input = (new File(filename)).openSync();
590 Expect.equals(0, input.positionSync()); 563 Expect.equals(0, input.positionSync());
591 List<int> buffer = new List<int>(100); 564 List<int> buffer = new List<int>(100);
592 input.readListSync(buffer, 0, 12); 565 input.readListSync(buffer, 0, 12);
593 Expect.equals(12, input.positionSync()); 566 Expect.equals(12, input.positionSync());
594 input.readListSync(buffer, 12, 6); 567 input.readListSync(buffer, 12, 6);
595 Expect.equals(18, input.positionSync()); 568 Expect.equals(18, input.positionSync());
596 input.setPositionSync(8); 569 input.setPositionSync(8);
597 Expect.equals(8, input.positionSync()); 570 Expect.equals(8, input.positionSync());
598 input.closeSync(); 571 input.closeSync();
599 } 572 }
600 573
601 static void testTruncate() { 574 static void testTruncate() {
602 File file = new File(tempDirectory.path + "/out_truncate"); 575 File file = new File(tempDirectory.path + "/out_truncate");
603 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 576 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
604 file.onError = (e) => Expect.fail("No errors expected: $e"); 577 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) {
605 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { 578 openedFile.writeList(buffer, 0, 10).then((ignore) {
606 openedFile.writeList(buffer, 0, 10); 579 openedFile.length().then((length) {
607 openedFile.onNoPendingWrites = () {
608 openedFile.length((length) {
609 Expect.equals(10, length); 580 Expect.equals(10, length);
610 openedFile.truncate(5, () { 581 openedFile.truncate(5).then((ignore) {
611 openedFile.length((length) { 582 openedFile.length().then((length) {
612 Expect.equals(5, length); 583 Expect.equals(5, length);
613 openedFile.close(() { 584 openedFile.close().then((ignore) {
614 file.delete(() { 585 file.delete().then((ignore) {
615 file.exists((exists) { 586 file.exists().then((exists) {
616 Expect.isFalse(exists); 587 Expect.isFalse(exists);
617 asyncTestDone("testTruncate"); 588 asyncTestDone("testTruncate");
618 }); 589 });
619 }); 590 });
620 }); 591 });
621 }); 592 });
622 }); 593 });
623 }); 594 });
624 }; 595 });
625 }); 596 });
626 asyncTestStarted(); 597 asyncTestStarted();
627 } 598 }
628 599
629 static void testTruncateSync() { 600 static void testTruncateSync() {
630 File file = new File(tempDirectory.path + "/out_truncate_sync"); 601 File file = new File(tempDirectory.path + "/out_truncate_sync");
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 602 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
632 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 603 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
633 openedFile.writeListSync(buffer, 0, 10); 604 openedFile.writeListSync(buffer, 0, 10);
634 Expect.equals(10, openedFile.lengthSync()); 605 Expect.equals(10, openedFile.lengthSync());
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 wrongExceptionCaught = true; 813 wrongExceptionCaught = true;
843 } 814 }
844 Expect.equals(true, exceptionCaught); 815 Expect.equals(true, exceptionCaught);
845 Expect.equals(true, !wrongExceptionCaught); 816 Expect.equals(true, !wrongExceptionCaught);
846 openedFile.closeSync(); 817 openedFile.closeSync();
847 file.deleteSync(); 818 file.deleteSync();
848 } 819 }
849 820
850 static void testOpenDirectoryAsFile() { 821 static void testOpenDirectoryAsFile() {
851 var f = new File('.'); 822 var f = new File('.');
852 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file')); 823 var future = f.open(FileMode.READ);
853 f.onError = (e) => null; 824 future.then((r) => Expect.fail('Directory opened as file'));
825 future.handleException((e) => true);
854 } 826 }
855 827
856 static void testOpenDirectoryAsFileSync() { 828 static void testOpenDirectoryAsFileSync() {
857 var f = new File('.'); 829 var f = new File('.');
858 try { 830 try {
859 f.openSync(); 831 f.openSync();
860 Expect.fail("Expected exception opening directory as file"); 832 Expect.fail("Expected exception opening directory as file");
861 } catch (var e) { 833 } catch (var e) {
862 Expect.isTrue(e is FileIOException); 834 Expect.isTrue(e is FileIOException);
863 } 835 }
864 } 836 }
865 837
866 static void testReadAsBytes() { 838 static void testReadAsBytes() {
867 var port = new ReceivePort(); 839 var port = new ReceivePort();
868 port.receive((result, replyTo) { 840 port.receive((result, replyTo) {
869 port.close(); 841 port.close();
870 Expect.equals(42, result); 842 Expect.equals(42, result);
871 }); 843 });
872 var name = getFilename("tests/vm/data/fixed_length_file"); 844 var name = getFilename("tests/vm/data/fixed_length_file");
873 var f = new File(name); 845 var f = new File(name);
874 f.readAsBytes((bytes) { 846 f.readAsBytes().then((bytes) {
875 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); 847 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
876 port.toSendPort().send(bytes.length); 848 port.toSendPort().send(bytes.length);
877 }); 849 });
878 f.onError = (e) => Expect.fail("No errors expected: $e");
879 } 850 }
880 851
881 static void testReadAsBytesSync() { 852 static void testReadAsBytesSync() {
882 var name = getFilename("tests/vm/data/fixed_length_file"); 853 var name = getFilename("tests/vm/data/fixed_length_file");
883 var bytes = new File(name).readAsBytesSync(); 854 var bytes = new File(name).readAsBytesSync();
884 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); 855 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
885 Expect.equals(bytes.length, 42); 856 Expect.equals(bytes.length, 42);
886 } 857 }
887 858
888 static void testReadAsText() { 859 static void testReadAsText() {
889 var port = new ReceivePort(); 860 var port = new ReceivePort();
890 port.receive((result, replyTo) { 861 port.receive((result, replyTo) {
891 port.close(); 862 port.close();
892 Expect.equals(1, result); 863 Expect.equals(1, result);
893 }); 864 });
894 var name = getFilename("tests/vm/data/fixed_length_file"); 865 var name = getFilename("tests/vm/data/fixed_length_file");
895 var f = new File(name); 866 var f = new File(name);
896 f.readAsText(Encoding.UTF_8, (text) { 867 f.readAsText(Encoding.UTF_8).then((text) {
897 Expect.isTrue(text.endsWith("42 bytes.")); 868 Expect.isTrue(text.endsWith("42 bytes."));
898 Expect.equals(42, text.length); 869 Expect.equals(42, text.length);
899 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); 870 var name = getDataFilename("tests/standalone/io/read_as_text.dat");
900 var f = new File(name); 871 var f = new File(name);
901 f.onError = (e) => Expect.fail("No errors expected: $e"); 872 f.readAsText(Encoding.UTF_8).then((text) {
902 f.readAsText(Encoding.UTF_8, (text) {
903 Expect.equals(6, text.length); 873 Expect.equals(6, text.length);
904 var expected = [955, 120, 46, 32, 120, 10]; 874 var expected = [955, 120, 46, 32, 120, 10];
905 Expect.listEquals(expected, text.charCodes()); 875 Expect.listEquals(expected, text.charCodes());
906 f.readAsText(Encoding.ISO_8859_1, (text) { 876 f.readAsText(Encoding.ISO_8859_1).then((text) {
907 Expect.equals(7, text.length); 877 Expect.equals(7, text.length);
908 var expected = [206, 187, 120, 46, 32, 120, 10]; 878 var expected = [206, 187, 120, 46, 32, 120, 10];
909 Expect.listEquals(expected, text.charCodes()); 879 Expect.listEquals(expected, text.charCodes());
910 f.onError = (e) { 880 var readAsTextFuture = f.readAsText(Encoding.ASCII);
881 readAsTextFuture.then((text) {
882 Expect.fail("Non-ascii char should cause error");
883 });
884 readAsTextFuture.handleException((e) {
911 port.toSendPort().send(1); 885 port.toSendPort().send(1);
912 }; 886 return true;
913 f.readAsText(Encoding.ASCII, (text) {
914 Expect.fail("Non-ascii char should cause error");
915 }); 887 });
916 }); 888 });
917 }); 889 });
918 }); 890 });
919 f.onError = (e) {
920 Expect.fail("No errors expected: $e");
921 };
922 } 891 }
923 892
924 static void testReadAsTextSync() { 893 static void testReadAsTextSync() {
925 var name = getFilename("tests/vm/data/fixed_length_file"); 894 var name = getFilename("tests/vm/data/fixed_length_file");
926 var text = new File(name).readAsTextSync(); 895 var text = new File(name).readAsTextSync();
927 Expect.isTrue(text.endsWith("42 bytes.")); 896 Expect.isTrue(text.endsWith("42 bytes."));
928 Expect.equals(42, text.length); 897 Expect.equals(42, text.length);
929 name = getDataFilename("tests/standalone/io/read_as_text.dat"); 898 name = getDataFilename("tests/standalone/io/read_as_text.dat");
930 text = new File(name).readAsTextSync(); 899 text = new File(name).readAsTextSync();
931 Expect.equals(6, text.length); 900 Expect.equals(6, text.length);
932 var expected = [955, 120, 46, 32, 120, 10]; 901 var expected = [955, 120, 46, 32, 120, 10];
933 Expect.listEquals(expected, text.charCodes()); 902 Expect.listEquals(expected, text.charCodes());
934 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); 903 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); });
935 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); 904 text = new File(name).readAsTextSync(Encoding.ISO_8859_1);
936 expected = [206, 187, 120, 46, 32, 120, 10]; 905 expected = [206, 187, 120, 46, 32, 120, 10];
937 Expect.equals(7, text.length); 906 Expect.equals(7, text.length);
938 Expect.listEquals(expected, text.charCodes()); 907 Expect.listEquals(expected, text.charCodes());
939 } 908 }
940 909
941 static void testReadAsLines() { 910 static void testReadAsLines() {
942 var port = new ReceivePort(); 911 var port = new ReceivePort();
943 port.receive((result, replyTo) { 912 port.receive((result, replyTo) {
944 port.close(); 913 port.close();
945 Expect.equals(42, result); 914 Expect.equals(42, result);
946 }); 915 });
947 var name = getFilename("tests/vm/data/fixed_length_file"); 916 var name = getFilename("tests/vm/data/fixed_length_file");
948 var f = new File(name); 917 var f = new File(name);
949 f.readAsLines(Encoding.UTF_8, (lines) { 918 f.readAsLines(Encoding.UTF_8).then((lines) {
950 Expect.equals(1, lines.length); 919 Expect.equals(1, lines.length);
951 var line = lines[0]; 920 var line = lines[0];
952 Expect.isTrue(line.endsWith("42 bytes.")); 921 Expect.isTrue(line.endsWith("42 bytes."));
953 port.toSendPort().send(line.length); 922 port.toSendPort().send(line.length);
954 }); 923 });
955 f.onError = (e) => Expect.fail("No errors expected: $e");
956 } 924 }
957 925
958 static void testReadAsLinesSync() { 926 static void testReadAsLinesSync() {
959 var name = getFilename("tests/vm/data/fixed_length_file"); 927 var name = getFilename("tests/vm/data/fixed_length_file");
960 var lines = new File(name).readAsLinesSync(); 928 var lines = new File(name).readAsLinesSync();
961 Expect.equals(1, lines.length); 929 Expect.equals(1, lines.length);
962 var line = lines[0]; 930 var line = lines[0];
963 Expect.isTrue(line.endsWith("42 bytes.")); 931 Expect.isTrue(line.endsWith("42 bytes."));
964 Expect.equals(42, line.length); 932 Expect.equals(42, line.length);
965 name = getDataFilename("tests/standalone/io/readline_test1.dat"); 933 name = getDataFilename("tests/standalone/io/readline_test1.dat");
966 lines = new File(name).readAsLinesSync(); 934 lines = new File(name).readAsLinesSync();
967 Expect.equals(10, lines.length); 935 Expect.equals(10, lines.length);
968 } 936 }
969 937
970 938
971 static void testReadAsErrors() { 939 static void testReadAsErrors() {
972 var port = new ReceivePort(); 940 var port = new ReceivePort();
973 port.receive((message, _) { 941 port.receive((message, _) {
974 port.close(); 942 port.close();
975 Expect.equals(1, message); 943 Expect.equals(1, message);
976 }); 944 });
977 var f = new File('.'); 945 var f = new File('.');
978 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); 946 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
979 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); 947 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
980 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); 948 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
981 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); 949 var readAsBytesFuture = f.readAsBytes();
982 f.onError = (e) { 950 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected"));
983 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); 951 readAsBytesFuture.handleException((e) {
984 f.onError = (e) { 952 var readAsTextFuture = f.readAsText(Encoding.UTF_8);
985 f.readAsLines(Encoding.UTF_8, 953 readAsTextFuture.then((text) => Expect.fail("no text expected"));
986 (lines) => Expect.fail("no lines expected")); 954 readAsTextFuture.handleException((e) {
987 f.onError = (e) => port.toSendPort().send(1); 955 var readAsLinesFuture = f.readAsLines(Encoding.UTF_8);
988 }; 956 readAsLinesFuture.then((lines) => Expect.fail("no lines expected"));
989 }; 957 readAsLinesFuture.handleException((e) {
958 port.toSendPort().send(1);
959 return true;
960 });
961 return true;
962 });
963 return true;
964 });
990 } 965 }
991 966
992 // Test that opens the same file for writing then for appending to test 967 // Test that opens the same file for writing then for appending to test
993 // that the file is not truncated when opened for appending. 968 // that the file is not truncated when opened for appending.
994 static void testAppend() { 969 static void testAppend() {
995 var file = new File('${tempDirectory.path}/out_append'); 970 var file = new File('${tempDirectory.path}/out_append');
996 file.open(FileMode.WRITE, (openedFile) { 971 file.open(FileMode.WRITE).then((openedFile) {
997 openedFile.writeString("asdf"); 972 openedFile.writeString("asdf").then((ignore) {
998 openedFile.onNoPendingWrites = () { 973 openedFile.close().then((ignore) {
999 openedFile.close(() { 974 file.open(FileMode.APPEND).then((openedFile) {
1000 file.open(FileMode.APPEND, (openedFile) { 975 openedFile.length().then((length) {
1001 openedFile.length((length) {
1002 Expect.equals(4, length); 976 Expect.equals(4, length);
1003 openedFile.writeString("asdf"); 977 openedFile.writeString("asdf").then((ignore) {
1004 openedFile.onNoPendingWrites = () { 978 openedFile.length().then((length) {
1005 openedFile.length((length) {
1006 Expect.equals(8, length); 979 Expect.equals(8, length);
1007 openedFile.close(() { 980 openedFile.close().then((ignore) {
1008 file.delete(() { 981 file.delete().then((ignore) {
1009 file.exists((exists) { 982 file.exists().then((exists) {
1010 Expect.isFalse(exists); 983 Expect.isFalse(exists);
1011 asyncTestDone("testAppend"); 984 asyncTestDone("testAppend");
1012 }); 985 });
1013 }); 986 });
1014 }); 987 });
1015 }); 988 });
1016 }; 989 });
1017 }); 990 });
1018 }); 991 });
1019 }); 992 });
1020 }; 993 });
1021 }); 994 });
1022 asyncTestStarted(); 995 asyncTestStarted();
1023 } 996 }
1024 997
1025 static void testAppendSync() { 998 static void testAppendSync() {
1026 var file = new File('${tempDirectory.path}/out_append_sync'); 999 var file = new File('${tempDirectory.path}/out_append_sync');
1027 var openedFile = file.openSync(FileMode.WRITE); 1000 var openedFile = file.openSync(FileMode.WRITE);
1028 openedFile.writeStringSync("asdf"); 1001 openedFile.writeStringSync("asdf");
1029 Expect.equals(4, openedFile.lengthSync()); 1002 Expect.equals(4, openedFile.lengthSync());
1030 openedFile.closeSync(); 1003 openedFile.closeSync();
1031 openedFile = file.openSync(FileMode.WRITE); 1004 openedFile = file.openSync(FileMode.WRITE);
1032 openedFile.setPositionSync(4); 1005 openedFile.setPositionSync(4);
1033 openedFile.writeStringSync("asdf"); 1006 openedFile.writeStringSync("asdf");
1034 Expect.equals(8, openedFile.lengthSync()); 1007 Expect.equals(8, openedFile.lengthSync());
1035 openedFile.closeSync(); 1008 openedFile.closeSync();
1036 file.deleteSync(); 1009 file.deleteSync();
1037 Expect.isFalse(file.existsSync()); 1010 Expect.isFalse(file.existsSync());
1038 } 1011 }
1039 1012
1040 // Helper method to be able to run the test from the runtime 1013 // Helper method to be able to run the test from the runtime
1041 // directory, or the top directory. 1014 // directory, or the top directory.
1042 static String getFilename(String path) => 1015 static String getFilename(String path) =>
1043 new File(path).existsSync() ? path : 'runtime/' + path; 1016 new File(path).existsSync() ? path : 'runtime/' + path;
1044 1017
1045 static String getDataFilename(String path) => 1018 static String getDataFilename(String path) =>
1046 new File(path).existsSync() ? path : '../' + path; 1019 new File(path).existsSync() ? path : '../' + path;
1047 1020
1048 // Main test entrypoint. 1021 // Main test entrypoint.
1049 static testMain() { 1022 static testMain() {
1023 port = new ReceivePort();
1050 testRead(); 1024 testRead();
1051 testReadSync(); 1025 testReadSync();
1052 testReadStream(); 1026 testReadStream();
1053 testLength(); 1027 testLength();
1054 testLengthSync(); 1028 testLengthSync();
1055 testPosition(); 1029 testPosition();
1056 testPositionSync(); 1030 testPositionSync();
1057 testOpenDirectoryAsFile(); 1031 testOpenDirectoryAsFile();
1058 testOpenDirectoryAsFileSync(); 1032 testOpenDirectoryAsFileSync();
1059 testReadAsBytes(); 1033 testReadAsBytes();
(...skipping 23 matching lines...) Expand all
1083 testWriteVariousLists(); 1057 testWriteVariousLists();
1084 testDirectory(); 1058 testDirectory();
1085 testDirectorySync(); 1059 testDirectorySync();
1086 }); 1060 });
1087 } 1061 }
1088 } 1062 }
1089 1063
1090 main() { 1064 main() {
1091 FileTest.testMain(); 1065 FileTest.testMain();
1092 } 1066 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698