forked from dart-lang/site-www
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library tour example tests for dart:io
Contributes to dart-lang#407 and dart-lang#416. Continuation of dart-lang#443
- Loading branch information
Showing
6 changed files
with
276 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Llamas | ||
Dromedaries | ||
Oases in the Desert | ||
And other oddities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.