| 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 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 Future expectThrowsAsync(Future future, String message) { | 31 Future expectThrowsAsync(Future future, String message) { |
| 32 return future.then((r) => Expect.fail(message)) | 32 return future.then((r) => Expect.fail(message)) |
| 33 .catchError((e) {}); | 33 .catchError((e) {}); |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 Future write(Directory dir) async { | 37 Future write(Directory dir) async { |
| 38 var f = new File('x'); | 38 var f = new File(dir.path + '/write'); |
| 39 var raf = await f.open(mode: WRITE_ONLY); | 39 var raf = await f.open(mode: WRITE_ONLY); |
| 40 await raf.writeString('Hello'); | 40 await raf.writeString('Hello'); |
| 41 await raf.setPosition(0); | 41 await raf.setPosition(0); |
| 42 await raf.writeString('Hello'); | 42 await raf.writeString('Hello'); |
| 43 await raf.setPosition(0); | 43 await raf.setPosition(0); |
| 44 await expectThrowsAsync( | 44 await expectThrowsAsync( |
| 45 raf.readByte(), 'Read from write only file succeeded'); | 45 raf.readByte(), 'Read from write only file succeeded'); |
| 46 await raf.close(); | 46 await raf.close(); |
| 47 raf = await f.open(mode: WRITE_ONLY_APPEND); | 47 raf = await f.open(mode: WRITE_ONLY_APPEND); |
| 48 await raf.writeString('Hello'); | 48 await raf.writeString('Hello'); |
| 49 await expectThrowsAsync( | 49 await expectThrowsAsync( |
| 50 raf.readByte(), 'Read from write only file succeeded'); | 50 raf.readByte(), 'Read from write only file succeeded'); |
| 51 await raf.setPosition(0); | 51 await raf.setPosition(0); |
| 52 await raf.writeString('Hello'); | 52 await raf.writeString('Hello'); |
| 53 await raf.close(); | 53 await raf.close(); |
| 54 Expect.equals(f.lengthSync(), 10); | 54 Expect.equals(f.lengthSync(), 10); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void writeSync(Directory dir) { | 57 void writeSync(Directory dir) { |
| 58 var f = new File('x'); | 58 var f = new File(dir.path + '/write_sync'); |
| 59 var raf = f.openSync(mode: WRITE_ONLY); | 59 var raf = f.openSync(mode: WRITE_ONLY); |
| 60 raf.writeStringSync('Hello'); | 60 raf.writeStringSync('Hello'); |
| 61 raf.setPositionSync(0); | 61 raf.setPositionSync(0); |
| 62 raf.writeStringSync('Hello'); | 62 raf.writeStringSync('Hello'); |
| 63 raf.setPositionSync(0); | 63 raf.setPositionSync(0); |
| 64 Expect.throws(() => raf.readByteSync()); | 64 Expect.throws(() => raf.readByteSync()); |
| 65 } | 65 } |
| 66 | 66 |
| 67 Future openWrite(Directory dir) async { | 67 Future openWrite(Directory dir) async { |
| 68 var f = new File('x'); | 68 var f = new File(dir.path + '/open_write'); |
| 69 var sink = f.openWrite(mode: WRITE_ONLY); | 69 var sink = f.openWrite(mode: WRITE_ONLY); |
| 70 sink.write('Hello'); | 70 sink.write('Hello'); |
| 71 await sink.close(); | 71 await sink.close(); |
| 72 sink = await f.openWrite(mode: WRITE_ONLY_APPEND); | 72 sink = await f.openWrite(mode: WRITE_ONLY_APPEND); |
| 73 sink.write('Hello'); | 73 sink.write('Hello'); |
| 74 await sink.close(); | 74 await sink.close(); |
| 75 Expect.equals(f.lengthSync(), 10); | 75 Expect.equals(f.lengthSync(), 10); |
| 76 } | 76 } |
| 77 | 77 |
| 78 main() async { | 78 main() async { |
| 79 asyncStart(); | 79 asyncStart(); |
| 80 await withTempDir('file_write_only_test', write); | 80 await withTempDir('file_write_only_test', write); |
| 81 withTempDirSync('file_write_only_test', writeSync); | 81 withTempDirSync('file_write_only_test', writeSync); |
| 82 await withTempDir('file_write_only_test', openWrite); | 82 await withTempDir('file_write_only_test', openWrite); |
| 83 asyncEnd(); | 83 asyncEnd(); |
| 84 } | 84 } |
| OLD | NEW |