| 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 | 8 |
| 9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 file.writeAsStringSync(buffer.toString()); | 61 file.writeAsStringSync(buffer.toString()); |
| 62 var length = file.lengthSync(); | 62 var length = file.lengthSync(); |
| 63 Expect.equals(buffer.length, length); | 63 Expect.equals(buffer.length, length); |
| 64 return length; | 64 return length; |
| 65 } | 65 } |
| 66 | 66 |
| 67 | 67 |
| 68 void testInputStreamTruncate() { | 68 void testInputStreamTruncate() { |
| 69 asyncStart(); | 69 asyncStart(); |
| 70 var temp = new Directory('').createTempSync(); | 70 var temp = Directory.systemTemp.createTempSync('file_input_stream_test'); |
| 71 var file = new File('${temp.path}/input_stream_truncate.txt'); | 71 var file = new File('${temp.path}/input_stream_truncate.txt'); |
| 72 var originalLength = writeLongFileSync(file); | 72 var originalLength = writeLongFileSync(file); |
| 73 // Start streaming the file. Pause after first chunk. Truncate | 73 // Start streaming the file. Pause after first chunk. Truncate |
| 74 // underlying file and check that the streaming stops with or | 74 // underlying file and check that the streaming stops with or |
| 75 // without getting all data. | 75 // without getting all data. |
| 76 var streamedBytes = 0; | 76 var streamedBytes = 0; |
| 77 var subscription; | 77 var subscription; |
| 78 subscription = file.openRead().listen( | 78 subscription = file.openRead().listen( |
| 79 (d) { | 79 (d) { |
| 80 if (streamedBytes == 0) { | 80 if (streamedBytes == 0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 93 Expect.isTrue(streamedBytes > 0 && streamedBytes <= originalLength); | 93 Expect.isTrue(streamedBytes > 0 && streamedBytes <= originalLength); |
| 94 temp.delete(recursive: true).then((_) => asyncEnd()); | 94 temp.delete(recursive: true).then((_) => asyncEnd()); |
| 95 }, | 95 }, |
| 96 onError: (e) { | 96 onError: (e) { |
| 97 Expect.fail("Unexpected error"); | 97 Expect.fail("Unexpected error"); |
| 98 }); | 98 }); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void testInputStreamDelete() { | 101 void testInputStreamDelete() { |
| 102 asyncStart(); | 102 asyncStart(); |
| 103 var temp = new Directory('').createTempSync(); | 103 var temp = Directory.systemTemp.createTempSync('file_input_stream_test'); |
| 104 var file = new File('${temp.path}/input_stream_delete.txt'); | 104 var file = new File('${temp.path}/input_stream_delete.txt'); |
| 105 var originalLength = writeLongFileSync(file); | 105 var originalLength = writeLongFileSync(file); |
| 106 // Start streaming the file. Pause after first chunk. Truncate | 106 // Start streaming the file. Pause after first chunk. Truncate |
| 107 // underlying file and check that the streaming stops with or | 107 // underlying file and check that the streaming stops with or |
| 108 // without getting all data. | 108 // without getting all data. |
| 109 var streamedBytes = 0; | 109 var streamedBytes = 0; |
| 110 var subscription; | 110 var subscription; |
| 111 subscription = file.openRead().listen( | 111 subscription = file.openRead().listen( |
| 112 (d) { | 112 (d) { |
| 113 if (streamedBytes == 0) { | 113 if (streamedBytes == 0) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 temp.delete(recursive: true).then((_) => asyncEnd()); | 133 temp.delete(recursive: true).then((_) => asyncEnd()); |
| 134 }, | 134 }, |
| 135 onError: (e) { | 135 onError: (e) { |
| 136 Expect.fail("Unexpected error"); | 136 Expect.fail("Unexpected error"); |
| 137 }); | 137 }); |
| 138 } | 138 } |
| 139 | 139 |
| 140 | 140 |
| 141 void testInputStreamAppend() { | 141 void testInputStreamAppend() { |
| 142 asyncStart(); | 142 asyncStart(); |
| 143 var temp = new Directory('').createTempSync(); | 143 var temp = Directory.systemTemp.createTempSync('file_input_stream_test'); |
| 144 var file = new File('${temp.path}/input_stream_append.txt'); | 144 var file = new File('${temp.path}/input_stream_append.txt'); |
| 145 var originalLength = writeLongFileSync(file); | 145 var originalLength = writeLongFileSync(file); |
| 146 // Start streaming the file. Pause after first chunk. Append to | 146 // Start streaming the file. Pause after first chunk. Append to |
| 147 // underlying file and check that the stream gets all the data. | 147 // underlying file and check that the stream gets all the data. |
| 148 var streamedBytes = 0; | 148 var streamedBytes = 0; |
| 149 var subscription; | 149 var subscription; |
| 150 subscription = file.openRead().listen( | 150 subscription = file.openRead().listen( |
| 151 (d) { | 151 (d) { |
| 152 if (streamedBytes == 0) { | 152 if (streamedBytes == 0) { |
| 153 subscription.pause(); | 153 subscription.pause(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 167 }, | 167 }, |
| 168 onError: (e) { | 168 onError: (e) { |
| 169 Expect.fail("Unexpected error"); | 169 Expect.fail("Unexpected error"); |
| 170 }); | 170 }); |
| 171 } | 171 } |
| 172 | 172 |
| 173 | 173 |
| 174 void testInputStreamOffset() { | 174 void testInputStreamOffset() { |
| 175 void test(int start, int end, int expectedBytes) { | 175 void test(int start, int end, int expectedBytes) { |
| 176 asyncStart(); | 176 asyncStart(); |
| 177 var temp = new Directory('').createTempSync(); | 177 var temp = Directory.systemTemp.createTempSync('file_input_stream_test'); |
| 178 var file = new File('${temp.path}/input_stream_offset.txt'); | 178 var file = new File('${temp.path}/input_stream_offset.txt'); |
| 179 var originalLength = writeLongFileSync(file); | 179 var originalLength = writeLongFileSync(file); |
| 180 var streamedBytes = 0; | 180 var streamedBytes = 0; |
| 181 if (expectedBytes < 0) expectedBytes = originalLength + expectedBytes; | 181 if (expectedBytes < 0) expectedBytes = originalLength + expectedBytes; |
| 182 file.openRead(start, end).listen( | 182 file.openRead(start, end).listen( |
| 183 (d) { | 183 (d) { |
| 184 streamedBytes += d.length; | 184 streamedBytes += d.length; |
| 185 }, | 185 }, |
| 186 onDone: () { | 186 onDone: () { |
| 187 Expect.equals(expectedBytes, streamedBytes); | 187 Expect.equals(expectedBytes, streamedBytes); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 198 test(null, 0, 0); | 198 test(null, 0, 0); |
| 199 test(null, 1, 1); | 199 test(null, 1, 1); |
| 200 test(1, null, -1); | 200 test(1, null, -1); |
| 201 test(20, null, -20); | 201 test(20, null, -20); |
| 202 } | 202 } |
| 203 | 203 |
| 204 | 204 |
| 205 void testInputStreamBadOffset() { | 205 void testInputStreamBadOffset() { |
| 206 void test(int start, int end) { | 206 void test(int start, int end) { |
| 207 asyncStart(); | 207 asyncStart(); |
| 208 var temp = new Directory('').createTempSync(); | 208 var temp = Directory.systemTemp.createTempSync('file_input_stream_test'); |
| 209 var file = new File('${temp.path}/input_stream_bad_offset.txt'); | 209 var file = new File('${temp.path}/input_stream_bad_offset.txt'); |
| 210 var originalLength = writeLongFileSync(file); | 210 var originalLength = writeLongFileSync(file); |
| 211 var streamedBytes = 0; | 211 var streamedBytes = 0; |
| 212 file.openRead(start, end).listen( | 212 file.openRead(start, end).listen( |
| 213 (d) { | 213 (d) { |
| 214 streamedBytes += d.length; | 214 streamedBytes += d.length; |
| 215 }, | 215 }, |
| 216 onDone: () { | 216 onDone: () { |
| 217 temp.delete(recursive: true); | 217 temp.delete(recursive: true); |
| 218 }, | 218 }, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |