| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 const sampleText = "Sample text file."; | |
| 6 | |
| 7 main() async { | |
| 8 const uriText = "package:package_test_data/resources/sample.txt"; | |
| 9 const resource = const Resource(uriText); | |
| 10 | |
| 11 if (resource.uri != Uri.parse(uriText)) { | |
| 12 throw "Incorrect URI: ${resource.uri}"; | |
| 13 } | |
| 14 | |
| 15 var text = await resource.readAsString(); | |
| 16 if (!text.startsWith("Sample text file.")) { | |
| 17 throw "Incorrect reading of text file: $text"; | |
| 18 } | |
| 19 | |
| 20 var bytes = await resource.readAsBytes(); | |
| 21 if (!compareBytes(bytes, sampleText.codeUnits)) { | |
| 22 throw "Incorrect reading of bytes: $bytes"; | |
| 23 } | |
| 24 | |
| 25 var streamBytes = []; | |
| 26 await for (var byteSlice in resource.openRead()) { | |
| 27 streamBytes.addAll(byteSlice); | |
| 28 } | |
| 29 if (!compareBytes(streamBytes, sampleText.codeUnits)) { | |
| 30 throw "Incorrect reading of bytes: $bytes"; | |
| 31 } | |
| 32 | |
| 33 if (!compareBytes(streamBytes, bytes)) { | |
| 34 throw "Inconsistent reading of bytes: $bytes / $streamBytes"; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 /// Checks that [bytes] starts with [expectedBytes]. | |
| 39 /// | |
| 40 /// The bytes may be longer (because the test file is a text file and its | |
| 41 /// terminating line ending may be mangled on some platforms). | |
| 42 bool compareBytes(bytes, expectedBytes) { | |
| 43 if (bytes.length < expectedBytes.length) return false; | |
| 44 for (int i = 0; i < expectedBytes.length; i++) { | |
| 45 if (bytes[i] != expectedBytes[i]) return false; | |
| 46 } | |
| 47 return true; | |
| 48 } | |
| OLD | NEW |