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

Unified Diff: tests/standalone/io/file_write_only_test.dart

Issue 1193653002: Add file modes for opening a file write only (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Addressed review comments + fixed analyzer reported issues Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/standalone/io/file_write_only_test.dart
diff --git a/tests/standalone/io/file_write_only_test.dart b/tests/standalone/io/file_write_only_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..c06be4741ef80a9a58aa2ec1b7ce16f26b92d8f5
--- /dev/null
+++ b/tests/standalone/io/file_write_only_test.dart
@@ -0,0 +1,84 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+// Dart test program for testing file I/O.
+
+import 'dart:async';
+import 'dart:io';
+
+import "package:async_helper/async_helper.dart";
+import "package:expect/expect.dart";
+
+Future withTempDir(String prefix, void test(Directory dir)) async {
+ var tempDir = Directory.systemTemp.createTempSync(prefix);
+ try {
+ await test(tempDir);
+ } finally {
+ tempDir.deleteSync(recursive: true);
+ }
+}
+
+void withTempDirSync(String prefix, void test(Directory dir)) async {
+ var tempDir = Directory.systemTemp.createTempSync(prefix);
+ try {
+ test(tempDir);
+ } finally {
+ tempDir.deleteSync(recursive: true);
+ }
+}
+
+Futire expectThrowsAsync(Future future, String message) {
+ return future.then((r) => Expect.fail(message))
+ .catchError((e) {});
+}
+
+
+Future write(Directory dir) async {
+ var f = new File('x');
+ var raf = await f.open(mode: WRITE_ONLY);
+ await raf.writeString('Hello');
+ await raf.setPosition(0);
+ await raf.writeString('Hello');
+ await raf.setPosition(0);
+ await expectThrowsAsync(
+ raf.readByte(), 'Read from write only file succeeded');
+ await raf.close();
+ raf = await f.open(mode: WRITE_ONLY_APPEND);
+ await raf.writeString('Hello');
+ await expectThrowsAsync(
+ raf.readByte(), 'Read from write only file succeeded');
+ await raf.setPosition(0);
+ await raf.writeString('Hello');
+ await raf.close();
+ Expect.equals(f.lengthSync(), 10);
+}
+
+Future writeSync(Directory dir) {
+ var f = new File('x');
+ var raf = f.openSync(mode: WRITE_ONLY);
+ raf.writeStringSync('Hello');
+ raf.setPositionSync(0);
+ raf.writeStringSync('Hello');
+ raf.setPositionSync(0);
+ Expect.throws(() => raf.readByteSync());
+}
+
+Future openWrite(Directory dir) async {
+ var f = new File('x');
+ var sink = f.openWrite(mode: WRITE_ONLY);
+ sink.write('Hello');
+ await sink.close();
+ sink = await f.openWrite(mode: WRITE_ONLY_APPEND);
+ sink.write('Hello');
+ await sink.close();
+ Expect.equals(f.lengthSync(), 10);
+}
+
+main() async {
+ asyncStart();
+ await withTempDir('file_write_only_test', write);
+ withTempDirSync('file_write_only_test', writeSync);
+ await withTempDir('file_write_only_test', openWrite);
+ asyncEnd();
+}
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698