| OLD | NEW |
| 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 { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 static void asyncTestStarted() { ++numLiveAsyncTests; } | 21 static void asyncTestStarted() { ++numLiveAsyncTests; } |
| 22 static void asyncTestDone(String name) { | 22 static void asyncTestDone(String name) { |
| 23 --numLiveAsyncTests; | 23 --numLiveAsyncTests; |
| 24 if (numLiveAsyncTests == 0) { | 24 if (numLiveAsyncTests == 0) { |
| 25 deleteTempDirectory(); | 25 deleteTempDirectory(); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 static void createTempDirectory(Function doNext) { | 29 static void createTempDirectory(Function doNext) { |
| 30 tempDirectory = new Directory(''); | 30 tempDirectory = new Directory(''); |
| 31 tempDirectory.errorHandler = (e) { | 31 tempDirectory.onError = (e) { |
| 32 Expect.fail("Failed creating temporary directory"); | 32 Expect.fail("Failed creating temporary directory"); |
| 33 }; | 33 }; |
| 34 tempDirectory.createTempHandler = doNext; | 34 tempDirectory.onCreateTemp = doNext; |
| 35 tempDirectory.createTemp(); | 35 tempDirectory.createTemp(); |
| 36 } | 36 } |
| 37 | 37 |
| 38 static void deleteTempDirectory() { | 38 static void deleteTempDirectory() { |
| 39 tempDirectory.deleteSync(recursive: true); | 39 tempDirectory.deleteSync(recursive: true); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Test for file read functionality. | 42 // Test for file read functionality. |
| 43 static void testReadStream() { | 43 static void testReadStream() { |
| 44 // Read a file and check part of it's contents. | 44 // Read a file and check part of it's contents. |
| 45 String filename = getFilename("bin/file_test.cc"); | 45 String filename = getFilename("bin/file_test.cc"); |
| 46 File file = new File(filename); | 46 File file = new File(filename); |
| 47 InputStream input = file.openInputStream(); | 47 InputStream input = file.openInputStream(); |
| 48 input.dataHandler = () { | 48 input.onData = () { |
| 49 List<int> buffer = new List<int>(42); | 49 List<int> buffer = new List<int>(42); |
| 50 int bytesRead = input.readInto(buffer, 0, 12); | 50 int bytesRead = input.readInto(buffer, 0, 12); |
| 51 Expect.equals(12, bytesRead); | 51 Expect.equals(12, bytesRead); |
| 52 bytesRead = input.readInto(buffer, 12, 30); | 52 bytesRead = input.readInto(buffer, 12, 30); |
| 53 input.close(); | 53 input.close(); |
| 54 Expect.equals(30, bytesRead); | 54 Expect.equals(30, bytesRead); |
| 55 Expect.equals(47, buffer[0]); // represents '/' in the file. | 55 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 56 Expect.equals(47, buffer[1]); // represents '/' in the file. | 56 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 57 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 57 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 58 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 58 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 | 73 |
| 74 // Read a file. | 74 // Read a file. |
| 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 76 File file; | 76 File file; |
| 77 InputStream input; | 77 InputStream input; |
| 78 int bytesRead; | 78 int bytesRead; |
| 79 | 79 |
| 80 // Test reading all using readInto. | 80 // Test reading all using readInto. |
| 81 file = new File(inFilename); | 81 file = new File(inFilename); |
| 82 input = file.openInputStream(); | 82 input = file.openInputStream(); |
| 83 input.dataHandler = () { | 83 input.onData = () { |
| 84 List<int> buffer1 = new List<int>(42); | 84 List<int> buffer1 = new List<int>(42); |
| 85 bytesRead = input.readInto(buffer1, 0, 42); | 85 bytesRead = input.readInto(buffer1, 0, 42); |
| 86 Expect.equals(42, bytesRead); | 86 Expect.equals(42, bytesRead); |
| 87 Expect.isTrue(input.closed); | 87 Expect.isTrue(input.closed); |
| 88 | 88 |
| 89 // Test reading all using readInto and read. | 89 // Test reading all using readInto and read. |
| 90 file = new File(inFilename); | 90 file = new File(inFilename); |
| 91 input = file.openInputStream(); | 91 input = file.openInputStream(); |
| 92 input.dataHandler = () { | 92 input.onData = () { |
| 93 bytesRead = input.readInto(buffer1, 0, 21); | 93 bytesRead = input.readInto(buffer1, 0, 21); |
| 94 Expect.equals(21, bytesRead); | 94 Expect.equals(21, bytesRead); |
| 95 buffer1 = input.read(); | 95 buffer1 = input.read(); |
| 96 Expect.equals(21, buffer1.length); | 96 Expect.equals(21, buffer1.length); |
| 97 Expect.isTrue(input.closed); | 97 Expect.isTrue(input.closed); |
| 98 | 98 |
| 99 // Test reading all using read and readInto. | 99 // Test reading all using read and readInto. |
| 100 file = new File(inFilename); | 100 file = new File(inFilename); |
| 101 input = file.openInputStream(); | 101 input = file.openInputStream(); |
| 102 input.dataHandler = () { | 102 input.onData = () { |
| 103 buffer1 = input.read(21); | 103 buffer1 = input.read(21); |
| 104 Expect.equals(21, buffer1.length); | 104 Expect.equals(21, buffer1.length); |
| 105 bytesRead = input.readInto(buffer1, 0, 21); | 105 bytesRead = input.readInto(buffer1, 0, 21); |
| 106 Expect.equals(21, bytesRead); | 106 Expect.equals(21, bytesRead); |
| 107 Expect.isTrue(input.closed); | 107 Expect.isTrue(input.closed); |
| 108 | 108 |
| 109 // Test reading all using read. | 109 // Test reading all using read. |
| 110 file = new File(inFilename); | 110 file = new File(inFilename); |
| 111 input = file.openInputStream(); | 111 input = file.openInputStream(); |
| 112 input.dataHandler = () { | 112 input.onData = () { |
| 113 buffer1 = input.read(); | 113 buffer1 = input.read(); |
| 114 Expect.equals(42, buffer1.length); | 114 Expect.equals(42, buffer1.length); |
| 115 Expect.isTrue(input.closed); | 115 Expect.isTrue(input.closed); |
| 116 | 116 |
| 117 // Write the contents of the file just read into another file. | 117 // Write the contents of the file just read into another file. |
| 118 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 118 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 119 file = new File(outFilename); | 119 file = new File(outFilename); |
| 120 OutputStream output = file.openOutputStream(); | 120 OutputStream output = file.openOutputStream(); |
| 121 bool writeDone = output.writeFrom(buffer1, 0, 42); | 121 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 122 Expect.equals(false, writeDone); | 122 Expect.equals(false, writeDone); |
| 123 output.noPendingWriteHandler = () { | 123 output.onNoPendingWrite = () { |
| 124 output.close(); | 124 output.close(); |
| 125 | 125 |
| 126 // Now read the contents of the file just written. | 126 // Now read the contents of the file just written. |
| 127 List<int> buffer2 = new List<int>(42); | 127 List<int> buffer2 = new List<int>(42); |
| 128 file = new File(outFilename); | 128 file = new File(outFilename); |
| 129 input = file.openInputStream(); | 129 input = file.openInputStream(); |
| 130 input.dataHandler = () { | 130 input.onData = () { |
| 131 bytesRead = input.readInto(buffer2, 0, 42); | 131 bytesRead = input.readInto(buffer2, 0, 42); |
| 132 Expect.equals(42, bytesRead); | 132 Expect.equals(42, bytesRead); |
| 133 // Now compare the two buffers to check if they are identical. | 133 // Now compare the two buffers to check if they are identical. |
| 134 for (int i = 0; i < buffer1.length; i++) { | 134 for (int i = 0; i < buffer1.length; i++) { |
| 135 Expect.equals(buffer1[i], buffer2[i]); | 135 Expect.equals(buffer1[i], buffer2[i]); |
| 136 } | 136 } |
| 137 input.closeHandler = () { | 137 input.onClose = () { |
| 138 // Delete the output file. | 138 // Delete the output file. |
| 139 file.deleteSync(); | 139 file.deleteSync(); |
| 140 Expect.isFalse(file.existsSync()); | 140 Expect.isFalse(file.existsSync()); |
| 141 asyncTestDone("testReadWriteStream"); | 141 asyncTestDone("testReadWriteStream"); |
| 142 }; | 142 }; |
| 143 }; | 143 }; |
| 144 }; | 144 }; |
| 145 }; | 145 }; |
| 146 }; | 146 }; |
| 147 }; | 147 }; |
| 148 }; | 148 }; |
| 149 } | 149 } |
| 150 | 150 |
| 151 static void testRead() { | 151 static void testRead() { |
| 152 // Read a file and check part of it's contents. | 152 // Read a file and check part of it's contents. |
| 153 String filename = getFilename("bin/file_test.cc"); | 153 String filename = getFilename("bin/file_test.cc"); |
| 154 File file = new File(filename); | 154 File file = new File(filename); |
| 155 file.errorHandler = (s) { | 155 file.onError = (s) { |
| 156 Expect.fail("No errors expected : $s"); | 156 Expect.fail("No errors expected : $s"); |
| 157 }; | 157 }; |
| 158 file.openHandler = (RandomAccessFile file) { | 158 file.onOpen = (RandomAccessFile file) { |
| 159 List<int> buffer = new List<int>(10); | 159 List<int> buffer = new List<int>(10); |
| 160 file.readListHandler = (bytes_read) { | 160 file.onReadList = (bytes_read) { |
| 161 Expect.equals(5, bytes_read); | 161 Expect.equals(5, bytes_read); |
| 162 file.readListHandler = (bytes_read) { | 162 file.onReadList = (bytes_read) { |
| 163 Expect.equals(5, bytes_read); | 163 Expect.equals(5, bytes_read); |
| 164 Expect.equals(47, buffer[0]); // represents '/' in the file. | 164 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 165 Expect.equals(47, buffer[1]); // represents '/' in the file. | 165 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 166 Expect.equals(32, buffer[2]); // 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. | 167 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| 168 Expect.equals(111, buffer[4]); // represents 'o' 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. | 169 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 170 Expect.equals(121, buffer[6]); // represents 'y' 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. | 171 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 172 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 172 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 202 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 202 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 203 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 203 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 204 Expect.equals(116, buffer[11]); // represents 't' in the file. | 204 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Test for file read and write functionality. | 207 // Test for file read and write functionality. |
| 208 static void testReadWrite() { | 208 static void testReadWrite() { |
| 209 // Read a file. | 209 // Read a file. |
| 210 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 210 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 211 final File file = new File(inFilename); | 211 final File file = new File(inFilename); |
| 212 file.errorHandler = (s) { | 212 file.onError = (s) { |
| 213 Expect.fail("No errors expected : $s"); | 213 Expect.fail("No errors expected : $s"); |
| 214 }; | 214 }; |
| 215 file.openHandler = (RandomAccessFile openedFile) { | 215 file.onOpen = (RandomAccessFile openedFile) { |
| 216 openedFile.errorHandler = (s) { | 216 openedFile.onError = (s) { |
| 217 Expect.fail("No errors expected : $s"); | 217 Expect.fail("No errors expected : $s"); |
| 218 }; | 218 }; |
| 219 List<int> buffer1 = new List<int>(42); | 219 List<int> buffer1 = new List<int>(42); |
| 220 openedFile.readListHandler = (bytes_read) { | 220 openedFile.onReadList = (bytes_read) { |
| 221 Expect.equals(42, bytes_read); | 221 Expect.equals(42, bytes_read); |
| 222 openedFile.closeHandler = () { | 222 openedFile.onClose = () { |
| 223 // Write the contents of the file just read into another file. | 223 // Write the contents of the file just read into another file. |
| 224 String outFilename = tempDirectory.path + "/out_read_write"; | 224 String outFilename = tempDirectory.path + "/out_read_write"; |
| 225 final File file2 = new File(outFilename); | 225 final File file2 = new File(outFilename); |
| 226 file2.errorHandler = (s) { | 226 file2.onError = (s) { |
| 227 Expect.fail("No errors expected : $s"); | 227 Expect.fail("No errors expected : $s"); |
| 228 }; | 228 }; |
| 229 file2.createHandler = () { | 229 file2.onCreate = () { |
| 230 file2.fullPathHandler = (s) { | 230 file2.onFullPath = (s) { |
| 231 Expect.isTrue(new File(s).existsSync()); | 231 Expect.isTrue(new File(s).existsSync()); |
| 232 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 232 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
| 233 Expect.fail("Not a full path"); | 233 Expect.fail("Not a full path"); |
| 234 } | 234 } |
| 235 file2.openHandler = (RandomAccessFile openedFile2) { | 235 file2.onOpen = (RandomAccessFile openedFile2) { |
| 236 openedFile2.errorHandler = (s) { | 236 openedFile2.onError = (s) { |
| 237 Expect.fail("No errors expected : $s"); | 237 Expect.fail("No errors expected : $s"); |
| 238 }; | 238 }; |
| 239 openedFile2.noPendingWriteHandler = () { | 239 openedFile2.onNoPendingWrite = () { |
| 240 openedFile2.closeHandler = () { | 240 openedFile2.onClose = () { |
| 241 List<int> buffer2 = new List<int>(bytes_read); | 241 List<int> buffer2 = new List<int>(bytes_read); |
| 242 final File file3 = new File(outFilename); | 242 final File file3 = new File(outFilename); |
| 243 file3.errorHandler = (s) { | 243 file3.onError = (s) { |
| 244 Expect.fail("No errors expected : $s"); | 244 Expect.fail("No errors expected : $s"); |
| 245 }; | 245 }; |
| 246 file3.openHandler = (RandomAccessFile openedFile3) { | 246 file3.onOpen = (RandomAccessFile openedFile3) { |
| 247 openedFile3.errorHandler = (s) { | 247 openedFile3.onError = (s) { |
| 248 Expect.fail("No errors expected : $s"); | 248 Expect.fail("No errors expected : $s"); |
| 249 }; | 249 }; |
| 250 openedFile3.readListHandler = (bytes_read) { | 250 openedFile3.onReadList = (bytes_read) { |
| 251 Expect.equals(42, bytes_read); | 251 Expect.equals(42, bytes_read); |
| 252 openedFile3.closeHandler = () { | 252 openedFile3.onClose = () { |
| 253 // Now compare the two buffers to check if they | 253 // Now compare the two buffers to check if they |
| 254 // are identical. | 254 // are identical. |
| 255 Expect.equals(buffer1.length, buffer2.length); | 255 Expect.equals(buffer1.length, buffer2.length); |
| 256 for (int i = 0; i < buffer1.length; i++) { | 256 for (int i = 0; i < buffer1.length; i++) { |
| 257 Expect.equals(buffer1[i], buffer2[i]); | 257 Expect.equals(buffer1[i], buffer2[i]); |
| 258 } | 258 } |
| 259 // Delete the output file. | 259 // Delete the output file. |
| 260 final file4 = file3; | 260 final file4 = file3; |
| 261 file4.deleteHandler = () { | 261 file4.onDelete = () { |
| 262 file4.existsHandler = (exists) { | 262 file4.onExists = (exists) { |
| 263 Expect.isFalse(exists); | 263 Expect.isFalse(exists); |
| 264 asyncTestDone("testReadWrite"); | 264 asyncTestDone("testReadWrite"); |
| 265 }; | 265 }; |
| 266 file4.exists(); | 266 file4.exists(); |
| 267 }; | 267 }; |
| 268 file4.delete(); | 268 file4.delete(); |
| 269 }; | 269 }; |
| 270 openedFile3.close(); | 270 openedFile3.close(); |
| 271 }; | 271 }; |
| 272 openedFile3.readList(buffer2, 0, 42); | 272 openedFile3.readList(buffer2, 0, 42); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 316 } |
| 317 | 317 |
| 318 static void testOutputStreamWriteAppend() { | 318 static void testOutputStreamWriteAppend() { |
| 319 String content = "foobar"; | 319 String content = "foobar"; |
| 320 String filename = tempDirectory.path + "/outstream_write_append"; | 320 String filename = tempDirectory.path + "/outstream_write_append"; |
| 321 File file = new File(filename); | 321 File file = new File(filename); |
| 322 file.createSync(); | 322 file.createSync(); |
| 323 List<int> buffer = content.charCodes(); | 323 List<int> buffer = content.charCodes(); |
| 324 OutputStream outStream = file.openOutputStream(); | 324 OutputStream outStream = file.openOutputStream(); |
| 325 outStream.write(buffer); | 325 outStream.write(buffer); |
| 326 outStream.noPendingWriteHandler = () { | 326 outStream.onNoPendingWrite = () { |
| 327 outStream.close(); | 327 outStream.close(); |
| 328 File file2 = new File(filename); | 328 File file2 = new File(filename); |
| 329 OutputStream appendingOutput = | 329 OutputStream appendingOutput = |
| 330 file2.openOutputStream(FileMode.APPEND); | 330 file2.openOutputStream(FileMode.APPEND); |
| 331 appendingOutput.write(buffer); | 331 appendingOutput.write(buffer); |
| 332 appendingOutput.noPendingWriteHandler = () { | 332 appendingOutput.onNoPendingWrite = () { |
| 333 appendingOutput.close(); | 333 appendingOutput.close(); |
| 334 File file3 = new File(filename); | 334 File file3 = new File(filename); |
| 335 file3.openHandler = (RandomAccessFile openedFile) { | 335 file3.onOpen = (RandomAccessFile openedFile) { |
| 336 openedFile.lengthHandler = (int length) { | 336 openedFile.onLength = (int length) { |
| 337 Expect.equals(content.length * 2, length); | 337 Expect.equals(content.length * 2, length); |
| 338 openedFile.closeHandler = () { | 338 openedFile.onClose = () { |
| 339 file3.deleteHandler = () { | 339 file3.onDelete = () { |
| 340 asyncTestDone("testOutputStreamWriteAppend"); | 340 asyncTestDone("testOutputStreamWriteAppend"); |
| 341 }; | 341 }; |
| 342 file3.delete(); | 342 file3.delete(); |
| 343 }; | 343 }; |
| 344 openedFile.close(); | 344 openedFile.close(); |
| 345 }; | 345 }; |
| 346 openedFile.length(); | 346 openedFile.length(); |
| 347 }; | 347 }; |
| 348 file3.open(); | 348 file3.open(); |
| 349 }; | 349 }; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 file.createSync(); | 396 file.createSync(); |
| 397 RandomAccessFile openedFile = file.openSync(); | 397 RandomAccessFile openedFile = file.openSync(); |
| 398 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); | 398 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); |
| 399 openedFile.closeSync(); | 399 openedFile.closeSync(); |
| 400 file.deleteSync(); | 400 file.deleteSync(); |
| 401 } | 401 } |
| 402 | 402 |
| 403 static void testReadEmptyFile() { | 403 static void testReadEmptyFile() { |
| 404 String fileName = tempDirectory.path + "/empty_file"; | 404 String fileName = tempDirectory.path + "/empty_file"; |
| 405 File file = new File(fileName); | 405 File file = new File(fileName); |
| 406 file.errorHandler = (s) { | 406 file.onError = (s) { |
| 407 Expect.fail("No errors expected : $s"); | 407 Expect.fail("No errors expected : $s"); |
| 408 }; | 408 }; |
| 409 file.createHandler = () { | 409 file.onCreate = () { |
| 410 file.openHandler = (RandomAccessFile openedFile) { | 410 file.onOpen = (RandomAccessFile openedFile) { |
| 411 openedFile.readByteHandler = (int byte) { | 411 openedFile.onReadByte = (int byte) { |
| 412 Expect.fail("Read byte from empty file"); | 412 Expect.fail("Read byte from empty file"); |
| 413 }; | 413 }; |
| 414 openedFile.errorHandler = (String err) { | 414 openedFile.onError = (String err) { |
| 415 Expect.isTrue(err.indexOf("failed") != -1); | 415 Expect.isTrue(err.indexOf("failed") != -1); |
| 416 openedFile.closeHandler = () { | 416 openedFile.onClose = () { |
| 417 file.deleteHandler = () { | 417 file.onDelete = () { |
| 418 asyncTestDone("testReadEmptyFile"); | 418 asyncTestDone("testReadEmptyFile"); |
| 419 }; | 419 }; |
| 420 file.delete(); | 420 file.delete(); |
| 421 }; | 421 }; |
| 422 openedFile.close(); | 422 openedFile.close(); |
| 423 }; | 423 }; |
| 424 openedFile.readByte(); | 424 openedFile.readByte(); |
| 425 }; | 425 }; |
| 426 file.open(); | 426 file.open(); |
| 427 }; | 427 }; |
| 428 asyncTestStarted(); | 428 asyncTestStarted(); |
| 429 file.create(); | 429 file.create(); |
| 430 } | 430 } |
| 431 | 431 |
| 432 // Test for file write of different types of lists. | 432 // Test for file write of different types of lists. |
| 433 static void testWriteVariousLists() { | 433 static void testWriteVariousLists() { |
| 434 asyncTestStarted(); | 434 asyncTestStarted(); |
| 435 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; | 435 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
| 436 final File file = new File(fileName); | 436 final File file = new File(fileName); |
| 437 file.create(); | 437 file.create(); |
| 438 file.createHandler = () { | 438 file.onCreate = () { |
| 439 file.open(FileMode.WRITE); | 439 file.open(FileMode.WRITE); |
| 440 file.openHandler = (RandomAccessFile openedFile) { | 440 file.onOpen = (RandomAccessFile openedFile) { |
| 441 // Write bytes from 0 to 7. | 441 // Write bytes from 0 to 7. |
| 442 openedFile.writeList([0], 0, 1); | 442 openedFile.writeList([0], 0, 1); |
| 443 openedFile.writeList(const [1], 0, 1); | 443 openedFile.writeList(const [1], 0, 1); |
| 444 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 444 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
| 445 var x = 12345678901234567890123456789012345678901234567890; | 445 var x = 12345678901234567890123456789012345678901234567890; |
| 446 var y = 12345678901234567890123456789012345678901234567893; | 446 var y = 12345678901234567890123456789012345678901234567893; |
| 447 openedFile.writeList([y - x], 0, 1); | 447 openedFile.writeList([y - x], 0, 1); |
| 448 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. | 448 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. |
| 449 openedFile.writeList(const [261], 0, 1); | 449 openedFile.writeList(const [261], 0, 1); |
| 450 openedFile.writeList(new MyListOfOneElement(262), 0, 1); | 450 openedFile.writeList(new MyListOfOneElement(262), 0, 1); |
| 451 x = 12345678901234567890123456789012345678901234567890; | 451 x = 12345678901234567890123456789012345678901234567890; |
| 452 y = 12345678901234567890123456789012345678901234568153; | 452 y = 12345678901234567890123456789012345678901234568153; |
| 453 openedFile.writeList([y - x], 0, 1); | 453 openedFile.writeList([y - x], 0, 1); |
| 454 | 454 |
| 455 openedFile.errorHandler = (s) { | 455 openedFile.onError = (s) { |
| 456 Expect.fail("No errors expected : $s"); | 456 Expect.fail("No errors expected : $s"); |
| 457 }; | 457 }; |
| 458 openedFile.noPendingWriteHandler = () { | 458 openedFile.onNoPendingWrite = () { |
| 459 openedFile.close(); | 459 openedFile.close(); |
| 460 }; | 460 }; |
| 461 openedFile.closeHandler = () { | 461 openedFile.onClose = () { |
| 462 // Check the written bytes. | 462 // Check the written bytes. |
| 463 final File file2 = new File(fileName); | 463 final File file2 = new File(fileName); |
| 464 var openedFile2 = file2.openSync(); | 464 var openedFile2 = file2.openSync(); |
| 465 var length = openedFile2.lengthSync(); | 465 var length = openedFile2.lengthSync(); |
| 466 Expect.equals(8, length); | 466 Expect.equals(8, length); |
| 467 List data = new List(length); | 467 List data = new List(length); |
| 468 openedFile2.readListSync(data, 0, length); | 468 openedFile2.readListSync(data, 0, length); |
| 469 for (var i = 0; i < data.length; i++) { | 469 for (var i = 0; i < data.length; i++) { |
| 470 Expect.equals(i, data[i]); | 470 Expect.equals(i, data[i]); |
| 471 } | 471 } |
| 472 openedFile2.closeSync(); | 472 openedFile2.closeSync(); |
| 473 file2.deleteSync(); | 473 file2.deleteSync(); |
| 474 asyncTestDone("testWriteVariousLists"); | 474 asyncTestDone("testWriteVariousLists"); |
| 475 }; | 475 }; |
| 476 }; | 476 }; |
| 477 file.errorHandler = (s) { | 477 file.onError = (s) { |
| 478 Expect.fail("No errors expected : $s"); | 478 Expect.fail("No errors expected : $s"); |
| 479 }; | 479 }; |
| 480 }; | 480 }; |
| 481 } | 481 } |
| 482 | 482 |
| 483 static void testDirectory() { | 483 static void testDirectory() { |
| 484 asyncTestStarted(); | 484 asyncTestStarted(); |
| 485 | 485 |
| 486 // Port to verify that the test completes. | 486 // Port to verify that the test completes. |
| 487 var port = new ReceivePort.singleShot(); | 487 var port = new ReceivePort.singleShot(); |
| 488 port.receive((message, replyTo) { | 488 port.receive((message, replyTo) { |
| 489 Expect.equals(1, message); | 489 Expect.equals(1, message); |
| 490 asyncTestDone("testDirectory"); | 490 asyncTestDone("testDirectory"); |
| 491 }); | 491 }); |
| 492 | 492 |
| 493 var tempDir = tempDirectory.path; | 493 var tempDir = tempDirectory.path; |
| 494 var file = new File("${tempDir}/testDirectory"); | 494 var file = new File("${tempDir}/testDirectory"); |
| 495 var errors = 0; | 495 var errors = 0; |
| 496 file.directory(); | 496 file.directory(); |
| 497 file.directoryHandler = (d) => Expect.fail("non-existing file"); | 497 file.onDirectory = (d) => Expect.fail("non-existing file"); |
| 498 file.errorHandler = (s) { | 498 file.onError = (s) { |
| 499 file.errorHandler = (s) => Expect.fail("no error expected"); | 499 file.onError = (s) => Expect.fail("no error expected"); |
| 500 file.create(); | 500 file.create(); |
| 501 file.createHandler = () { | 501 file.onCreate = () { |
| 502 file.directory(); | 502 file.directory(); |
| 503 file.directoryHandler = (Directory d) { | 503 file.onDirectory = (Directory d) { |
| 504 d.exists(); | 504 d.exists(); |
| 505 d.errorHandler = (s) => Expect.fail("no error expected"); | 505 d.onError = (s) => Expect.fail("no error expected"); |
| 506 d.existsHandler = (exists) { | 506 d.onExists = (exists) { |
| 507 Expect.isTrue(exists); | 507 Expect.isTrue(exists); |
| 508 Expect.isTrue(d.path.endsWith(tempDir)); | 508 Expect.isTrue(d.path.endsWith(tempDir)); |
| 509 file.delete(); | 509 file.delete(); |
| 510 file.deleteHandler = () { | 510 file.onDelete = () { |
| 511 var file_dir = new File("."); | 511 var file_dir = new File("."); |
| 512 file_dir.directory(); | 512 file_dir.directory(); |
| 513 file_dir.directoryHandler = (d) { | 513 file_dir.onDirectory = (d) { |
| 514 Expect.fail("non-existing file"); | 514 Expect.fail("non-existing file"); |
| 515 }; | 515 }; |
| 516 file_dir.errorHandler = (s) { | 516 file_dir.onError = (s) { |
| 517 var file_dir = new File(tempDir); | 517 var file_dir = new File(tempDir); |
| 518 file_dir.directory(); | 518 file_dir.directory(); |
| 519 file_dir.directoryHandler = (d) { | 519 file_dir.onDirectory = (d) { |
| 520 Expect.fail("non-existing file"); | 520 Expect.fail("non-existing file"); |
| 521 }; | 521 }; |
| 522 file_dir.errorHandler = (s) { | 522 file_dir.onError = (s) { |
| 523 port.toSendPort().send(1); | 523 port.toSendPort().send(1); |
| 524 }; | 524 }; |
| 525 }; | 525 }; |
| 526 }; | 526 }; |
| 527 }; | 527 }; |
| 528 }; | 528 }; |
| 529 }; | 529 }; |
| 530 }; | 530 }; |
| 531 } | 531 } |
| 532 | 532 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 545 var file_dir = new File("."); | 545 var file_dir = new File("."); |
| 546 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 546 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
| 547 file_dir = new File(tempDir); | 547 file_dir = new File(tempDir); |
| 548 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 548 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
| 549 } | 549 } |
| 550 | 550 |
| 551 // Test for file length functionality. | 551 // Test for file length functionality. |
| 552 static void testLength() { | 552 static void testLength() { |
| 553 String filename = getFilename("tests/vm/data/fixed_length_file"); | 553 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 554 RandomAccessFile input = (new File(filename)).openSync(); | 554 RandomAccessFile input = (new File(filename)).openSync(); |
| 555 input.errorHandler = (s) { | 555 input.onError = (s) { |
| 556 Expect.fail("No errors expected"); | 556 Expect.fail("No errors expected"); |
| 557 }; | 557 }; |
| 558 input.lengthHandler = (length) { | 558 input.onLength = (length) { |
| 559 Expect.equals(42, length); | 559 Expect.equals(42, length); |
| 560 input.close(); | 560 input.close(); |
| 561 }; | 561 }; |
| 562 input.length(); | 562 input.length(); |
| 563 } | 563 } |
| 564 | 564 |
| 565 static void testLengthSync() { | 565 static void testLengthSync() { |
| 566 String filename = getFilename("tests/vm/data/fixed_length_file"); | 566 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 567 RandomAccessFile input = (new File(filename)).openSync(); | 567 RandomAccessFile input = (new File(filename)).openSync(); |
| 568 Expect.equals(42, input.lengthSync()); | 568 Expect.equals(42, input.lengthSync()); |
| 569 input.closeSync(); | 569 input.closeSync(); |
| 570 } | 570 } |
| 571 | 571 |
| 572 // Test for file position functionality. | 572 // Test for file position functionality. |
| 573 static void testPosition() { | 573 static void testPosition() { |
| 574 String filename = getFilename("tests/vm/data/fixed_length_file"); | 574 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 575 RandomAccessFile input = (new File(filename)).openSync(); | 575 RandomAccessFile input = (new File(filename)).openSync(); |
| 576 input.errorHandler = (s) { | 576 input.onError = (s) { |
| 577 Expect.fail("No errors expected"); | 577 Expect.fail("No errors expected"); |
| 578 }; | 578 }; |
| 579 input.positionHandler = (position) { | 579 input.onPosition = (position) { |
| 580 Expect.equals(0, position); | 580 Expect.equals(0, position); |
| 581 List<int> buffer = new List<int>(100); | 581 List<int> buffer = new List<int>(100); |
| 582 input.readListHandler = (bytes_read) { | 582 input.onReadList = (bytes_read) { |
| 583 input.positionHandler = (position) { | 583 input.onPosition = (position) { |
| 584 Expect.equals(12, position); | 584 Expect.equals(12, position); |
| 585 input.readListHandler = (bytes_read) { | 585 input.onReadList = (bytes_read) { |
| 586 input.positionHandler = (position) { | 586 input.onPosition = (position) { |
| 587 Expect.equals(18, position); | 587 Expect.equals(18, position); |
| 588 input.setPositionHandler = () { | 588 input.onSetPosition = () { |
| 589 input.positionHandler = (position) { | 589 input.onPosition = (position) { |
| 590 Expect.equals(8, position); | 590 Expect.equals(8, position); |
| 591 input.close(); | 591 input.close(); |
| 592 }; | 592 }; |
| 593 input.position(); | 593 input.position(); |
| 594 }; | 594 }; |
| 595 input.setPosition(8); | 595 input.setPosition(8); |
| 596 }; | 596 }; |
| 597 }; | 597 }; |
| 598 input.readList(buffer, 12, 6); | 598 input.readList(buffer, 12, 6); |
| 599 }; | 599 }; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 614 input.readListSync(buffer, 12, 6); | 614 input.readListSync(buffer, 12, 6); |
| 615 Expect.equals(18, input.positionSync()); | 615 Expect.equals(18, input.positionSync()); |
| 616 input.setPositionSync(8); | 616 input.setPositionSync(8); |
| 617 Expect.equals(8, input.positionSync()); | 617 Expect.equals(8, input.positionSync()); |
| 618 input.closeSync(); | 618 input.closeSync(); |
| 619 } | 619 } |
| 620 | 620 |
| 621 static void testTruncate() { | 621 static void testTruncate() { |
| 622 File file = new File(tempDirectory.path + "/out_truncate"); | 622 File file = new File(tempDirectory.path + "/out_truncate"); |
| 623 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 623 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
| 624 file.errorHandler = (error) { | 624 file.onError = (error) { |
| 625 Expect.fail("testTruncate: No errors expected"); | 625 Expect.fail("testTruncate: No errors expected"); |
| 626 }; | 626 }; |
| 627 file.openHandler = (RandomAccessFile openedFile) { | 627 file.onOpen = (RandomAccessFile openedFile) { |
| 628 openedFile.noPendingWriteHandler = () { | 628 openedFile.onNoPendingWrite = () { |
| 629 openedFile.lengthHandler = (length) { | 629 openedFile.onLength = (length) { |
| 630 Expect.equals(10, length); | 630 Expect.equals(10, length); |
| 631 openedFile.truncateHandler = () { | 631 openedFile.onTruncate = () { |
| 632 openedFile.lengthHandler = (length) { | 632 openedFile.onLength = (length) { |
| 633 Expect.equals(5, length); | 633 Expect.equals(5, length); |
| 634 openedFile.closeHandler = () { | 634 openedFile.onClose = () { |
| 635 file.deleteHandler = () { | 635 file.onDelete = () { |
| 636 file.existsHandler = (exists) { | 636 file.onExists = (exists) { |
| 637 Expect.isFalse(exists); | 637 Expect.isFalse(exists); |
| 638 asyncTestDone("testTruncate"); | 638 asyncTestDone("testTruncate"); |
| 639 }; | 639 }; |
| 640 file.exists(); | 640 file.exists(); |
| 641 }; | 641 }; |
| 642 file.delete(); | 642 file.delete(); |
| 643 }; | 643 }; |
| 644 openedFile.close(); | 644 openedFile.close(); |
| 645 }; | 645 }; |
| 646 openedFile.length(); | 646 openedFile.length(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 input.deleteSync(); | 759 input.deleteSync(); |
| 760 } | 760 } |
| 761 | 761 |
| 762 // Tests stream exception handling after file was closed. | 762 // Tests stream exception handling after file was closed. |
| 763 static void testCloseExceptionStream() { | 763 static void testCloseExceptionStream() { |
| 764 asyncTestStarted(); | 764 asyncTestStarted(); |
| 765 List<int> buffer = new List<int>(42); | 765 List<int> buffer = new List<int>(42); |
| 766 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 766 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 767 file.createSync(); | 767 file.createSync(); |
| 768 InputStream input = file.openInputStream(); | 768 InputStream input = file.openInputStream(); |
| 769 input.closeHandler = () { | 769 input.onClose = () { |
| 770 Expect.isTrue(input.closed); | 770 Expect.isTrue(input.closed); |
| 771 Expect.isNull(input.readInto(buffer, 0, 12)); | 771 Expect.isNull(input.readInto(buffer, 0, 12)); |
| 772 OutputStream output = file.openOutputStream(); | 772 OutputStream output = file.openOutputStream(); |
| 773 output.close(); | 773 output.close(); |
| 774 Expect.throws(() => output.writeFrom(buffer, 0, 12)); | 774 Expect.throws(() => output.writeFrom(buffer, 0, 12)); |
| 775 output.closeHandler = () { | 775 output.onClose = () { |
| 776 file.deleteSync(); | 776 file.deleteSync(); |
| 777 asyncTestDone("testCloseExceptionStream"); | 777 asyncTestDone("testCloseExceptionStream"); |
| 778 }; | 778 }; |
| 779 }; | 779 }; |
| 780 } | 780 } |
| 781 | 781 |
| 782 // Tests buffer out of bounds exception. | 782 // Tests buffer out of bounds exception. |
| 783 static void testBufferOutOfBoundsException() { | 783 static void testBufferOutOfBoundsException() { |
| 784 bool exceptionCaught = false; | 784 bool exceptionCaught = false; |
| 785 bool wrongExceptionCaught = false; | 785 bool wrongExceptionCaught = false; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 } | 872 } |
| 873 Expect.equals(true, exceptionCaught); | 873 Expect.equals(true, exceptionCaught); |
| 874 Expect.equals(true, !wrongExceptionCaught); | 874 Expect.equals(true, !wrongExceptionCaught); |
| 875 openedFile.closeSync(); | 875 openedFile.closeSync(); |
| 876 file.deleteSync(); | 876 file.deleteSync(); |
| 877 } | 877 } |
| 878 | 878 |
| 879 static void testMixedSyncAndAsync() { | 879 static void testMixedSyncAndAsync() { |
| 880 var name = getFilename("tests/vm/data/fixed_length_file"); | 880 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 881 var f = new File(name); | 881 var f = new File(name); |
| 882 f.errorHandler = (s) { | 882 f.onError = (s) { |
| 883 Expect.fail("No errors expected"); | 883 Expect.fail("No errors expected"); |
| 884 }; | 884 }; |
| 885 f.existsHandler = (exists) { | 885 f.onExists = (exists) { |
| 886 try { | 886 try { |
| 887 f.existsSync(); | 887 f.existsSync(); |
| 888 Expect.fail("Expected exception"); | 888 Expect.fail("Expected exception"); |
| 889 } catch (var e) { | 889 } catch (var e) { |
| 890 Expect.isTrue(e is FileIOException); | 890 Expect.isTrue(e is FileIOException); |
| 891 } | 891 } |
| 892 }; | 892 }; |
| 893 f.exists(); | 893 f.exists(); |
| 894 } | 894 } |
| 895 | 895 |
| 896 static void testOpenDirectoryAsFile() { | 896 static void testOpenDirectoryAsFile() { |
| 897 var f = new File('.'); | 897 var f = new File('.'); |
| 898 f.open(); | 898 f.open(); |
| 899 f.openHandler = (r) => Expect.fail('Directory opened as file'); | 899 f.onOpen = (r) => Expect.fail('Directory opened as file'); |
| 900 } | 900 } |
| 901 | 901 |
| 902 static void testOpenDirectoryAsFileSync() { | 902 static void testOpenDirectoryAsFileSync() { |
| 903 var f = new File('.'); | 903 var f = new File('.'); |
| 904 try { | 904 try { |
| 905 f.openSync(); | 905 f.openSync(); |
| 906 Expect.fail("Expected exception opening directory as file"); | 906 Expect.fail("Expected exception opening directory as file"); |
| 907 } catch (var e) { | 907 } catch (var e) { |
| 908 Expect.isTrue(e is FileIOException); | 908 Expect.isTrue(e is FileIOException); |
| 909 } | 909 } |
| 910 } | 910 } |
| 911 | 911 |
| 912 static void testReadAsBytes() { | 912 static void testReadAsBytes() { |
| 913 var port = new ReceivePort.singleShot(); | 913 var port = new ReceivePort.singleShot(); |
| 914 port.receive((result, replyTo) { | 914 port.receive((result, replyTo) { |
| 915 Expect.equals(42, result); | 915 Expect.equals(42, result); |
| 916 }); | 916 }); |
| 917 var name = getFilename("tests/vm/data/fixed_length_file"); | 917 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 918 var f = new File(name); | 918 var f = new File(name); |
| 919 f.readAsBytes(); | 919 f.readAsBytes(); |
| 920 f.readAsBytesHandler = (bytes) { | 920 f.onReadAsBytes = (bytes) { |
| 921 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 921 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 922 port.toSendPort().send(bytes.length); | 922 port.toSendPort().send(bytes.length); |
| 923 }; | 923 }; |
| 924 f.errorHandler = (e) { | 924 f.onError = (e) { |
| 925 Expect.fail("No errors expected: $e"); | 925 Expect.fail("No errors expected: $e"); |
| 926 }; | 926 }; |
| 927 } | 927 } |
| 928 | 928 |
| 929 static void testReadAsBytesSync() { | 929 static void testReadAsBytesSync() { |
| 930 var name = getFilename("tests/vm/data/fixed_length_file"); | 930 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 931 var bytes = new File(name).readAsBytesSync(); | 931 var bytes = new File(name).readAsBytesSync(); |
| 932 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 932 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 933 Expect.equals(bytes.length, 42); | 933 Expect.equals(bytes.length, 42); |
| 934 } | 934 } |
| 935 | 935 |
| 936 static void testReadAsText() { | 936 static void testReadAsText() { |
| 937 var port = new ReceivePort.singleShot(); | 937 var port = new ReceivePort.singleShot(); |
| 938 port.receive((result, replyTo) { | 938 port.receive((result, replyTo) { |
| 939 Expect.equals(1, result); | 939 Expect.equals(1, result); |
| 940 }); | 940 }); |
| 941 var name = getFilename("tests/vm/data/fixed_length_file"); | 941 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 942 var f = new File(name); | 942 var f = new File(name); |
| 943 f.readAsText('UTF-8'); | 943 f.readAsText('UTF-8'); |
| 944 f.readAsTextHandler = (text) { | 944 f.onReadAsText = (text) { |
| 945 Expect.isTrue(text.endsWith("42 bytes.")); | 945 Expect.isTrue(text.endsWith("42 bytes.")); |
| 946 Expect.equals(42, text.length); | 946 Expect.equals(42, text.length); |
| 947 var name = getDataFilename("tests/standalone/src/read_as_text.dat"); | 947 var name = getDataFilename("tests/standalone/src/read_as_text.dat"); |
| 948 var f = new File(name); | 948 var f = new File(name); |
| 949 f.errorHandler = (e) => Expect.fail("No errors expected"); | 949 f.onError = (e) => Expect.fail("No errors expected"); |
| 950 f.readAsText('UTF-8'); | 950 f.readAsText('UTF-8'); |
| 951 f.readAsTextHandler = (text) { | 951 f.onReadAsText = (text) { |
| 952 Expect.equals(6, text.length); | 952 Expect.equals(6, text.length); |
| 953 var expected = [955, 120, 46, 32, 120, 10]; | 953 var expected = [955, 120, 46, 32, 120, 10]; |
| 954 Expect.listEquals(expected, text.charCodes()); | 954 Expect.listEquals(expected, text.charCodes()); |
| 955 f.readAsText('ISO-8859-1'); | 955 f.readAsText('ISO-8859-1'); |
| 956 f.readAsTextHandler = (text) { | 956 f.onReadAsText = (text) { |
| 957 Expect.equals(7, text.length); | 957 Expect.equals(7, text.length); |
| 958 var expected = [206, 187, 120, 46, 32, 120, 10]; | 958 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 959 Expect.listEquals(expected, text.charCodes()); | 959 Expect.listEquals(expected, text.charCodes()); |
| 960 f.readAsText('ASCII'); | 960 f.readAsText('ASCII'); |
| 961 f.errorHandler = (e) { | 961 f.onError = (e) { |
| 962 port.toSendPort().send(1); | 962 port.toSendPort().send(1); |
| 963 }; | 963 }; |
| 964 f.readAsTextHandler = (text) { | 964 f.onReadAsText = (text) { |
| 965 Expect.fail("Non-ascii char should cause error"); | 965 Expect.fail("Non-ascii char should cause error"); |
| 966 }; | 966 }; |
| 967 }; | 967 }; |
| 968 }; | 968 }; |
| 969 }; | 969 }; |
| 970 f.errorHandler = (e) { | 970 f.onError = (e) { |
| 971 Expect.fail("No errors expected: $e"); | 971 Expect.fail("No errors expected: $e"); |
| 972 }; | 972 }; |
| 973 } | 973 } |
| 974 | 974 |
| 975 static void testReadAsTextSync() { | 975 static void testReadAsTextSync() { |
| 976 var name = getFilename("tests/vm/data/fixed_length_file"); | 976 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 977 var text = new File(name).readAsTextSync(); | 977 var text = new File(name).readAsTextSync(); |
| 978 Expect.isTrue(text.endsWith("42 bytes.")); | 978 Expect.isTrue(text.endsWith("42 bytes.")); |
| 979 Expect.equals(42, text.length); | 979 Expect.equals(42, text.length); |
| 980 name = getDataFilename("tests/standalone/src/read_as_text.dat"); | 980 name = getDataFilename("tests/standalone/src/read_as_text.dat"); |
| 981 text = new File(name).readAsTextSync(); | 981 text = new File(name).readAsTextSync(); |
| 982 Expect.equals(6, text.length); | 982 Expect.equals(6, text.length); |
| 983 var expected = [955, 120, 46, 32, 120, 10]; | 983 var expected = [955, 120, 46, 32, 120, 10]; |
| 984 Expect.listEquals(expected, text.charCodes()); | 984 Expect.listEquals(expected, text.charCodes()); |
| 985 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); | 985 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); |
| 986 text = new File(name).readAsTextSync("ISO-8859-1"); | 986 text = new File(name).readAsTextSync("ISO-8859-1"); |
| 987 expected = [206, 187, 120, 46, 32, 120, 10]; | 987 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 988 Expect.equals(7, text.length); | 988 Expect.equals(7, text.length); |
| 989 Expect.listEquals(expected, text.charCodes()); | 989 Expect.listEquals(expected, text.charCodes()); |
| 990 } | 990 } |
| 991 | 991 |
| 992 static void testReadAsLines() { | 992 static void testReadAsLines() { |
| 993 var port = new ReceivePort.singleShot(); | 993 var port = new ReceivePort.singleShot(); |
| 994 port.receive((result, replyTo) { | 994 port.receive((result, replyTo) { |
| 995 Expect.equals(42, result); | 995 Expect.equals(42, result); |
| 996 }); | 996 }); |
| 997 var name = getFilename("tests/vm/data/fixed_length_file"); | 997 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 998 var f = new File(name); | 998 var f = new File(name); |
| 999 f.readAsLines('UTF-8'); | 999 f.readAsLines('UTF-8'); |
| 1000 f.readAsLinesHandler = (lines) { | 1000 f.onReadAsLines = (lines) { |
| 1001 Expect.equals(1, lines.length); | 1001 Expect.equals(1, lines.length); |
| 1002 var line = lines[0]; | 1002 var line = lines[0]; |
| 1003 Expect.isTrue(line.endsWith("42 bytes.")); | 1003 Expect.isTrue(line.endsWith("42 bytes.")); |
| 1004 port.toSendPort().send(line.length); | 1004 port.toSendPort().send(line.length); |
| 1005 }; | 1005 }; |
| 1006 f.errorHandler = (e) { | 1006 f.onError = (e) { |
| 1007 Expect.fail("No errors expected: $e"); | 1007 Expect.fail("No errors expected: $e"); |
| 1008 }; | 1008 }; |
| 1009 } | 1009 } |
| 1010 | 1010 |
| 1011 static void testReadAsLinesSync() { | 1011 static void testReadAsLinesSync() { |
| 1012 var name = getFilename("tests/vm/data/fixed_length_file"); | 1012 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 1013 var lines = new File(name).readAsLinesSync(); | 1013 var lines = new File(name).readAsLinesSync(); |
| 1014 Expect.equals(1, lines.length); | 1014 Expect.equals(1, lines.length); |
| 1015 var line = lines[0]; | 1015 var line = lines[0]; |
| 1016 Expect.isTrue(line.endsWith("42 bytes.")); | 1016 Expect.isTrue(line.endsWith("42 bytes.")); |
| 1017 Expect.equals(42, line.length); | 1017 Expect.equals(42, line.length); |
| 1018 name = getDataFilename("tests/standalone/src/readline_test1.dat"); | 1018 name = getDataFilename("tests/standalone/src/readline_test1.dat"); |
| 1019 lines = new File(name).readAsLinesSync(); | 1019 lines = new File(name).readAsLinesSync(); |
| 1020 Expect.equals(10, lines.length); | 1020 Expect.equals(10, lines.length); |
| 1021 } | 1021 } |
| 1022 | 1022 |
| 1023 | 1023 |
| 1024 static void testReadAsErrors() { | 1024 static void testReadAsErrors() { |
| 1025 var port = new ReceivePort.singleShot(); | 1025 var port = new ReceivePort.singleShot(); |
| 1026 port.receive((message, _) { | 1026 port.receive((message, _) { |
| 1027 Expect.equals(1, message); | 1027 Expect.equals(1, message); |
| 1028 }); | 1028 }); |
| 1029 var f = new File('.'); | 1029 var f = new File('.'); |
| 1030 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 1030 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 1031 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 1031 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 1032 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 1032 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 1033 f.readAsBytes(); | 1033 f.readAsBytes(); |
| 1034 f.errorHandler = (e) { | 1034 f.onError = (e) { |
| 1035 f.readAsText(); | 1035 f.readAsText(); |
| 1036 f.errorHandler = (e) { | 1036 f.onError = (e) { |
| 1037 f.readAsLines(); | 1037 f.readAsLines(); |
| 1038 f.errorHandler = (e) { | 1038 f.onError = (e) { |
| 1039 port.toSendPort().send(1); | 1039 port.toSendPort().send(1); |
| 1040 }; | 1040 }; |
| 1041 }; | 1041 }; |
| 1042 }; | 1042 }; |
| 1043 } | 1043 } |
| 1044 | 1044 |
| 1045 // Test that opens the same file for writing then for appending to test | 1045 // Test that opens the same file for writing then for appending to test |
| 1046 // that the file is not truncated when opened for appending. | 1046 // that the file is not truncated when opened for appending. |
| 1047 static void testAppend() { | 1047 static void testAppend() { |
| 1048 var file = new File('${tempDirectory.path}/out_append'); | 1048 var file = new File('${tempDirectory.path}/out_append'); |
| 1049 file.openHandler = (openedFile) { | 1049 file.onOpen = (openedFile) { |
| 1050 openedFile.noPendingWriteHandler = () { | 1050 openedFile.onNoPendingWrite = () { |
| 1051 openedFile.closeHandler = () { | 1051 openedFile.onClose = () { |
| 1052 file.openHandler = (openedFile) { | 1052 file.onOpen = (openedFile) { |
| 1053 openedFile.lengthHandler = (length) { | 1053 openedFile.onLength = (length) { |
| 1054 Expect.equals(4, length); | 1054 Expect.equals(4, length); |
| 1055 openedFile.setPositionHandler = () { | 1055 openedFile.onSetPosition = () { |
| 1056 openedFile.noPendingWriteHandler = () { | 1056 openedFile.onNoPendingWrite = () { |
| 1057 openedFile.lengthHandler = (length) { | 1057 openedFile.onLength = (length) { |
| 1058 Expect.equals(8, length); | 1058 Expect.equals(8, length); |
| 1059 openedFile.closeHandler = () { | 1059 openedFile.onClose = () { |
| 1060 file.deleteHandler = () { | 1060 file.onDelete = () { |
| 1061 file.existsHandler = (exists) { | 1061 file.onExists = (exists) { |
| 1062 Expect.isFalse(exists); | 1062 Expect.isFalse(exists); |
| 1063 asyncTestDone("testAppend"); | 1063 asyncTestDone("testAppend"); |
| 1064 }; | 1064 }; |
| 1065 file.exists(); | 1065 file.exists(); |
| 1066 }; | 1066 }; |
| 1067 file.delete(); | 1067 file.delete(); |
| 1068 }; | 1068 }; |
| 1069 openedFile.close(); | 1069 openedFile.close(); |
| 1070 }; | 1070 }; |
| 1071 openedFile.length(); | 1071 openedFile.length(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1147 testWriteVariousLists(); | 1147 testWriteVariousLists(); |
| 1148 testDirectory(); | 1148 testDirectory(); |
| 1149 testDirectorySync(); | 1149 testDirectorySync(); |
| 1150 }); | 1150 }); |
| 1151 } | 1151 } |
| 1152 } | 1152 } |
| 1153 | 1153 |
| 1154 main() { | 1154 main() { |
| 1155 FileTest.testMain(); | 1155 FileTest.testMain(); |
| 1156 } | 1156 } |
| OLD | NEW |