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

Side by Side Diff: tests/standalone/io/file_test.dart

Issue 2771453003: Format all tests. (Closed)
Patch Set: Format files Created 3 years, 8 months 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // Dart test program for testing file I/O. 5 // Dart test program for testing file I/O.
6 6
7 // OtherResources=empty_file 7 // OtherResources=empty_file
8 // OtherResources=file_test.txt 8 // OtherResources=file_test.txt
9 // OtherResources=fixed_length_file 9 // OtherResources=fixed_length_file
10 // OtherResources=read_as_text.dat 10 // OtherResources=read_as_text.dat
11 // OtherResources=readline_test1.dat 11 // OtherResources=readline_test1.dat
12 12
13 import 'dart:async'; 13 import 'dart:async';
14 import 'dart:convert'; 14 import 'dart:convert';
15 import 'dart:collection'; 15 import 'dart:collection';
16 import 'dart:io'; 16 import 'dart:io';
17 17
18 import "package:async_helper/async_helper.dart"; 18 import "package:async_helper/async_helper.dart";
19 import "package:expect/expect.dart"; 19 import "package:expect/expect.dart";
20 import "package:path/path.dart"; 20 import "package:path/path.dart";
21 21
22 class MyListOfOneElement 22 class MyListOfOneElement extends Object
23 extends Object with ListMixin<int> implements List<int> { 23 with ListMixin<int>
24 implements List<int> {
24 int _value; 25 int _value;
25 MyListOfOneElement(this._value); 26 MyListOfOneElement(this._value);
26 int get length => 1; 27 int get length => 1;
27 operator [](int index) => _value; 28 operator [](int index) => _value;
28 void set length(int index) { throw "Unsupported"; } 29 void set length(int index) {
30 throw "Unsupported";
31 }
32
29 operator []=(int index, value) { 33 operator []=(int index, value) {
30 if (index != 0) { 34 if (index != 0) {
31 throw "Unsupported"; 35 throw "Unsupported";
32 } else { 36 } else {
33 _value = value; 37 _value = value;
34 } 38 }
35 } 39 }
36 } 40 }
37 41
38 class FileTest { 42 class FileTest {
39 static Directory tempDirectory; 43 static Directory tempDirectory;
40 static int numLiveAsyncTests = 0; 44 static int numLiveAsyncTests = 0;
41 45
42 static void asyncTestStarted() { 46 static void asyncTestStarted() {
43 asyncStart(); 47 asyncStart();
44 ++numLiveAsyncTests; 48 ++numLiveAsyncTests;
45 } 49 }
50
46 static void asyncTestDone(String name) { 51 static void asyncTestDone(String name) {
47 asyncEnd(); 52 asyncEnd();
48 --numLiveAsyncTests; 53 --numLiveAsyncTests;
49 if (numLiveAsyncTests == 0) { 54 if (numLiveAsyncTests == 0) {
50 deleteTempDirectory(); 55 deleteTempDirectory();
51 } 56 }
52 } 57 }
53 58
54 static void createTempDirectory(Function doNext) { 59 static void createTempDirectory(Function doNext) {
55 Directory.systemTemp.createTemp('dart_file').then((temp) { 60 Directory.systemTemp.createTemp('dart_file').then((temp) {
56 tempDirectory = temp; 61 tempDirectory = temp;
57 doNext(); 62 doNext();
58 }); 63 });
59 } 64 }
60 65
61 static void deleteTempDirectory() { 66 static void deleteTempDirectory() {
62 tempDirectory.deleteSync(recursive: true); 67 tempDirectory.deleteSync(recursive: true);
63 } 68 }
64 69
65 // Test for file read functionality. 70 // Test for file read functionality.
66 static void testReadStream() { 71 static void testReadStream() {
67 // Read a file and check part of it's contents. 72 // Read a file and check part of it's contents.
68 String filename = getFilename("file_test.txt"); 73 String filename = getFilename("file_test.txt");
69 File file = new File(filename); 74 File file = new File(filename);
70 Expect.isTrue('$file'.contains(file.path)); 75 Expect.isTrue('$file'.contains(file.path));
71 var subscription; 76 var subscription;
72 List<int> buffer = new List<int>(); 77 List<int> buffer = new List<int>();
73 subscription = file.openRead().listen( 78 subscription = file.openRead().listen((d) {
74 (d) { 79 buffer.addAll(d);
75 buffer.addAll(d); 80 if (buffer.length >= 12) {
76 if (buffer.length >= 12) { 81 subscription.cancel();
77 subscription.cancel(); 82 Expect.equals(47, buffer[0]); // represents '/' in the file.
78 Expect.equals(47, buffer[0]); // represents '/' in the file. 83 Expect.equals(47, buffer[1]); // represents '/' in the file.
79 Expect.equals(47, buffer[1]); // represents '/' in the file. 84 Expect.equals(32, buffer[2]); // represents ' ' in the file.
80 Expect.equals(32, buffer[2]); // represents ' ' in the file. 85 Expect.equals(67, buffer[3]); // represents 'C' in the file.
81 Expect.equals(67, buffer[3]); // represents 'C' in the file. 86 Expect.equals(111, buffer[4]); // represents 'o' in the file.
82 Expect.equals(111, buffer[4]); // represents 'o' in the file. 87 Expect.equals(112, buffer[5]); // represents 'p' in the file.
83 Expect.equals(112, buffer[5]); // represents 'p' in the file. 88 Expect.equals(121, buffer[6]); // represents 'y' in the file.
84 Expect.equals(121, buffer[6]); // represents 'y' in the file. 89 Expect.equals(114, buffer[7]); // represents 'r' in the file.
85 Expect.equals(114, buffer[7]); // represents 'r' in the file. 90 Expect.equals(105, buffer[8]); // represents 'i' in the file.
86 Expect.equals(105, buffer[8]); // represents 'i' in the file. 91 Expect.equals(103, buffer[9]); // represents 'g' in the file.
87 Expect.equals(103, buffer[9]); // represents 'g' in the file. 92 Expect.equals(104, buffer[10]); // represents 'h' in the file.
88 Expect.equals(104, buffer[10]); // represents 'h' in the file. 93 Expect.equals(116, buffer[11]); // represents 't' in the file.
89 Expect.equals(116, buffer[11]); // represents 't' in the file. 94 }
90 } 95 });
91 });
92 } 96 }
93 97
94 // Test for file read and write functionality. 98 // Test for file read and write functionality.
95 static void testReadWriteStream() { 99 static void testReadWriteStream() {
96 asyncTestStarted(); 100 asyncTestStarted();
97 101
98 // Read a file. 102 // Read a file.
99 String inFilename = getFilename("fixed_length_file"); 103 String inFilename = getFilename("fixed_length_file");
100 File file; 104 File file;
101 int bytesRead; 105 int bytesRead;
102 106
103 var file1 = new File(inFilename); 107 var file1 = new File(inFilename);
104 List<int> buffer = new List<int>(); 108 List<int> buffer = new List<int>();
105 file1.openRead().listen( 109 file1.openRead().listen((d) {
106 (d) { 110 buffer.addAll(d);
107 buffer.addAll(d); 111 }, onDone: () {
108 }, 112 Expect.equals(42, buffer.length);
109 onDone: () { 113 // Write the contents of the file just read into another file.
110 Expect.equals(42, buffer.length); 114 String outFilename = tempDirectory.path + "/out_read_write_stream";
111 // Write the contents of the file just read into another file. 115 var file2 = new File(outFilename);
112 String outFilename = 116 var output = file2.openWrite();
113 tempDirectory.path + "/out_read_write_stream"; 117 output.add(buffer);
114 var file2 = new File(outFilename); 118 output.flush().then((_) => output.close());
115 var output = file2.openWrite(); 119 output.done.then((_) {
116 output.add(buffer); 120 // Now read the contents of the file just written.
117 output.flush().then((_) => output.close()); 121 List<int> buffer2 = new List<int>();
118 output.done.then((_) { 122 new File(outFilename).openRead().listen((d) {
119 // Now read the contents of the file just written. 123 buffer2.addAll(d);
120 List<int> buffer2 = new List<int>(); 124 }, onDone: () {
121 new File(outFilename).openRead().listen( 125 Expect.equals(42, buffer2.length);
122 (d) { 126 // Now compare the two buffers to check if they are
123 buffer2.addAll(d); 127 // identical.
124 }, 128 for (int i = 0; i < buffer.length; i++) {
125 onDone: () { 129 Expect.equals(buffer[i], buffer2[i]);
126 Expect.equals(42, buffer2.length); 130 }
127 // Now compare the two buffers to check if they are 131 // Delete the output file.
128 // identical. 132 file2.deleteSync();
129 for (int i = 0; i < buffer.length; i++) { 133 Expect.isFalse(file2.existsSync());
130 Expect.equals(buffer[i], buffer2[i]); 134 asyncTestDone("testReadWriteStream");
131 } 135 });
132 // Delete the output file.
133 file2.deleteSync();
134 Expect.isFalse(file2.existsSync());
135 asyncTestDone("testReadWriteStream");
136 });
137 });
138 }); 136 });
137 });
139 } 138 }
140 139
141 // Test for file stream buffered handling of large files. 140 // Test for file stream buffered handling of large files.
142 static void testReadWriteStreamLargeFile() { 141 static void testReadWriteStreamLargeFile() {
143 // Create the test data - arbitrary binary data. 142 // Create the test data - arbitrary binary data.
144 List<int> buffer = new List<int>(100000); 143 List<int> buffer = new List<int>(100000);
145 for (var i = 0; i < buffer.length; ++i) { 144 for (var i = 0; i < buffer.length; ++i) {
146 buffer[i] = i % 256; 145 buffer[i] = i % 256;
147 } 146 }
148 String filename = 147 String filename = tempDirectory.path + "/out_read_write_stream_large_file";
149 tempDirectory.path + "/out_read_write_stream_large_file";
150 File file = new File(filename); 148 File file = new File(filename);
151 IOSink output = file.openWrite(); 149 IOSink output = file.openWrite();
152 output.add(buffer); 150 output.add(buffer);
153 output.add(buffer); 151 output.add(buffer);
154 output.flush().then((_) => output.close()); 152 output.flush().then((_) => output.close());
155 153
156 asyncTestStarted(); 154 asyncTestStarted();
157 output.done.then((_) { 155 output.done
158 Stream input = file.openRead(); 156 .then((_) {
159 int position = 0; 157 Stream input = file.openRead();
160 final int expectedLength = 200000; 158 int position = 0;
159 final int expectedLength = 200000;
161 160
162 // Start an independent asynchronous check on the length. 161 // Start an independent asynchronous check on the length.
163 Future lengthTest() { 162 Future lengthTest() {
164 asyncTestStarted(); 163 asyncTestStarted();
165 return file.length().then((len) { 164 return file.length().then((len) {
166 Expect.equals(expectedLength, len); 165 Expect.equals(expectedLength, len);
167 asyncTestDone('testReadWriteStreamLargeFile: length check'); 166 asyncTestDone('testReadWriteStreamLargeFile: length check');
168 }); 167 });
169 } 168 }
170 169
171 // Immediate read should read 0 bytes. 170 // Immediate read should read 0 bytes.
172 Future contentTest() { 171 Future contentTest() {
173 asyncTestStarted(); 172 asyncTestStarted();
174 var completer = new Completer(); 173 var completer = new Completer();
175 input.listen( 174 input.listen((data) {
176 (data) { 175 for (int i = 0; i < data.length; ++i) {
177 for (int i = 0; i < data.length; ++i) { 176 Expect.equals(buffer[(i + position) % buffer.length], data[i]);
178 Expect.equals(buffer[(i + position) % buffer.length], data[i]); 177 }
179 } 178 position += data.length;
180 position += data.length; 179 }, onError: (error, trace) {
181 }, 180 print('Error on input in testReadWriteStreamLargeFile');
182 onError: (error, trace) { 181 print('with error $error');
183 print('Error on input in testReadWriteStreamLargeFile');
184 print('with error $error');
185 if (trace != null) print("StackTrace: $trace");
186 throw error;
187 },
188 onDone: () {
189 Expect.equals(expectedLength, position);
190 testPipe(file, buffer).then((_) {
191 asyncTestDone('testReadWriteStreamLargeFile: main test');
192 }).catchError((error, trace) {
193 print('Exception while deleting ReadWriteStreamLargeFile file');
194 print('Exception $error');
195 if (trace != null) print("StackTrace: $trace"); 182 if (trace != null) print("StackTrace: $trace");
196 throw error; 183 throw error;
197 }).whenComplete(completer.complete); 184 }, onDone: () {
198 }); 185 Expect.equals(expectedLength, position);
199 return completer.future; 186 testPipe(file, buffer).then((_) {
200 } 187 asyncTestDone('testReadWriteStreamLargeFile: main test');
188 }).catchError((error, trace) {
189 print('Exception while deleting ReadWriteStreamLargeFile file');
190 print('Exception $error');
191 if (trace != null) print("StackTrace: $trace");
192 throw error;
193 }).whenComplete(completer.complete);
194 });
195 return completer.future;
196 }
201 197
202 return Future.forEach([lengthTest, contentTest], (test) => test()); 198 return Future.forEach([lengthTest, contentTest], (test) => test());
203 }).whenComplete(file.delete).whenComplete(() { 199 })
204 asyncTestDone('testReadWriteStreamLargeFile finished'); 200 .whenComplete(file.delete)
205 }); 201 .whenComplete(() {
202 asyncTestDone('testReadWriteStreamLargeFile finished');
203 });
206 } 204 }
207 205
208 static Future testPipe(File file, buffer) { 206 static Future testPipe(File file, buffer) {
209 String outputFilename = '${file.path}_copy'; 207 String outputFilename = '${file.path}_copy';
210 File outputFile = new File(outputFilename); 208 File outputFile = new File(outputFilename);
211 var input = file.openRead(); 209 var input = file.openRead();
212 var output = outputFile.openWrite(); 210 var output = outputFile.openWrite();
213 Completer done = new Completer(); 211 Completer done = new Completer();
214 input.pipe(output).then((_) { 212 input.pipe(output).then((_) {
215 var copy = outputFile.openRead(); 213 var copy = outputFile.openRead();
216 int position = 0; 214 int position = 0;
217 copy.listen( 215 copy.listen((d) {
218 (d) { 216 for (int i = 0; i < d.length; i++) {
219 for (int i = 0; i < d.length; i++) { 217 Expect.equals(buffer[(position + i) % buffer.length], d[i]);
220 Expect.equals(buffer[(position + i) % buffer.length], d[i]); 218 }
221 } 219 position += d.length;
222 position += d.length; 220 }, onDone: () {
223 }, 221 Expect.equals(2 * buffer.length, position);
224 onDone: () { 222 outputFile.delete().then((ignore) {
225 Expect.equals(2 * buffer.length, position); 223 done.complete();
226 outputFile.delete().then((ignore) { done.complete(); }); 224 });
227 });
228 }); 225 });
226 });
229 return done.future; 227 return done.future;
230 } 228 }
231 229
232 static void testRead() { 230 static void testRead() {
233 asyncStart(); 231 asyncStart();
234 // Read a file and check part of it's contents. 232 // Read a file and check part of it's contents.
235 String filename = getFilename("file_test.txt"); 233 String filename = getFilename("file_test.txt");
236 File file = new File(filename); 234 File file = new File(filename);
237 file.open(mode: READ).then((RandomAccessFile file) { 235 file.open(mode: READ).then((RandomAccessFile file) {
238 List<int> buffer = new List<int>(10); 236 List<int> buffer = new List<int>(10);
239 file.readInto(buffer, 0, 5).then((bytes_read) { 237 file.readInto(buffer, 0, 5).then((bytes_read) {
240 Expect.equals(5, bytes_read); 238 Expect.equals(5, bytes_read);
241 file.readInto(buffer, 5, 10).then((bytes_read) { 239 file.readInto(buffer, 5, 10).then((bytes_read) {
242 Expect.equals(5, bytes_read); 240 Expect.equals(5, bytes_read);
243 Expect.equals(47, buffer[0]); // represents '/' in the file. 241 Expect.equals(47, buffer[0]); // represents '/' in the file.
244 Expect.equals(47, buffer[1]); // represents '/' in the file. 242 Expect.equals(47, buffer[1]); // represents '/' in the file.
245 Expect.equals(32, buffer[2]); // represents ' ' in the file. 243 Expect.equals(32, buffer[2]); // represents ' ' in the file.
246 Expect.equals(67, buffer[3]); // represents 'C' in the file. 244 Expect.equals(67, buffer[3]); // represents 'C' in the file.
247 Expect.equals(111, buffer[4]); // represents 'o' in the file. 245 Expect.equals(111, buffer[4]); // represents 'o' in the file.
248 Expect.equals(112, buffer[5]); // represents 'p' in the file. 246 Expect.equals(112, buffer[5]); // represents 'p' in the file.
249 Expect.equals(121, buffer[6]); // represents 'y' in the file. 247 Expect.equals(121, buffer[6]); // represents 'y' in the file.
250 Expect.equals(114, buffer[7]); // represents 'r' in the file. 248 Expect.equals(114, buffer[7]); // represents 'r' in the file.
251 Expect.equals(105, buffer[8]); // represents 'i' in the file. 249 Expect.equals(105, buffer[8]); // represents 'i' in the file.
252 Expect.equals(103, buffer[9]); // represents 'g' in the file. 250 Expect.equals(103, buffer[9]); // represents 'g' in the file.
253 file.close().then((ignore) => asyncEnd()); 251 file.close().then((ignore) => asyncEnd());
254 }); 252 });
255 }); 253 });
256 }); 254 });
257 } 255 }
258 256
259 static void testReadSync() { 257 static void testReadSync() {
260 // Read a file and check part of it's contents. 258 // Read a file and check part of it's contents.
261 String filename = getFilename("file_test.txt"); 259 String filename = getFilename("file_test.txt");
262 RandomAccessFile raf = (new File(filename)).openSync(); 260 RandomAccessFile raf = (new File(filename)).openSync();
263 List<int> buffer = new List<int>(42); 261 List<int> buffer = new List<int>(42);
264 int bytes_read = 0; 262 int bytes_read = 0;
265 bytes_read = raf.readIntoSync(buffer, 0, 12); 263 bytes_read = raf.readIntoSync(buffer, 0, 12);
266 Expect.equals(12, bytes_read); 264 Expect.equals(12, bytes_read);
267 bytes_read = raf.readIntoSync(buffer, 12, 42); 265 bytes_read = raf.readIntoSync(buffer, 12, 42);
268 Expect.equals(30, bytes_read); 266 Expect.equals(30, bytes_read);
269 Expect.equals(47, buffer[0]); // represents '/' in the file. 267 Expect.equals(47, buffer[0]); // represents '/' in the file.
270 Expect.equals(47, buffer[1]); // represents '/' in the file. 268 Expect.equals(47, buffer[1]); // represents '/' in the file.
271 Expect.equals(32, buffer[2]); // represents ' ' in the file. 269 Expect.equals(32, buffer[2]); // represents ' ' in the file.
272 Expect.equals(67, buffer[3]); // represents 'C' in the file. 270 Expect.equals(67, buffer[3]); // represents 'C' in the file.
273 Expect.equals(111, buffer[4]); // represents 'o' in the file. 271 Expect.equals(111, buffer[4]); // represents 'o' in the file.
274 Expect.equals(112, buffer[5]); // represents 'p' in the file. 272 Expect.equals(112, buffer[5]); // represents 'p' in the file.
275 Expect.equals(121, buffer[6]); // represents 'y' in the file. 273 Expect.equals(121, buffer[6]); // represents 'y' in the file.
276 Expect.equals(114, buffer[7]); // represents 'r' in the file. 274 Expect.equals(114, buffer[7]); // represents 'r' in the file.
277 Expect.equals(105, buffer[8]); // represents 'i' in the file. 275 Expect.equals(105, buffer[8]); // represents 'i' in the file.
278 Expect.equals(103, buffer[9]); // represents 'g' in the file. 276 Expect.equals(103, buffer[9]); // represents 'g' in the file.
279 Expect.equals(104, buffer[10]); // represents 'h' in the file. 277 Expect.equals(104, buffer[10]); // represents 'h' in the file.
280 Expect.equals(116, buffer[11]); // represents 't' in the file. 278 Expect.equals(116, buffer[11]); // represents 't' in the file.
281 raf.closeSync(); 279 raf.closeSync();
282 280
283 filename = getFilename("fixed_length_file"); 281 filename = getFilename("fixed_length_file");
284 File file = new File(filename); 282 File file = new File(filename);
285 int len = file.lengthSync(); 283 int len = file.lengthSync();
286 int read(int length) { 284 int read(int length) {
287 var f = file.openSync(); 285 var f = file.openSync();
288 int res = f.readSync(length).length; 286 int res = f.readSync(length).length;
289 f.closeSync(); 287 f.closeSync();
290 return res; 288 return res;
291 } 289 }
290
292 Expect.equals(0, read(0)); 291 Expect.equals(0, read(0));
293 Expect.equals(1, read(1)); 292 Expect.equals(1, read(1));
294 Expect.equals(len - 1, read(len - 1)); 293 Expect.equals(len - 1, read(len - 1));
295 Expect.equals(len, read(len)); 294 Expect.equals(len, read(len));
296 Expect.equals(len, read(len + 1)); 295 Expect.equals(len, read(len + 1));
297 Expect.equals(len, read(len * 2)); 296 Expect.equals(len, read(len * 2));
298 Expect.equals(len, read(len * 10)); 297 Expect.equals(len, read(len * 10));
299 } 298 }
300 299
301 // Test for file read and write functionality. 300 // Test for file read and write functionality.
(...skipping 22 matching lines...) Expand all
324 List<int> buffer2 = new List<int>(bytes_read); 323 List<int> buffer2 = new List<int>(bytes_read);
325 final File file3 = new File(outFilename); 324 final File file3 = new File(outFilename);
326 file3.open(mode: READ).then((openedFile3) { 325 file3.open(mode: READ).then((openedFile3) {
327 openedFile3.readInto(buffer2, 0, 42).then((bytes_read) { 326 openedFile3.readInto(buffer2, 0, 42).then((bytes_read) {
328 Expect.equals(42, bytes_read); 327 Expect.equals(42, bytes_read);
329 openedFile3.close().then((ignore) { 328 openedFile3.close().then((ignore) {
330 // Now compare the two buffers to check if they 329 // Now compare the two buffers to check if they
331 // are identical. 330 // are identical.
332 Expect.equals(buffer1.length, buffer2.length); 331 Expect.equals(buffer1.length, buffer2.length);
333 for (int i = 0; i < buffer1.length; i++) { 332 for (int i = 0; i < buffer1.length; i++) {
334 Expect.equals(buffer1[i], buffer2[i]); 333 Expect.equals(buffer1[i], buffer2[i]);
335 } 334 }
336 // Delete the output file. 335 // Delete the output file.
337 final file4 = file3; 336 final file4 = file3;
338 file4.delete().then((ignore) { 337 file4.delete().then((ignore) {
339 file4.exists().then((exists) { 338 file4.exists().then((exists) {
340 Expect.isFalse(exists); 339 Expect.isFalse(exists);
341 asyncTestDone("testReadWrite"); 340 asyncTestDone("testReadWrite");
342 }); 341 });
343 }); 342 });
344 }); 343 });
(...skipping 21 matching lines...) Expand all
366 openedFile.closeSync(); 365 openedFile.closeSync();
367 // Reopen the file in write mode to ensure that we overwrite the content. 366 // Reopen the file in write mode to ensure that we overwrite the content.
368 openedFile = (new File(filename)).openSync(mode: WRITE); 367 openedFile = (new File(filename)).openSync(mode: WRITE);
369 openedFile.writeFromSync(buffer, 0, buffer.length); 368 openedFile.writeFromSync(buffer, 0, buffer.length);
370 Expect.equals(content.length, openedFile.lengthSync()); 369 Expect.equals(content.length, openedFile.lengthSync());
371 openedFile.closeSync(); 370 openedFile.closeSync();
372 // Open the file in append mode and ensure that we do not overwrite 371 // Open the file in append mode and ensure that we do not overwrite
373 // the existing content. 372 // the existing content.
374 openedFile = (new File(filename)).openSync(mode: APPEND); 373 openedFile = (new File(filename)).openSync(mode: APPEND);
375 openedFile.writeFromSync(buffer, 2, buffer.length - 2); 374 openedFile.writeFromSync(buffer, 2, buffer.length - 2);
376 Expect.equals(content.length + content.length - 4, 375 Expect.equals(content.length + content.length - 4, openedFile.lengthSync());
377 openedFile.lengthSync());
378 Expect.equals(content + content.substring(2, content.length - 2), 376 Expect.equals(content + content.substring(2, content.length - 2),
379 file.readAsStringSync()); 377 file.readAsStringSync());
380 openedFile.closeSync(); 378 openedFile.closeSync();
381 file.deleteSync(); 379 file.deleteSync();
382 } 380 }
383 381
384 static void testOutputStreamWriteAppend() { 382 static void testOutputStreamWriteAppend() {
385 asyncTestStarted(); 383 asyncTestStarted();
386 String content = "foobar"; 384 String content = "foobar";
387 String filename = tempDirectory.path + "/outstream_write_append"; 385 String filename = tempDirectory.path + "/outstream_write_append";
388 File file = new File(filename); 386 File file = new File(filename);
389 file.createSync(); 387 file.createSync();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 output.close(); 431 output.close();
434 output.done.then((_) { 432 output.done.then((_) {
435 RandomAccessFile raf = file.openSync(); 433 RandomAccessFile raf = file.openSync();
436 Expect.equals(38, raf.lengthSync()); 434 Expect.equals(38, raf.lengthSync());
437 raf.close().then((ignore) { 435 raf.close().then((ignore) {
438 asyncTestDone("testOutputStreamWriteString"); 436 asyncTestDone("testOutputStreamWriteString");
439 }); 437 });
440 }); 438 });
441 } 439 }
442 440
443
444 static void testReadWriteSync() { 441 static void testReadWriteSync() {
445 // Read a file. 442 // Read a file.
446 String inFilename = getFilename("fixed_length_file"); 443 String inFilename = getFilename("fixed_length_file");
447 RandomAccessFile file = (new File(inFilename)).openSync(); 444 RandomAccessFile file = (new File(inFilename)).openSync();
448 List<int> buffer1 = new List<int>(42); 445 List<int> buffer1 = new List<int>(42);
449 int bytes_read = 0; 446 int bytes_read = 0;
450 int bytes_written = 0; 447 int bytes_written = 0;
451 bytes_read = file.readIntoSync(buffer1, 0, 42); 448 bytes_read = file.readIntoSync(buffer1, 0, 42);
452 Expect.equals(42, bytes_read); 449 Expect.equals(42, bytes_read);
453 file.closeSync(); 450 file.closeSync();
(...skipping 11 matching lines...) Expand all
465 openedFile.closeSync(); 462 openedFile.closeSync();
466 // Now read the contents of the file just written. 463 // Now read the contents of the file just written.
467 List<int> buffer2 = new List<int>(bytes_read); 464 List<int> buffer2 = new List<int>(bytes_read);
468 openedFile = (new File(outFilename)).openSync(); 465 openedFile = (new File(outFilename)).openSync();
469 bytes_read = openedFile.readIntoSync(buffer2, 0, 42); 466 bytes_read = openedFile.readIntoSync(buffer2, 0, 42);
470 Expect.equals(42, bytes_read); 467 Expect.equals(42, bytes_read);
471 openedFile.closeSync(); 468 openedFile.closeSync();
472 // Now compare the two buffers to check if they are identical. 469 // Now compare the two buffers to check if they are identical.
473 Expect.equals(buffer1.length, buffer2.length); 470 Expect.equals(buffer1.length, buffer2.length);
474 for (int i = 0; i < buffer1.length; i++) { 471 for (int i = 0; i < buffer1.length; i++) {
475 Expect.equals(buffer1[i], buffer2[i]); 472 Expect.equals(buffer1[i], buffer2[i]);
476 } 473 }
477 // Delete the output file. 474 // Delete the output file.
478 outFile.deleteSync(); 475 outFile.deleteSync();
479 Expect.isFalse(outFile.existsSync()); 476 Expect.isFalse(outFile.existsSync());
480 } 477 }
481 478
482 static void testReadWriteNoArgsSync() { 479 static void testReadWriteNoArgsSync() {
483 // Read a file. 480 // Read a file.
484 String inFilename = getFilename("fixed_length_file"); 481 String inFilename = getFilename("fixed_length_file");
485 RandomAccessFile file = (new File(inFilename)).openSync(); 482 RandomAccessFile file = (new File(inFilename)).openSync();
(...skipping 17 matching lines...) Expand all
503 openedFile.closeSync(); 500 openedFile.closeSync();
504 // Now read the contents of the file just written. 501 // Now read the contents of the file just written.
505 List<int> buffer2 = new List<int>(bytes_read); 502 List<int> buffer2 = new List<int>(bytes_read);
506 openedFile = (new File(outFilename)).openSync(); 503 openedFile = (new File(outFilename)).openSync();
507 bytes_read = openedFile.readIntoSync(buffer2, 0); 504 bytes_read = openedFile.readIntoSync(buffer2, 0);
508 Expect.equals(42, bytes_read); 505 Expect.equals(42, bytes_read);
509 openedFile.closeSync(); 506 openedFile.closeSync();
510 // Now compare the two buffers to check if they are identical. 507 // Now compare the two buffers to check if they are identical.
511 Expect.equals(buffer1.length, buffer2.length); 508 Expect.equals(buffer1.length, buffer2.length);
512 for (int i = 0; i < buffer1.length; i++) { 509 for (int i = 0; i < buffer1.length; i++) {
513 Expect.equals(buffer1[i], buffer2[i]); 510 Expect.equals(buffer1[i], buffer2[i]);
514 } 511 }
515 // Delete the output file. 512 // Delete the output file.
516 outFile.deleteSync(); 513 outFile.deleteSync();
517 Expect.isFalse(outFile.existsSync()); 514 Expect.isFalse(outFile.existsSync());
518 } 515 }
519 516
520 static void testReadEmptyFileSync() { 517 static void testReadEmptyFileSync() {
521 String fileName = tempDirectory.path + "/empty_file_sync"; 518 String fileName = tempDirectory.path + "/empty_file_sync";
522 File file = new File(fileName); 519 File file = new File(fileName);
523 file.createSync(); 520 file.createSync();
(...skipping 27 matching lines...) Expand all
551 final File file = new File(fileName); 548 final File file = new File(fileName);
552 file.create().then((ignore) { 549 file.create().then((ignore) {
553 file.open(mode: WRITE).then((RandomAccessFile openedFile) { 550 file.open(mode: WRITE).then((RandomAccessFile openedFile) {
554 // Write bytes from 0 to 7. 551 // Write bytes from 0 to 7.
555 openedFile.writeFromSync([0], 0, 1); 552 openedFile.writeFromSync([0], 0, 1);
556 openedFile.writeFromSync(const [1], 0, 1); 553 openedFile.writeFromSync(const [1], 0, 1);
557 openedFile.writeFromSync(new MyListOfOneElement(2), 0, 1); 554 openedFile.writeFromSync(new MyListOfOneElement(2), 0, 1);
558 var x = 12345678901234567890123456789012345678901234567890; 555 var x = 12345678901234567890123456789012345678901234567890;
559 var y = 12345678901234567890123456789012345678901234567893; 556 var y = 12345678901234567890123456789012345678901234567893;
560 openedFile.writeFromSync([y - x], 0, 1); 557 openedFile.writeFromSync([y - x], 0, 1);
561 openedFile.writeFromSync([260], 0, 1); // 260 = 256 + 4 = 0x104. 558 openedFile.writeFromSync([260], 0, 1); // 260 = 256 + 4 = 0x104.
562 openedFile.writeFromSync(const [261], 0, 1); 559 openedFile.writeFromSync(const [261], 0, 1);
563 openedFile.writeFromSync(new MyListOfOneElement(262), 0, 1); 560 openedFile.writeFromSync(new MyListOfOneElement(262), 0, 1);
564 x = 12345678901234567890123456789012345678901234567890; 561 x = 12345678901234567890123456789012345678901234567890;
565 y = 12345678901234567890123456789012345678901234568153; 562 y = 12345678901234567890123456789012345678901234568153;
566 openedFile.writeFrom([y - x], 0, 1).then((ignore) { 563 openedFile.writeFrom([y - x], 0, 1).then((ignore) {
567 openedFile.close().then((ignore) { 564 openedFile.close().then((ignore) {
568 // Check the written bytes. 565 // Check the written bytes.
569 final File file2 = new File(fileName); 566 final File file2 = new File(fileName);
570 var openedFile2 = file2.openSync(); 567 var openedFile2 = file2.openSync();
571 var length = openedFile2.lengthSync(); 568 var length = openedFile2.lengthSync();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 if (tmp != null) { 600 if (tmp != null) {
604 tmp.deleteSync(recursive: true); 601 tmp.deleteSync(recursive: true);
605 } 602 }
606 } 603 }
607 } 604 }
608 605
609 static void testDirectory() { 606 static void testDirectory() {
610 asyncTestStarted(); 607 asyncTestStarted();
611 var tempDir = tempDirectory.path; 608 var tempDir = tempDirectory.path;
612 var file = new File("${tempDir}/testDirectory"); 609 var file = new File("${tempDir}/testDirectory");
613 file.create().then((ignore) { 610 file.create().then((ignore) {
614 Directory d = file.parent; 611 Directory d = file.parent;
615 d.exists().then((xexists) { 612 d.exists().then((xexists) {
616 Expect.isTrue(xexists); 613 Expect.isTrue(xexists);
617 Expect.isTrue(d.path.endsWith(tempDir)); 614 Expect.isTrue(d.path.endsWith(tempDir));
618 file.delete().then((ignore) => asyncTestDone("testDirectory")); 615 file.delete().then((ignore) => asyncTestDone("testDirectory"));
619 });
620 }); 616 });
617 });
621 } 618 }
622 619
623 static void testDirectorySync() { 620 static void testDirectorySync() {
624 var tempDir = tempDirectory.path; 621 var tempDir = tempDirectory.path;
625 var file = new File("${tempDir}/testDirectorySync"); 622 var file = new File("${tempDir}/testDirectorySync");
626 // Non-existing file still provides the directory. 623 // Non-existing file still provides the directory.
627 Expect.equals("${tempDir}", file.parent.path); 624 Expect.equals("${tempDir}", file.parent.path);
628 file.createSync(); 625 file.createSync();
629 // Check that the path of the returned directory is the temp directory. 626 // Check that the path of the returned directory is the temp directory.
630 Directory d = file.parent; 627 Directory d = file.parent;
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 } 831 }
835 832
836 static testWriteFrom() async { 833 static testWriteFrom() async {
837 asyncTestStarted(); 834 asyncTestStarted();
838 File file = new File(tempDirectory.path + "/out_write_from"); 835 File file = new File(tempDirectory.path + "/out_write_from");
839 836
840 var buffer = const [1, 2, 3]; 837 var buffer = const [1, 2, 3];
841 var openedFile = await file.open(mode: WRITE); 838 var openedFile = await file.open(mode: WRITE);
842 839
843 await openedFile.writeFrom(buffer); 840 await openedFile.writeFrom(buffer);
844 var result = []..addAll(buffer);; 841 var result = []..addAll(buffer);
842 ;
845 843
846 write([start, end]) async { 844 write([start, end]) async {
847 var returnValue = await openedFile.writeFrom(buffer, start, end); 845 var returnValue = await openedFile.writeFrom(buffer, start, end);
848 Expect.identical(openedFile, returnValue); 846 Expect.identical(openedFile, returnValue);
849 result.addAll(buffer.sublist(start, end)); 847 result.addAll(buffer.sublist(start, end));
850 } 848 }
849
851 await write(0, 3); 850 await write(0, 3);
852 await write(0, 2); 851 await write(0, 2);
853 await write(1, 2); 852 await write(1, 2);
854 await write(1, 3); 853 await write(1, 3);
855 await write(2, 3); 854 await write(2, 3);
856 await write(0, 0); 855 await write(0, 0);
857 856
858 var bytesFromFile = await file.readAsBytes(); 857 var bytesFromFile = await file.readAsBytes();
859 Expect.listEquals(result, bytesFromFile); 858 Expect.listEquals(result, bytesFromFile);
860 859
861 await openedFile.close(); 860 await openedFile.close();
862 861
863 asyncTestDone("testWriteFrom"); 862 asyncTestDone("testWriteFrom");
864 } 863 }
865 864
866 static void testWriteFromSync() { 865 static void testWriteFromSync() {
867 File file = new File(tempDirectory.path + "/out_write_from_sync"); 866 File file = new File(tempDirectory.path + "/out_write_from_sync");
868 867
869 var buffer = const [1, 2, 3]; 868 var buffer = const [1, 2, 3];
870 var openedFile = file.openSync(mode: WRITE); 869 var openedFile = file.openSync(mode: WRITE);
871 870
872 openedFile.writeFromSync(buffer); 871 openedFile.writeFromSync(buffer);
873 var result = []..addAll(buffer);; 872 var result = []..addAll(buffer);
873 ;
874 874
875 write([start, end]) { 875 write([start, end]) {
876 var returnValue = openedFile.writeFromSync(buffer, start, end); 876 var returnValue = openedFile.writeFromSync(buffer, start, end);
877 result.addAll(buffer.sublist(start, end)); 877 result.addAll(buffer.sublist(start, end));
878 } 878 }
879
879 write(0, 3); 880 write(0, 3);
880 write(0, 2); 881 write(0, 2);
881 write(1, 2); 882 write(1, 2);
882 write(1, 3); 883 write(1, 3);
883 write(2, 3); 884 write(2, 3);
884 885
885 var bytesFromFile = file.readAsBytesSync(); 886 var bytesFromFile = file.readAsBytesSync();
886 Expect.listEquals(result, bytesFromFile); 887 Expect.listEquals(result, bytesFromFile);
887 888
888 openedFile.closeSync(); 889 openedFile.closeSync();
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 } 977 }
977 Expect.equals(true, exceptionCaught); 978 Expect.equals(true, exceptionCaught);
978 Expect.equals(true, !wrongExceptionCaught); 979 Expect.equals(true, !wrongExceptionCaught);
979 input.deleteSync(); 980 input.deleteSync();
980 } 981 }
981 982
982 // Tests stream exception handling after file was closed. 983 // Tests stream exception handling after file was closed.
983 static void testCloseExceptionStream() { 984 static void testCloseExceptionStream() {
984 asyncTestStarted(); 985 asyncTestStarted();
985 List<int> buffer = new List<int>(42); 986 List<int> buffer = new List<int>(42);
986 File file = 987 File file = new File(tempDirectory.path + "/out_close_exception_stream");
987 new File(tempDirectory.path + "/out_close_exception_stream");
988 file.createSync(); 988 file.createSync();
989 var output = file.openWrite(); 989 var output = file.openWrite();
990 output.close(); 990 output.close();
991 output.add(buffer); // Ignored. 991 output.add(buffer); // Ignored.
992 output.done.then((_) { 992 output.done.then((_) {
993 file.deleteSync(); 993 file.deleteSync();
994 asyncTestDone("testCloseExceptionStream"); 994 asyncTestDone("testCloseExceptionStream");
995 }); 995 });
996 } 996 }
997 997
998 // Tests buffer out of bounds exception. 998 // Tests buffer out of bounds exception.
999 static void testBufferOutOfBoundsException() { 999 static void testBufferOutOfBoundsException() {
1000 bool exceptionCaught = false; 1000 bool exceptionCaught = false;
1001 bool wrongExceptionCaught = false; 1001 bool wrongExceptionCaught = false;
1002 File file = 1002 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
1003 new File(tempDirectory.path + "/out_buffer_out_of_bounds");
1004 RandomAccessFile openedFile = file.openSync(mode: WRITE); 1003 RandomAccessFile openedFile = file.openSync(mode: WRITE);
1005 try { 1004 try {
1006 List<int> buffer = new List<int>(10); 1005 List<int> buffer = new List<int>(10);
1007 openedFile.readIntoSync(buffer, 0, 12); 1006 openedFile.readIntoSync(buffer, 0, 12);
1008 } on RangeError catch (ex) { 1007 } on RangeError catch (ex) {
1009 exceptionCaught = true; 1008 exceptionCaught = true;
1010 } on Exception catch (ex) { 1009 } on Exception catch (ex) {
1011 wrongExceptionCaught = true; 1010 wrongExceptionCaught = true;
1012 } 1011 }
1013 Expect.equals(true, exceptionCaught); 1012 Expect.equals(true, exceptionCaught);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 } 1088 }
1090 Expect.equals(true, exceptionCaught); 1089 Expect.equals(true, exceptionCaught);
1091 Expect.equals(true, !wrongExceptionCaught); 1090 Expect.equals(true, !wrongExceptionCaught);
1092 openedFile.closeSync(); 1091 openedFile.closeSync();
1093 file.deleteSync(); 1092 file.deleteSync();
1094 } 1093 }
1095 1094
1096 static void testOpenDirectoryAsFile() { 1095 static void testOpenDirectoryAsFile() {
1097 var f = new File('.'); 1096 var f = new File('.');
1098 var future = f.open(mode: READ); 1097 var future = f.open(mode: READ);
1099 future.then((r) => Expect.fail('Directory opened as file')) 1098 future
1100 .catchError((e) {}); 1099 .then((r) => Expect.fail('Directory opened as file'))
1100 .catchError((e) {});
1101 } 1101 }
1102 1102
1103 static void testOpenDirectoryAsFileSync() { 1103 static void testOpenDirectoryAsFileSync() {
1104 var f = new File('.'); 1104 var f = new File('.');
1105 try { 1105 try {
1106 f.openSync(); 1106 f.openSync();
1107 Expect.fail("Expected exception opening directory as file"); 1107 Expect.fail("Expected exception opening directory as file");
1108 } catch (e) { 1108 } catch (e) {
1109 Expect.isTrue(e is FileSystemException); 1109 Expect.isTrue(e is FileSystemException);
1110 } 1110 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 var text = new File(name).readAsStringSync(); 1188 var text = new File(name).readAsStringSync();
1189 Expect.isTrue(text.endsWith("42 bytes.")); 1189 Expect.isTrue(text.endsWith("42 bytes."));
1190 Expect.equals(42, text.length); 1190 Expect.equals(42, text.length);
1191 name = getFilename("read_as_text.dat"); 1191 name = getFilename("read_as_text.dat");
1192 text = new File(name).readAsStringSync(); 1192 text = new File(name).readAsStringSync();
1193 Expect.equals(6, text.length); 1193 Expect.equals(6, text.length);
1194 var expected = [955, 120, 46, 32, 120, 10]; 1194 var expected = [955, 120, 46, 32, 120, 10];
1195 Expect.listEquals(expected, text.codeUnits); 1195 Expect.listEquals(expected, text.codeUnits);
1196 // First character is not ASCII. The default ASCII decoder will throw. 1196 // First character is not ASCII. The default ASCII decoder will throw.
1197 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), 1197 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII),
1198 (e) => e is FileSystemException); 1198 (e) => e is FileSystemException);
1199 // We can use an ASCII decoder that inserts the replacement character. 1199 // We can use an ASCII decoder that inserts the replacement character.
1200 var lenientAscii = const AsciiCodec(allowInvalid: true); 1200 var lenientAscii = const AsciiCodec(allowInvalid: true);
1201 text = new File(name).readAsStringSync(encoding: lenientAscii); 1201 text = new File(name).readAsStringSync(encoding: lenientAscii);
1202 // Default replacement character is the Unicode replacement character. 1202 // Default replacement character is the Unicode replacement character.
1203 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, 1203 expected = [
1204 UNICODE_REPLACEMENT_CHARACTER_RUNE, 1204 UNICODE_REPLACEMENT_CHARACTER_RUNE,
1205 120, 46, 32, 120, 10]; 1205 UNICODE_REPLACEMENT_CHARACTER_RUNE,
1206 120,
1207 46,
1208 32,
1209 120,
1210 10
1211 ];
1206 Expect.listEquals(expected, text.codeUnits); 1212 Expect.listEquals(expected, text.codeUnits);
1207 text = new File(name).readAsStringSync(encoding: LATIN1); 1213 text = new File(name).readAsStringSync(encoding: LATIN1);
1208 expected = [206, 187, 120, 46, 32, 120, 10]; 1214 expected = [206, 187, 120, 46, 32, 120, 10];
1209 Expect.equals(7, text.length); 1215 Expect.equals(7, text.length);
1210 Expect.listEquals(expected, text.codeUnits); 1216 Expect.listEquals(expected, text.codeUnits);
1211 } 1217 }
1212 1218
1213 static void testReadAsTextSyncEmptyFile() { 1219 static void testReadAsTextSyncEmptyFile() {
1214 var name = getFilename("empty_file"); 1220 var name = getFilename("empty_file");
1215 var text = new File(name).readAsStringSync(); 1221 var text = new File(name).readAsStringSync();
(...skipping 18 matching lines...) Expand all
1234 var lines = new File(name).readAsLinesSync(); 1240 var lines = new File(name).readAsLinesSync();
1235 Expect.equals(1, lines.length); 1241 Expect.equals(1, lines.length);
1236 var line = lines[0]; 1242 var line = lines[0];
1237 Expect.isTrue(line.endsWith("42 bytes.")); 1243 Expect.isTrue(line.endsWith("42 bytes."));
1238 Expect.equals(42, line.length); 1244 Expect.equals(42, line.length);
1239 name = getFilename("readline_test1.dat"); 1245 name = getFilename("readline_test1.dat");
1240 lines = new File(name).readAsLinesSync(); 1246 lines = new File(name).readAsLinesSync();
1241 Expect.equals(10, lines.length); 1247 Expect.equals(10, lines.length);
1242 } 1248 }
1243 1249
1244
1245 static void testReadAsErrors() { 1250 static void testReadAsErrors() {
1246 asyncTestStarted(); 1251 asyncTestStarted();
1247 var f = new File('.'); 1252 var f = new File('.');
1248 Expect.throws(f.readAsBytesSync, (e) => e is FileSystemException); 1253 Expect.throws(f.readAsBytesSync, (e) => e is FileSystemException);
1249 Expect.throws(f.readAsStringSync, (e) => e is FileSystemException); 1254 Expect.throws(f.readAsStringSync, (e) => e is FileSystemException);
1250 Expect.throws(f.readAsLinesSync, (e) => e is FileSystemException); 1255 Expect.throws(f.readAsLinesSync, (e) => e is FileSystemException);
1251 var readAsBytesFuture = f.readAsBytes(); 1256 var readAsBytesFuture = f.readAsBytes();
1252 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) 1257 readAsBytesFuture
1253 .catchError((e) { 1258 .then((bytes) => Expect.fail("no bytes expected"))
1259 .catchError((e) {
1254 var readAsStringFuture = f.readAsString(encoding: UTF8); 1260 var readAsStringFuture = f.readAsString(encoding: UTF8);
1255 readAsStringFuture.then((text) => Expect.fail("no text expected")) 1261 readAsStringFuture
1256 .catchError((e) { 1262 .then((text) => Expect.fail("no text expected"))
1263 .catchError((e) {
1257 var readAsLinesFuture = f.readAsLines(encoding: UTF8); 1264 var readAsLinesFuture = f.readAsLines(encoding: UTF8);
1258 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) 1265 readAsLinesFuture
1259 .catchError((e) { 1266 .then((lines) => Expect.fail("no lines expected"))
1267 .catchError((e) {
1260 asyncTestDone("testReadAsLines"); 1268 asyncTestDone("testReadAsLines");
1261 }); 1269 });
1262 }); 1270 });
1263 }); 1271 });
1264 } 1272 }
1265 1273
1266 static void testLastModified() { 1274 static void testLastModified() {
1267 asyncTestStarted(); 1275 asyncTestStarted();
1268 new File(Platform.executable).lastModified().then((modified) { 1276 new File(Platform.executable).lastModified().then((modified) {
1269 Expect.isTrue(modified is DateTime); 1277 Expect.isTrue(modified is DateTime);
(...skipping 11 matching lines...) Expand all
1281 }); 1289 });
1282 } 1290 }
1283 1291
1284 static void testDoubleAsyncOperation() { 1292 static void testDoubleAsyncOperation() {
1285 asyncTestStarted(); 1293 asyncTestStarted();
1286 var file = new File(Platform.executable).openSync(); 1294 var file = new File(Platform.executable).openSync();
1287 var completer = new Completer(); 1295 var completer = new Completer();
1288 int done = 0; 1296 int done = 0;
1289 bool error = false; 1297 bool error = false;
1290 void getLength() { 1298 void getLength() {
1291 file.length() 1299 file.length().catchError((e) {
1292 .catchError((e) { error = true; }) 1300 error = true;
1293 .whenComplete(() { 1301 }).whenComplete(() {
1294 if (++done == 2) { 1302 if (++done == 2) {
1295 asyncTestDone("testDoubleAsyncOperation"); 1303 asyncTestDone("testDoubleAsyncOperation");
1296 Expect.isTrue(error); 1304 Expect.isTrue(error);
1297 file.lengthSync(); 1305 file.lengthSync();
1298 file.closeSync(); 1306 file.closeSync();
1299 } 1307 }
1300 }); 1308 });
1301 } 1309 }
1310
1302 getLength(); 1311 getLength();
1303 getLength(); 1312 getLength();
1304 Expect.throws(() => file.lengthSync()); 1313 Expect.throws(() => file.lengthSync());
1305 } 1314 }
1306 1315
1307 static void testLastModifiedSync() { 1316 static void testLastModifiedSync() {
1308 var modified = new File(Platform.executable).lastModifiedSync(); 1317 var modified = new File(Platform.executable).lastModifiedSync();
1309 Expect.isTrue(modified is DateTime); 1318 Expect.isTrue(modified is DateTime);
1310 Expect.isTrue(modified.isBefore(new DateTime.now())); 1319 Expect.isTrue(modified.isBefore(new DateTime.now()));
1311 } 1320 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1355 File file = new File(newFilePath); 1364 File file = new File(newFilePath);
1356 file.createSync(); 1365 file.createSync();
1357 DateTime modifiedTime = new DateTime(2016, 1, 1); 1366 DateTime modifiedTime = new DateTime(2016, 1, 1);
1358 file.setLastModifiedSync(modifiedTime); 1367 file.setLastModifiedSync(modifiedTime);
1359 FileStat stat = file.statSync(); 1368 FileStat stat = file.statSync();
1360 Expect.equals(2016, stat.modified.year); 1369 Expect.equals(2016, stat.modified.year);
1361 Expect.equals(1, stat.modified.month); 1370 Expect.equals(1, stat.modified.month);
1362 Expect.equals(1, stat.modified.day); 1371 Expect.equals(1, stat.modified.day);
1363 } 1372 }
1364 1373
1365
1366 static testSetLastModified() async { 1374 static testSetLastModified() async {
1367 asyncTestStarted(); 1375 asyncTestStarted();
1368 String newFilePath = '${tempDirectory.path}/set_last_modified_test'; 1376 String newFilePath = '${tempDirectory.path}/set_last_modified_test';
1369 File file = new File(newFilePath); 1377 File file = new File(newFilePath);
1370 file.createSync(); 1378 file.createSync();
1371 DateTime modifiedTime = new DateTime(2016, 1, 1); 1379 DateTime modifiedTime = new DateTime(2016, 1, 1);
1372 await file.setLastModified(modifiedTime); 1380 await file.setLastModified(modifiedTime);
1373 FileStat stat = await file.stat(); 1381 FileStat stat = await file.stat();
1374 Expect.equals(2016, stat.modified.year); 1382 Expect.equals(2016, stat.modified.year);
1375 Expect.equals(1, stat.modified.month); 1383 Expect.equals(1, stat.modified.month);
1376 Expect.equals(1, stat.modified.day); 1384 Expect.equals(1, stat.modified.day);
1377 asyncTestDone("testSetLastModified"); 1385 asyncTestDone("testSetLastModified");
1378 } 1386 }
1379 1387
1380
1381 static void testSetLastModifiedSyncDirectory() { 1388 static void testSetLastModifiedSyncDirectory() {
1382 Directory tmp = tempDirectory.createTempSync('file_last_modified_test_'); 1389 Directory tmp = tempDirectory.createTempSync('file_last_modified_test_');
1383 String dirPath = '${tmp.path}/dir'; 1390 String dirPath = '${tmp.path}/dir';
1384 new Directory(dirPath).createSync(); 1391 new Directory(dirPath).createSync();
1385 try { 1392 try {
1386 DateTime modifiedTime = new DateTime(2016, 1, 1); 1393 DateTime modifiedTime = new DateTime(2016, 1, 1);
1387 new File(dirPath).setLastModifiedSync(modifiedTime); 1394 new File(dirPath).setLastModifiedSync(modifiedTime);
1388 Expect.fail('Expected operation to throw'); 1395 Expect.fail('Expected operation to throw');
1389 } catch (e) { 1396 } catch (e) {
1390 if (e is! FileSystemException) { 1397 if (e is! FileSystemException) {
(...skipping 10 matching lines...) Expand all
1401 File file = new File(newFilePath); 1408 File file = new File(newFilePath);
1402 file.createSync(); 1409 file.createSync();
1403 DateTime accessedTime = new DateTime(2016, 1, 1); 1410 DateTime accessedTime = new DateTime(2016, 1, 1);
1404 file.setLastAccessedSync(accessedTime); 1411 file.setLastAccessedSync(accessedTime);
1405 FileStat stat = file.statSync(); 1412 FileStat stat = file.statSync();
1406 Expect.equals(2016, stat.accessed.year); 1413 Expect.equals(2016, stat.accessed.year);
1407 Expect.equals(1, stat.accessed.month); 1414 Expect.equals(1, stat.accessed.month);
1408 Expect.equals(1, stat.accessed.day); 1415 Expect.equals(1, stat.accessed.day);
1409 } 1416 }
1410 1417
1411
1412 static testSetLastAccessed() async { 1418 static testSetLastAccessed() async {
1413 asyncTestStarted(); 1419 asyncTestStarted();
1414 String newFilePath = '${tempDirectory.path}/set_last_accessed_test'; 1420 String newFilePath = '${tempDirectory.path}/set_last_accessed_test';
1415 File file = new File(newFilePath); 1421 File file = new File(newFilePath);
1416 file.createSync(); 1422 file.createSync();
1417 DateTime accessedTime = new DateTime(2016, 1, 1); 1423 DateTime accessedTime = new DateTime(2016, 1, 1);
1418 await file.setLastAccessed(accessedTime); 1424 await file.setLastAccessed(accessedTime);
1419 FileStat stat = await file.stat(); 1425 FileStat stat = await file.stat();
1420 Expect.equals(2016, stat.accessed.year); 1426 Expect.equals(2016, stat.accessed.year);
1421 Expect.equals(1, stat.accessed.month); 1427 Expect.equals(1, stat.accessed.month);
1422 Expect.equals(1, stat.accessed.day); 1428 Expect.equals(1, stat.accessed.day);
1423 asyncTestDone("testSetLastAccessed"); 1429 asyncTestDone("testSetLastAccessed");
1424 } 1430 }
1425 1431
1426
1427 static void testSetLastAccessedSyncDirectory() { 1432 static void testSetLastAccessedSyncDirectory() {
1428 Directory tmp = tempDirectory.createTempSync('file_last_accessed_test_'); 1433 Directory tmp = tempDirectory.createTempSync('file_last_accessed_test_');
1429 String dirPath = '${tmp.path}/dir'; 1434 String dirPath = '${tmp.path}/dir';
1430 new Directory(dirPath).createSync(); 1435 new Directory(dirPath).createSync();
1431 try { 1436 try {
1432 DateTime accessedTime = new DateTime(2016, 1, 1); 1437 DateTime accessedTime = new DateTime(2016, 1, 1);
1433 new File(dirPath).setLastAccessedSync(accessedTime); 1438 new File(dirPath).setLastAccessedSync(accessedTime);
1434 Expect.fail('Expected operation to throw'); 1439 Expect.fail('Expected operation to throw');
1435 } catch (e) { 1440 } catch (e) {
1436 if (e is! FileSystemException) { 1441 if (e is! FileSystemException) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 file.open(mode: APPEND).then((openedFile) { 1505 file.open(mode: APPEND).then((openedFile) {
1501 openedFile.setPosition(2).then((_) { 1506 openedFile.setPosition(2).then((_) {
1502 openedFile.writeString(string).then((_) { 1507 openedFile.writeString(string).then((_) {
1503 openedFile.length().then((l) { 1508 openedFile.length().then((l) {
1504 Expect.equals(4, l); 1509 Expect.equals(4, l);
1505 openedFile.close().then((_) { 1510 openedFile.close().then((_) {
1506 file.readAsString().then((readBack) { 1511 file.readAsString().then((readBack) {
1507 Expect.stringEquals(readBack, '$string$string'); 1512 Expect.stringEquals(readBack, '$string$string');
1508 file.delete().then((_) { 1513 file.delete().then((_) {
1509 file.exists().then((e) { 1514 file.exists().then((e) {
1510 Expect.isFalse(e); 1515 Expect.isFalse(e);
1511 asyncTestDone("testWriteStringUtf8"); 1516 asyncTestDone("testWriteStringUtf8");
1512 }); 1517 });
1513 }); 1518 });
1514 }); 1519 });
1515 }); 1520 });
1516 }); 1521 });
1517 }); 1522 });
1518 }); 1523 });
1519 }); 1524 });
1520 }); 1525 });
1521 }); 1526 });
(...skipping 13 matching lines...) Expand all
1535 openedFile.writeStringSync(string); 1540 openedFile.writeStringSync(string);
1536 Expect.equals(4, openedFile.lengthSync()); 1541 Expect.equals(4, openedFile.lengthSync());
1537 openedFile.closeSync(); 1542 openedFile.closeSync();
1538 var readBack = file.readAsStringSync(); 1543 var readBack = file.readAsStringSync();
1539 Expect.stringEquals(readBack, '$string$string'); 1544 Expect.stringEquals(readBack, '$string$string');
1540 file.deleteSync(); 1545 file.deleteSync();
1541 Expect.isFalse(file.existsSync()); 1546 Expect.isFalse(file.existsSync());
1542 } 1547 }
1543 1548
1544 static void testRename({bool targetExists}) { 1549 static void testRename({bool targetExists}) {
1545 lift(Function f) => 1550 lift(Function f) => (futureValue) => futureValue.then((value) => f(value));
1546 (futureValue) => futureValue.then((value) => f(value));
1547 asyncTestStarted(); 1551 asyncTestStarted();
1548 1552
1549 String source = join(tempDirectory.path, 'rename_${targetExists}_source'); 1553 String source = join(tempDirectory.path, 'rename_${targetExists}_source');
1550 String dest = join(tempDirectory.path, 'rename_${targetExists}_dest'); 1554 String dest = join(tempDirectory.path, 'rename_${targetExists}_dest');
1551 var file = new File(source); 1555 var file = new File(source);
1552 var newfile = new File(dest); 1556 var newfile = new File(dest);
1553 file.create() 1557 file
1554 .then((_) => targetExists ? newfile.create() : null) 1558 .create()
1555 .then((_) => file.rename(dest)) 1559 .then((_) => targetExists ? newfile.create() : null)
1556 .then((_) => lift(Expect.isFalse)(file.exists())) 1560 .then((_) => file.rename(dest))
1557 .then((_) => lift(Expect.isTrue)(newfile.exists())) 1561 .then((_) => lift(Expect.isFalse)(file.exists()))
1558 .then((_) => newfile.delete()) 1562 .then((_) => lift(Expect.isTrue)(newfile.exists()))
1559 .then((_) => lift(Expect.isFalse)(newfile.exists())) 1563 .then((_) => newfile.delete())
1560 .then((_) { 1564 .then((_) => lift(Expect.isFalse)(newfile.exists()))
1561 if (Platform.operatingSystem != "windows") { 1565 .then((_) {
1562 new Link(source).create(dest) 1566 if (Platform.operatingSystem != "windows") {
1563 .then((_) => file.rename("xxx")) 1567 new Link(source).create(dest).then((_) => file.rename("xxx")).then((_) {
1564 .then((_) { throw "Rename of broken link succeeded"; }) 1568 throw "Rename of broken link succeeded";
1565 .catchError((e) { 1569 }).catchError((e) {
1566 Expect.isTrue(e is FileSystemException); 1570 Expect.isTrue(e is FileSystemException);
1567 asyncTestDone("testRename$targetExists");
1568 });
1569 } else {
1570 asyncTestDone("testRename$targetExists"); 1571 asyncTestDone("testRename$targetExists");
1571 } 1572 });
1572 }); 1573 } else {
1574 asyncTestDone("testRename$targetExists");
1575 }
1576 });
1573 } 1577 }
1574 1578
1575 static void testRenameSync({bool targetExists}) { 1579 static void testRenameSync({bool targetExists}) {
1576 String source = join(tempDirectory.path, 'rename_source'); 1580 String source = join(tempDirectory.path, 'rename_source');
1577 String dest = join(tempDirectory.path, 'rename_dest'); 1581 String dest = join(tempDirectory.path, 'rename_dest');
1578 var file = new File(source); 1582 var file = new File(source);
1579 var newfile = new File(dest); 1583 var newfile = new File(dest);
1580 file.createSync(); 1584 file.createSync();
1581 if (targetExists) { 1585 if (targetExists) {
1582 newfile.createSync(); 1586 newfile.createSync();
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1671 testSetLastAccessedSyncDirectory(); 1675 testSetLastAccessedSyncDirectory();
1672 testDoubleAsyncOperation(); 1676 testDoubleAsyncOperation();
1673 asyncEnd(); 1677 asyncEnd();
1674 }); 1678 });
1675 } 1679 }
1676 } 1680 }
1677 1681
1678 main() { 1682 main() {
1679 FileTest.testMain(); 1683 FileTest.testMain();
1680 } 1684 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698