Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: tests/corelib/package_resource_test.dart

Issue 1276263002: Add resource tests. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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:expect/resources/sample.txt";
Søren Gjesse 2015/08/07 13:51:16 Where does this resource come from?
Lasse Reichstein Nielsen 2015/08/07 13:55:52 I just added it in this CL. The alternative is to
Lasse Reichstein Nielsen 2015/08/10 10:40:09 I've created a package_test_data/ package.
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 if (!compareBytes(streamBytes, bytes)) {
33 throw "Inconsistent reading of bytes: $bytes / $streamBytes";
34 }
35 }
36
37 /// Checks that [bytes] starts with [expectedBytes].
38 ///
39 /// The bytes may be longer (because the test file is a text file and its
40 /// terminating line ending may be mangled on some platforms).
41 bool compareBytes(bytes, expectedBytes) {
42 if (bytes.length < expectedBytes.length) return false;
43 for (int i = 0; i < expectedBytes.length; i++) {
44 if (bytes[i] != expectedBytes[i]) return false;
45 }
46 return true;
47 }
OLDNEW
« tests/corelib/http_resource_test.dart ('K') | « tests/corelib/http_resource_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698