Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: tests/standalone/src/FileInvalidArgumentsTest.dart

Issue 8491014: Handling of wrong types of arguments in the File API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/string.dart ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
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.
4
5 class FileTest {
6 static void testOpenInvalidArgs(name, [writable = false]) {
7 var file = new File(name);
8 try {
9 file.openSync();
10 Expect.fail('exception expected');
11 } catch (var e) {
12 Expect.isTrue(e is FileIOException);
13 Expect.isTrue(e.toString().contains('open file'));
14 }
15
16 file.errorHandler = (s) {
17 Expect.isTrue(s.contains('open file'));
18 };
19 file.openHandler = () {
20 Expect.fail('non-string name open');
21 };
22 file.open();
23 }
24
25 static void testExistsInvalidArgs(name) {
26 var file = new File(name);
27 try {
28 file.existsSync();
29 Expect.fail('exception expected');
30 } catch (var e) {
31 Expect.isTrue(e is FileIOException);
32 Expect.isTrue(e.toString().contains('is not a string'));
33 }
34
35 file.errorHandler = (s) {
36 Expect.isTrue(s.contains('is not a string'));
37 };
38 file.existsHandler = (bool) {
39 Expect.fail('non-string name exists');
40 };
41 file.exists();
42 }
43
44 static void testCreateInvalidArgs(name) {
45 var file = new File(name);
46 try {
47 file.createSync();
48 Expect.fail('exception expected');
49 } catch (var e) {
50 Expect.isTrue(e is FileIOException);
51 Expect.isTrue(e.toString().contains('Cannot create file'));
52 }
53
54 file.errorHandler = (s) {
55 Expect.isTrue(s.contains('Cannot create file'));
56 };
57 file.createHandler = (created) {
58 Expect.fail('non-string name exists');
59 };
60 file.create();
61 }
62
63 static void testCloseNonOpened() {
64 var file = new File(0);
65 try {
66 file.closeSync();
67 Expect.fail('exception expected');
68 } catch (var e) {
69 Expect.isTrue(e is FileIOException);
70 Expect.isTrue(e.toString().contains('Cannot close file'));
71 }
72
73 file.errorHandler = (s) {
74 Expect.isTrue(s.contains('Cannot close file'));
75 };
76 file.closeHandler = () {
77 Expect.fail('close non-opened file');
78 };
79 file.close();
80 }
81
82 static void testReadListInvalidArgs(buffer, offset, length) {
83 String filename = getFilename("tests/vm/data/fixed_length_file");
84 var file = new File(filename);
85 file.openSync();
86 try {
87 file.readListSync(buffer, offset, length);
88 Expect.fail('exception expected');
89 } catch (var e) {
90 Expect.isTrue(e is FileIOException);
91 Expect.isTrue(e.toString().contains('Invalid arguments'));
92 }
93
94 var errors = 0;
95 file.errorHandler = (s) {
96 errors++;
97 Expect.isTrue(s.contains('Invalid arguments'));
98 };
99 file.readListHandler = (bytes) {
100 Expect.fail('read list invalid arguments');
101 };
102 file.readList(buffer, offset, length);
103 file.closeHandler = () {
104 Expect.equals(1, errors);
105 };
106 file.close();
107 }
108
109 static void testWriteByteInvalidArgs(value) {
110 String filename = getFilename("tests/vm/data/fixed_length_file");
111 var file = new File(filename + "_out");
112 file.openSync(true);
113 try {
114 file.writeByteSync(value);
115 Expect.fail('exception expected');
116 } catch (var e) {
117 Expect.isTrue(e is FileIOException);
118 Expect.isTrue(e.toString().contains('Invalid argument'));
119 }
120
121 var errors = 0;
122 file.errorHandler = (s) {
123 errors++;
124 Expect.isTrue(s.contains('Invalid argument'));
125 };
126 file.noPendingWriteHandler = (bytes) {
127 Expect.fail('write list invalid argument');
128 };
129 file.writeByte(value);
130 file.closeHandler = () {
131 Expect.equals(1, errors);
132 };
133 file.close();
134 }
135
136 static void testWriteListInvalidArgs(buffer, offset, bytes) {
137 String filename = getFilename("tests/vm/data/fixed_length_file");
138 var file = new File(filename + "_out");
139 file.openSync(true);
140 try {
141 file.writeListSync(buffer, offset, bytes);
142 Expect.fail('exception expected');
143 } catch (var e) {
144 Expect.isTrue(e is FileIOException);
145 Expect.isTrue(e.toString().contains('Invalid arguments'));
146 }
147
148 var errors = 0;
149 file.errorHandler = (s) {
150 errors++;
151 Expect.isTrue(s.contains('Invalid arguments'));
152 };
153 file.noPendingWriteHandler = (bytes) {
154 Expect.fail('write list invalid argument');
155 };
156 file.writeList(buffer, offset, bytes);
157 file.closeHandler = () {
158 Expect.equals(1, errors);
159 };
160 file.close();
161 }
162
163 static void testWriteStringInvalidArgs(string) {
164 String filename = getFilename("tests/vm/data/fixed_length_file");
165 var file = new File(filename + "_out");
166 file.openSync(true);
167 try {
168 file.writeString(string);
169 Expect.fail('exception expected');
170 } catch (var e) {
171 Expect.isTrue(e is FileIOException);
172 Expect.isTrue(e.toString().contains('writeString failed'));
173 }
174
175 var errors = 0;
176 file.errorHandler = (s) {
177 errors++;
178 Expect.isTrue(s.contains('writeString failed'));
179 };
180 file.noPendingWriteHandler = (bytes) {
181 Expect.fail('write string invalid argument');
182 };
183 file.writeString(string);
184 file.closeHandler = () {
185 Expect.equals(1, errors);
186 };
187 file.close();
188 }
189
190 static void testFullPathInvalidArgs(name) {
191 var file = new File(name);
192 try {
193 file.fullPathSync();
194 Expect.fail('exception expected');
195 } catch (var e) {
196 Expect.isTrue(e is FileIOException);
197 Expect.isTrue(e.toString().contains('fullPath failed'));
198 }
199
200 file.errorHandler = (s) {
201 Expect.isTrue(s.contains('fullPath failed'));
202 };
203 file.fullPathHandler = (path) {
204 Expect.fail('full path invalid argument');
205 };
206 file.fullPath();
207 }
208
209 static String getFilename(String path) =>
210 new File(path).existsSync() ? path : 'runtime/' + path;
211 }
212
213 main() {
214 FileTest.testOpenInvalidArgs(0);
215 FileTest.testOpenInvalidArgs('name', 0);
216 FileTest.testExistsInvalidArgs(0);
217 FileTest.testCreateInvalidArgs(0);
218 FileTest.testCloseNonOpened();
219 FileTest.testReadListInvalidArgs(12, 0, 1);
220 FileTest.testReadListInvalidArgs(new List(10), '0', 1);
221 FileTest.testReadListInvalidArgs(new List(10), 0, '1');
222 FileTest.testWriteByteInvalidArgs('asdf');
223 FileTest.testWriteListInvalidArgs(12, 0, 1);
224 FileTest.testWriteListInvalidArgs(new List(10), '0', 1);
225 FileTest.testWriteListInvalidArgs(new List(10), 0, '1');
226 FileTest.testFullPathInvalidArgs(12);
227 }
OLDNEW
« no previous file with comments | « runtime/lib/string.dart ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698