Skip to content

Commit c26095b

Browse files
committed
1 parent dab1aaa commit c26095b

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sample text file.

pkg/package_test_data/pubspec.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: package_test_data
2+
version: 0.0.1
3+
author: "Dart Team <[email protected]>"
4+
description: >
5+
Contains data used by platform tests using package: URIs.
6+
Should *not* be published.
7+
environment:
8+
sdk: ">=1.13.0"

tests/corelib/corelib.status

+6
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ regexp/*: Skip
205205
[ $runtime == vm ]
206206
regexp/global_test: Skip # Timeout. Issue 21709 and 21708
207207

208+
[ $runtime != vm ]
209+
package_resource_test: RuntimeError # Issue 23825 (not implemented yet).
210+
data_resource_test: RuntimeError # Issue 23825 (not implemented yet).
211+
http_resource_test: Skip, OK # VM specific test, uses dart:io.
212+
file_resource_test: Skip, OK # VM specific test, uses dart:io.
213+
208214
[ $mode == debug ]
209215
regexp/pcre_test: Pass, Slow # Timeout. Issue 22008
210216

tests/corelib/data_resource_test.dart

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
var uriEncoded = sampleText.replaceAll(' ', '%20');
9+
await testUri("data:application/dart;charset=utf-8,$uriEncoded");
10+
// TODO: Support other data: URI formats too.
11+
// See: https://github.com/dart-lang/sdk/issues/24030
12+
// await testUri("data:text/plain;charset=utf-8,$uriEncoded");
13+
var base64Encoded = "U2FtcGxlIHRleHQgZmlsZS4=";
14+
// await testUri("data:application/dart;charset=utf-8;base64,$base64Encoded");
15+
// await testUri("data:text/plain;charset=utf-8;base64,$base64Encoded");
16+
}
17+
18+
testUri(uriText) async {
19+
var resource = new Resource(uriText);
20+
21+
if (resource.uri != Uri.parse(uriText)) {
22+
throw "uriText: Incorrect URI: ${resource.uri}";
23+
}
24+
25+
var text = await resource.readAsString();
26+
if (text != sampleText) {
27+
throw "uriText: Incorrect reading of text file: $text";
28+
}
29+
30+
var bytes = await resource.readAsBytes();
31+
if (!compareBytes(bytes, sampleText.codeUnits)) {
32+
throw "uriText: Incorrect reading of bytes: $bytes";
33+
}
34+
35+
var streamBytes = [];
36+
await for (var byteSlice in resource.openRead()) {
37+
streamBytes.addAll(byteSlice);
38+
}
39+
if (!compareBytes(streamBytes, sampleText.codeUnits)) {
40+
throw "uriText: Incorrect reading of bytes: $bytes";
41+
}
42+
}
43+
44+
/// Checks that [bytes] and [expectedBytes] have the same contents.
45+
bool compareBytes(bytes, expectedBytes) {
46+
if (bytes.length != expectedBytes.length) return false;
47+
for (int i = 0; i < expectedBytes.length; i++) {
48+
if (bytes[i] != expectedBytes[i]) return false;
49+
}
50+
return true;
51+
}

tests/corelib/file_resource_test.dart

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import "dart:io";
6+
7+
const sampleText = "Sample text file.";
8+
9+
main() async {
10+
var file = await createFile();
11+
var uri = new Uri.file(file.path);
12+
13+
var resource = new Resource(uri.toString());
14+
15+
if (resource.uri != uri) {
16+
throw "Incorrect URI: ${resource.uri}";
17+
}
18+
19+
var text = await resource.readAsString();
20+
if (text != sampleText) {
21+
throw "Incorrect reading of text file: $text";
22+
}
23+
24+
var bytes = await resource.readAsBytes();
25+
if (!compareBytes(bytes, sampleText.codeUnits)) {
26+
throw "Incorrect reading of bytes: $bytes";
27+
}
28+
29+
var streamBytes = [];
30+
await for (var byteSlice in resource.openRead()) {
31+
streamBytes.addAll(byteSlice);
32+
}
33+
if (!compareBytes(streamBytes, sampleText.codeUnits)) {
34+
throw "Incorrect reading of bytes: $bytes";
35+
}
36+
37+
await deleteFile(file);
38+
}
39+
40+
/// Checks that [bytes] and [expectedBytes] have the same contents.
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+
}
48+
49+
createFile() async {
50+
var tempDir = await Directory.systemTemp.createTemp("sample");
51+
var filePath = tempDir.path + Platform.pathSeparator + "sample.txt";
52+
var file = new File(filePath);
53+
await file.create();
54+
await file.writeAsString(sampleText);
55+
return file;
56+
}
57+
58+
deleteFile(File file) async {
59+
// Removes the file and the temporary directory it's in.
60+
var parentDir = new Directory(file.path.substring(0,
61+
file.path.lastIndexOf(Platform.pathSeparator)));
62+
await parentDir.delete(recursive: true);
63+
}

tests/corelib/http_resource_test.dart

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
import "dart:io";
6+
7+
const sampleText = "Sample text file.";
8+
9+
main() async {
10+
var server = await startServer();
11+
var uriText = "http://localhost:${server.port}/sample.txt?query#fragment";
12+
var resource = new Resource(uriText);
13+
14+
if (resource.uri != Uri.parse(uriText)) {
15+
throw "Incorrect URI: ${resource.uri}";
16+
}
17+
18+
var text = await resource.readAsString();
19+
if (text != sampleText) {
20+
throw "Incorrect reading of text file: $text";
21+
}
22+
23+
var bytes = await resource.readAsBytes();
24+
if (!compareBytes(bytes, sampleText.codeUnits)) {
25+
throw "Incorrect reading of bytes: $bytes";
26+
}
27+
28+
var streamBytes = [];
29+
await for (var byteSlice in resource.openRead()) {
30+
streamBytes.addAll(byteSlice);
31+
}
32+
if (!compareBytes(streamBytes, sampleText.codeUnits)) {
33+
throw "Incorrect reading of bytes: $bytes";
34+
}
35+
36+
await server.close();
37+
}
38+
39+
/// Checks that [bytes] and [expectedBytes] have the same contents.
40+
bool compareBytes(bytes, expectedBytes) {
41+
if (bytes.length != expectedBytes.length) return false;
42+
for (int i = 0; i < expectedBytes.length; i++) {
43+
if (bytes[i] != expectedBytes[i]) return false;
44+
}
45+
return true;
46+
}
47+
48+
startServer() async {
49+
var server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0);
50+
var expectedUri = new Uri(path: "/sample.txt", query: "query");
51+
server.forEach((request) async {
52+
await request.drain();
53+
var response = request.response;
54+
if (request.uri == expectedUri) {
55+
response.write(sampleText);
56+
} else {
57+
response.write("INCORRECT PATH!: ${request.uri}");
58+
}
59+
response.close();
60+
});
61+
return server;
62+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)