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