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 testReadListInvalidArgs(buffer, offset, length) { | 8 static void testReadListInvalidArgs(buffer, offset, length) { |
9 String filename = getFilename("tests/vm/data/fixed_length_file"); | 9 String filename = getFilename("tests/vm/data/fixed_length_file"); |
10 var file = (new File(filename)).openSync(); | 10 var file = (new File(filename)).openSync(); |
11 try { | 11 try { |
12 file.readListSync(buffer, offset, length); | 12 file.readListSync(buffer, offset, length); |
13 Expect.fail('exception expected'); | 13 Expect.fail('exception expected'); |
14 } catch (e) { | 14 } catch (e) { |
15 Expect.isTrue(e is FileIOException); | 15 Expect.isTrue(e is FileIOException); |
16 Expect.isTrue(e.toString().contains('Invalid arguments')); | 16 Expect.isTrue(e.toString().contains('Invalid arguments')); |
17 } | 17 } |
18 | 18 |
19 var errors = 0; | 19 var errors = 0; |
20 var readListFuture = file.readList(buffer, offset, length); | 20 var readListFuture = file.readList(buffer, offset, length); |
21 readListFuture.handleException((e) { | 21 readListFuture.then((bytes) { |
| 22 Expect.fail('read list invalid arguments'); |
| 23 }).catchError((e) { |
22 errors++; | 24 errors++; |
23 Expect.isTrue(e is FileIOException); | 25 Expect.isTrue(e.error is FileIOException); |
24 Expect.isTrue(e.toString().contains('Invalid arguments')); | 26 Expect.isTrue(e.error.toString().contains('Invalid arguments')); |
25 file.close().then((ignore) { | 27 file.close().then((ignore) { |
26 Expect.equals(1, errors); | 28 Expect.equals(1, errors); |
27 }); | 29 }); |
28 return true; | |
29 }); | |
30 readListFuture.then((bytes) { | |
31 Expect.fail('read list invalid arguments'); | |
32 }); | 30 }); |
33 } | 31 } |
34 | 32 |
35 static void testWriteByteInvalidArgs(value) { | 33 static void testWriteByteInvalidArgs(value) { |
36 String filename = getFilename("tests/vm/data/fixed_length_file"); | 34 String filename = getFilename("tests/vm/data/fixed_length_file"); |
37 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); | 35 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); |
38 try { | 36 try { |
39 file.writeByteSync(value); | 37 file.writeByteSync(value); |
40 Expect.fail('exception expected'); | 38 Expect.fail('exception expected'); |
41 } catch (e) { | 39 } catch (e) { |
42 Expect.isTrue(e is FileIOException); | 40 Expect.isTrue(e is FileIOException); |
43 Expect.isTrue(e.toString().contains('Invalid argument')); | 41 Expect.isTrue(e.toString().contains('Invalid argument')); |
44 } | 42 } |
45 | 43 |
46 var writeByteFuture = file.writeByte(value); | 44 var writeByteFuture = file.writeByte(value); |
47 writeByteFuture.then((ignore) { | 45 writeByteFuture.then((ignore) { |
48 Expect.fail('write byte invalid argument'); | 46 Expect.fail('write byte invalid argument'); |
49 }); | 47 }).catchError((s) { |
50 writeByteFuture.handleException((s) { | 48 Expect.isTrue(s.error is FileIOException); |
51 Expect.isTrue(s is FileIOException); | 49 Expect.isTrue(s.error.toString().contains('Invalid argument')); |
52 Expect.isTrue(s.toString().contains('Invalid argument')); | |
53 file.close(); | 50 file.close(); |
54 return true; | |
55 }); | 51 }); |
56 } | 52 } |
57 | 53 |
58 static void testWriteListInvalidArgs(buffer, offset, bytes) { | 54 static void testWriteListInvalidArgs(buffer, offset, bytes) { |
59 String filename = getFilename("tests/vm/data/fixed_length_file"); | 55 String filename = getFilename("tests/vm/data/fixed_length_file"); |
60 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); | 56 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); |
61 try { | 57 try { |
62 file.writeListSync(buffer, offset, bytes); | 58 file.writeListSync(buffer, offset, bytes); |
63 Expect.fail('exception expected'); | 59 Expect.fail('exception expected'); |
64 } catch (e) { | 60 } catch (e) { |
65 Expect.isTrue(e is FileIOException); | 61 Expect.isTrue(e is FileIOException); |
66 Expect.isTrue(e.toString().contains('Invalid arguments')); | 62 Expect.isTrue(e.toString().contains('Invalid arguments')); |
67 } | 63 } |
68 | 64 |
69 var writeListFuture = file.writeList(buffer, offset, bytes); | 65 var writeListFuture = file.writeList(buffer, offset, bytes); |
70 writeListFuture.then((ignore) { | 66 writeListFuture.then((ignore) { |
71 Expect.fail('write list invalid argument'); | 67 Expect.fail('write list invalid argument'); |
72 }); | 68 }).catchError((s) { |
73 writeListFuture.handleException((s) { | 69 Expect.isTrue(s.error is FileIOException); |
74 Expect.isTrue(s is FileIOException); | 70 Expect.isTrue(s.error.toString().contains('Invalid arguments')); |
75 Expect.isTrue(s.toString().contains('Invalid arguments')); | |
76 file.close(); | 71 file.close(); |
77 return true; | |
78 }); | 72 }); |
79 } | 73 } |
80 | 74 |
81 static void testWriteStringInvalidArgs(string) { | 75 static void testWriteStringInvalidArgs(string) { |
82 String filename = getFilename("tests/vm/data/fixed_length_file"); | 76 String filename = getFilename("tests/vm/data/fixed_length_file"); |
83 var file = new File(filename.concat("_out")); | 77 var file = new File(filename.concat("_out")); |
84 file.openSync(FileMode.WRITE); | 78 file.openSync(FileMode.WRITE); |
85 try { | 79 try { |
86 file.writeString(string); | 80 file.writeString(string); |
87 Expect.fail('exception expected'); | 81 Expect.fail('exception expected'); |
(...skipping 17 matching lines...) Expand all Loading... |
105 }; | 99 }; |
106 file.close(); | 100 file.close(); |
107 } | 101 } |
108 | 102 |
109 static String getFilename(String path) => | 103 static String getFilename(String path) => |
110 new File(path).existsSync() ? path : 'runtime/$path'; | 104 new File(path).existsSync() ? path : 'runtime/$path'; |
111 } | 105 } |
112 | 106 |
113 main() { | 107 main() { |
114 FileTest.testReadListInvalidArgs(12, 0, 1); | 108 FileTest.testReadListInvalidArgs(12, 0, 1); |
115 FileTest.testReadListInvalidArgs(new List(10), '0', 1); | 109 FileTest.testReadListInvalidArgs(new List.fixedLength(10), '0', 1); |
116 FileTest.testReadListInvalidArgs(new List(10), 0, '1'); | 110 FileTest.testReadListInvalidArgs(new List.fixedLength(10), 0, '1'); |
117 FileTest.testWriteByteInvalidArgs('asdf'); | 111 FileTest.testWriteByteInvalidArgs('asdf'); |
118 FileTest.testWriteListInvalidArgs(12, 0, 1); | 112 FileTest.testWriteListInvalidArgs(12, 0, 1); |
119 FileTest.testWriteListInvalidArgs(new List(10), '0', 1); | 113 FileTest.testWriteListInvalidArgs(new List.fixedLength(10), '0', 1); |
120 FileTest.testWriteListInvalidArgs(new List(10), 0, '1'); | 114 FileTest.testWriteListInvalidArgs(new List.fixedLength(10), 0, '1'); |
121 } | 115 } |
OLD | NEW |