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

Side by Side Diff: pkg/analyzer_experimental/test/services/formatter_test.dart

Issue 22928013: Formatter fixes and tweaks. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'package:unittest/unittest.dart'; 5 import 'package:unittest/unittest.dart';
6 6
7 import 'package:analyzer_experimental/src/generated/scanner.dart'; 7 import 'package:analyzer_experimental/src/generated/scanner.dart';
8 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; 8 import 'package:analyzer_experimental/src/services/formatter_impl.dart';
9 import 'package:analyzer_experimental/src/services/writer.dart'; 9 import 'package:analyzer_experimental/src/services/writer.dart';
10 10
11 main() { 11 main() {
12 12
13 /// Formatter tests 13 /// Formatter tests
14 group('formatter', () { 14 group('formatter', () {
15 15
16 test('failed parse', () { 16 test('failed parse', () {
17 var formatter = new CodeFormatter(); 17 var formatter = new CodeFormatter();
18 expect(() => formatter.format(CodeKind.COMPILATION_UNIT, '~'), 18 expect(() => formatter.format(CodeKind.COMPILATION_UNIT, '~'),
19 throwsA(new isInstanceOf<FormatterException>())); 19 throwsA(new isInstanceOf<FormatterException>()));
20 }); 20 });
21 21
22 test('CU (1)', () { 22 test('CU (1)', () {
23 expectCUFormatsTo( 23 expectCUFormatsTo(
24 'class A {\n' 24 'class A {\n'
25 ' inc(int x) => ++x;\n'
25 '}\n', 26 '}\n',
26 'class A {\n' 27 'class A {\n'
28 ' inc(int x) => ++x;\n'
27 '}\n' 29 '}\n'
28 ); 30 );
29 }); 31 });
30 32
31 test('CU (2)', () { 33 test('CU (2)', () {
32 expectCUFormatsTo( 34 expectCUFormatsTo(
33 'class A { \n' 35 'class A { \n'
34 '}\n', 36 '}\n',
35 'class A {\n' 37 'class A {\n'
36 '}\n' 38 '}\n'
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 'foo() {\n' 88 'foo() {\n'
87 '}\n' 89 '}\n'
88 'bar() {\n' 90 'bar() {\n'
89 '}\n', 91 '}\n',
90 '\n\n' 92 '\n\n'
91 'foo() {\n' 93 'foo() {\n'
92 '}\n' 94 '}\n'
93 'bar() {\n' 95 'bar() {\n'
94 '}\n' 96 '}\n'
95 ); 97 );
98 expectCUFormatsTo(
99 'const A = 42;\n'
100 'final foo = 32;\n',
101 'const A = 42;\n'
102 'final foo = 32;\n'
103 );
96 }); 104 });
97 105
98 test('CU - imports', () { 106 test('CU - imports', () {
99 expectCUFormatsTo( 107 expectCUFormatsTo(
100 'import "dart:io";\n\n' 108 'import "dart:io";\n\n'
101 'import "package:unittest/unittest.dart";\n' 109 'import "package:unittest/unittest.dart";\n'
102 'foo() {\n' 110 'foo() {\n'
103 '}\n', 111 '}\n',
104 'import "dart:io";\n\n' 112 'import "dart:io";\n\n'
105 'import "package:unittest/unittest.dart";\n' 113 'import "package:unittest/unittest.dart";\n'
106 'foo() {\n' 114 'foo() {\n'
107 '}\n' 115 '}\n'
108 ); 116 );
109 }); 117 });
110 118
111 test('CU w/class decl comment', () { 119 test('CU w/class decl comment', () {
112 expectCUFormatsTo( 120 expectCUFormatsTo(
113 'import "foo";\n\n' 121 'import "foo";\n\n'
114 '//Killer class\n' 122 '//Killer class\n'
115 'class A {\n' 123 'class A {\n'
116 '}', 124 '}',
117 'import "foo";\n\n' 125 'import "foo";\n\n'
118 '//Killer class\n' 126 '//Killer class\n'
119 'class A {\n' 127 'class A {\n'
120 '}' 128 '}'
121 ); 129 );
122 }); 130 });
123 131
124 132 test('CU (method body)', () {
133 expectCUFormatsTo(
134 'class A {\n'
135 ' foo(path) {\n'
136 ' var buffer = new StringBuffer();\n'
137 ' var file = new File(path);\n'
138 ' return file;\n'
139 ' }\n'
140 '}\n',
141 'class A {\n'
142 ' foo(path) {\n'
143 ' var buffer = new StringBuffer();\n'
144 ' var file = new File(path);\n'
145 ' return file;\n'
146 ' }\n'
147 '}\n'
148 );
149 expectCUFormatsTo(
150 'class A {\n'
151 ' foo(files) {\n'
152 ' for (var file in files) {\n'
153 ' print(file);\n'
154 ' }\n'
155 ' }\n'
156 '}\n',
157 'class A {\n'
158 ' foo(files) {\n'
159 ' for (var file in files) {\n'
Brian Wilkerson 2013/08/21 18:18:53 I don't think you want to produce two spaces here.
pquitslund 2013/08/21 19:40:40 Aha! That explains why the code you commented on
160 ' print(file);\n'
161 ' }\n'
162 ' }\n'
163 '}\n'
164 );
165 });
166
125 test('CU (method indent)', () { 167 test('CU (method indent)', () {
126 expectCUFormatsTo( 168 expectCUFormatsTo(
127 'class A {\n' 169 'class A {\n'
128 'void x(){\n' 170 'void x(){\n'
129 '}\n' 171 '}\n'
130 '}\n', 172 '}\n',
131 'class A {\n' 173 'class A {\n'
132 ' void x() {\n' 174 ' void x() {\n'
133 ' }\n' 175 ' }\n'
134 '}\n' 176 '}\n'
135 ); 177 );
136 }); 178 });
137 179
138 test('CU (method indent - 2)', () { 180 test('CU (method indent - 2)', () {
139 expectCUFormatsTo( 181 expectCUFormatsTo(
140 'class A {\n' 182 'class A {\n'
141 ' static bool x(){\n' 183 ' static bool x(){\n'
142 'return true; }\n' 184 'return true; }\n'
143 ' }\n', 185 ' }\n',
144 'class A {\n' 186 'class A {\n'
145 ' static bool x() {\n' 187 ' static bool x() {\n'
(...skipping 11 matching lines...) Expand all
157 'class A {\n' 199 'class A {\n'
158 ' int x() => 42 + 3;\n' 200 ' int x() => 42 + 3;\n'
159 '}\n' 201 '}\n'
160 ); 202 );
161 }); 203 });
162 204
163 test('CU (method indent - 4)', () { 205 test('CU (method indent - 4)', () {
164 expectCUFormatsTo( 206 expectCUFormatsTo(
165 'class A {\n' 207 'class A {\n'
166 ' int x() { \n' 208 ' int x() { \n'
167 'if (true) {\nreturn 42;\n' 209 'if (true) {\n'
210 'return 42;\n'
168 '} else {\n' 211 '} else {\n'
169 'return 13;\n }\n' 212 'return 13;\n }\n'
170 ' }' 213 ' }'
171 '}\n', 214 '}\n',
172 'class A {\n' 215 'class A {\n'
173 ' int x() {\n' 216 ' int x() {\n'
174 ' if (true) {\n' 217 ' if (true) {\n'
175 ' return 42;\n' 218 ' return 42;\n'
176 ' } else {\n' 219 ' } else {\n'
177 ' return 13;\n' 220 ' return 13;\n'
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 String formatCU(src, {options: const FormatterOptions()}) => 478 String formatCU(src, {options: const FormatterOptions()}) =>
436 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); 479 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src);
437 480
438 String formatStatement(src, {options: const FormatterOptions()}) => 481 String formatStatement(src, {options: const FormatterOptions()}) =>
439 new CodeFormatter(options).format(CodeKind.STATEMENT, src); 482 new CodeFormatter(options).format(CodeKind.STATEMENT, src);
440 483
441 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); 484 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected));
442 485
443 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), 486 expectStmtFormatsTo(src, expected) => expect(formatStatement(src),
444 equals(expected)); 487 equals(expected));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698