Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(823)

Unified Diff: test/formatter_test.dart

Issue 822273004: Add API to provide a selection range, and return the updated selection after formatting. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Remove redundant argument check. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/source_visitor.dart ('k') | test/selections/selections.stmt » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/formatter_test.dart
diff --git a/test/formatter_test.dart b/test/formatter_test.dart
index 13c0dadad230e6955759ee44647ac67d4346e486..e35c6d3b0fe3ba6dad6d99e2f5a42597f907c486 100644
--- a/test/formatter_test.dart
+++ b/test/formatter_test.dart
@@ -20,6 +20,7 @@ void main() {
testDirectory("comments");
testDirectory("regression");
+ testDirectory("selections");
testDirectory("splitting");
testDirectory("whitespace");
@@ -47,6 +48,44 @@ void main() {
}
});
+ group('SourceCode', () {
+ test('throws on negative start', () {
+ expect(() {
+ new SourceCode("12345;", selectionStart: -1, selectionLength: 0);
+ }, throwsArgumentError);
+ });
+
+ test('throws on out of bounds start', () {
+ expect(() {
+ new SourceCode("12345;", selectionStart: 7, selectionLength: 0);
+ }, throwsArgumentError);
+ });
+
+ test('throws on negative length', () {
+ expect(() {
+ new SourceCode("12345;", selectionStart: 1, selectionLength: -1);
+ }, throwsArgumentError);
+ });
+
+ test('throws on out of bounds length', () {
+ expect(() {
+ new SourceCode("12345;", selectionStart: 2, selectionLength: 5);
+ }, throwsArgumentError);
+ });
+
+ test('throws is start is null and length is not', () {
+ expect(() {
+ new SourceCode("12345;", selectionStart: 0);
+ }, throwsArgumentError);
+ });
+
+ test('throws is length is null and start is not', () {
+ expect(() {
+ new SourceCode("12345;", selectionLength: 1);
+ }, throwsArgumentError);
+ });
+ });
+
test("adds newline to unit", () {
expect(new DartFormatter().format("var x = 1;"),
equals("var x = 1;\n"));
@@ -149,19 +188,52 @@ void testDirectory(String name) {
}
test(description, () {
+ var isCompilationUnit = p.extension(entry.path) == ".unit";
+
+ var inputCode = _extractSelection(input,
+ isCompilationUnit: isCompilationUnit);
+
+ var expected = _extractSelection(expectedOutput,
+ isCompilationUnit: isCompilationUnit);
+
var formatter = new DartFormatter(
pageWidth: pageWidth, indent: leadingIndent);
- var result;
- if (p.extension(entry.path) == ".stmt") {
- result = formatter.formatStatement(input) + "\n";
- } else {
- result = formatter.format(input);
- }
+ var actual = formatter.formatSource(inputCode);
+
+ // The test files always put a newline at the end of the expectation.
+ // Statements from the formatter (correctly) don't have that, so add
+ // one to line up with the expected result.
+ var actualText = actual.text;
+ if (!isCompilationUnit) actualText += "\n";
- expect(result, equals(expectedOutput));
+ expect(actualText, equals(expected.text));
+ expect(actual.selectionStart, equals(expected.selectionStart));
+ expect(actual.selectionLength, equals(expected.selectionLength));
});
}
});
}
}
+
+/// Given a source string that contains ‹ and › to indicate a selection, returns
+/// a [SourceCode] with the text (with the selection markers removed) and the
+/// correct selection range.
+SourceCode _extractSelection(String source, {bool isCompilationUnit: false}) {
+ var start = source.indexOf("‹");
+ source = source.replaceAll("‹", "");
+
+ var end = source.indexOf("›");
+ source = source.replaceAll("›", "");
+
+ // If the selection end is after a trailing newline, there will be an extra
+ // newline *after* the "›", so remove it.
+ if (end != -1 && source.endsWith("\n\n")) {
+ source = source.substring(0, source.length - 1);
+ }
+
+ return new SourceCode(source,
+ isCompilationUnit: isCompilationUnit,
+ selectionStart: start == -1 ? null : start,
+ selectionLength: end == -1 ? null : end - start);
+}
« no previous file with comments | « lib/src/source_visitor.dart ('k') | test/selections/selections.stmt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698