| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:io"); | 6 #import("dart:io"); | 
| 7 #import("dart:isolate"); | 7 #import("dart:isolate"); | 
| 8 | 8 | 
| 9 // Helper method to be able to run the test from the runtime | 9 // Helper method to be able to run the test from the runtime | 
| 10 // directory, or the top directory. | 10 // directory, or the top directory. | 
| 11 String getFilename(String path) => | 11 String getFilename(String path) => | 
| 12     new File(path).existsSync() ? path : '../' + path; | 12     new File(path).existsSync() ? path : '../' + path; | 
| 13 | 13 | 
| 14 void testStringInputStreamSync() { | 14 void testStringInputStreamSync() { | 
| 15   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 15   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 
| 16   // File contains "Hello Dart\nwassup!\n" | 16   // File contains "Hello Dart\nwassup!\n" | 
| 17   File file = new File(fileName); | 17   File file = new File(fileName); | 
| 18   StringInputStream x = new StringInputStream(file.openInputStream()); | 18   StringInputStream x = new StringInputStream(file.openInputStream()); | 
| 19   x.lineHandler = () { | 19   x.onLine = () { | 
| 20     // The file input stream is known (for now) to have read the whole | 20     // The file input stream is known (for now) to have read the whole | 
| 21     // file when the data handler is called. | 21     // file when the data handler is called. | 
| 22     String line = x.readLine(); | 22     String line = x.readLine(); | 
| 23     Expect.equals("Hello Dart", line); | 23     Expect.equals("Hello Dart", line); | 
| 24     line = x.readLine(); | 24     line = x.readLine(); | 
| 25     Expect.equals("wassup!", line); | 25     Expect.equals("wassup!", line); | 
| 26   }; | 26   }; | 
| 27 } | 27 } | 
| 28 | 28 | 
| 29 void testInputStreamAsync() { | 29 void testInputStreamAsync() { | 
| 30   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 30   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 
| 31   // File contains "Hello Dart\nwassup!\n" | 31   // File contains "Hello Dart\nwassup!\n" | 
| 32   var expected = "Hello Dart\nwassup!\n".charCodes(); | 32   var expected = "Hello Dart\nwassup!\n".charCodes(); | 
| 33   InputStream x = (new File(fileName)).openInputStream(); | 33   InputStream x = (new File(fileName)).openInputStream(); | 
| 34   var byteCount = 0; | 34   var byteCount = 0; | 
| 35   x.dataHandler = () { | 35   x.onData = () { | 
| 36     Expect.equals(expected[byteCount], x.read(1)[0]); | 36     Expect.equals(expected[byteCount], x.read(1)[0]); | 
| 37     byteCount++; | 37     byteCount++; | 
| 38   }; | 38   }; | 
| 39   x.closeHandler = () { | 39   x.onClosed = () { | 
| 40     Expect.equals(expected.length, byteCount); | 40     Expect.equals(expected.length, byteCount); | 
| 41   }; | 41   }; | 
| 42 } | 42 } | 
| 43 | 43 | 
| 44 | 44 | 
| 45 void testStringInputStreamAsync(String name, int length) { | 45 void testStringInputStreamAsync(String name, int length) { | 
| 46   String fileName = getFilename("tests/standalone/src/$name"); | 46   String fileName = getFilename("tests/standalone/src/$name"); | 
| 47   // File contains 10 lines. | 47   // File contains 10 lines. | 
| 48   File file = new File(fileName); | 48   File file = new File(fileName); | 
| 49   Expect.equals(length, file.openSync().lengthSync()); | 49   Expect.equals(length, file.openSync().lengthSync()); | 
| 50   StringInputStream x = new StringInputStream(file.openInputStream()); | 50   StringInputStream x = new StringInputStream(file.openInputStream()); | 
| 51   int lineCount = 0; | 51   int lineCount = 0; | 
| 52   x.lineHandler = () { | 52   x.onLine = () { | 
| 53     var line = x.readLine(); | 53     var line = x.readLine(); | 
| 54     lineCount++; | 54     lineCount++; | 
| 55     Expect.isTrue(lineCount <= 10); | 55     Expect.isTrue(lineCount <= 10); | 
| 56     if (line[0] != "#") { | 56     if (line[0] != "#") { | 
| 57       Expect.equals("Line $lineCount", line); | 57       Expect.equals("Line $lineCount", line); | 
| 58     } | 58     } | 
| 59   }; | 59   }; | 
| 60   x.closeHandler = () { | 60   x.onClosed = () { | 
| 61     Expect.equals(10, lineCount); | 61     Expect.equals(10, lineCount); | 
| 62   }; | 62   }; | 
| 63 } | 63 } | 
| 64 | 64 | 
| 65 | 65 | 
| 66 void testChunkedInputStream() { | 66 void testChunkedInputStream() { | 
| 67   // Force the test to timeout if it does not finish. | 67   // Force the test to timeout if it does not finish. | 
| 68   ReceivePort done = new ReceivePort.singleShot(); | 68   ReceivePort done = new ReceivePort.singleShot(); | 
| 69   done.receive((message, replyTo) {}); | 69   done.receive((message, replyTo) {}); | 
| 70 | 70 | 
| 71   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 71   String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 
| 72   // File contains 19 bytes ("Hello Dart\nwassup!") | 72   // File contains 19 bytes ("Hello Dart\nwassup!") | 
| 73   File file = new File(fileName); | 73   File file = new File(fileName); | 
| 74   ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 74   ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 
| 75   x.chunkSize = 9; | 75   x.chunkSize = 9; | 
| 76   x.dataHandler = () { | 76   x.onData = () { | 
| 77     List<int> chunk = x.read(); | 77     List<int> chunk = x.read(); | 
| 78     Expect.equals(9, chunk.length); | 78     Expect.equals(9, chunk.length); | 
| 79     x.chunkSize = 5; | 79     x.chunkSize = 5; | 
| 80     x.dataHandler = () { | 80     x.onData = () { | 
| 81       chunk = x.read(); | 81       chunk = x.read(); | 
| 82       Expect.equals(5, chunk.length); | 82       Expect.equals(5, chunk.length); | 
| 83       x.dataHandler = () { | 83       x.onData = () { | 
| 84         chunk = x.read(); | 84         chunk = x.read(); | 
| 85         Expect.equals(5, chunk.length); | 85         Expect.equals(5, chunk.length); | 
| 86         chunk = x.read(); | 86         chunk = x.read(); | 
| 87         Expect.equals(null, chunk); | 87         Expect.equals(null, chunk); | 
| 88         done.toSendPort().send(null); | 88         done.toSendPort().send(null); | 
| 89       }; | 89       }; | 
| 90     }; | 90     }; | 
| 91   }; | 91   }; | 
| 92 } | 92 } | 
| 93 | 93 | 
| 94 | 94 | 
| 95 main() { | 95 main() { | 
| 96   testStringInputStreamSync(); | 96   testStringInputStreamSync(); | 
| 97   testInputStreamAsync(); | 97   testInputStreamAsync(); | 
| 98   // Check the length of these files as both are text files where one | 98   // Check the length of these files as both are text files where one | 
| 99   // is without a terminating line separator which can easily be added | 99   // is without a terminating line separator which can easily be added | 
| 100   // back if accidentally opened in a text editor. | 100   // back if accidentally opened in a text editor. | 
| 101   testStringInputStreamAsync("readline_test1.dat", 111); | 101   testStringInputStreamAsync("readline_test1.dat", 111); | 
| 102   testStringInputStreamAsync("readline_test2.dat", 114); | 102   testStringInputStreamAsync("readline_test2.dat", 114); | 
| 103   testChunkedInputStream(); | 103   testChunkedInputStream(); | 
| 104 } | 104 } | 
| OLD | NEW | 
|---|