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 // |
| 6 // OtherResources=readuntil_test.dat |
| 7 // OtherResources=readline_test1.dat |
| 8 // OtherResources=readline_test2.dat |
5 | 9 |
6 import "dart:convert"; | 10 import "dart:convert"; |
7 import "dart:io"; | 11 import "dart:io"; |
8 | 12 |
9 import "package:async_helper/async_helper.dart"; | 13 import "package:async_helper/async_helper.dart"; |
10 import "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
11 | 15 |
12 // Helper method to be able to run the test from the runtime | |
13 // directory, or the top directory. | |
14 String getFilename(String path) { | 16 String getFilename(String path) { |
15 var testPath = Platform.script.resolve('../../../$path'); | 17 return Platform.script.resolve(path).toFilePath(); |
16 return new File.fromUri(testPath).existsSync() | |
17 ? testPath.toFilePath() | |
18 : Platform.script.resolve('../../../runtime/$path').toFilePath(); | |
19 } | 18 } |
20 | 19 |
21 void testStringLineSplitter() { | 20 void testStringLineSplitter() { |
22 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 21 String fileName = getFilename("readuntil_test.dat"); |
23 // File contains "Hello Dart\nwassup!\n" | 22 // File contains "Hello Dart\nwassup!\n" |
24 File file = new File(fileName); | 23 File file = new File(fileName); |
25 int linesRead = 0; | 24 int linesRead = 0; |
26 var lineStream = file.openRead() | 25 var lineStream = file.openRead() |
27 .transform(UTF8.decoder) | 26 .transform(UTF8.decoder) |
28 .transform(new LineSplitter()); | 27 .transform(new LineSplitter()); |
29 lineStream.listen((line) { | 28 lineStream.listen((line) { |
30 linesRead++; | 29 linesRead++; |
31 if (linesRead == 1) { | 30 if (linesRead == 1) { |
32 Expect.equals("Hello Dart", line); | 31 Expect.equals("Hello Dart", line); |
33 } else if (linesRead == 2) { | 32 } else if (linesRead == 2) { |
34 Expect.equals("wassup!", line); | 33 Expect.equals("wassup!", line); |
35 } else { | 34 } else { |
36 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); | 35 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); |
37 } | 36 } |
38 }); | 37 }); |
39 } | 38 } |
40 | 39 |
41 | 40 |
42 void testOpenStreamAsync() { | 41 void testOpenStreamAsync() { |
43 asyncStart(); | 42 asyncStart(); |
44 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 43 String fileName = getFilename("readuntil_test.dat"); |
45 // File contains "Hello Dart\nwassup!\n" | 44 // File contains "Hello Dart\nwassup!\n" |
46 var expected = "Hello Dart\nwassup!\n".codeUnits; | 45 var expected = "Hello Dart\nwassup!\n".codeUnits; |
47 var byteCount = 0; | 46 var byteCount = 0; |
48 (new File(fileName)).openRead().listen( | 47 (new File(fileName)).openRead().listen( |
49 (d) => byteCount += d.length, | 48 (d) => byteCount += d.length, |
50 onDone: () { | 49 onDone: () { |
51 Expect.equals(expected.length, byteCount); | 50 Expect.equals(expected.length, byteCount); |
52 asyncEnd(); | 51 asyncEnd(); |
53 }); | 52 }); |
54 } | 53 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 error = true; | 226 error = true; |
228 }); | 227 }); |
229 } | 228 } |
230 test(-1, null); | 229 test(-1, null); |
231 test(100, 99); | 230 test(100, 99); |
232 test(null, -1); | 231 test(null, -1); |
233 } | 232 } |
234 | 233 |
235 | 234 |
236 void testStringLineSplitterEnding(String name, int length) { | 235 void testStringLineSplitterEnding(String name, int length) { |
237 String fileName = getFilename("tests/standalone/io/$name"); | 236 String fileName = getFilename(name); |
238 // File contains 10 lines. | 237 // File contains 10 lines. |
239 File file = new File(fileName); | 238 File file = new File(fileName); |
240 Expect.equals(length, file.lengthSync()); | 239 Expect.equals(length, file.lengthSync()); |
241 var lineStream = file.openRead() | 240 var lineStream = file.openRead() |
242 .transform(UTF8.decoder) | 241 .transform(UTF8.decoder) |
243 .transform(new LineSplitter()); | 242 .transform(new LineSplitter()); |
244 int lineCount = 0; | 243 int lineCount = 0; |
245 lineStream.listen( | 244 lineStream.listen( |
246 (line) { | 245 (line) { |
247 lineCount++; | 246 lineCount++; |
(...skipping 15 matching lines...) Expand all Loading... |
263 testInputStreamDelete(); | 262 testInputStreamDelete(); |
264 testInputStreamAppend(); | 263 testInputStreamAppend(); |
265 testInputStreamOffset(); | 264 testInputStreamOffset(); |
266 testInputStreamBadOffset(); | 265 testInputStreamBadOffset(); |
267 // Check the length of these files as both are text files where one | 266 // Check the length of these files as both are text files where one |
268 // is without a terminating line separator which can easily be added | 267 // is without a terminating line separator which can easily be added |
269 // back if accidentally opened in a text editor. | 268 // back if accidentally opened in a text editor. |
270 testStringLineSplitterEnding("readline_test1.dat", 111); | 269 testStringLineSplitterEnding("readline_test1.dat", 111); |
271 testStringLineSplitterEnding("readline_test2.dat", 114); | 270 testStringLineSplitterEnding("readline_test2.dat", 114); |
272 } | 271 } |
OLD | NEW |