OLD | NEW |
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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 | 10 |
11 testWriteAsBytesSync(dir) { | 11 testWriteAsBytesSync(dir) { |
12 var f = new File('${dir.path}/bytes_sync.txt'); | 12 var f = new File('${dir.path}/bytes_sync.txt'); |
13 var data = [50,50,50]; | 13 var data = [50, 50, 50]; |
14 f.writeAsBytesSync(data); | 14 f.writeAsBytesSync(data); |
15 Expect.listEquals(data, f.readAsBytesSync()); | 15 Expect.listEquals(data, f.readAsBytesSync()); |
16 f.writeAsBytesSync(data, mode: FileMode.APPEND); | 16 f.writeAsBytesSync(data, mode: FileMode.APPEND, flush: true); |
17 var expected = [50, 50, 50, 50, 50, 50]; | 17 var expected = [50, 50, 50, 50, 50, 50]; |
18 Expect.listEquals(expected, f.readAsBytesSync()); | 18 Expect.listEquals(expected, f.readAsBytesSync()); |
19 } | 19 } |
20 | 20 |
21 testWriteAsStringSync(dir) { | 21 testWriteAsStringSync(dir) { |
22 var f = new File('${dir.path}/string_sync.txt'); | 22 var f = new File('${dir.path}/string_sync.txt'); |
23 var data = 'asdf'; | 23 var data = 'asdf'; |
24 f.writeAsStringSync(data); | 24 f.writeAsStringSync(data); |
25 Expect.equals(data, f.readAsStringSync()); | 25 Expect.equals(data, f.readAsStringSync()); |
26 f.writeAsStringSync(data, mode: FileMode.APPEND); | 26 f.writeAsStringSync(data, mode: FileMode.APPEND, flush: true); |
27 Expect.equals('$data$data', f.readAsStringSync()); | 27 Expect.equals('$data$data', f.readAsStringSync()); |
28 } | 28 } |
29 | 29 |
30 Future testWriteAsBytes(dir) { | 30 Future testWriteAsBytes(dir) { |
31 var completer = new Completer(); | 31 var completer = new Completer(); |
32 var f = new File('${dir.path}/bytes.txt'); | 32 var f = new File('${dir.path}/bytes.txt'); |
33 var data = [50,50,50]; | 33 var data = [50, 50, 50]; |
34 f.writeAsBytes(data).then((file){ | 34 f.writeAsBytes(data).then((file){ |
35 Expect.equals(f, file); | 35 Expect.equals(f, file); |
36 f.readAsBytes().then((bytes) { | 36 f.readAsBytes().then((bytes) { |
37 Expect.listEquals(data, bytes); | 37 Expect.listEquals(data, bytes); |
38 f.writeAsBytes(data, mode: FileMode.APPEND).then((file) { | 38 f.writeAsBytes(data, mode: FileMode.APPEND, flush: true).then((file) { |
39 Expect.equals(f, file); | 39 Expect.equals(f, file); |
40 f.readAsBytes().then((bytes) { | 40 f.readAsBytes().then((bytes) { |
41 var expected = [50, 50, 50, 50, 50, 50]; | 41 var expected = [50, 50, 50, 50, 50, 50]; |
42 Expect.listEquals(expected, bytes); | 42 Expect.listEquals(expected, bytes); |
43 completer.complete(true); | 43 completer.complete(true); |
44 }); | 44 }); |
45 }); | 45 }); |
46 }); | 46 }); |
47 }); | 47 }); |
48 return completer.future; | 48 return completer.future; |
49 } | 49 } |
50 | 50 |
51 Future testWriteAsString(dir) { | 51 Future testWriteAsString(dir) { |
52 var completer = new Completer(); | 52 var completer = new Completer(); |
53 var f = new File('${dir.path}/strings.txt'); | 53 var f = new File('${dir.path}/strings.txt'); |
54 var data = 'asdf'; | 54 var data = 'asdf'; |
55 f.writeAsString(data).then((file){ | 55 f.writeAsString(data).then((file){ |
56 Expect.equals(f, file); | 56 Expect.equals(f, file); |
57 f.readAsString().then((str) { | 57 f.readAsString().then((str) { |
58 Expect.equals(data, str); | 58 Expect.equals(data, str); |
59 f.writeAsString(data, mode: FileMode.APPEND).then((file) { | 59 f.writeAsString(data, mode: FileMode.APPEND, flush: true).then((file) { |
60 Expect.equals(f, file); | 60 Expect.equals(f, file); |
61 f.readAsString().then((str) { | 61 f.readAsString().then((str) { |
62 Expect.equals('$data$data', str); | 62 Expect.equals('$data$data', str); |
63 completer.complete(true); | 63 completer.complete(true); |
64 }); | 64 }); |
65 }); | 65 }); |
66 }); | 66 }); |
67 }); | 67 }); |
68 return completer.future; | 68 return completer.future; |
69 } | 69 } |
70 | 70 |
71 main() { | 71 main() { |
72 asyncStart(); | 72 asyncStart(); |
73 var tempDir = Directory.systemTemp.createTempSync('dart_file_write_as'); | 73 var tempDir = Directory.systemTemp.createTempSync('dart_file_write_as'); |
74 testWriteAsBytesSync(tempDir); | 74 testWriteAsBytesSync(tempDir); |
75 testWriteAsStringSync(tempDir); | 75 testWriteAsStringSync(tempDir); |
76 testWriteAsBytes(tempDir).then((_) { | 76 testWriteAsBytes(tempDir).then((_) { |
77 return testWriteAsString(tempDir); | 77 return testWriteAsString(tempDir); |
78 }).then((_) { | 78 }).then((_) { |
79 tempDir.deleteSync(recursive: true); | 79 tempDir.deleteSync(recursive: true); |
80 asyncEnd(); | 80 asyncEnd(); |
81 }); | 81 }); |
82 } | 82 } |
OLD | NEW |