| 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 #import("dart:io"); | 5 #import("dart:io"); |
| 6 | 6 |
| 7 class FileTest { | 7 class FileTest { |
| 8 static void testOpenInvalidArgs(name) { | 8 static void testOpenInvalidArgs(name) { |
| 9 var file = new File(name); | 9 var file = new File(name); |
| 10 try { | 10 try { |
| 11 file.openSync(); | 11 file.openSync(); |
| 12 Expect.fail('exception expected'); | 12 Expect.fail('exception expected'); |
| 13 } catch (var e) { | 13 } catch (var e) { |
| 14 Expect.isTrue(e is IllegalArgumentException); | 14 Expect.isTrue(e is IllegalArgumentException); |
| 15 } | 15 } |
| 16 | 16 |
| 17 file.onError = (e) { | 17 var openFuture = file.open(FileMode.READ); |
| 18 openFuture.handleException((e) { |
| 18 Expect.isTrue(e is IllegalArgumentException); | 19 Expect.isTrue(e is IllegalArgumentException); |
| 19 }; | 20 return true; |
| 20 file.open(FileMode.READ, (opened) { | 21 }); |
| 22 openFuture.then((opened) { |
| 21 Expect.fail('non-string name open'); | 23 Expect.fail('non-string name open'); |
| 22 }); | 24 }); |
| 23 } | 25 } |
| 24 | 26 |
| 25 static void testExistsInvalidArgs(name) { | 27 static void testExistsInvalidArgs(name) { |
| 26 var file = new File(name); | 28 var file = new File(name); |
| 27 try { | 29 try { |
| 28 file.existsSync(); | 30 file.existsSync(); |
| 29 Expect.fail('exception expected'); | 31 Expect.fail('exception expected'); |
| 30 } catch (var e) { | 32 } catch (var e) { |
| 31 Expect.isTrue(e is IllegalArgumentException); | 33 Expect.isTrue(e is IllegalArgumentException); |
| 32 } | 34 } |
| 33 | 35 |
| 34 file.onError = (e) { | 36 var existsFuture = file.exists(); |
| 37 existsFuture.handleException((e) { |
| 35 Expect.isTrue(e is IllegalArgumentException); | 38 Expect.isTrue(e is IllegalArgumentException); |
| 36 }; | 39 return true; |
| 37 file.exists((bool) { | 40 }); |
| 41 existsFuture.then((bool) { |
| 38 Expect.fail('non-string name exists'); | 42 Expect.fail('non-string name exists'); |
| 39 }); | 43 }); |
| 40 } | 44 } |
| 41 | 45 |
| 42 static void testCreateInvalidArgs(name) { | 46 static void testCreateInvalidArgs(name) { |
| 43 var file = new File(name); | 47 var file = new File(name); |
| 44 try { | 48 try { |
| 45 file.createSync(); | 49 file.createSync(); |
| 46 Expect.fail('exception expected'); | 50 Expect.fail('exception expected'); |
| 47 } catch (var e) { | 51 } catch (var e) { |
| 48 Expect.isTrue(e is IllegalArgumentException); | 52 Expect.isTrue(e is IllegalArgumentException); |
| 49 } | 53 } |
| 50 | 54 |
| 51 file.onError = (e) => Expect.isTrue(e is IllegalArgumentException); | 55 var createFuture = file.create(); |
| 52 file.create(() => Expect.fail('non-string name exists')); | 56 createFuture.handleException((e) { |
| 57 Expect.isTrue(e is IllegalArgumentException); |
| 58 return true; |
| 59 }); |
| 60 createFuture.then((ignore) => Expect.fail('non-string name exists')); |
| 53 } | 61 } |
| 54 | 62 |
| 55 static void testReadListInvalidArgs(buffer, offset, length) { | 63 static void testReadListInvalidArgs(buffer, offset, length) { |
| 56 String filename = getFilename("tests/vm/data/fixed_length_file"); | 64 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 57 var file = (new File(filename)).openSync(); | 65 var file = (new File(filename)).openSync(); |
| 58 try { | 66 try { |
| 59 file.readListSync(buffer, offset, length); | 67 file.readListSync(buffer, offset, length); |
| 60 Expect.fail('exception expected'); | 68 Expect.fail('exception expected'); |
| 61 } catch (var e) { | 69 } catch (var e) { |
| 62 Expect.isTrue(e is FileIOException); | 70 Expect.isTrue(e is FileIOException); |
| 63 Expect.isTrue(e.toString().contains('Invalid arguments')); | 71 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 64 } | 72 } |
| 65 | 73 |
| 66 var errors = 0; | 74 var errors = 0; |
| 67 file.onError = (s) { | 75 var readListFuture = file.readList(buffer, offset, length); |
| 76 readListFuture.handleException((e) { |
| 68 errors++; | 77 errors++; |
| 69 Expect.isTrue(s is FileIOException); | 78 Expect.isTrue(e is FileIOException); |
| 70 Expect.isTrue(s.toString().contains('Invalid arguments')); | 79 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 71 file.close(() { | 80 file.close().then((ignore) { |
| 72 Expect.equals(1, errors); | 81 Expect.equals(1, errors); |
| 73 }); | 82 }); |
| 74 }; | 83 return true; |
| 75 file.readList(buffer, offset, length, (bytes) { | 84 }); |
| 85 readListFuture.then((bytes) { |
| 76 Expect.fail('read list invalid arguments'); | 86 Expect.fail('read list invalid arguments'); |
| 77 }); | 87 }); |
| 78 } | 88 } |
| 79 | 89 |
| 80 static void testWriteByteInvalidArgs(value) { | 90 static void testWriteByteInvalidArgs(value) { |
| 81 String filename = getFilename("tests/vm/data/fixed_length_file"); | 91 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 82 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); | 92 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); |
| 83 try { | 93 try { |
| 84 file.writeByteSync(value); | 94 file.writeByteSync(value); |
| 85 Expect.fail('exception expected'); | 95 Expect.fail('exception expected'); |
| 86 } catch (var e) { | 96 } catch (var e) { |
| 87 Expect.isTrue(e is FileIOException); | 97 Expect.isTrue(e is FileIOException); |
| 88 Expect.isTrue(e.toString().contains('Invalid argument')); | 98 Expect.isTrue(e.toString().contains('Invalid argument')); |
| 89 } | 99 } |
| 90 | 100 |
| 91 var errors = 0; | 101 var writeByteFuture = file.writeByte(value); |
| 92 file.onError = (s) { | 102 writeByteFuture.then((ignore) { |
| 93 errors++; | 103 Expect.fail('write byte invalid argument'); |
| 104 }); |
| 105 writeByteFuture.handleException((s) { |
| 94 Expect.isTrue(s is FileIOException); | 106 Expect.isTrue(s is FileIOException); |
| 95 Expect.isTrue(s.toString().contains('Invalid argument')); | 107 Expect.isTrue(s.toString().contains('Invalid argument')); |
| 96 file.close(() { | 108 file.close(); |
| 97 Expect.equals(1, errors); | 109 return true; |
| 98 }); | 110 }); |
| 99 }; | |
| 100 var calls = 0; | |
| 101 file.writeByte(value); | |
| 102 file.onNoPendingWrites = () { | |
| 103 if (++calls > 1) Expect.fail('write list invalid argument'); | |
| 104 }; | |
| 105 } | 111 } |
| 106 | 112 |
| 107 static void testWriteListInvalidArgs(buffer, offset, bytes) { | 113 static void testWriteListInvalidArgs(buffer, offset, bytes) { |
| 108 String filename = getFilename("tests/vm/data/fixed_length_file"); | 114 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 109 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); | 115 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); |
| 110 try { | 116 try { |
| 111 file.writeListSync(buffer, offset, bytes); | 117 file.writeListSync(buffer, offset, bytes); |
| 112 Expect.fail('exception expected'); | 118 Expect.fail('exception expected'); |
| 113 } catch (var e) { | 119 } catch (var e) { |
| 114 Expect.isTrue(e is FileIOException); | 120 Expect.isTrue(e is FileIOException); |
| 115 Expect.isTrue(e.toString().contains('Invalid arguments')); | 121 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 116 } | 122 } |
| 117 | 123 |
| 118 var errors = 0; | 124 var writeListFuture = file.writeList(buffer, offset, bytes); |
| 119 file.onError = (s) { | 125 writeListFuture.then((ignore) { |
| 120 errors++; | 126 Expect.fail('write list invalid argument'); |
| 127 }); |
| 128 writeListFuture.handleException((s) { |
| 121 Expect.isTrue(s is FileIOException); | 129 Expect.isTrue(s is FileIOException); |
| 122 Expect.isTrue(s.toString().contains('Invalid arguments')); | 130 Expect.isTrue(s.toString().contains('Invalid arguments')); |
| 123 file.close(() { | 131 file.close(); |
| 124 Expect.equals(1, errors); | 132 return true; |
| 125 }); | 133 }); |
| 126 }; | |
| 127 var calls = 0; | |
| 128 file.writeList(buffer, offset, bytes); | |
| 129 file.onNoPendingWrites = () { | |
| 130 if (++calls > 1) Expect.fail('write string invalid argument'); | |
| 131 }; | |
| 132 } | 134 } |
| 133 | 135 |
| 134 static void testWriteStringInvalidArgs(string) { | 136 static void testWriteStringInvalidArgs(string) { |
| 135 String filename = getFilename("tests/vm/data/fixed_length_file"); | 137 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 136 var file = new File(filename + "_out"); | 138 var file = new File(filename + "_out"); |
| 137 file.openSync(FileMode.WRITE); | 139 file.openSync(FileMode.WRITE); |
| 138 try { | 140 try { |
| 139 file.writeString(string); | 141 file.writeString(string); |
| 140 Expect.fail('exception expected'); | 142 Expect.fail('exception expected'); |
| 141 } catch (var e) { | 143 } catch (var e) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 | 163 |
| 162 static void testFullPathInvalidArgs(name) { | 164 static void testFullPathInvalidArgs(name) { |
| 163 var file = new File(name); | 165 var file = new File(name); |
| 164 try { | 166 try { |
| 165 file.fullPathSync(); | 167 file.fullPathSync(); |
| 166 Expect.fail('exception expected'); | 168 Expect.fail('exception expected'); |
| 167 } catch (var e) { | 169 } catch (var e) { |
| 168 Expect.isTrue(e is IllegalArgumentException); | 170 Expect.isTrue(e is IllegalArgumentException); |
| 169 } | 171 } |
| 170 | 172 |
| 171 file.onError = (e) { | 173 var fullPathFuture = file.fullPath(); |
| 174 fullPathFuture.handleException((e) { |
| 172 Expect.isTrue(e is IllegalArgumentException); | 175 Expect.isTrue(e is IllegalArgumentException); |
| 173 }; | 176 return true; |
| 174 file.fullPath((path) { | 177 }); |
| 178 fullPathFuture.then((path) { |
| 175 Expect.fail('full path invalid argument'); | 179 Expect.fail('full path invalid argument'); |
| 176 }); | 180 }); |
| 177 } | 181 } |
| 178 | 182 |
| 179 static String getFilename(String path) => | 183 static String getFilename(String path) => |
| 180 new File(path).existsSync() ? path : 'runtime/' + path; | 184 new File(path).existsSync() ? path : 'runtime/' + path; |
| 181 } | 185 } |
| 182 | 186 |
| 183 main() { | 187 main() { |
| 184 FileTest.testOpenInvalidArgs(0); | 188 FileTest.testOpenInvalidArgs(0); |
| 185 FileTest.testExistsInvalidArgs(0); | 189 FileTest.testExistsInvalidArgs(0); |
| 186 FileTest.testCreateInvalidArgs(0); | 190 FileTest.testCreateInvalidArgs(0); |
| 187 FileTest.testReadListInvalidArgs(12, 0, 1); | 191 FileTest.testReadListInvalidArgs(12, 0, 1); |
| 188 FileTest.testReadListInvalidArgs(new List(10), '0', 1); | 192 FileTest.testReadListInvalidArgs(new List(10), '0', 1); |
| 189 FileTest.testReadListInvalidArgs(new List(10), 0, '1'); | 193 FileTest.testReadListInvalidArgs(new List(10), 0, '1'); |
| 190 FileTest.testWriteByteInvalidArgs('asdf'); | 194 FileTest.testWriteByteInvalidArgs('asdf'); |
| 191 FileTest.testWriteListInvalidArgs(12, 0, 1); | 195 FileTest.testWriteListInvalidArgs(12, 0, 1); |
| 192 FileTest.testWriteListInvalidArgs(new List(10), '0', 1); | 196 FileTest.testWriteListInvalidArgs(new List(10), '0', 1); |
| 193 FileTest.testWriteListInvalidArgs(new List(10), 0, '1'); | 197 FileTest.testWriteListInvalidArgs(new List(10), 0, '1'); |
| 194 FileTest.testFullPathInvalidArgs(12); | 198 FileTest.testFullPathInvalidArgs(12); |
| 195 } | 199 } |
| OLD | NEW |