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

Side by Side Diff: packages/dart_style/test/formatter_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 unified diff | Download patch
« no previous file with comments | « packages/dart_style/test/comments/top_level.unit ('k') | packages/dart_style/test/io_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 @TestOn("vm")
6 library dart_style.test.formatter_test;
7
8 import 'dart:io';
9 import 'dart:mirrors';
10
11 import 'package:path/path.dart' as p;
12 import 'package:test/test.dart';
13
14 import 'package:dart_style/dart_style.dart';
15
16 void main() {
17 testDirectory("comments");
18 testDirectory("regression");
19 testDirectory("selections");
20 testDirectory("splitting");
21 testDirectory("whitespace");
22
23 test("throws a FormatterException on failed parse", () {
24 var formatter = new DartFormatter();
25 expect(() => formatter.format('wat?!'),
26 throwsA(new isInstanceOf<FormatterException>()));
27 });
28
29 test("FormatterException.message() does not throw", () {
30 // This is a regression test for #358 where an error whose position is
31 // past the end of the source caused FormatterException to throw.
32 try {
33 new DartFormatter().format("library");
34 } on FormatterException catch (err) {
35 var message = err.message();
36 expect(message, contains("Could not format"));
37 }
38 });
39
40 test("FormatterException describes parse errors", () {
41 try {
42 new DartFormatter().format(
43 """
44
45 var a = some error;
46
47 var b = another one;
48 """,
49 uri: "my_file.dart");
50
51 fail("Should throw.");
52 } on FormatterException catch (err) {
53 var message = err.message();
54 expect(message, contains("my_file.dart"));
55 expect(message, contains("line 2"));
56 expect(message, contains("line 4"));
57 }
58 });
59
60 test("adds newline to unit", () {
61 expect(new DartFormatter().format("var x = 1;"), equals("var x = 1;\n"));
62 });
63
64 test("adds newline to unit after trailing comment", () {
65 expect(new DartFormatter().format("library foo; //zamm"),
66 equals("library foo; //zamm\n"));
67 });
68
69 test("removes extra newlines", () {
70 expect(
71 new DartFormatter().format("var x = 1;\n\n\n"), equals("var x = 1;\n"));
72 });
73
74 test("does not add newline to statement", () {
75 expect(new DartFormatter().formatStatement("var x = 1;"),
76 equals("var x = 1;"));
77 });
78
79 test("fails if anything is after the statement", () {
80 try {
81 new DartFormatter().formatStatement("var x = 1;;");
82
83 fail("Should throw.");
84 } catch (err) {
85 expect(err, new isInstanceOf<FormatterException>());
86 var message = err.message();
87 expect(message, contains("Unexpected token"));
88 expect(message, contains("column 11"));
89 }
90 });
91
92 test('preserves initial indent', () {
93 var formatter = new DartFormatter(indent: 3);
94 expect(
95 formatter.formatStatement('if (foo) {bar;}'),
96 equals(' if (foo) {\n'
97 ' bar;\n'
98 ' }'));
99 });
100
101 group('line endings', () {
102 test('uses given line ending', () {
103 expect(new DartFormatter(lineEnding: "%").format("var i = 1;"),
104 equals("var i = 1;%"));
105 });
106
107 test('infers \\r\\n if the first newline uses that', () {
108 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"),
109 equals("var i = 1;\r\n"));
110 });
111
112 test('infers \\n if the first newline uses that', () {
113 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"),
114 equals("var i = 1;\n"));
115 });
116
117 test('defaults to \\n if there are no newlines', () {
118 expect(new DartFormatter().format("var i =1;"), equals("var i = 1;\n"));
119 });
120
121 test('handles Windows line endings in multiline strings', () {
122 expect(
123 new DartFormatter(lineEnding: "\r\n").formatStatement(' """first\r\n'
124 'second\r\n'
125 'third""" ;'),
126 equals('"""first\r\n'
127 'second\r\n'
128 'third""";'));
129 });
130 });
131 }
132
133 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name].
134 void testDirectory(String name) {
135 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*");
136
137 // Locate the "test" directory. Use mirrors so that this works with the test
138 // package, which loads this suite into an isolate.
139 var testDir = p.dirname(currentMirrorSystem()
140 .findLibrary(#dart_style.test.formatter_test)
141 .uri
142 .path);
143
144 var entries = new Directory(p.join(testDir, name))
145 .listSync(recursive: true, followLinks: false);
146 for (var entry in entries) {
147 if (!entry.path.endsWith(".stmt") && !entry.path.endsWith(".unit")) {
148 continue;
149 }
150
151 group("$name ${p.basename(entry.path)}", () {
152 var lines = (entry as File).readAsLinesSync();
153
154 // The first line may have a "|" to indicate the page width.
155 var pageWidth;
156 if (lines[0].endsWith("|")) {
157 pageWidth = lines[0].indexOf("|");
158 lines = lines.skip(1).toList();
159 }
160
161 var i = 0;
162 while (i < lines.length) {
163 var description = lines[i++].replaceAll(">>>", "").trim();
164
165 // Let the test specify a leading indentation. This is handy for
166 // regression tests which often come from a chunk of nested code.
167 var leadingIndent = 0;
168 var indentMatch = indentPattern.firstMatch(description);
169 if (indentMatch != null) {
170 leadingIndent = int.parse(indentMatch[1]);
171 description = description.substring(indentMatch.end);
172 }
173
174 if (description == "") {
175 description = "line ${i + 1}";
176 } else {
177 description = "line ${i + 1}: $description";
178 }
179
180 var input = "";
181 while (!lines[i].startsWith("<<<")) {
182 input += lines[i++] + "\n";
183 }
184
185 var expectedOutput = "";
186 while (++i < lines.length && !lines[i].startsWith(">>>")) {
187 expectedOutput += lines[i] + "\n";
188 }
189
190 test(description, () {
191 var isCompilationUnit = p.extension(entry.path) == ".unit";
192
193 var inputCode =
194 _extractSelection(input, isCompilationUnit: isCompilationUnit);
195
196 var expected = _extractSelection(expectedOutput,
197 isCompilationUnit: isCompilationUnit);
198
199 var formatter =
200 new DartFormatter(pageWidth: pageWidth, indent: leadingIndent);
201
202 var actual = formatter.formatSource(inputCode);
203
204 // The test files always put a newline at the end of the expectation.
205 // Statements from the formatter (correctly) don't have that, so add
206 // one to line up with the expected result.
207 var actualText = actual.text;
208 if (!isCompilationUnit) actualText += "\n";
209
210 expect(actualText, equals(expected.text));
211 expect(actual.selectionStart, equals(expected.selectionStart));
212 expect(actual.selectionLength, equals(expected.selectionLength));
213 });
214 }
215 });
216 }
217 }
218
219 /// Given a source string that contains ‹ and › to indicate a selection, returns
220 /// a [SourceCode] with the text (with the selection markers removed) and the
221 /// correct selection range.
222 SourceCode _extractSelection(String source, {bool isCompilationUnit: false}) {
223 var start = source.indexOf("‹");
224 source = source.replaceAll("‹", "");
225
226 var end = source.indexOf("›");
227 source = source.replaceAll("›", "");
228
229 return new SourceCode(source,
230 isCompilationUnit: isCompilationUnit,
231 selectionStart: start == -1 ? null : start,
232 selectionLength: end == -1 ? null : end - start);
233 }
OLDNEW
« no previous file with comments | « packages/dart_style/test/comments/top_level.unit ('k') | packages/dart_style/test/io_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698