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 import "dart:isolate"; | |
6 | 7 |
7 class FileTest { | 8 void testReadInvalidArgs(arg) { |
8 static void testReadListInvalidArgs(buffer, offset, length) { | 9 var port = new ReceivePort(); |
9 String filename = getFilename("tests/vm/data/fixed_length_file"); | 10 String filename = getFilename("tests/vm/data/fixed_length_file"); |
10 var file = (new File(filename)).openSync(); | 11 var file = (new File(filename)).openSync(); |
11 try { | 12 try { |
12 file.readListSync(buffer, offset, length); | 13 file.readSync(arg); |
13 Expect.fail('exception expected'); | 14 Expect.fail('exception expected'); |
14 } catch (e) { | 15 } catch (e) { |
15 Expect.isTrue(e is FileIOException); | 16 Expect.isTrue(e is FileIOException); |
16 Expect.isTrue(e.toString().contains('Invalid arguments')); | 17 Expect.isTrue(e.toString().contains('Invalid arguments')); |
17 } | |
18 | |
19 var errors = 0; | |
20 var readListFuture = file.readList(buffer, offset, length); | |
21 readListFuture.then((bytes) { | |
22 Expect.fail('read list invalid arguments'); | |
23 }).catchError((e) { | |
24 errors++; | |
25 Expect.isTrue(e.error is FileIOException); | |
26 Expect.isTrue(e.error.toString().contains('Invalid arguments')); | |
27 file.close().then((ignore) { | |
28 Expect.equals(1, errors); | |
29 }); | |
30 }); | |
31 } | 18 } |
32 | 19 |
33 static void testWriteByteInvalidArgs(value) { | 20 var errors = 0; |
34 String filename = getFilename("tests/vm/data/fixed_length_file"); | 21 var readFuture = file.read(arg); |
35 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); | 22 readFuture.then((bytes) { |
36 try { | 23 Expect.fail('read list invalid arguments'); |
floitsch
2013/01/09 09:12:59
replace string with "Exception expected".
The e.er
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
37 file.writeByteSync(value); | 24 }).catchError((e) { |
38 Expect.fail('exception expected'); | 25 errors++; |
39 } catch (e) { | 26 Expect.isTrue(e.error is FileIOException); |
floitsch
2013/01/09 09:12:59
Alternatively you could use "test: (error) => erro
Mads Ager (google)
2013/01/09 09:29:14
I find this more readable since I want to check mo
| |
40 Expect.isTrue(e is FileIOException); | 27 Expect.isTrue(e.error.toString().contains('Invalid arguments')); |
41 Expect.isTrue(e.toString().contains('Invalid argument')); | 28 file.close().then((ignore) { |
42 } | 29 Expect.equals(1, errors); |
30 port.close(); | |
31 }); | |
32 }); | |
33 } | |
43 | 34 |
44 var writeByteFuture = file.writeByte(value); | 35 void testReadListInvalidArgs(buffer, offset, length) { |
45 writeByteFuture.then((ignore) { | 36 String filename = getFilename("tests/vm/data/fixed_length_file"); |
46 Expect.fail('write byte invalid argument'); | 37 var file = (new File(filename)).openSync(); |
47 }).catchError((s) { | 38 try { |
48 Expect.isTrue(s.error is FileIOException); | 39 file.readListSync(buffer, offset, length); |
49 Expect.isTrue(s.error.toString().contains('Invalid argument')); | 40 Expect.fail('exception expected'); |
50 file.close(); | 41 } catch (e) { |
51 }); | 42 Expect.isTrue(e is FileIOException); |
43 Expect.isTrue(e.toString().contains('Invalid arguments')); | |
52 } | 44 } |
53 | 45 |
54 static void testWriteListInvalidArgs(buffer, offset, bytes) { | 46 var errors = 0; |
55 String filename = getFilename("tests/vm/data/fixed_length_file"); | 47 var readListFuture = file.readList(buffer, offset, length); |
56 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); | 48 readListFuture.then((bytes) { |
57 try { | 49 Expect.fail('read list invalid arguments'); |
58 file.writeListSync(buffer, offset, bytes); | 50 }).catchError((e) { |
59 Expect.fail('exception expected'); | 51 errors++; |
60 } catch (e) { | 52 Expect.isTrue(e.error is FileIOException); |
61 Expect.isTrue(e is FileIOException); | 53 Expect.isTrue(e.error.toString().contains('Invalid arguments')); |
62 Expect.isTrue(e.toString().contains('Invalid arguments')); | 54 file.close().then((ignore) { |
63 } | 55 Expect.equals(1, errors); |
floitsch
2013/01/09 09:12:59
add a receive-port to make sure the test is run.
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
56 }); | |
57 }); | |
58 } | |
64 | 59 |
65 var writeListFuture = file.writeList(buffer, offset, bytes); | 60 void testWriteByteInvalidArgs(value) { |
66 writeListFuture.then((ignore) { | 61 String filename = getFilename("tests/vm/data/fixed_length_file"); |
67 Expect.fail('write list invalid argument'); | 62 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); |
68 }).catchError((s) { | 63 try { |
69 Expect.isTrue(s.error is FileIOException); | 64 file.writeByteSync(value); |
70 Expect.isTrue(s.error.toString().contains('Invalid arguments')); | 65 Expect.fail('exception expected'); |
71 file.close(); | 66 } catch (e) { |
72 }); | 67 Expect.isTrue(e is FileIOException); |
68 Expect.isTrue(e.toString().contains('Invalid argument')); | |
73 } | 69 } |
74 | 70 |
75 static void testWriteStringInvalidArgs(string) { | 71 var writeByteFuture = file.writeByte(value); |
76 String filename = getFilename("tests/vm/data/fixed_length_file"); | 72 writeByteFuture.then((ignore) { |
77 var file = new File(filename.concat("_out")); | 73 Expect.fail('write byte invalid argument'); |
floitsch
2013/01/09 09:12:59
ditto (change exception string).
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
78 file.openSync(FileMode.WRITE); | 74 }).catchError((s) { |
79 try { | 75 Expect.isTrue(s.error is FileIOException); |
80 file.writeString(string); | 76 Expect.isTrue(s.error.toString().contains('Invalid argument')); |
81 Expect.fail('exception expected'); | 77 file.close(); |
floitsch
2013/01/09 09:12:59
ditto (receive-port).
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
82 } catch (e) { | 78 }); |
83 Expect.isTrue(e is FileIOException); | 79 } |
84 Expect.isTrue(e.toString().contains('writeString failed')); | |
85 } | |
86 | 80 |
87 var errors = 0; | 81 void testWriteListInvalidArgs(buffer, offset, bytes) { |
88 file.onError = (s) { | 82 String filename = getFilename("tests/vm/data/fixed_length_file"); |
89 errors++; | 83 var file = (new File(filename.concat("_out"))).openSync(FileMode.WRITE); |
90 Expect.isTrue(s.contains('writeString failed')); | 84 try { |
91 }; | 85 file.writeListSync(buffer, offset, bytes); |
92 var calls = 0; | 86 Expect.fail('exception expected'); |
93 file.onNoPendingWrites = () { | 87 } catch (e) { |
94 if (++calls > 1) Expect.fail('write list invalid argument'); | 88 Expect.isTrue(e is FileIOException); |
95 }; | 89 Expect.isTrue(e.toString().contains('Invalid arguments')); |
96 file.writeString(string); | |
97 file.onClosed = () { | |
98 Expect.equals(1, errors); | |
99 }; | |
100 file.close(); | |
101 } | 90 } |
102 | 91 |
103 static String getFilename(String path) => | 92 var writeListFuture = file.writeList(buffer, offset, bytes); |
104 new File(path).existsSync() ? path : 'runtime/$path'; | 93 writeListFuture.then((ignore) { |
94 Expect.fail('write list invalid argument'); | |
floitsch
2013/01/09 09:12:59
ditto (change exception string).
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
95 }).catchError((s) { | |
96 Expect.isTrue(s.error is FileIOException); | |
97 Expect.isTrue(s.error.toString().contains('Invalid arguments')); | |
98 file.close(); | |
floitsch
2013/01/09 09:12:59
ditto (receive-port).
Mads Ager (google)
2013/01/09 09:29:14
Done.
| |
99 }); | |
100 } | |
101 | |
102 void testWriteStringInvalidArgs(string) { | |
103 String filename = getFilename("tests/vm/data/fixed_length_file"); | |
104 var file = new File(filename.concat("_out")); | |
105 file.openSync(FileMode.WRITE); | |
106 try { | |
107 file.writeString(string); | |
108 Expect.fail('exception expected'); | |
109 } catch (e) { | |
110 Expect.isTrue(e is FileIOException); | |
111 Expect.isTrue(e.toString().contains('writeString failed')); | |
112 } | |
113 | |
114 var errors = 0; | |
115 file.onError = (s) { | |
116 errors++; | |
117 Expect.isTrue(s.contains('writeString failed')); | |
118 }; | |
119 var calls = 0; | |
120 file.onNoPendingWrites = () { | |
121 if (++calls > 1) Expect.fail('write list invalid argument'); | |
122 }; | |
123 file.writeString(string); | |
124 file.onClosed = () { | |
125 Expect.equals(1, errors); | |
Mads Ager (google)
2013/01/09 09:29:14
Added port to this test as well and closed it here
| |
126 }; | |
127 file.close(); | |
128 } | |
129 | |
130 String getFilename(String path) { | |
131 return new File(path).existsSync() ? path : 'runtime/$path'; | |
105 } | 132 } |
106 | 133 |
107 main() { | 134 main() { |
108 FileTest.testReadListInvalidArgs(12, 0, 1); | 135 testReadInvalidArgs('asdf'); |
109 FileTest.testReadListInvalidArgs(new List.fixedLength(10), '0', 1); | 136 testReadListInvalidArgs(12, 0, 1); |
110 FileTest.testReadListInvalidArgs(new List.fixedLength(10), 0, '1'); | 137 testReadListInvalidArgs(new List.fixedLength(10), '0', 1); |
111 FileTest.testWriteByteInvalidArgs('asdf'); | 138 testReadListInvalidArgs(new List.fixedLength(10), 0, '1'); |
112 FileTest.testWriteListInvalidArgs(12, 0, 1); | 139 testWriteByteInvalidArgs('asdf'); |
113 FileTest.testWriteListInvalidArgs(new List.fixedLength(10), '0', 1); | 140 testWriteListInvalidArgs(12, 0, 1); |
114 FileTest.testWriteListInvalidArgs(new List.fixedLength(10), 0, '1'); | 141 testWriteListInvalidArgs(new List.fixedLength(10), '0', 1); |
142 testWriteListInvalidArgs(new List.fixedLength(10), 0, '1'); | |
115 } | 143 } |
OLD | NEW |