Skip to content

Commit

Permalink
Library tour example tests for dart:io
Browse files Browse the repository at this point in the history
Contributes to dart-lang#407 and dart-lang#416.

Continuation of dart-lang#443
  • Loading branch information
chalin committed Nov 23, 2017
1 parent 792c517 commit 3d98378
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 86 deletions.
31 changes: 31 additions & 0 deletions examples/lib/library_tour/io/http_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// #docplaster
import 'dart:async';
import 'dart:io';

int stopAfter = 10;
// #docregion
Future main() async {
var requests = await HttpServer.bind('localhost', 8888);
// #enddocregion
requests = requests.take(stopAfter);
// #docregion
await for (var request in requests) {
processRequest(request);
}
}

void processRequest(HttpRequest request) {
print('Got request for ${request.uri.path}');
final response = request.response;
if (request.uri.path == '/dart') {
response
..headers.contentType = new ContentType(
'text',
'plain',
)
..write('Hello from the server');
} else {
response.statusCode = HttpStatus.NOT_FOUND;
}
response.close();
}
16 changes: 1 addition & 15 deletions examples/test/library_tour/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void main() {
// #enddocregion initWebSocket

final t = wsStream.stream.timeout(
new Duration(seconds: 1),
new Duration(seconds: 5),
onTimeout: (s) => s.add('Timeout!'),
);

Expand All @@ -195,18 +195,4 @@ void main() {
await wsStream.close();
}
});

group('xxxx', () {
test('xxxxx', () {
// #docregion xxxx
11111;
// #enddocregion xxxx
});

test('xxxxx', () {
// #docregion xxxx
11111;
// #enddocregion xxxx
});
});
}
179 changes: 179 additions & 0 deletions examples/test/library_tour/io_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// ignore_for_file: non_constant_identifier_names
// #docplaster
// #docregion read-from-stream
import 'dart:async';
// #docregion import
import 'dart:io';
// #enddocregion import
import 'dart:convert';

// #enddocregion read-from-stream
import 'package:test/test.dart';
import '../../lib/library_tour/io/http_server.dart'
as http_server;
import '../util/print_matcher.dart' as m;

void main() {
test('readAsString, readAsLines', () async {
// #docregion readAsString
Future main() async {
var config = new File('test_data/config.txt');
var contents;

// Put the whole file in a single string.
contents = await config.readAsString();
print('The file is ${contents.length} characters long.');

// Put each line of the file into its own string.
contents = await config.readAsLines();
print('The file is ${contents.length} lines long.');
}
// #enddocregion readAsString

expect(
main,
m.prints([
'The file is 58 characters long.',
'The file is 4 lines long.'
]));
});

test('readAsBytes', () {
// #docregion readAsBytes
Future main() async {
var config = new File('test_data/config.txt');

var contents = await config.readAsBytes();
print('The file is ${contents.length} bytes long.');
}
// #enddocregion readAsBytes

expect(main, m.prints('The file is 58 bytes long.'));
});

test('try-catch', () {
// #docregion try-catch
Future main() async {
var config = new File('does-not-exist.txt');
try {
var contents = await config.readAsString();
print(contents);
} catch (e) {
print(e);
}
}

// #enddocregion try-catch
expect(main, prints(startsWith('FileSystemException')));
});

test('read-from-stream', () {
expect(
main_test_read_from_stream,
prints(allOf([
contains(
new RegExp(r'Got \d+ characters from stream')),
contains('file is now closed'),
])));
});

test('write-file', () async {
// #docregion write-file
var logFile = new File('test_data/log.txt');
var sink = logFile.openWrite();
sink.write('FILE ACCESSED ${new DateTime.now()}\n');
await sink.flush();
await sink.close();
// #enddocregion write-file
try {
expect(logFile.existsSync(), isTrue);
expect(logFile.readAsStringSync(),
startsWith('FILE ACCESSED'));
} finally {
logFile?.delete();
}
});

test('list-dir', () {
// #docregion list-dir
Future main() async {
var dir = new Directory('test_data');

try {
var dirList = dir.list();
await for (FileSystemEntity f in dirList) {
if (f is File) {
print('Found file ${f.path}');
} else if (f is Directory) {
print('Found dir ${f.path}');
}
}
} catch (e) {
print(e.toString());
}
}

// #enddocregion list-dir
expect(main, prints(contains('Found file')));
});

test('client-server', () async {
// #docregion client
Future main() async {
var url = Uri.parse('http://localhost:8888/dart');
var httpClient = new HttpClient();
var request = await httpClient.getUrl(url);
var response = await request.close();
var data = await response.transform(UTF8.decoder).toList();
print('Response ${response.statusCode}: $data');
httpClient.close();
}
// #enddocregion client

http_server.stopAfter = 1;
final clientAndServer = () => Future.wait([
http_server.main(),
main(),
]);
expect(
clientAndServer,
m.prints([
'Got request for /dart',
'Response 200: [Hello from the server]'
]));
});
}

// The following function is defined here so that it is left
// aligned with the import statements, which are also
// included in the excerpt.

// #docregion read-from-stream
Future main_test_read_from_stream() async {
var config = new File('test_data/config.txt');
Stream<List<int>> inputStream = config.openRead();

var lines = inputStream
.transform(UTF8.decoder)
.transform(new LineSplitter());
try {
await for (var line in lines) {
print('Got ${line.length} characters from stream');
}
print('file is now closed');
} catch (e) {
print(e);
}
}
// #enddocregion read-from-stream

/// No tests below this point. Excerpts only illustrate declarations.
void miscDeclAnalyzedButNotTested() {
{
var logFile = new File('test_data/log.txt');
// #docregion append
var sink = logFile.openWrite(mode: FileMode.APPEND);
// #enddocregion append
sink.close();
}
}
4 changes: 4 additions & 0 deletions examples/test_data/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Llamas
Dromedaries
Oases in the Desert
And other oddities
4 changes: 3 additions & 1 deletion scripts/dartfmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ $DARTFMT -l 65 \
examples/lib/language_tour/exceptions.dart \
examples/test/library_tour/core_test.dart \
examples/lib/library_tour/async/future.dart \
examples/lib/library_tour/async/stream.dart
examples/lib/library_tour/async/stream.dart \
examples/test/library_tour/io_test.dart


# If any files were changed, then exit 1:
REFORMATTED_FILES=$(git status --short)
Expand Down
Loading

0 comments on commit 3d98378

Please sign in to comment.