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 // Testing file input stream, VM-only, standalone test. | 4 // Testing file input stream, VM-only, standalone test. |
5 | 5 |
6 import "dart:convert"; | 6 import "dart:convert"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 import "dart:isolate"; | 8 import "dart:isolate"; |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
12 // Helper method to be able to run the test from the runtime | 12 // Helper method to be able to run the test from the runtime |
13 // directory, or the top directory. | 13 // directory, or the top directory. |
14 String getFilename(String path) => | 14 String getFilename(String path) => |
15 new File(path).existsSync() ? path : '../$path'; | 15 new File(path).existsSync() ? path : '../$path'; |
16 | 16 |
17 void testStringLineSplitter() { | 17 void testStringLineSplitter() { |
18 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 18 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); |
19 // File contains "Hello Dart\nwassup!\n" | 19 // File contains "Hello Dart\nwassup!\n" |
20 File file = new File(fileName); | 20 File file = new File(fileName); |
21 int linesRead = 0; | 21 int linesRead = 0; |
22 var lineStream = file.openRead() | 22 var lineStream = file.openRead() |
23 .transform(new StringDecoder()) | 23 .transform(UTF8.decoder) |
24 .transform(new LineSplitter()); | 24 .transform(new LineSplitter()); |
25 lineStream.listen((line) { | 25 lineStream.listen((line) { |
26 linesRead++; | 26 linesRead++; |
27 if (linesRead == 1) { | 27 if (linesRead == 1) { |
28 Expect.equals("Hello Dart", line); | 28 Expect.equals("Hello Dart", line); |
29 } else if (linesRead == 2) { | 29 } else if (linesRead == 2) { |
30 Expect.equals("wassup!", line); | 30 Expect.equals("wassup!", line); |
31 } else { | 31 } else { |
32 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); | 32 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); |
33 } | 33 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 test(null, -1); | 225 test(null, -1); |
226 } | 226 } |
227 | 227 |
228 | 228 |
229 void testStringLineSplitterEnding(String name, int length) { | 229 void testStringLineSplitterEnding(String name, int length) { |
230 String fileName = getFilename("tests/standalone/io/$name"); | 230 String fileName = getFilename("tests/standalone/io/$name"); |
231 // File contains 10 lines. | 231 // File contains 10 lines. |
232 File file = new File(fileName); | 232 File file = new File(fileName); |
233 Expect.equals(length, file.openSync().lengthSync()); | 233 Expect.equals(length, file.openSync().lengthSync()); |
234 var lineStream = file.openRead() | 234 var lineStream = file.openRead() |
235 .transform(new StringDecoder()) | 235 .transform(UTF8.decoder) |
236 .transform(new LineSplitter()); | 236 .transform(new LineSplitter()); |
237 int lineCount = 0; | 237 int lineCount = 0; |
238 lineStream.listen( | 238 lineStream.listen( |
239 (line) { | 239 (line) { |
240 lineCount++; | 240 lineCount++; |
241 Expect.isTrue(lineCount <= 10); | 241 Expect.isTrue(lineCount <= 10); |
242 if (line[0] != "#") { | 242 if (line[0] != "#") { |
243 Expect.equals("Line $lineCount", line); | 243 Expect.equals("Line $lineCount", line); |
244 } | 244 } |
245 }, | 245 }, |
(...skipping 10 matching lines...) Expand all Loading... |
256 testInputStreamDelete(); | 256 testInputStreamDelete(); |
257 testInputStreamAppend(); | 257 testInputStreamAppend(); |
258 testInputStreamOffset(); | 258 testInputStreamOffset(); |
259 testInputStreamBadOffset(); | 259 testInputStreamBadOffset(); |
260 // Check the length of these files as both are text files where one | 260 // Check the length of these files as both are text files where one |
261 // is without a terminating line separator which can easily be added | 261 // is without a terminating line separator which can easily be added |
262 // back if accidentally opened in a text editor. | 262 // back if accidentally opened in a text editor. |
263 testStringLineSplitterEnding("readline_test1.dat", 111); | 263 testStringLineSplitterEnding("readline_test1.dat", 111); |
264 testStringLineSplitterEnding("readline_test2.dat", 114); | 264 testStringLineSplitterEnding("readline_test2.dat", 114); |
265 } | 265 } |
OLD | NEW |