| OLD | NEW |
| 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/ast.dart'; | |
| 8 import 'package:analyzer_experimental/src/generated/scanner.dart'; | 7 import 'package:analyzer_experimental/src/generated/scanner.dart'; |
| 9 import 'package:analyzer_experimental/src/services/formatter.dart'; | |
| 10 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 8 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 11 import 'package:analyzer_experimental/src/services/writer.dart'; | 9 import 'package:analyzer_experimental/src/services/writer.dart'; |
| 12 | 10 |
| 13 main() { | 11 main() { |
| 14 | 12 |
| 15 /// Formatter tests | 13 /// Formatter tests |
| 16 group('formatter', () { | 14 group('formatter', () { |
| 17 | 15 |
| 18 test('failed parse', () { | 16 test('failed parse', () { |
| 19 var formatter = new CodeFormatter(); | 17 var formatter = new CodeFormatter(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 50 | 48 |
| 51 test('CU (4)', () { | 49 test('CU (4)', () { |
| 52 expectCUFormatsTo( | 50 expectCUFormatsTo( |
| 53 ' class A {\n' | 51 ' class A {\n' |
| 54 '}', | 52 '}', |
| 55 'class A {\n' | 53 'class A {\n' |
| 56 '}\n' | 54 '}\n' |
| 57 ); | 55 ); |
| 58 }); | 56 }); |
| 59 | 57 |
| 58 test('CU w/class decl comment', () { |
| 59 expectCUFormatsTo( |
| 60 'import "foo";\n\n' |
| 61 '//Killer class\n' |
| 62 'class A {\n' |
| 63 '}', |
| 64 'import "foo";\n\n' |
| 65 '//Killer class\n' |
| 66 'class A {\n' |
| 67 '}\n' |
| 68 ); |
| 69 }); |
| 70 |
| 71 |
| 60 test('CU (method indent)', () { | 72 test('CU (method indent)', () { |
| 61 expectCUFormatsTo( | 73 expectCUFormatsTo( |
| 62 'class A {\n' | 74 'class A {\n' |
| 63 'void x(){\n' | 75 'void x(){\n' |
| 64 '}\n' | 76 '}\n' |
| 65 '}', | 77 '}', |
| 66 'class A {\n' | 78 'class A {\n' |
| 67 ' void x() {\n' | 79 ' void x() {\n' |
| 68 ' }\n' | 80 ' }\n' |
| 69 '}\n' | 81 '}\n' |
| (...skipping 22 matching lines...) Expand all Loading... |
| 92 ' int x() => 42 + 3;\n' | 104 ' int x() => 42 + 3;\n' |
| 93 '}\n' | 105 '}\n' |
| 94 ); | 106 ); |
| 95 }); | 107 }); |
| 96 | 108 |
| 97 test('CU (method indent - 4)', () { | 109 test('CU (method indent - 4)', () { |
| 98 expectCUFormatsTo( | 110 expectCUFormatsTo( |
| 99 'class A {\n' | 111 'class A {\n' |
| 100 ' int x() { \n' | 112 ' int x() { \n' |
| 101 'if (true) {return 42;\n' | 113 'if (true) {return 42;\n' |
| 102 '} else { return false; }\n' | 114 '} else { return 13; }\n' |
| 103 ' }' | 115 ' }' |
| 104 '}', | 116 '}', |
| 105 'class A {\n' | 117 'class A {\n' |
| 106 ' int x() {\n' | 118 ' int x() {\n' |
| 107 ' if (true) {\n' | 119 ' if (true) {\n' |
| 108 ' return 42;\n' | 120 ' return 42;\n' |
| 109 ' } else {\n' | 121 ' } else {\n' |
| 110 ' return false;\n' | 122 ' return 13;\n' |
| 111 ' }\n' | 123 ' }\n' |
| 112 ' }\n' | 124 ' }\n' |
| 113 '}\n' | 125 '}\n' |
| 114 ); | 126 ); |
| 115 }); | 127 }); |
| 116 | 128 |
| 117 test('CU (multiple members)', () { | 129 test('CU (multiple members)', () { |
| 118 expectCUFormatsTo( | 130 expectCUFormatsTo( |
| 119 'class A {\n' | 131 'class A {\n' |
| 120 '}\n' | 132 '}\n' |
| 121 'class B {\n' | 133 'class B {\n' |
| 122 '}\n', | 134 '}\n', |
| 123 'class A {\n' | 135 'class A {\n' |
| 124 '}\n' | 136 '}\n' |
| 125 'class B {\n' | 137 'class B {\n' |
| 126 '}\n' | 138 '}\n' |
| 127 ); | 139 ); |
| 128 }); | 140 }); |
| 129 | 141 |
| 130 test('CU (multiple members w/blanks)', () { | 142 test('CU (multiple members w/blanks)', () { |
| 131 expectCUFormatsTo( | 143 expectCUFormatsTo( |
| 132 'class A {\n' | 144 'class A {\n' |
| 133 '}\n\n' | 145 '}\n\n' |
| 134 'class B {\n' | 146 'class B {\n\n\n' |
| 147 ' int b() => 42;\n\n' |
| 148 ' int c() => b();\n\n' |
| 135 '}\n', | 149 '}\n', |
| 136 'class A {\n' | 150 'class A {\n' |
| 137 '}\n\n' | 151 '}\n\n' |
| 138 'class B {\n' | 152 'class B {\n\n\n' |
| 153 ' int b() => 42;\n\n' |
| 154 ' int c() => b();\n\n' |
| 139 '}\n' | 155 '}\n' |
| 140 ); | 156 ); |
| 141 }); | 157 }); |
| 142 | |
| 143 | 158 |
| 144 test('stmt', () { | 159 test('stmt', () { |
| 145 expectStmtFormatsTo( | 160 expectStmtFormatsTo( |
| 146 'if (true){\n' | 161 'if (true){\n' |
| 147 'if (true){\n' | 162 'if (true){\n' |
| 148 'if (true){\n' | 163 'if (true){\n' |
| 149 'return true;\n' | 164 'return true;\n' |
| 150 '} else{\n' | 165 '} else{\n' |
| 151 'return false;\n' | 166 'return false;\n' |
| 152 '}\n' | 167 '}\n' |
| 153 '}\n' | 168 '}\n' |
| 154 '}else{\n' | 169 '}else{\n' |
| 155 'return false;\n' | 170 'return false;\n' |
| 156 '}', | 171 '}', |
| 157 'if (true) {\n' | 172 'if (true) {\n' |
| 158 ' if (true) {\n' | 173 ' if (true) {\n' |
| 159 ' if (true) {\n' | 174 ' if (true) {\n' |
| 160 ' return true;\n' | 175 ' return true;\n' |
| 161 ' } else {\n' | 176 ' } else {\n' |
| 162 ' return false;\n' | 177 ' return false;\n' |
| 163 ' }\n' | 178 ' }\n' |
| 164 ' }\n' | 179 ' }\n' |
| 165 '} else {\n' | 180 '} else {\n' |
| 166 ' return false;\n' | 181 ' return false;\n' |
| 167 '}' | 182 '}' |
| 168 ); | 183 ); |
| 169 }); | 184 }); |
| 170 | 185 |
| 171 | |
| 172 test('initialIndent', () { | 186 test('initialIndent', () { |
| 173 var formatter = new CodeFormatter( | 187 var formatter = new CodeFormatter( |
| 174 new FormatterOptions(initialIndentationLevel: 2)); | 188 new FormatterOptions(initialIndentationLevel: 2)); |
| 175 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;'); | 189 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;'); |
| 176 expect(formattedSource, startsWith(' ')); | 190 expect(formattedSource, startsWith(' ')); |
| 177 }); | 191 }); |
| 178 | 192 |
| 179 }); | 193 }); |
| 180 | 194 |
| 181 | 195 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 203 line.addToken(new LineToken('foo')); | 217 line.addToken(new LineToken('foo')); |
| 204 expect(line.toString(), equals('foo')); | 218 expect(line.toString(), equals('foo')); |
| 205 }); | 219 }); |
| 206 | 220 |
| 207 test('addToken (2)', () { | 221 test('addToken (2)', () { |
| 208 var line = new Line(indent: 1); | 222 var line = new Line(indent: 1); |
| 209 line.addToken(new LineToken('foo')); | 223 line.addToken(new LineToken('foo')); |
| 210 expect(line.toString(), equals(' foo')); | 224 expect(line.toString(), equals(' foo')); |
| 211 }); | 225 }); |
| 212 | 226 |
| 227 test('isWhitespace', () { |
| 228 var line = new Line(indent: 1); |
| 229 expect(line.isWhitespace(), isTrue); |
| 230 }); |
| 231 |
| 213 }); | 232 }); |
| 214 | 233 |
| 215 | 234 |
| 216 /// Writer tests | 235 /// Writer tests |
| 217 group('writer', () { | 236 group('writer', () { |
| 218 | 237 |
| 219 test('basic print', () { | 238 test('basic print', () { |
| 220 var writer = new SourceWriter(); | 239 var writer = new SourceWriter(); |
| 221 writer.print('foo'); | 240 writer.print('foo'); |
| 222 writer.print(' '); | 241 writer.print(' '); |
| 223 writer.print('bar'); | 242 writer.print('bar'); |
| 224 expect(writer.toString(), equals('foo bar')); | 243 expect(writer.toString(), equals('foo bar')); |
| 225 }); | 244 }); |
| 226 | 245 |
| 227 test('newline', () { | 246 test('newline', () { |
| 228 var writer = new SourceWriter(); | 247 var writer = new SourceWriter(); |
| 229 writer.print('foo'); | 248 writer.print('foo'); |
| 230 writer.newline(); | 249 writer.newline(); |
| 231 expect(writer.toString(), equals('foo\n')); | 250 expect(writer.toString(), equals('foo\n')); |
| 232 }); | 251 }); |
| 233 | 252 |
| 253 test('newline trims whitespace', () { |
| 254 var writer = new SourceWriter(indentCount:2); |
| 255 writer.newline(); |
| 256 expect(writer.toString(), equals('\n')); |
| 257 }); |
| 258 |
| 234 test('basic print (with indents)', () { | 259 test('basic print (with indents)', () { |
| 235 var writer = new SourceWriter(); | 260 var writer = new SourceWriter(); |
| 236 writer.print('foo'); | 261 writer.print('foo'); |
| 237 writer.indent(); | 262 writer.indent(); |
| 238 writer.newline(); | 263 writer.newline(); |
| 239 writer.print('bar'); | 264 writer.print('bar'); |
| 240 writer.unindent(); | 265 writer.unindent(); |
| 241 writer.newline(); | 266 writer.newline(); |
| 242 writer.print('baz'); | 267 writer.print('baz'); |
| 243 expect(writer.toString(), equals('foo\n bar\nbaz')); | 268 expect(writer.toString(), equals('foo\n bar\nbaz')); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 String formatCU(src, {options: const FormatterOptions()}) => | 318 String formatCU(src, {options: const FormatterOptions()}) => |
| 294 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 319 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 295 | 320 |
| 296 String formatStatement(src, {options: const FormatterOptions()}) => | 321 String formatStatement(src, {options: const FormatterOptions()}) => |
| 297 new CodeFormatter(options).format(CodeKind.STATEMENT, src); | 322 new CodeFormatter(options).format(CodeKind.STATEMENT, src); |
| 298 | 323 |
| 299 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); | 324 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); |
| 300 | 325 |
| 301 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), | 326 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), |
| 302 equals(expected)); | 327 equals(expected)); |
| OLD | NEW |