mirror of
https://github.com/dart-lang/sdk
synced 2024-09-20 05:51:37 +00:00
Support functions for testing multiple files.
R=johnniwinther@google.com Review URL: https://codereview.chromium.org//814013002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@42456 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
743cd666d4
commit
acddc1ba37
|
@ -39,3 +39,76 @@ List<String> expandUpdates(List updates) {
|
|||
|
||||
return result.map((e) => '$e').toList();
|
||||
}
|
||||
|
||||
/// Returns [files] split into mulitple named files. The keys in the returned
|
||||
/// map are filenames, the values are the files' content.
|
||||
///
|
||||
/// Names are indicated by a line on the form "==> filename <==". Spaces are
|
||||
/// significant. For example, given this input:
|
||||
///
|
||||
/// ==> file1.dart <==
|
||||
/// First line of file 1.
|
||||
/// Second line of file 1.
|
||||
/// Third line of file 1.
|
||||
/// ==> empty.dart <==
|
||||
/// ==> file2.dart <==
|
||||
/// First line of file 2.
|
||||
/// Second line of file 2.
|
||||
/// Third line of file 2.
|
||||
///
|
||||
/// This function would return:
|
||||
///
|
||||
/// {
|
||||
/// "file1.dart": """
|
||||
/// First line of file 1.
|
||||
/// Second line of file 1.
|
||||
/// Third line of file 1.
|
||||
/// """,
|
||||
///
|
||||
/// "empty.dart":"",
|
||||
///
|
||||
/// "file2.dart":"""
|
||||
/// First line of file 2.
|
||||
/// Second line of file 2.
|
||||
/// Third line of file 2.
|
||||
/// """
|
||||
/// }
|
||||
Map<String, String> splitFiles(String files) {
|
||||
Map<String, String> result = <String, String>{};
|
||||
String currentName;
|
||||
List<String> content;
|
||||
void finishFile() {
|
||||
if (currentName != null) {
|
||||
if (result.containsKey(currentName)) {
|
||||
throw new ArgumentError("Duplicated filename $currentName in $files");
|
||||
}
|
||||
result[currentName] = content.join('');
|
||||
}
|
||||
content = null;
|
||||
}
|
||||
void processDirective(String line) {
|
||||
finishFile();
|
||||
if (line.length < 8 || !line.endsWith(" <==\n")) {
|
||||
throw new ArgumentError(
|
||||
"Malformed line: expected '==> ... <==', but got: '$line'");
|
||||
}
|
||||
currentName = line.substring(4, line.length - 5);
|
||||
content = <String>[];
|
||||
}
|
||||
for (String line in splitLines(files)) {
|
||||
if (line.startsWith("==>")) {
|
||||
processDirective(line);
|
||||
} else {
|
||||
content.add(line);
|
||||
}
|
||||
}
|
||||
finishFile();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Split [text] into lines preserving trailing newlines (unlike
|
||||
/// String.split("\n"). Also, if [text] is empty, return an empty list (unlike
|
||||
/// String.split("\n")).
|
||||
List<String> splitLines(String text) {
|
||||
return text.split(new RegExp('^', multiLine: true));
|
||||
}
|
||||
|
|
|
@ -5,11 +5,16 @@
|
|||
/// Test [source_update.dart].
|
||||
library trydart.source_update_test;
|
||||
|
||||
import 'dart:convert' show
|
||||
JSON;
|
||||
|
||||
import 'package:expect/expect.dart' show
|
||||
Expect;
|
||||
|
||||
import 'source_update.dart' show
|
||||
expandUpdates;
|
||||
expandUpdates,
|
||||
splitFiles,
|
||||
splitLines;
|
||||
|
||||
main() {
|
||||
Expect.listEquals(
|
||||
|
@ -27,4 +32,40 @@ main() {
|
|||
Expect.throws(() {
|
||||
expandUpdates(["head ", ["v1", "v2"], " tail ", ["v1", "v2", "v3"]]);
|
||||
});
|
||||
|
||||
Expect.stringEquals(
|
||||
JSON.encode({
|
||||
"file1.dart": """
|
||||
First line of file 1.
|
||||
Second line of file 1.
|
||||
Third line of file 1.
|
||||
""",
|
||||
"empty.dart":"",
|
||||
"file2.dart":"""
|
||||
First line of file 2.
|
||||
Second line of file 2.
|
||||
Third line of file 2.
|
||||
"""}),
|
||||
|
||||
JSON.encode(splitFiles(r"""
|
||||
==> file1.dart <==
|
||||
First line of file 1.
|
||||
Second line of file 1.
|
||||
Third line of file 1.
|
||||
==> empty.dart <==
|
||||
==> file2.dart <==
|
||||
First line of file 2.
|
||||
Second line of file 2.
|
||||
Third line of file 2.
|
||||
""")));
|
||||
|
||||
Expect.stringEquals("{}", JSON.encode(splitFiles("")));
|
||||
|
||||
Expect.stringEquals("[]", JSON.encode(splitLines("")));
|
||||
|
||||
Expect.stringEquals('["1"]', JSON.encode(splitLines("1")));
|
||||
|
||||
Expect.stringEquals('["\\n"]', JSON.encode(splitLines("\n")));
|
||||
|
||||
Expect.stringEquals('["\\n","1"]', JSON.encode(splitLines("\n1")));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue