| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a | 
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. | 
| 4 // | 4 // | 
| 5 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. | 
| 6 | 6 | 
|  | 7 // OtherResources=empty_file | 
|  | 8 // OtherResources=file_test.txt | 
| 7 // OtherResources=fixed_length_file | 9 // OtherResources=fixed_length_file | 
| 8 // OtherResources=read_as_text.dat | 10 // OtherResources=read_as_text.dat | 
| 9 // OtherResources=readline_test1.dat | 11 // OtherResources=readline_test1.dat | 
| 10 | 12 | 
| 11 import 'dart:async'; | 13 import 'dart:async'; | 
| 12 import 'dart:convert'; | 14 import 'dart:convert'; | 
| 13 import 'dart:collection'; | 15 import 'dart:collection'; | 
| 14 import 'dart:io'; | 16 import 'dart:io'; | 
| 15 | 17 | 
| 16 import "package:async_helper/async_helper.dart"; | 18 import "package:async_helper/async_helper.dart"; | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 56     }); | 58     }); | 
| 57   } | 59   } | 
| 58 | 60 | 
| 59   static void deleteTempDirectory() { | 61   static void deleteTempDirectory() { | 
| 60     tempDirectory.deleteSync(recursive: true); | 62     tempDirectory.deleteSync(recursive: true); | 
| 61   } | 63   } | 
| 62 | 64 | 
| 63   // Test for file read functionality. | 65   // Test for file read functionality. | 
| 64   static void testReadStream() { | 66   static void testReadStream() { | 
| 65     // Read a file and check part of it's contents. | 67     // Read a file and check part of it's contents. | 
| 66     String filename = getFilename("bin/file_test.cc"); | 68     String filename = getFilename("file_test.txt"); | 
| 67     File file = new File(filename); | 69     File file = new File(filename); | 
| 68     Expect.isTrue('$file'.contains(file.path)); | 70     Expect.isTrue('$file'.contains(file.path)); | 
| 69     var subscription; | 71     var subscription; | 
| 70     List<int> buffer = new List<int>(); | 72     List<int> buffer = new List<int>(); | 
| 71     subscription = file.openRead().listen( | 73     subscription = file.openRead().listen( | 
| 72         (d) { | 74         (d) { | 
| 73           buffer.addAll(d); | 75           buffer.addAll(d); | 
| 74           if (buffer.length >= 12) { | 76           if (buffer.length >= 12) { | 
| 75             subscription.cancel(); | 77             subscription.cancel(); | 
| 76             Expect.equals(47, buffer[0]);  // represents '/' in the file. | 78             Expect.equals(47, buffer[0]);  // represents '/' in the file. | 
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 223             Expect.equals(2 * buffer.length, position); | 225             Expect.equals(2 * buffer.length, position); | 
| 224             outputFile.delete().then((ignore) { done.complete(); }); | 226             outputFile.delete().then((ignore) { done.complete(); }); | 
| 225           }); | 227           }); | 
| 226       }); | 228       }); | 
| 227     return done.future; | 229     return done.future; | 
| 228   } | 230   } | 
| 229 | 231 | 
| 230   static void testRead() { | 232   static void testRead() { | 
| 231     asyncStart(); | 233     asyncStart(); | 
| 232     // Read a file and check part of it's contents. | 234     // Read a file and check part of it's contents. | 
| 233     String filename = getFilename("bin/file_test.cc"); | 235     String filename = getFilename("file_test.txt"); | 
| 234     File file = new File(filename); | 236     File file = new File(filename); | 
| 235     file.open(mode: READ).then((RandomAccessFile file) { | 237     file.open(mode: READ).then((RandomAccessFile file) { | 
| 236       List<int> buffer = new List<int>(10); | 238       List<int> buffer = new List<int>(10); | 
| 237       file.readInto(buffer, 0, 5).then((bytes_read) { | 239       file.readInto(buffer, 0, 5).then((bytes_read) { | 
| 238         Expect.equals(5, bytes_read); | 240         Expect.equals(5, bytes_read); | 
| 239         file.readInto(buffer, 5, 10).then((bytes_read) { | 241         file.readInto(buffer, 5, 10).then((bytes_read) { | 
| 240           Expect.equals(5, bytes_read); | 242           Expect.equals(5, bytes_read); | 
| 241           Expect.equals(47, buffer[0]);  // represents '/' in the file. | 243           Expect.equals(47, buffer[0]);  // represents '/' in the file. | 
| 242           Expect.equals(47, buffer[1]);  // represents '/' in the file. | 244           Expect.equals(47, buffer[1]);  // represents '/' in the file. | 
| 243           Expect.equals(32, buffer[2]);  // represents ' ' in the file. | 245           Expect.equals(32, buffer[2]);  // represents ' ' in the file. | 
| 244           Expect.equals(67, buffer[3]);  // represents 'C' in the file. | 246           Expect.equals(67, buffer[3]);  // represents 'C' in the file. | 
| 245           Expect.equals(111, buffer[4]);  // represents 'o' in the file. | 247           Expect.equals(111, buffer[4]);  // represents 'o' in the file. | 
| 246           Expect.equals(112, buffer[5]);  // represents 'p' in the file. | 248           Expect.equals(112, buffer[5]);  // represents 'p' in the file. | 
| 247           Expect.equals(121, buffer[6]);  // represents 'y' in the file. | 249           Expect.equals(121, buffer[6]);  // represents 'y' in the file. | 
| 248           Expect.equals(114, buffer[7]);  // represents 'r' in the file. | 250           Expect.equals(114, buffer[7]);  // represents 'r' in the file. | 
| 249           Expect.equals(105, buffer[8]);  // represents 'i' in the file. | 251           Expect.equals(105, buffer[8]);  // represents 'i' in the file. | 
| 250           Expect.equals(103, buffer[9]);  // represents 'g' in the file. | 252           Expect.equals(103, buffer[9]);  // represents 'g' in the file. | 
| 251           file.close().then((ignore) => asyncEnd()); | 253           file.close().then((ignore) => asyncEnd()); | 
| 252         }); | 254         }); | 
| 253       }); | 255       }); | 
| 254     }); | 256     }); | 
| 255   } | 257   } | 
| 256 | 258 | 
| 257   static void testReadSync() { | 259   static void testReadSync() { | 
| 258     // Read a file and check part of it's contents. | 260     // Read a file and check part of it's contents. | 
| 259     String filename = getFilename("bin/file_test.cc"); | 261     String filename = getFilename("file_test.txt"); | 
| 260     RandomAccessFile raf = (new File(filename)).openSync(); | 262     RandomAccessFile raf = (new File(filename)).openSync(); | 
| 261     List<int> buffer = new List<int>(42); | 263     List<int> buffer = new List<int>(42); | 
| 262     int bytes_read = 0; | 264     int bytes_read = 0; | 
| 263     bytes_read = raf.readIntoSync(buffer, 0, 12); | 265     bytes_read = raf.readIntoSync(buffer, 0, 12); | 
| 264     Expect.equals(12, bytes_read); | 266     Expect.equals(12, bytes_read); | 
| 265     bytes_read = raf.readIntoSync(buffer, 12, 42); | 267     bytes_read = raf.readIntoSync(buffer, 12, 42); | 
| 266     Expect.equals(30, bytes_read); | 268     Expect.equals(30, bytes_read); | 
| 267     Expect.equals(47, buffer[0]);  // represents '/' in the file. | 269     Expect.equals(47, buffer[0]);  // represents '/' in the file. | 
| 268     Expect.equals(47, buffer[1]);  // represents '/' in the file. | 270     Expect.equals(47, buffer[1]);  // represents '/' in the file. | 
| 269     Expect.equals(32, buffer[2]);  // represents ' ' in the file. | 271     Expect.equals(32, buffer[2]);  // represents ' ' in the file. | 
| (...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1064     var f = new File(name); | 1066     var f = new File(name); | 
| 1065     f.readAsBytes().then((bytes) { | 1067     f.readAsBytes().then((bytes) { | 
| 1066       Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 1068       Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 
| 1067       Expect.equals(42, bytes.length); | 1069       Expect.equals(42, bytes.length); | 
| 1068       asyncTestDone("testReadAsBytes"); | 1070       asyncTestDone("testReadAsBytes"); | 
| 1069     }); | 1071     }); | 
| 1070   } | 1072   } | 
| 1071 | 1073 | 
| 1072   static void testReadAsBytesEmptyFile() { | 1074   static void testReadAsBytesEmptyFile() { | 
| 1073     asyncTestStarted(); | 1075     asyncTestStarted(); | 
| 1074     var name = getFilename("tests/vm/data/empty_file"); | 1076     var name = getFilename("empty_file"); | 
| 1075     var f = new File(name); | 1077     var f = new File(name); | 
| 1076     f.readAsBytes().then((bytes) { | 1078     f.readAsBytes().then((bytes) { | 
| 1077       Expect.equals(0, bytes.length); | 1079       Expect.equals(0, bytes.length); | 
| 1078       asyncTestDone("testReadAsBytesEmptyFile"); | 1080       asyncTestDone("testReadAsBytesEmptyFile"); | 
| 1079     }); | 1081     }); | 
| 1080   } | 1082   } | 
| 1081 | 1083 | 
| 1082   static void testReadAsBytesSync() { | 1084   static void testReadAsBytesSync() { | 
| 1083     var name = getFilename("fixed_length_file"); | 1085     var name = getFilename("fixed_length_file"); | 
| 1084     var bytes = new File(name).readAsBytesSync(); | 1086     var bytes = new File(name).readAsBytesSync(); | 
| 1085     Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 1087     Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 
| 1086     Expect.equals(bytes.length, 42); | 1088     Expect.equals(bytes.length, 42); | 
| 1087   } | 1089   } | 
| 1088 | 1090 | 
| 1089   static void testReadAsBytesSyncEmptyFile() { | 1091   static void testReadAsBytesSyncEmptyFile() { | 
| 1090     var name = getFilename("tests/vm/data/empty_file"); | 1092     var name = getFilename("empty_file"); | 
| 1091     var bytes = new File(name).readAsBytesSync(); | 1093     var bytes = new File(name).readAsBytesSync(); | 
| 1092     Expect.equals(bytes.length, 0); | 1094     Expect.equals(bytes.length, 0); | 
| 1093   } | 1095   } | 
| 1094 | 1096 | 
| 1095   static void testReadAsText() { | 1097   static void testReadAsText() { | 
| 1096     asyncTestStarted(); | 1098     asyncTestStarted(); | 
| 1097     var name = getFilename("fixed_length_file"); | 1099     var name = getFilename("fixed_length_file"); | 
| 1098     var f = new File(name); | 1100     var f = new File(name); | 
| 1099     f.readAsString(encoding: UTF8).then((text) { | 1101     f.readAsString(encoding: UTF8).then((text) { | 
| 1100       Expect.isTrue(text.endsWith("42 bytes.")); | 1102       Expect.isTrue(text.endsWith("42 bytes.")); | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 1115           }).catchError((e) { | 1117           }).catchError((e) { | 
| 1116             asyncTestDone("testReadAsText"); | 1118             asyncTestDone("testReadAsText"); | 
| 1117           }); | 1119           }); | 
| 1118         }); | 1120         }); | 
| 1119       }); | 1121       }); | 
| 1120     }); | 1122     }); | 
| 1121   } | 1123   } | 
| 1122 | 1124 | 
| 1123   static void testReadAsTextEmptyFile() { | 1125   static void testReadAsTextEmptyFile() { | 
| 1124     asyncTestStarted(); | 1126     asyncTestStarted(); | 
| 1125     var name = getFilename("tests/vm/data/empty_file"); | 1127     var name = getFilename("empty_file"); | 
| 1126     var f = new File(name); | 1128     var f = new File(name); | 
| 1127     f.readAsString(encoding: UTF8).then((text) { | 1129     f.readAsString(encoding: UTF8).then((text) { | 
| 1128       Expect.equals(0, text.length); | 1130       Expect.equals(0, text.length); | 
| 1129       asyncTestDone("testReadAsTextEmptyFile"); | 1131       asyncTestDone("testReadAsTextEmptyFile"); | 
| 1130       return true; | 1132       return true; | 
| 1131     }); | 1133     }); | 
| 1132   } | 1134   } | 
| 1133 | 1135 | 
| 1134   static void testReadAsTextSync() { | 1136   static void testReadAsTextSync() { | 
| 1135     var name = getFilename("fixed_length_file"); | 1137     var name = getFilename("fixed_length_file"); | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 1152                 UNICODE_REPLACEMENT_CHARACTER_RUNE, | 1154                 UNICODE_REPLACEMENT_CHARACTER_RUNE, | 
| 1153                 120, 46, 32, 120, 10]; | 1155                 120, 46, 32, 120, 10]; | 
| 1154     Expect.listEquals(expected, text.codeUnits); | 1156     Expect.listEquals(expected, text.codeUnits); | 
| 1155     text = new File(name).readAsStringSync(encoding: LATIN1); | 1157     text = new File(name).readAsStringSync(encoding: LATIN1); | 
| 1156     expected = [206, 187, 120, 46, 32, 120, 10]; | 1158     expected = [206, 187, 120, 46, 32, 120, 10]; | 
| 1157     Expect.equals(7, text.length); | 1159     Expect.equals(7, text.length); | 
| 1158     Expect.listEquals(expected, text.codeUnits); | 1160     Expect.listEquals(expected, text.codeUnits); | 
| 1159   } | 1161   } | 
| 1160 | 1162 | 
| 1161   static void testReadAsTextSyncEmptyFile() { | 1163   static void testReadAsTextSyncEmptyFile() { | 
| 1162     var name = getFilename("tests/vm/data/empty_file"); | 1164     var name = getFilename("empty_file"); | 
| 1163     var text = new File(name).readAsStringSync(); | 1165     var text = new File(name).readAsStringSync(); | 
| 1164     Expect.equals(0, text.length); | 1166     Expect.equals(0, text.length); | 
| 1165   } | 1167   } | 
| 1166 | 1168 | 
| 1167   static void testReadAsLines() { | 1169   static void testReadAsLines() { | 
| 1168     asyncTestStarted(); | 1170     asyncTestStarted(); | 
| 1169     var name = getFilename("fixed_length_file"); | 1171     var name = getFilename("fixed_length_file"); | 
| 1170     var f = new File(name); | 1172     var f = new File(name); | 
| 1171     f.readAsLines(encoding: UTF8).then((lines) { | 1173     f.readAsLines(encoding: UTF8).then((lines) { | 
| 1172       Expect.equals(1, lines.length); | 1174       Expect.equals(1, lines.length); | 
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1395     newfile.deleteSync(); | 1397     newfile.deleteSync(); | 
| 1396     Expect.isFalse(newfile.existsSync()); | 1398     Expect.isFalse(newfile.existsSync()); | 
| 1397     if (Platform.operatingSystem != "windows") { | 1399     if (Platform.operatingSystem != "windows") { | 
| 1398       var brokenLink = new Link(source); | 1400       var brokenLink = new Link(source); | 
| 1399       brokenLink.createSync(dest); | 1401       brokenLink.createSync(dest); | 
| 1400       Expect.throws(() => file.renameSync('xxx')); | 1402       Expect.throws(() => file.renameSync('xxx')); | 
| 1401       brokenLink.deleteSync(); | 1403       brokenLink.deleteSync(); | 
| 1402     } | 1404     } | 
| 1403   } | 1405   } | 
| 1404 | 1406 | 
| 1405   // Helper method to be able to run the test from the runtime |  | 
| 1406   // directory, or the top directory. |  | 
| 1407   static String getFilename(String path) { | 1407   static String getFilename(String path) { | 
| 1408     var testPath = Platform.script.resolve(path); | 1408     return Platform.script.resolve(path).toFilePath(); | 
| 1409     if (new File.fromUri(testPath).existsSync()) { |  | 
| 1410       return testPath.toFilePath(); |  | 
| 1411     } |  | 
| 1412     return Uri.parse(Platform.resolvedExecutable) |  | 
| 1413               .resolve('../../runtime/$path').toFilePath(); |  | 
| 1414   } | 1409   } | 
| 1415 | 1410 | 
| 1416   // Main test entrypoint. | 1411   // Main test entrypoint. | 
| 1417   static testMain() { | 1412   static testMain() { | 
| 1418     asyncStart(); | 1413     asyncStart(); | 
| 1419 | 1414 | 
| 1420     testRead(); | 1415     testRead(); | 
| 1421     testReadSync(); | 1416     testReadSync(); | 
| 1422     testReadStream(); | 1417     testReadStream(); | 
| 1423     testLengthSync(); | 1418     testLengthSync(); | 
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1473       testLastModified(); | 1468       testLastModified(); | 
| 1474       testDoubleAsyncOperation(); | 1469       testDoubleAsyncOperation(); | 
| 1475       asyncEnd(); | 1470       asyncEnd(); | 
| 1476     }); | 1471     }); | 
| 1477   } | 1472   } | 
| 1478 } | 1473 } | 
| 1479 | 1474 | 
| 1480 main() { | 1475 main() { | 
| 1481   FileTest.testMain(); | 1476   FileTest.testMain(); | 
| 1482 } | 1477 } | 
| OLD | NEW | 
|---|