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, [writable = false]) { | 8 static void testOpenInvalidArgs(name, [writable = false]) { |
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 FileIOException); | 14 Expect.isTrue(e is FileIOException); |
15 Expect.isTrue(e.toString().contains('open file')); | 15 Expect.isTrue(e.toString().contains('open file')); |
16 } | 16 } |
17 | 17 |
18 file.onError = (s) { | 18 file.errorHandler = (s) { |
19 Expect.isTrue(s.contains('open file')); | 19 Expect.isTrue(s.contains('open file')); |
20 }; | 20 }; |
21 file.open(FileMode.READ, (opened) { | 21 file.openHandler = () { |
22 Expect.fail('non-string name open'); | 22 Expect.fail('non-string name open'); |
23 }); | 23 }; |
| 24 file.open(); |
24 } | 25 } |
25 | 26 |
26 static void testExistsInvalidArgs(name) { | 27 static void testExistsInvalidArgs(name) { |
27 var file = new File(name); | 28 var file = new File(name); |
28 try { | 29 try { |
29 file.existsSync(); | 30 file.existsSync(); |
30 Expect.fail('exception expected'); | 31 Expect.fail('exception expected'); |
31 } catch (var e) { | 32 } catch (var e) { |
32 Expect.isTrue(e is FileIOException); | 33 Expect.isTrue(e is FileIOException); |
33 Expect.isTrue(e.toString().contains('is not a string')); | 34 Expect.isTrue(e.toString().contains('is not a string')); |
34 } | 35 } |
35 | 36 |
36 file.onError = (s) { | 37 file.errorHandler = (s) { |
37 Expect.isTrue(s.contains('is not a string')); | 38 Expect.isTrue(s.contains('is not a string')); |
38 }; | 39 }; |
39 file.exists((bool) { | 40 file.existsHandler = (bool) { |
40 Expect.fail('non-string name exists'); | 41 Expect.fail('non-string name exists'); |
41 }); | 42 }; |
| 43 file.exists(); |
42 } | 44 } |
43 | 45 |
44 static void testCreateInvalidArgs(name) { | 46 static void testCreateInvalidArgs(name) { |
45 var file = new File(name); | 47 var file = new File(name); |
46 try { | 48 try { |
47 file.createSync(); | 49 file.createSync(); |
48 Expect.fail('exception expected'); | 50 Expect.fail('exception expected'); |
49 } catch (var e) { | 51 } catch (var e) { |
50 Expect.isTrue(e is FileIOException); | 52 Expect.isTrue(e is FileIOException); |
51 Expect.isTrue(e.toString().contains('Cannot create file')); | 53 Expect.isTrue(e.toString().contains('Cannot create file')); |
52 } | 54 } |
53 | 55 |
54 file.onError = (s) { | 56 file.errorHandler = (s) { |
55 Expect.isTrue(s.contains('Cannot create file')); | 57 Expect.isTrue(s.contains('Cannot create file')); |
56 }; | 58 }; |
57 file.create((created) { | 59 file.createHandler = (created) { |
58 Expect.fail('non-string name exists'); | 60 Expect.fail('non-string name exists'); |
59 }); | 61 }; |
| 62 file.create(); |
60 } | 63 } |
61 | 64 |
62 static void testReadListInvalidArgs(buffer, offset, length) { | 65 static void testReadListInvalidArgs(buffer, offset, length) { |
63 String filename = getFilename("tests/vm/data/fixed_length_file"); | 66 String filename = getFilename("tests/vm/data/fixed_length_file"); |
64 var file = (new File(filename)).openSync(); | 67 var file = (new File(filename)).openSync(); |
65 try { | 68 try { |
66 file.readListSync(buffer, offset, length); | 69 file.readListSync(buffer, offset, length); |
67 Expect.fail('exception expected'); | 70 Expect.fail('exception expected'); |
68 } catch (var e) { | 71 } catch (var e) { |
69 Expect.isTrue(e is FileIOException); | 72 Expect.isTrue(e is FileIOException); |
70 Expect.isTrue(e.toString().contains('Invalid arguments')); | 73 Expect.isTrue(e.toString().contains('Invalid arguments')); |
71 } | 74 } |
72 | 75 |
73 var errors = 0; | 76 var errors = 0; |
74 file.onError = (s) { | 77 file.errorHandler = (s) { |
75 errors++; | 78 errors++; |
76 Expect.isTrue(s.contains('Invalid arguments')); | 79 Expect.isTrue(s.contains('Invalid arguments')); |
77 file.close(() { | |
78 Expect.equals(1, errors); | |
79 }); | |
80 }; | 80 }; |
81 file.readList(buffer, offset, length, (bytes) { | 81 file.readListHandler = (bytes) { |
82 Expect.fail('read list invalid arguments'); | 82 Expect.fail('read list invalid arguments'); |
83 }); | 83 }; |
| 84 file.readList(buffer, offset, length); |
| 85 file.closeHandler = () { |
| 86 Expect.equals(1, errors); |
| 87 }; |
| 88 file.close(); |
84 } | 89 } |
85 | 90 |
86 static void testWriteByteInvalidArgs(value) { | 91 static void testWriteByteInvalidArgs(value) { |
87 String filename = getFilename("tests/vm/data/fixed_length_file"); | 92 String filename = getFilename("tests/vm/data/fixed_length_file"); |
88 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); | 93 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); |
89 try { | 94 try { |
90 file.writeByteSync(value); | 95 file.writeByteSync(value); |
91 Expect.fail('exception expected'); | 96 Expect.fail('exception expected'); |
92 } catch (var e) { | 97 } catch (var e) { |
93 Expect.isTrue(e is FileIOException); | 98 Expect.isTrue(e is FileIOException); |
94 Expect.isTrue(e.toString().contains('Invalid argument')); | 99 Expect.isTrue(e.toString().contains('Invalid argument')); |
95 } | 100 } |
96 | 101 |
97 var errors = 0; | 102 var errors = 0; |
98 file.onError = (s) { | 103 file.errorHandler = (s) { |
99 errors++; | 104 errors++; |
100 Expect.isTrue(s.contains('Invalid argument')); | 105 Expect.isTrue(s.contains('Invalid argument')); |
101 file.close(() { | |
102 Expect.equals(1, errors); | |
103 }); | |
104 }; | 106 }; |
105 var calls = 0; | 107 var calls = 0; |
106 file.writeByte(value); | 108 file.noPendingWriteHandler = () { |
107 file.onNoPendingWrites = () { | |
108 if (++calls > 1) Expect.fail('write list invalid argument'); | 109 if (++calls > 1) Expect.fail('write list invalid argument'); |
109 }; | 110 }; |
| 111 file.writeByte(value); |
| 112 file.closeHandler = () { |
| 113 Expect.equals(1, errors); |
| 114 }; |
| 115 file.close(); |
110 } | 116 } |
111 | 117 |
112 static void testWriteListInvalidArgs(buffer, offset, bytes) { | 118 static void testWriteListInvalidArgs(buffer, offset, bytes) { |
113 String filename = getFilename("tests/vm/data/fixed_length_file"); | 119 String filename = getFilename("tests/vm/data/fixed_length_file"); |
114 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); | 120 var file = (new File(filename + "_out")).openSync(FileMode.WRITE); |
115 try { | 121 try { |
116 file.writeListSync(buffer, offset, bytes); | 122 file.writeListSync(buffer, offset, bytes); |
117 Expect.fail('exception expected'); | 123 Expect.fail('exception expected'); |
118 } catch (var e) { | 124 } catch (var e) { |
119 Expect.isTrue(e is FileIOException); | 125 Expect.isTrue(e is FileIOException); |
120 Expect.isTrue(e.toString().contains('Invalid arguments')); | 126 Expect.isTrue(e.toString().contains('Invalid arguments')); |
121 } | 127 } |
122 | 128 |
123 var errors = 0; | 129 var errors = 0; |
124 file.onError = (s) { | 130 file.errorHandler = (s) { |
125 errors++; | 131 errors++; |
126 Expect.isTrue(s.contains('Invalid arguments')); | 132 Expect.isTrue(s.contains('Invalid arguments')); |
127 file.close(() { | |
128 Expect.equals(1, errors); | |
129 }); | |
130 }; | 133 }; |
131 var calls = 0; | 134 var calls = 0; |
132 file.writeList(buffer, offset, bytes); | 135 file.noPendingWriteHandler = () { |
133 file.onNoPendingWrites = () { | |
134 if (++calls > 1) Expect.fail('write string invalid argument'); | 136 if (++calls > 1) Expect.fail('write string invalid argument'); |
135 }; | 137 }; |
| 138 file.writeList(buffer, offset, bytes); |
| 139 file.closeHandler = () { |
| 140 Expect.equals(1, errors); |
| 141 }; |
| 142 file.close(); |
136 } | 143 } |
137 | 144 |
138 static void testWriteStringInvalidArgs(string) { | 145 static void testWriteStringInvalidArgs(string) { |
139 String filename = getFilename("tests/vm/data/fixed_length_file"); | 146 String filename = getFilename("tests/vm/data/fixed_length_file"); |
140 var file = new File(filename + "_out"); | 147 var file = new File(filename + "_out"); |
141 file.openSync(FileMode.WRITE); | 148 file.openSync(FileMode.WRITE); |
142 try { | 149 try { |
143 file.writeString(string); | 150 file.writeString(string); |
144 Expect.fail('exception expected'); | 151 Expect.fail('exception expected'); |
145 } catch (var e) { | 152 } catch (var e) { |
146 Expect.isTrue(e is FileIOException); | 153 Expect.isTrue(e is FileIOException); |
147 Expect.isTrue(e.toString().contains('writeString failed')); | 154 Expect.isTrue(e.toString().contains('writeString failed')); |
148 } | 155 } |
149 | 156 |
150 var errors = 0; | 157 var errors = 0; |
151 file.onError = (s) { | 158 file.errorHandler = (s) { |
152 errors++; | 159 errors++; |
153 Expect.isTrue(s.contains('writeString failed')); | 160 Expect.isTrue(s.contains('writeString failed')); |
154 }; | 161 }; |
155 var calls = 0; | 162 var calls = 0; |
156 file.onNoPendingWrites = () { | 163 file.noPendingWriteHandler = () { |
157 if (++calls > 1) Expect.fail('write list invalid argument'); | 164 if (++calls > 1) Expect.fail('write list invalid argument'); |
158 }; | 165 }; |
159 file.writeString(string); | 166 file.writeString(string); |
160 file.onClosed = () { | 167 file.closeHandler = () { |
161 Expect.equals(1, errors); | 168 Expect.equals(1, errors); |
162 }; | 169 }; |
163 file.close(); | 170 file.close(); |
164 } | 171 } |
165 | 172 |
166 static void testFullPathInvalidArgs(name) { | 173 static void testFullPathInvalidArgs(name) { |
167 var file = new File(name); | 174 var file = new File(name); |
168 try { | 175 try { |
169 file.fullPathSync(); | 176 file.fullPathSync(); |
170 Expect.fail('exception expected'); | 177 Expect.fail('exception expected'); |
171 } catch (var e) { | 178 } catch (var e) { |
172 Expect.isTrue(e is FileIOException); | 179 Expect.isTrue(e is FileIOException); |
173 Expect.isTrue(e.toString().contains('fullPath failed')); | 180 Expect.isTrue(e.toString().contains('fullPath failed')); |
174 } | 181 } |
175 | 182 |
176 file.onError = (s) { | 183 file.errorHandler = (s) { |
177 Expect.isTrue(s.contains('fullPath failed')); | 184 Expect.isTrue(s.contains('fullPath failed')); |
178 }; | 185 }; |
179 file.fullPath((path) { | 186 file.fullPathHandler = (path) { |
180 Expect.fail('full path invalid argument'); | 187 Expect.fail('full path invalid argument'); |
181 }); | 188 }; |
| 189 file.fullPath(); |
182 } | 190 } |
183 | 191 |
184 static String getFilename(String path) => | 192 static String getFilename(String path) => |
185 new File(path).existsSync() ? path : 'runtime/' + path; | 193 new File(path).existsSync() ? path : 'runtime/' + path; |
186 } | 194 } |
187 | 195 |
188 main() { | 196 main() { |
189 FileTest.testOpenInvalidArgs(0); | 197 FileTest.testOpenInvalidArgs(0); |
190 FileTest.testOpenInvalidArgs('name', 0); | 198 FileTest.testOpenInvalidArgs('name', 0); |
191 FileTest.testExistsInvalidArgs(0); | 199 FileTest.testExistsInvalidArgs(0); |
192 FileTest.testCreateInvalidArgs(0); | 200 FileTest.testCreateInvalidArgs(0); |
193 FileTest.testReadListInvalidArgs(12, 0, 1); | 201 FileTest.testReadListInvalidArgs(12, 0, 1); |
194 FileTest.testReadListInvalidArgs(new List(10), '0', 1); | 202 FileTest.testReadListInvalidArgs(new List(10), '0', 1); |
195 FileTest.testReadListInvalidArgs(new List(10), 0, '1'); | 203 FileTest.testReadListInvalidArgs(new List(10), 0, '1'); |
196 FileTest.testWriteByteInvalidArgs('asdf'); | 204 FileTest.testWriteByteInvalidArgs('asdf'); |
197 FileTest.testWriteListInvalidArgs(12, 0, 1); | 205 FileTest.testWriteListInvalidArgs(12, 0, 1); |
198 FileTest.testWriteListInvalidArgs(new List(10), '0', 1); | 206 FileTest.testWriteListInvalidArgs(new List(10), '0', 1); |
199 FileTest.testWriteListInvalidArgs(new List(10), 0, '1'); | 207 FileTest.testWriteListInvalidArgs(new List(10), 0, '1'); |
200 FileTest.testFullPathInvalidArgs(12); | 208 FileTest.testFullPathInvalidArgs(12); |
201 } | 209 } |
OLD | NEW |