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:
ahe@google.com 2014-12-18 11:36:02 +00:00
parent 743cd666d4
commit acddc1ba37
2 changed files with 115 additions and 1 deletions

View file

@ -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));
}

View file

@ -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")));
}