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

Side by Side Diff: tests/standalone/src/FileTest.dart

Issue 9572005: Move all tests using dart:io to a separate directory (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 years, 9 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Dart test program for testing file I/O.
6
7 #import("dart:io");
8 #import("dart:isolate");
9
10 class MyListOfOneElement implements List {
11 int _value;
12 MyListOfOneElement(this._value);
13 int get length() => 1;
14 operator [](int index) => _value;
15 }
16
17 class FileTest {
18 static Directory tempDirectory;
19 static int numLiveAsyncTests = 0;
20
21 static void asyncTestStarted() { ++numLiveAsyncTests; }
22 static void asyncTestDone(String name) {
23 --numLiveAsyncTests;
24 if (numLiveAsyncTests == 0) {
25 deleteTempDirectory();
26 }
27 }
28
29 static void createTempDirectory(Function doNext) {
30 tempDirectory = new Directory('');
31 tempDirectory.onError = (e) {
32 Expect.fail("Failed creating temporary directory");
33 };
34 tempDirectory.createTemp(doNext);
35 }
36
37 static void deleteTempDirectory() {
38 tempDirectory.deleteRecursivelySync();
39 }
40
41 // Test for file read functionality.
42 static void testReadStream() {
43 // Read a file and check part of it's contents.
44 String filename = getFilename("bin/file_test.cc");
45 File file = new File(filename);
46 InputStream input = file.openInputStream();
47 input.onData = () {
48 List<int> buffer = new List<int>(42);
49 int bytesRead = input.readInto(buffer, 0, 12);
50 Expect.equals(12, bytesRead);
51 bytesRead = input.readInto(buffer, 12, 30);
52 input.close();
53 Expect.equals(30, bytesRead);
54 Expect.equals(47, buffer[0]); // represents '/' in the file.
55 Expect.equals(47, buffer[1]); // represents '/' in the file.
56 Expect.equals(32, buffer[2]); // represents ' ' in the file.
57 Expect.equals(67, buffer[3]); // represents 'C' in the file.
58 Expect.equals(111, buffer[4]); // represents 'o' in the file.
59 Expect.equals(112, buffer[5]); // represents 'p' in the file.
60 Expect.equals(121, buffer[6]); // represents 'y' in the file.
61 Expect.equals(114, buffer[7]); // represents 'r' in the file.
62 Expect.equals(105, buffer[8]); // represents 'i' in the file.
63 Expect.equals(103, buffer[9]); // represents 'g' in the file.
64 Expect.equals(104, buffer[10]); // represents 'h' in the file.
65 Expect.equals(116, buffer[11]); // represents 't' in the file.
66 };
67 }
68
69 // Test for file read and write functionality.
70 static void testReadWriteStream() {
71 asyncTestStarted();
72
73 // Read a file.
74 String inFilename = getFilename("tests/vm/data/fixed_length_file");
75 File file;
76 InputStream input;
77 int bytesRead;
78
79 // Test reading all using readInto.
80 file = new File(inFilename);
81 input = file.openInputStream();
82 input.onData = () {
83 List<int> buffer1 = new List<int>(42);
84 bytesRead = input.readInto(buffer1, 0, 42);
85 Expect.equals(42, bytesRead);
86 Expect.isTrue(input.closed);
87
88 // Test reading all using readInto and read.
89 file = new File(inFilename);
90 input = file.openInputStream();
91 input.onData = () {
92 bytesRead = input.readInto(buffer1, 0, 21);
93 Expect.equals(21, bytesRead);
94 buffer1 = input.read();
95 Expect.equals(21, buffer1.length);
96 Expect.isTrue(input.closed);
97
98 // Test reading all using read and readInto.
99 file = new File(inFilename);
100 input = file.openInputStream();
101 input.onData = () {
102 buffer1 = input.read(21);
103 Expect.equals(21, buffer1.length);
104 bytesRead = input.readInto(buffer1, 0, 21);
105 Expect.equals(21, bytesRead);
106 Expect.isTrue(input.closed);
107
108 // Test reading all using read.
109 file = new File(inFilename);
110 input = file.openInputStream();
111 input.onData = () {
112 buffer1 = input.read();
113 Expect.equals(42, buffer1.length);
114 Expect.isTrue(input.closed);
115
116 // Write the contents of the file just read into another file.
117 String outFilename = tempDirectory.path + "/out_read_write_stream";
118 file = new File(outFilename);
119 OutputStream output = file.openOutputStream();
120 bool writeDone = output.writeFrom(buffer1, 0, 42);
121 Expect.equals(false, writeDone);
122 output.onNoPendingWrites = () {
123 output.close();
124 output.onClosed = () {
125 // Now read the contents of the file just written.
126 List<int> buffer2 = new List<int>(42);
127 file = new File(outFilename);
128 input = file.openInputStream();
129 input.onData = () {
130 bytesRead = input.readInto(buffer2, 0, 42);
131 Expect.equals(42, bytesRead);
132 // Now compare the two buffers to check if they are identical.
133 for (int i = 0; i < buffer1.length; i++) {
134 Expect.equals(buffer1[i], buffer2[i]);
135 }
136 };
137 input.onClosed = () {
138 // Delete the output file.
139 file.deleteSync();
140 Expect.isFalse(file.existsSync());
141 asyncTestDone("testReadWriteStream");
142 };
143 };
144 };
145 };
146 };
147 };
148 };
149 }
150
151 static void testRead() {
152 // Read a file and check part of it's contents.
153 String filename = getFilename("bin/file_test.cc");
154 File file = new File(filename);
155 file.onError = (s) {
156 Expect.fail("No errors expected : $s");
157 };
158 file.open(FileMode.READ, (RandomAccessFile file) {
159 List<int> buffer = new List<int>(10);
160 file.readList(buffer, 0, 5, (bytes_read) {
161 Expect.equals(5, bytes_read);
162 file.readList(buffer, 5, 5, (bytes_read) {
163 Expect.equals(5, bytes_read);
164 Expect.equals(47, buffer[0]); // represents '/' in the file.
165 Expect.equals(47, buffer[1]); // represents '/' in the file.
166 Expect.equals(32, buffer[2]); // represents ' ' in the file.
167 Expect.equals(67, buffer[3]); // represents 'C' in the file.
168 Expect.equals(111, buffer[4]); // represents 'o' in the file.
169 Expect.equals(112, buffer[5]); // represents 'p' in the file.
170 Expect.equals(121, buffer[6]); // represents 'y' in the file.
171 Expect.equals(114, buffer[7]); // represents 'r' in the file.
172 Expect.equals(105, buffer[8]); // represents 'i' in the file.
173 Expect.equals(103, buffer[9]); // represents 'g' in the file.
174 file.close(() => null);
175 });
176 });
177 });
178 }
179
180 static void testReadSync() {
181 // Read a file and check part of it's contents.
182 String filename = getFilename("bin/file_test.cc");
183 RandomAccessFile file = (new File(filename)).openSync();
184 List<int> buffer = new List<int>(42);
185 int bytes_read = 0;
186 bytes_read = file.readListSync(buffer, 0, 12);
187 Expect.equals(12, bytes_read);
188 bytes_read = file.readListSync(buffer, 12, 30);
189 Expect.equals(30, bytes_read);
190 Expect.equals(47, buffer[0]); // represents '/' in the file.
191 Expect.equals(47, buffer[1]); // represents '/' in the file.
192 Expect.equals(32, buffer[2]); // represents ' ' in the file.
193 Expect.equals(67, buffer[3]); // represents 'C' in the file.
194 Expect.equals(111, buffer[4]); // represents 'o' in the file.
195 Expect.equals(112, buffer[5]); // represents 'p' in the file.
196 Expect.equals(121, buffer[6]); // represents 'y' in the file.
197 Expect.equals(114, buffer[7]); // represents 'r' in the file.
198 Expect.equals(105, buffer[8]); // represents 'i' in the file.
199 Expect.equals(103, buffer[9]); // represents 'g' in the file.
200 Expect.equals(104, buffer[10]); // represents 'h' in the file.
201 Expect.equals(116, buffer[11]); // represents 't' in the file.
202 }
203
204 // Test for file read and write functionality.
205 static void testReadWrite() {
206 // Read a file.
207 String inFilename = getFilename("tests/vm/data/fixed_length_file");
208 final File file = new File(inFilename);
209 file.onError = (s) {
210 Expect.fail("No errors expected : $s");
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);
217 openedFile.readList(buffer1, 0, 42, (bytes_read) {
218 Expect.equals(42, bytes_read);
219 openedFile.close(() {
220 // Write the contents of the file just read into another file.
221 String outFilename = tempDirectory.path + "/out_read_write";
222 final File file2 = new File(outFilename);
223 file2.onError = (s) {
224 Expect.fail("No errors expected : $s");
225 };
226 file2.create(() {
227 file2.fullPath((s) {
228 Expect.isTrue(new File(s).existsSync());
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
230 Expect.fail("Not a full path");
231 }
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) {
233 openedFile2.onError = (s) {
234 Expect.fail("No errors expected : $s");
235 };
236 openedFile2.writeList(buffer1, 0, bytes_read);
237 openedFile2.onNoPendingWrites = () {
238 openedFile2.close(() {
239 List<int> buffer2 = new List<int>(bytes_read);
240 final File file3 = new File(outFilename);
241 file3.onError = (s) {
242 Expect.fail("No errors expected : $s");
243 };
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) {
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
252 // are identical.
253 Expect.equals(buffer1.length, buffer2.length);
254 for (int i = 0; i < buffer1.length; i++) {
255 Expect.equals(buffer1[i], buffer2[i]);
256 }
257 // Delete the output file.
258 final file4 = file3;
259 file4.delete(() {
260 file4.exists((exists) {
261 Expect.isFalse(exists);
262 asyncTestDone("testReadWrite");
263 });
264 });
265 });
266 });
267 });
268 });
269 };
270 });
271 });
272 });
273 });
274 });
275 });
276 asyncTestStarted();
277 }
278
279 static void testWriteAppend() {
280 String content = "foobar";
281 String filename = tempDirectory.path + "/write_append";
282 File file = new File(filename);
283 file.createSync();
284 Expect.isTrue(new File(filename).existsSync());
285 List<int> buffer = content.charCodes();
286 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
287 openedFile.writeListSync(buffer, 0, buffer.length);
288 openedFile.closeSync();
289 // Reopen the file in write mode to ensure that we overwrite the content.
290 openedFile = (new File(filename)).openSync(FileMode.WRITE);
291 openedFile.writeListSync(buffer, 0, buffer.length);
292 Expect.equals(content.length, openedFile.lengthSync());
293 openedFile.closeSync();
294 // Open the file in append mode and ensure that we do not overwrite
295 // the existing content.
296 openedFile = (new File(filename)).openSync(FileMode.APPEND);
297 openedFile.writeListSync(buffer, 0, buffer.length);
298 Expect.equals(content.length * 2, openedFile.lengthSync());
299 openedFile.closeSync();
300 file.deleteSync();
301 }
302
303 static void testOutputStreamWriteAppend() {
304 String content = "foobar";
305 String filename = tempDirectory.path + "/outstream_write_append";
306 File file = new File(filename);
307 file.createSync();
308 List<int> buffer = content.charCodes();
309 OutputStream outStream = file.openOutputStream();
310 outStream.write(buffer);
311 outStream.onNoPendingWrites = () {
312 outStream.close();
313 File file2 = new File(filename);
314 OutputStream appendingOutput =
315 file2.openOutputStream(FileMode.APPEND);
316 appendingOutput.write(buffer);
317 appendingOutput.onNoPendingWrites = () {
318 appendingOutput.close();
319 File file3 = new File(filename);
320 file3.open(FileMode.READ, (RandomAccessFile openedFile) {
321 openedFile.length((int length) {
322 Expect.equals(content.length * 2, length);
323 openedFile.close(() {
324 file3.delete(() {
325 asyncTestDone("testOutputStreamWriteAppend");
326 });
327 });
328 });
329 });
330 };
331 };
332 asyncTestStarted();
333 }
334
335
336 static void testReadWriteSync() {
337 // Read a file.
338 String inFilename = getFilename("tests/vm/data/fixed_length_file");
339 RandomAccessFile file = (new File(inFilename)).openSync();
340 List<int> buffer1 = new List<int>(42);
341 int bytes_read = 0;
342 int bytes_written = 0;
343 bytes_read = file.readListSync(buffer1, 0, 42);
344 Expect.equals(42, bytes_read);
345 file.closeSync();
346 // Write the contents of the file just read into another file.
347 String outFilename = tempDirectory.path + "/out_read_write_sync";
348 File outFile = new File(outFilename);
349 outFile.createSync();
350 String path = outFile.fullPathSync();
351 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
352 Expect.fail("Not a full path");
353 }
354 Expect.isTrue(new File(path).existsSync());
355 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE);
356 openedFile.writeListSync(buffer1, 0, bytes_read);
357 openedFile.closeSync();
358 // Now read the contents of the file just written.
359 List<int> buffer2 = new List<int>(bytes_read);
360 openedFile = (new File(outFilename)).openSync();
361 bytes_read = openedFile.readListSync(buffer2, 0, 42);
362 Expect.equals(42, bytes_read);
363 openedFile.closeSync();
364 // Now compare the two buffers to check if they are identical.
365 Expect.equals(buffer1.length, buffer2.length);
366 for (int i = 0; i < buffer1.length; i++) {
367 Expect.equals(buffer1[i], buffer2[i]);
368 }
369 // Delete the output file.
370 outFile.deleteSync();
371 Expect.isFalse(outFile.existsSync());
372 }
373
374 static void testReadEmptyFileSync() {
375 String fileName = tempDirectory.path + "/empty_file_sync";
376 File file = new File(fileName);
377 file.createSync();
378 RandomAccessFile openedFile = file.openSync();
379 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException);
380 openedFile.closeSync();
381 file.deleteSync();
382 }
383
384 static void testReadEmptyFile() {
385 String fileName = tempDirectory.path + "/empty_file";
386 File file = new File(fileName);
387 file.onError = (s) {
388 Expect.fail("No errors expected : $s");
389 };
390 asyncTestStarted();
391 file.create(() {
392 file.open(FileMode.READ, (RandomAccessFile openedFile) {
393 openedFile.readByte((int byte) {
394 Expect.fail("Read byte from empty file");
395 });
396 openedFile.onError = (String err) {
397 Expect.isTrue(err.indexOf("failed") != -1);
398 openedFile.close(() {
399 file.delete(() {
400 asyncTestDone("testReadEmptyFile");
401 });
402 });
403 };
404 });
405 });
406 }
407
408 // Test for file write of different types of lists.
409 static void testWriteVariousLists() {
410 asyncTestStarted();
411 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
412 final File file = new File(fileName);
413 file.onError = (s) {
414 Expect.fail("No errors expected : $s");
415 };
416 file.create(() {
417 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
418 // Write bytes from 0 to 7.
419 openedFile.writeList([0], 0, 1);
420 openedFile.writeList(const [1], 0, 1);
421 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
422 var x = 12345678901234567890123456789012345678901234567890;
423 var y = 12345678901234567890123456789012345678901234567893;
424 openedFile.writeList([y - x], 0, 1);
425 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
426 openedFile.writeList(const [261], 0, 1);
427 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
428 x = 12345678901234567890123456789012345678901234567890;
429 y = 12345678901234567890123456789012345678901234568153;
430 openedFile.writeList([y - x], 0, 1);
431
432 openedFile.onError = (s) {
433 Expect.fail("No errors expected : $s");
434 };
435 openedFile.onNoPendingWrites = () {
436 openedFile.close(() {
437 // Check the written bytes.
438 final File file2 = new File(fileName);
439 var openedFile2 = file2.openSync();
440 var length = openedFile2.lengthSync();
441 Expect.equals(8, length);
442 List data = new List(length);
443 openedFile2.readListSync(data, 0, length);
444 for (var i = 0; i < data.length; i++) {
445 Expect.equals(i, data[i]);
446 }
447 openedFile2.closeSync();
448 file2.deleteSync();
449 asyncTestDone("testWriteVariousLists");
450 });
451 };
452 });
453 });
454 }
455
456 static void testDirectory() {
457 asyncTestStarted();
458
459 // Port to verify that the test completes.
460 var port = new ReceivePort.singleShot();
461 port.receive((message, replyTo) {
462 Expect.equals(1, message);
463 asyncTestDone("testDirectory");
464 });
465
466 var tempDir = tempDirectory.path;
467 var file = new File("${tempDir}/testDirectory");
468 var errors = 0;
469 file.directory((d) => Expect.fail("non-existing file"));
470 file.onError = (s) {
471 file.onError = (s) => Expect.fail("no error expected");
472 file.create(() {
473 file.directory((Directory d) {
474 d.onError = (s) => Expect.fail("no error expected");
475 d.exists((exists) {
476 Expect.isTrue(exists);
477 Expect.isTrue(d.path.endsWith(tempDir));
478 file.delete(() {
479 var file_dir = new File(".");
480 file_dir.directory((d) => Expect.fail("non-existing file"));
481 file_dir.onError = (s) {
482 var file_dir = new File(tempDir);
483 file_dir.directory((d) => Expect.fail("non-existing file"));
484 file_dir.onError = (s) {
485 port.toSendPort().send(1);
486 };
487 };
488 });
489 });
490 });
491 });
492 };
493 }
494
495 static void testDirectorySync() {
496 var tempDir = tempDirectory.path;
497 var file = new File("${tempDir}/testDirectorySync");
498 // Non-existing file should throw exception.
499 Expect.throws(file.directorySync, (e) { return e is FileIOException; });
500 file.createSync();
501 // Check that the path of the returned directory is the temp directory.
502 Directory d = file.directorySync();
503 Expect.isTrue(d.existsSync());
504 Expect.isTrue(d.path.endsWith(tempDir));
505 file.deleteSync();
506 // Directories should throw exception.
507 var file_dir = new File(".");
508 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
509 file_dir = new File(tempDir);
510 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
511 }
512
513 // Test for file length functionality.
514 static void testLength() {
515 String filename = getFilename("tests/vm/data/fixed_length_file");
516 RandomAccessFile input = (new File(filename)).openSync();
517 input.onError = (s) {
518 Expect.fail("No errors expected");
519 };
520 input.length((length) {
521 Expect.equals(42, length);
522 input.close(() => null);
523 });
524 }
525
526 static void testLengthSync() {
527 String filename = getFilename("tests/vm/data/fixed_length_file");
528 RandomAccessFile input = (new File(filename)).openSync();
529 Expect.equals(42, input.lengthSync());
530 input.closeSync();
531 }
532
533 // Test for file position functionality.
534 static void testPosition() {
535 String filename = getFilename("tests/vm/data/fixed_length_file");
536 RandomAccessFile input = (new File(filename)).openSync();
537 input.onError = (s) {
538 Expect.fail("No errors expected");
539 };
540 input.position((position) {
541 Expect.equals(0, position);
542 List<int> buffer = new List<int>(100);
543 input.readList(buffer, 0, 12, (bytes_read) {
544 input.position((position) {
545 Expect.equals(12, position);
546 input.readList(buffer, 12, 6, (bytes_read) {
547 input.position((position) {
548 Expect.equals(18, position);
549 input.setPosition(8, () {
550 input.position((position) {
551 Expect.equals(8, position);
552 input.close(() => null);
553 });
554 });
555 });
556 });
557 });
558 });
559 });
560 }
561
562 static void testPositionSync() {
563 String filename = getFilename("tests/vm/data/fixed_length_file");
564 RandomAccessFile input = (new File(filename)).openSync();
565 Expect.equals(0, input.positionSync());
566 List<int> buffer = new List<int>(100);
567 input.readListSync(buffer, 0, 12);
568 Expect.equals(12, input.positionSync());
569 input.readListSync(buffer, 12, 6);
570 Expect.equals(18, input.positionSync());
571 input.setPositionSync(8);
572 Expect.equals(8, input.positionSync());
573 input.closeSync();
574 }
575
576 static void testTruncate() {
577 File file = new File(tempDirectory.path + "/out_truncate");
578 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
579 file.onError = (error) {
580 Expect.fail("testTruncate: No errors expected");
581 };
582 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
583 openedFile.writeList(buffer, 0, 10);
584 openedFile.onNoPendingWrites = () {
585 openedFile.length((length) {
586 Expect.equals(10, length);
587 openedFile.truncate(5, () {
588 openedFile.length((length) {
589 Expect.equals(5, length);
590 openedFile.close(() {
591 file.delete(() {
592 file.exists((exists) {
593 Expect.isFalse(exists);
594 asyncTestDone("testTruncate");
595 });
596 });
597 });
598 });
599 });
600 });
601 };
602 });
603 asyncTestStarted();
604 }
605
606 static void testTruncateSync() {
607 File file = new File(tempDirectory.path + "/out_truncate_sync");
608 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
609 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
610 openedFile.writeListSync(buffer, 0, 10);
611 Expect.equals(10, openedFile.lengthSync());
612 openedFile.truncateSync(5);
613 Expect.equals(5, openedFile.lengthSync());
614 openedFile.closeSync();
615 file.deleteSync();
616 Expect.isFalse(file.existsSync());
617 }
618
619 // Tests exception handling after file was closed.
620 static void testCloseException() {
621 bool exceptionCaught = false;
622 bool wrongExceptionCaught = false;
623 File input = new File(tempDirectory.path + "/out_close_exception");
624 RandomAccessFile openedFile = input.openSync(FileMode.WRITE);
625 openedFile.closeSync();
626 try {
627 openedFile.readByteSync();
628 } catch (FileIOException ex) {
629 exceptionCaught = true;
630 } catch (Exception ex) {
631 wrongExceptionCaught = true;
632 }
633 Expect.equals(true, exceptionCaught);
634 Expect.equals(true, !wrongExceptionCaught);
635 exceptionCaught = false;
636 try {
637 openedFile.writeByteSync(1);
638 } catch (FileIOException ex) {
639 exceptionCaught = true;
640 } catch (Exception ex) {
641 wrongExceptionCaught = true;
642 }
643 Expect.equals(true, exceptionCaught);
644 Expect.equals(true, !wrongExceptionCaught);
645 exceptionCaught = false;
646 try {
647 openedFile.writeStringSync("Test");
648 } catch (FileIOException ex) {
649 exceptionCaught = true;
650 } catch (Exception ex) {
651 wrongExceptionCaught = true;
652 }
653 Expect.equals(true, exceptionCaught);
654 Expect.equals(true, !wrongExceptionCaught);
655 exceptionCaught = false;
656 try {
657 List<int> buffer = new List<int>(100);
658 openedFile.readListSync(buffer, 0, 10);
659 } catch (FileIOException ex) {
660 exceptionCaught = true;
661 } catch (Exception ex) {
662 wrongExceptionCaught = true;
663 }
664 Expect.equals(true, exceptionCaught);
665 Expect.equals(true, !wrongExceptionCaught);
666 exceptionCaught = false;
667 try {
668 List<int> buffer = new List<int>(100);
669 openedFile.writeListSync(buffer, 0, 10);
670 } catch (FileIOException ex) {
671 exceptionCaught = true;
672 } catch (Exception ex) {
673 wrongExceptionCaught = true;
674 }
675 Expect.equals(true, exceptionCaught);
676 Expect.equals(true, !wrongExceptionCaught);
677 exceptionCaught = false;
678 try {
679 openedFile.positionSync();
680 } catch (FileIOException ex) {
681 exceptionCaught = true;
682 } catch (Exception ex) {
683 wrongExceptionCaught = true;
684 }
685 Expect.equals(true, exceptionCaught);
686 Expect.equals(true, !wrongExceptionCaught);
687 exceptionCaught = false;
688 try {
689 openedFile.lengthSync();
690 } catch (FileIOException ex) {
691 exceptionCaught = true;
692 } catch (Exception ex) {
693 wrongExceptionCaught = true;
694 }
695 Expect.equals(true, exceptionCaught);
696 Expect.equals(true, !wrongExceptionCaught);
697 exceptionCaught = false;
698 try {
699 openedFile.flushSync();
700 } catch (FileIOException ex) {
701 exceptionCaught = true;
702 } catch (Exception ex) {
703 wrongExceptionCaught = true;
704 }
705 Expect.equals(true, exceptionCaught);
706 Expect.equals(true, !wrongExceptionCaught);
707 input.deleteSync();
708 }
709
710 // Tests stream exception handling after file was closed.
711 static void testCloseExceptionStream() {
712 asyncTestStarted();
713 List<int> buffer = new List<int>(42);
714 File file = new File(tempDirectory.path + "/out_close_exception_stream");
715 file.createSync();
716 InputStream input = file.openInputStream();
717 input.onClosed = () {
718 Expect.isTrue(input.closed);
719 Expect.isNull(input.readInto(buffer, 0, 12));
720 OutputStream output = file.openOutputStream();
721 output.close();
722 Expect.throws(() => output.writeFrom(buffer, 0, 12));
723 output.onClosed = () {
724 file.deleteSync();
725 asyncTestDone("testCloseExceptionStream");
726 };
727 };
728 }
729
730 // Tests buffer out of bounds exception.
731 static void testBufferOutOfBoundsException() {
732 bool exceptionCaught = false;
733 bool wrongExceptionCaught = false;
734 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
735 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
736 try {
737 List<int> buffer = new List<int>(10);
738 bool readDone = openedFile.readListSync(buffer, 0, 12);
739 } catch (IndexOutOfRangeException ex) {
740 exceptionCaught = true;
741 } catch (Exception ex) {
742 wrongExceptionCaught = true;
743 }
744 Expect.equals(true, exceptionCaught);
745 Expect.equals(true, !wrongExceptionCaught);
746 exceptionCaught = false;
747 try {
748 List<int> buffer = new List<int>(10);
749 bool readDone = openedFile.readListSync(buffer, 6, 6);
750 } catch (IndexOutOfRangeException ex) {
751 exceptionCaught = true;
752 } catch (Exception ex) {
753 wrongExceptionCaught = true;
754 }
755 Expect.equals(true, exceptionCaught);
756 Expect.equals(true, !wrongExceptionCaught);
757 exceptionCaught = false;
758 try {
759 List<int> buffer = new List<int>(10);
760 bool readDone = openedFile.readListSync(buffer, -1, 1);
761 } catch (IndexOutOfRangeException ex) {
762 exceptionCaught = true;
763 } catch (Exception ex) {
764 wrongExceptionCaught = true;
765 }
766 Expect.equals(true, exceptionCaught);
767 Expect.equals(true, !wrongExceptionCaught);
768 exceptionCaught = false;
769 try {
770 List<int> buffer = new List<int>(10);
771 bool readDone = openedFile.readListSync(buffer, 0, -1);
772 } catch (IndexOutOfRangeException ex) {
773 exceptionCaught = true;
774 } catch (Exception ex) {
775 wrongExceptionCaught = true;
776 }
777 Expect.equals(true, exceptionCaught);
778 Expect.equals(true, !wrongExceptionCaught);
779 exceptionCaught = false;
780 try {
781 List<int> buffer = new List<int>(10);
782 bool readDone = openedFile.writeListSync(buffer, 0, 12);
783 } catch (IndexOutOfRangeException ex) {
784 exceptionCaught = true;
785 } catch (Exception ex) {
786 wrongExceptionCaught = true;
787 }
788 Expect.equals(true, exceptionCaught);
789 Expect.equals(true, !wrongExceptionCaught);
790 exceptionCaught = false;
791 try {
792 List<int> buffer = new List<int>(10);
793 bool readDone = openedFile.writeListSync(buffer, 6, 6);
794 } catch (IndexOutOfRangeException ex) {
795 exceptionCaught = true;
796 } catch (Exception ex) {
797 wrongExceptionCaught = true;
798 }
799 Expect.equals(true, exceptionCaught);
800 Expect.equals(true, !wrongExceptionCaught);
801 exceptionCaught = false;
802 try {
803 List<int> buffer = new List<int>(10);
804 bool readDone = openedFile.writeListSync(buffer, -1, 1);
805 } catch (IndexOutOfRangeException ex) {
806 exceptionCaught = true;
807 } catch (Exception ex) {
808 wrongExceptionCaught = true;
809 }
810 Expect.equals(true, exceptionCaught);
811 Expect.equals(true, !wrongExceptionCaught);
812 exceptionCaught = false;
813 try {
814 List<int> buffer = new List<int>(10);
815 bool readDone = openedFile.writeListSync(buffer, 0, -1);
816 } catch (IndexOutOfRangeException ex) {
817 exceptionCaught = true;
818 } catch (Exception ex) {
819 wrongExceptionCaught = true;
820 }
821 Expect.equals(true, exceptionCaught);
822 Expect.equals(true, !wrongExceptionCaught);
823 openedFile.closeSync();
824 file.deleteSync();
825 }
826
827 static void testMixedSyncAndAsync() {
828 var name = getFilename("tests/vm/data/fixed_length_file");
829 var f = new File(name);
830 f.onError = (s) {
831 Expect.fail("No errors expected");
832 };
833 f.exists((exists) {
834 try {
835 f.existsSync();
836 Expect.fail("Expected exception");
837 } catch (var e) {
838 Expect.isTrue(e is FileIOException);
839 }
840 });
841 }
842
843 static void testOpenDirectoryAsFile() {
844 var f = new File('.');
845 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file'));
846 }
847
848 static void testOpenDirectoryAsFileSync() {
849 var f = new File('.');
850 try {
851 f.openSync();
852 Expect.fail("Expected exception opening directory as file");
853 } catch (var e) {
854 Expect.isTrue(e is FileIOException);
855 }
856 }
857
858 static void testReadAsBytes() {
859 var port = new ReceivePort.singleShot();
860 port.receive((result, replyTo) {
861 Expect.equals(42, result);
862 });
863 var name = getFilename("tests/vm/data/fixed_length_file");
864 var f = new File(name);
865 f.readAsBytes((bytes) {
866 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
867 port.toSendPort().send(bytes.length);
868 });
869 f.onError = (e) {
870 Expect.fail("No errors expected: $e");
871 };
872 }
873
874 static void testReadAsBytesSync() {
875 var name = getFilename("tests/vm/data/fixed_length_file");
876 var bytes = new File(name).readAsBytesSync();
877 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
878 Expect.equals(bytes.length, 42);
879 }
880
881 static void testReadAsText() {
882 var port = new ReceivePort.singleShot();
883 port.receive((result, replyTo) {
884 Expect.equals(1, result);
885 });
886 var name = getFilename("tests/vm/data/fixed_length_file");
887 var f = new File(name);
888 f.readAsText('UTF-8', (text) {
889 Expect.isTrue(text.endsWith("42 bytes."));
890 Expect.equals(42, text.length);
891 var name = getDataFilename("tests/standalone/src/read_as_text.dat");
892 var f = new File(name);
893 f.onError = (e) => Expect.fail("No errors expected");
894 f.readAsText('UTF-8', (text) {
895 Expect.equals(6, text.length);
896 var expected = [955, 120, 46, 32, 120, 10];
897 Expect.listEquals(expected, text.charCodes());
898 f.readAsText('ISO-8859-1', (text) {
899 Expect.equals(7, text.length);
900 var expected = [206, 187, 120, 46, 32, 120, 10];
901 Expect.listEquals(expected, text.charCodes());
902 f.onError = (e) {
903 port.toSendPort().send(1);
904 };
905 f.readAsText('ASCII', (text) {
906 Expect.fail("Non-ascii char should cause error");
907 });
908 });
909 });
910 });
911 f.onError = (e) {
912 Expect.fail("No errors expected: $e");
913 };
914 }
915
916 static void testReadAsTextSync() {
917 var name = getFilename("tests/vm/data/fixed_length_file");
918 var text = new File(name).readAsTextSync();
919 Expect.isTrue(text.endsWith("42 bytes."));
920 Expect.equals(42, text.length);
921 name = getDataFilename("tests/standalone/src/read_as_text.dat");
922 text = new File(name).readAsTextSync();
923 Expect.equals(6, text.length);
924 var expected = [955, 120, 46, 32, 120, 10];
925 Expect.listEquals(expected, text.charCodes());
926 Expect.throws(() { new File(name).readAsTextSync("ASCII"); });
927 text = new File(name).readAsTextSync("ISO-8859-1");
928 expected = [206, 187, 120, 46, 32, 120, 10];
929 Expect.equals(7, text.length);
930 Expect.listEquals(expected, text.charCodes());
931 }
932
933 static void testReadAsLines() {
934 var port = new ReceivePort.singleShot();
935 port.receive((result, replyTo) {
936 Expect.equals(42, result);
937 });
938 var name = getFilename("tests/vm/data/fixed_length_file");
939 var f = new File(name);
940 f.readAsLines('UTF-8', (lines) {
941 Expect.equals(1, lines.length);
942 var line = lines[0];
943 Expect.isTrue(line.endsWith("42 bytes."));
944 port.toSendPort().send(line.length);
945 });
946 f.onError = (e) {
947 Expect.fail("No errors expected: $e");
948 };
949 }
950
951 static void testReadAsLinesSync() {
952 var name = getFilename("tests/vm/data/fixed_length_file");
953 var lines = new File(name).readAsLinesSync();
954 Expect.equals(1, lines.length);
955 var line = lines[0];
956 Expect.isTrue(line.endsWith("42 bytes."));
957 Expect.equals(42, line.length);
958 name = getDataFilename("tests/standalone/src/readline_test1.dat");
959 lines = new File(name).readAsLinesSync();
960 Expect.equals(10, lines.length);
961 }
962
963
964 static void testReadAsErrors() {
965 var port = new ReceivePort.singleShot();
966 port.receive((message, _) {
967 Expect.equals(1, message);
968 });
969 var f = new File('.');
970 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
971 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
972 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
973 f.readAsBytes((bytes) => Expect.fail("no bytes expected"));
974 f.onError = (e) {
975 f.readAsText('UTF-8', (text) => Expect.fail("no text expected"));
976 f.onError = (e) {
977 f.readAsLines('UTF-8', (lines) => Expect.fail("no lines expected"));
978 f.onError = (e) {
979 port.toSendPort().send(1);
980 };
981 };
982 };
983 }
984
985 // Test that opens the same file for writing then for appending to test
986 // that the file is not truncated when opened for appending.
987 static void testAppend() {
988 var file = new File('${tempDirectory.path}/out_append');
989 file.open(FileMode.WRITE, (openedFile) {
990 openedFile.writeString("asdf");
991 openedFile.onNoPendingWrites = () {
992 openedFile.close(() {
993 file.open(FileMode.APPEND, (openedFile) {
994 openedFile.length((length) {
995 Expect.equals(4, length);
996 openedFile.writeString("asdf");
997 openedFile.onNoPendingWrites = () {
998 openedFile.length((length) {
999 Expect.equals(8, length);
1000 openedFile.close(() {
1001 file.delete(() {
1002 file.exists((exists) {
1003 Expect.isFalse(exists);
1004 asyncTestDone("testAppend");
1005 });
1006 });
1007 });
1008 });
1009 };
1010 });
1011 });
1012 });
1013 };
1014 });
1015 asyncTestStarted();
1016 }
1017
1018 static void testAppendSync() {
1019 var file = new File('${tempDirectory.path}/out_append_sync');
1020 var openedFile = file.openSync(FileMode.WRITE);
1021 openedFile.writeStringSync("asdf");
1022 Expect.equals(4, openedFile.lengthSync());
1023 openedFile.closeSync();
1024 openedFile = file.openSync(FileMode.WRITE);
1025 openedFile.setPositionSync(4);
1026 openedFile.writeStringSync("asdf");
1027 Expect.equals(8, openedFile.lengthSync());
1028 openedFile.closeSync();
1029 file.deleteSync();
1030 Expect.isFalse(file.existsSync());
1031 }
1032
1033 // Helper method to be able to run the test from the runtime
1034 // directory, or the top directory.
1035 static String getFilename(String path) =>
1036 new File(path).existsSync() ? path : 'runtime/' + path;
1037
1038 static String getDataFilename(String path) =>
1039 new File(path).existsSync() ? path : '../' + path;
1040
1041 // Main test entrypoint.
1042 static testMain() {
1043 testRead();
1044 testReadSync();
1045 testReadStream();
1046 testLength();
1047 testLengthSync();
1048 testPosition();
1049 testPositionSync();
1050 testMixedSyncAndAsync();
1051 testOpenDirectoryAsFile();
1052 testOpenDirectoryAsFileSync();
1053 testReadAsBytes();
1054 testReadAsBytesSync();
1055 testReadAsText();
1056 testReadAsTextSync();
1057 testReadAsLines();
1058 testReadAsLinesSync();
1059 testReadAsErrors();
1060
1061 createTempDirectory(() {
1062 testReadWrite();
1063 testReadWriteSync();
1064 testReadWriteStream();
1065 testReadEmptyFileSync();
1066 testReadEmptyFile();
1067 testTruncate();
1068 testTruncateSync();
1069 testCloseException();
1070 testCloseExceptionStream();
1071 testBufferOutOfBoundsException();
1072 testAppend();
1073 testAppendSync();
1074 testWriteAppend();
1075 testOutputStreamWriteAppend();
1076 testWriteVariousLists();
1077 testDirectory();
1078 testDirectorySync();
1079 });
1080 }
1081 }
1082
1083 main() {
1084 FileTest.testMain();
1085 }
OLDNEW
« no previous file with comments | « tests/standalone/src/FileOutputStreamTest.dart ('k') | tests/standalone/src/HttpParserTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698