OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // Dart test program for testing file I/O. |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import "package:async_helper/async_helper.dart"; |
| 11 import "package:expect/expect.dart"; |
| 12 |
| 13 Future withTempDir(String prefix, void test(Directory dir)) async { |
| 14 var tempDir = Directory.systemTemp.createTempSync(prefix); |
| 15 try { |
| 16 await test(tempDir); |
| 17 } finally { |
| 18 tempDir.deleteSync(recursive: true); |
| 19 } |
| 20 } |
| 21 |
| 22 void withTempDirSync(String prefix, void test(Directory dir)) async { |
| 23 var tempDir = Directory.systemTemp.createTempSync(prefix); |
| 24 try { |
| 25 test(tempDir); |
| 26 } finally { |
| 27 tempDir.deleteSync(recursive: true); |
| 28 } |
| 29 } |
| 30 |
| 31 Futire expectThrowsAsync(Future future, String message) { |
| 32 return future.then((r) => Expect.fail(message)) |
| 33 .catchError((e) {}); |
| 34 } |
| 35 |
| 36 |
| 37 Future write(Directory dir) async { |
| 38 var f = new File('x'); |
| 39 var raf = await f.open(mode: WRITE_ONLY); |
| 40 await raf.writeString('Hello'); |
| 41 await raf.setPosition(0); |
| 42 await raf.writeString('Hello'); |
| 43 await raf.setPosition(0); |
| 44 await expectThrowsAsync( |
| 45 raf.readByte(), 'Read from write only file succeeded'); |
| 46 await raf.close(); |
| 47 raf = await f.open(mode: WRITE_ONLY_APPEND); |
| 48 await raf.writeString('Hello'); |
| 49 await expectThrowsAsync( |
| 50 raf.readByte(), 'Read from write only file succeeded'); |
| 51 await raf.setPosition(0); |
| 52 await raf.writeString('Hello'); |
| 53 await raf.close(); |
| 54 Expect.equals(f.lengthSync(), 10); |
| 55 } |
| 56 |
| 57 Future writeSync(Directory dir) { |
| 58 var f = new File('x'); |
| 59 var raf = f.openSync(mode: WRITE_ONLY); |
| 60 raf.writeStringSync('Hello'); |
| 61 raf.setPositionSync(0); |
| 62 raf.writeStringSync('Hello'); |
| 63 raf.setPositionSync(0); |
| 64 Expect.throws(() => raf.readByteSync()); |
| 65 } |
| 66 |
| 67 Future openWrite(Directory dir) async { |
| 68 var f = new File('x'); |
| 69 var sink = f.openWrite(mode: WRITE_ONLY); |
| 70 sink.write('Hello'); |
| 71 await sink.close(); |
| 72 sink = await f.openWrite(mode: WRITE_ONLY_APPEND); |
| 73 sink.write('Hello'); |
| 74 await sink.close(); |
| 75 Expect.equals(f.lengthSync(), 10); |
| 76 } |
| 77 |
| 78 main() async { |
| 79 asyncStart(); |
| 80 await withTempDir('file_write_only_test', write); |
| 81 withTempDirSync('file_write_only_test', writeSync); |
| 82 await withTempDir('file_write_only_test', openWrite); |
| 83 asyncEnd(); |
| 84 } |
OLD | NEW |