| 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/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 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 }); | 60 }); |
| 61 | 61 |
| 62 test('CU (5)', () { | 62 test('CU (5)', () { |
| 63 expectCUFormatsTo( | 63 expectCUFormatsTo( |
| 64 'class A { int meaningOfLife() => 42; }', | 64 'class A { int meaningOfLife() => 42; }', |
| 65 'class A {\n' | 65 'class A {\n' |
| 66 ' int meaningOfLife() => 42;\n' | 66 ' int meaningOfLife() => 42;\n' |
| 67 '}' | 67 '}' |
| 68 ); | 68 ); |
| 69 }); | 69 }); |
| 70 | 70 |
| 71 | 71 |
| 72 // test('CU - comments', () { | 72 // test('CU - comments', () { |
| 73 // expectCUFormatsTo( | 73 // expectCUFormatsTo( |
| 74 // 'library foo;\n' | 74 // 'library foo;\n' |
| 75 // '\n' | 75 // '\n' |
| 76 // '//comment one\n\n' | 76 // '//comment one\n\n' |
| 77 // '//comment two\n\n' | 77 // '//comment two\n\n' |
| 78 // 'class C {\n}\n', | 78 // 'class C {\n}\n', |
| 79 // 'library foo;\n' | 79 // 'library foo;\n' |
| 80 // '\n' | 80 // '\n' |
| 81 // '//comment one\n\n' | 81 // '//comment one\n\n' |
| 82 // '//comment two\n\n' | 82 // '//comment two\n\n' |
| 83 // 'class C {\n}\n' | 83 // 'class C {\n}\n' |
| 84 // ); | 84 // ); |
| 85 // }); | 85 // }); |
| 86 | 86 |
| 87 test('CU - top level', () { | 87 test('CU - top level', () { |
| 88 expectCUFormatsTo( | 88 expectCUFormatsTo( |
| 89 '\n\n' | 89 '\n\n' |
| 90 'foo() {\n' | 90 'foo() {\n' |
| 91 '}\n' | 91 '}\n' |
| 92 'bar() {\n' | 92 'bar() {\n' |
| 93 '}\n', | 93 '}\n', |
| 94 '\n\n' | 94 '\n\n' |
| 95 'foo() {\n' | 95 'foo() {\n' |
| 96 '}\n' | 96 '}\n' |
| 97 'bar() {\n' | 97 'bar() {\n' |
| 98 '}\n' | 98 '}\n' |
| 99 ); | 99 ); |
| 100 expectCUFormatsTo( | 100 expectCUFormatsTo( |
| 101 'const A = 42;\n' | 101 'const A = 42;\n' |
| 102 'final foo = 32;\n', | 102 'final foo = 32;\n', |
| 103 'const A = 42;\n' | 103 'const A = 42;\n' |
| 104 'final foo = 32;\n' | 104 'final foo = 32;\n' |
| 105 ); | 105 ); |
| 106 }); | 106 }); |
| 107 | 107 |
| 108 test('CU - imports', () { | 108 test('CU - imports', () { |
| 109 expectCUFormatsTo( | 109 expectCUFormatsTo( |
| 110 'import "dart:io";\n\n' | 110 'import "dart:io";\n\n' |
| 111 'import "package:unittest/unittest.dart";\n' | 111 'import "package:unittest/unittest.dart";\n' |
| 112 'foo() {\n' | 112 'foo() {\n' |
| 113 '}\n', | 113 '}\n', |
| 114 'import "dart:io";\n\n' | 114 'import "dart:io";\n\n' |
| 115 'import "package:unittest/unittest.dart";\n' | 115 'import "package:unittest/unittest.dart";\n' |
| 116 'foo() {\n' | 116 'foo() {\n' |
| 117 '}\n' | 117 '}\n' |
| 118 ); | 118 ); |
| 119 }); | 119 }); |
| 120 | 120 |
| 121 test('CU - method invocations', () { | 121 test('CU - method invocations', () { |
| 122 expectCUFormatsTo( | 122 expectCUFormatsTo( |
| 123 'class A {\n' | 123 'class A {\n' |
| 124 ' foo() {\n' | 124 ' foo() {\n' |
| 125 ' bar();\n' | 125 ' bar();\n' |
| 126 ' for (int i = 0; i < 42; i++) {\n' | 126 ' for (int i = 0; i < 42; i++) {\n' |
| 127 ' baz();\n' | 127 ' baz();\n' |
| 128 ' }\n' | 128 ' }\n' |
| 129 ' }\n' | 129 ' }\n' |
| 130 '}\n', | 130 '}\n', |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 '}\n', | 179 '}\n', |
| 180 'class A {\n' | 180 'class A {\n' |
| 181 ' foo(files) {\n' | 181 ' foo(files) {\n' |
| 182 ' for (var file in files) {\n' | 182 ' for (var file in files) {\n' |
| 183 ' print(file);\n' | 183 ' print(file);\n' |
| 184 ' }\n' | 184 ' }\n' |
| 185 ' }\n' | 185 ' }\n' |
| 186 '}\n' | 186 '}\n' |
| 187 ); | 187 ); |
| 188 }); | 188 }); |
| 189 | 189 |
| 190 test('CU (method indent)', () { | 190 test('CU (method indent)', () { |
| 191 expectCUFormatsTo( | 191 expectCUFormatsTo( |
| 192 'class A {\n' | 192 'class A {\n' |
| 193 'void x(){\n' | 193 'void x(){\n' |
| 194 '}\n' | 194 '}\n' |
| 195 '}\n', | 195 '}\n', |
| 196 'class A {\n' | 196 'class A {\n' |
| 197 ' void x() {\n' | 197 ' void x() {\n' |
| 198 ' }\n' | 198 ' }\n' |
| 199 '}\n' | 199 '}\n' |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 ' int c() => b();\n\n' | 269 ' int c() => b();\n\n' |
| 270 '}\n', | 270 '}\n', |
| 271 'class A {\n' | 271 'class A {\n' |
| 272 '}\n\n' | 272 '}\n\n' |
| 273 'class B {\n\n\n' | 273 'class B {\n\n\n' |
| 274 ' int b() => 42;\n\n' | 274 ' int b() => 42;\n\n' |
| 275 ' int c() => b();\n\n' | 275 ' int c() => b();\n\n' |
| 276 '}\n' | 276 '}\n' |
| 277 ); | 277 ); |
| 278 }); | 278 }); |
| 279 | 279 |
| 280 test('CU - constructor', () { | 280 test('CU - constructor', () { |
| 281 expectCUFormatsTo( | 281 expectCUFormatsTo( |
| 282 'class A {\n' | 282 'class A {\n' |
| 283 ' const _a;\n' | 283 ' const _a;\n' |
| 284 ' A();\n' | 284 ' A();\n' |
| 285 ' int a() => _a;\n' | 285 ' int a() => _a;\n' |
| 286 '}\n', | 286 '}\n', |
| 287 'class A {\n' | 287 'class A {\n' |
| 288 ' const _a;\n' | 288 ' const _a;\n' |
| 289 ' A();\n' | 289 ' A();\n' |
| 290 ' int a() => _a;\n' | 290 ' int a() => _a;\n' |
| 291 '}\n' | 291 '}\n' |
| 292 ); | 292 ); |
| 293 }); | 293 }); |
| 294 | 294 |
| 295 test('CU - method decl w/ named params', () { | 295 test('CU - method decl w/ named params', () { |
| 296 expectCUFormatsTo( | 296 expectCUFormatsTo( |
| 297 'class A {\n' | 297 'class A {\n' |
| 298 ' int a(var x, {optional: null}) => null;\n' | 298 ' int a(var x, {optional: null}) => null;\n' |
| 299 '}\n', | 299 '}\n', |
| 300 'class A {\n' | 300 'class A {\n' |
| 301 ' int a(var x, {optional: null}) => null;\n' | 301 ' int a(var x, {optional: null}) => null;\n' |
| 302 '}\n' | 302 '}\n' |
| 303 ); | 303 ); |
| 304 }); | 304 }); |
| 305 | 305 |
| 306 test('CU - method decl w/ optional params', () { | 306 test('CU - method decl w/ optional params', () { |
| 307 expectCUFormatsTo( | 307 expectCUFormatsTo( |
| 308 'class A {\n' | 308 'class A {\n' |
| 309 ' int a(var x, [optional = null]) => null;\n' | 309 ' int a(var x, [optional = null]) => null;\n' |
| 310 '}\n', | 310 '}\n', |
| 311 'class A {\n' | 311 'class A {\n' |
| 312 ' int a(var x, [optional = null]) => null;\n' | 312 ' int a(var x, [optional = null]) => null;\n' |
| 313 '}\n' | 313 '}\n' |
| 314 ); | 314 ); |
| 315 }); | 315 }); |
| 316 | 316 |
| 317 test('CU - factory constructor redirects', () { | 317 test('CU - factory constructor redirects', () { |
| 318 expectCUFormatsTo( | 318 expectCUFormatsTo( |
| 319 'class A {\n' | 319 'class A {\n' |
| 320 ' const factory A() = B;\n' | 320 ' const factory A() = B;\n' |
| 321 '}\n', | 321 '}\n', |
| 322 'class A {\n' | 322 'class A {\n' |
| 323 ' const factory A() = B;\n' | 323 ' const factory A() = B;\n' |
| 324 '}\n' | 324 '}\n' |
| 325 ); | 325 ); |
| 326 }); | 326 }); |
| 327 | 327 |
| 328 test('CU - constructor initializers', () { | 328 test('CU - constructor initializers', () { |
| 329 expectCUFormatsTo( | 329 expectCUFormatsTo( |
| 330 'class A {\n' | 330 'class A {\n' |
| 331 ' int _a;\n' | 331 ' int _a;\n' |
| 332 ' A(a) : _a = a;\n' | 332 ' A(a) : _a = a;\n' |
| 333 '}\n', | 333 '}\n', |
| 334 'class A {\n' | 334 'class A {\n' |
| 335 ' int _a;\n' | 335 ' int _a;\n' |
| 336 ' A(a) : _a = a;\n' | 336 ' A(a) : _a = a;\n' |
| 337 '}\n' | 337 '}\n' |
| 338 ); | 338 ); |
| 339 }); | 339 }); |
| 340 | 340 |
| 341 test('CU - constructor auto field inits', () { |
| 342 expectCUFormatsTo( |
| 343 'class A {\n' |
| 344 ' int _a;\n' |
| 345 ' A(this._a);\n' |
| 346 '}\n', |
| 347 'class A {\n' |
| 348 ' int _a;\n' |
| 349 ' A(this._a);\n' |
| 350 '}\n' |
| 351 ); |
| 352 }); |
| 353 |
| 354 test('CU - parts', () { |
| 355 expectCUFormatsTo( |
| 356 'part of foo;', |
| 357 'part of foo;' |
| 358 ); |
| 359 }); |
| 360 |
| 341 test('stmt', () { | 361 test('stmt', () { |
| 342 expectStmtFormatsTo( | 362 expectStmtFormatsTo( |
| 343 'if (true){\n' | 363 'if (true){\n' |
| 344 'if (true){\n' | 364 'if (true){\n' |
| 345 'if (true){\n' | 365 'if (true){\n' |
| 346 'return true;\n' | 366 'return true;\n' |
| 347 '} else{\n' | 367 '} else{\n' |
| 348 'return false;\n' | 368 'return false;\n' |
| 349 '}\n' | 369 '}\n' |
| 350 '}\n' | 370 '}\n' |
| (...skipping 27 matching lines...) Expand all Loading... |
| 378 'switch (fruit) {\n' | 398 'switch (fruit) {\n' |
| 379 ' case "apple":\n' | 399 ' case "apple":\n' |
| 380 ' print("delish");\n' | 400 ' print("delish");\n' |
| 381 ' break;\n' | 401 ' break;\n' |
| 382 ' case "fig":\n' | 402 ' case "fig":\n' |
| 383 ' print("bleh");\n' | 403 ' print("bleh");\n' |
| 384 ' break;\n' | 404 ' break;\n' |
| 385 '}' | 405 '}' |
| 386 ); | 406 ); |
| 387 }); | 407 }); |
| 388 | 408 |
| 389 test('stmt (generics)', () { | 409 test('stmt (generics)', () { |
| 390 expectStmtFormatsTo( | 410 expectStmtFormatsTo( |
| 391 'var numbers = <int>[1, 2, (3 + 4)];', | 411 'var numbers = <int>[1, 2, (3 + 4)];', |
| 392 'var numbers = <int>[1, 2, (3 + 4)];' | 412 'var numbers = <int>[1, 2, (3 + 4)];' |
| 393 ); | 413 ); |
| 394 }); | 414 }); |
| 395 | 415 |
| 396 test('stmt (try/catch)', () { | 416 test('stmt (try/catch)', () { |
| 397 expectStmtFormatsTo( | 417 expectStmtFormatsTo( |
| 398 'try {\n' | 418 'try {\n' |
| 399 'doSomething();\n' | 419 'doSomething();\n' |
| 400 '} catch (e) {\n' | 420 '} catch (e) {\n' |
| 401 'print(e);\n' | 421 'print(e);\n' |
| 402 '}', | 422 '}', |
| 403 'try {\n' | 423 'try {\n' |
| 404 ' doSomething();\n' | 424 ' doSomething();\n' |
| 405 '} catch (e) {\n' | 425 '} catch (e) {\n' |
| 406 ' print(e);\n' | 426 ' print(e);\n' |
| 407 '}' | 427 '}' |
| 408 ); | 428 ); |
| 409 }); | 429 }); |
| 410 | 430 |
| 411 test('stmt (binary/ternary ops)', () { | 431 test('stmt (binary/ternary ops)', () { |
| 412 expectStmtFormatsTo( | 432 expectStmtFormatsTo( |
| 413 'var a = 1 + 2 / (3 * -b);', | 433 'var a = 1 + 2 / (3 * -b);', |
| 414 'var a = 1 + 2 / (3 * -b);' | 434 'var a = 1 + 2 / (3 * -b);' |
| 415 ); | 435 ); |
| 416 expectStmtFormatsTo( | 436 expectStmtFormatsTo( |
| 417 'var c = !condition == a > b;', | 437 'var c = !condition == a > b;', |
| 418 'var c = !condition == a > b;' | 438 'var c = !condition == a > b;' |
| 419 ); | 439 ); |
| 420 expectStmtFormatsTo( | 440 expectStmtFormatsTo( |
| 421 'var d = condition ? b : object.method(a, b, c);', | 441 'var d = condition ? b : object.method(a, b, c);', |
| 422 'var d = condition ? b : object.method(a, b, c);' | 442 'var d = condition ? b : object.method(a, b, c);' |
| 423 ); | 443 ); |
| 424 expectStmtFormatsTo( | 444 expectStmtFormatsTo( |
| 425 'var d = obj is! SomeType;', | 445 'var d = obj is! SomeType;', |
| 426 'var d = obj is! SomeType;' | 446 'var d = obj is! SomeType;' |
| 427 ); | 447 ); |
| 428 }); | 448 }); |
| 429 | 449 |
| 450 test('stmt (for in)', () { |
| 451 expectStmtFormatsTo( |
| 452 'for (Foo foo in bar.foos) {\n' |
| 453 ' print(foo);\n' |
| 454 '}', |
| 455 'for (Foo foo in bar.foos) {\n' |
| 456 ' print(foo);\n' |
| 457 '}' |
| 458 ); |
| 459 expectStmtFormatsTo( |
| 460 'for (final Foo foo in bar.foos) {\n' |
| 461 ' print(foo);\n' |
| 462 '}', |
| 463 'for (final Foo foo in bar.foos) {\n' |
| 464 ' print(foo);\n' |
| 465 '}' |
| 466 ); |
| 467 expectStmtFormatsTo( |
| 468 'for (final foo in bar.foos) {\n' |
| 469 ' print(foo);\n' |
| 470 '}', |
| 471 'for (final foo in bar.foos) {\n' |
| 472 ' print(foo);\n' |
| 473 '}' |
| 474 ); |
| 475 }); |
| 476 |
| 430 test('initialIndent', () { | 477 test('initialIndent', () { |
| 431 var formatter = new CodeFormatter( | 478 var formatter = new CodeFormatter( |
| 432 new FormatterOptions(initialIndentationLevel: 2)); | 479 new FormatterOptions(initialIndentationLevel: 2)); |
| 433 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;'); | 480 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;'); |
| 434 expect(formattedSource, startsWith(' ')); | 481 expect(formattedSource, startsWith(' ')); |
| 435 }); | 482 }); |
| 436 | 483 |
| 437 }); | 484 }); |
| 438 | 485 |
| 439 | 486 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 String formatCU(src, {options: const FormatterOptions()}) => | 609 String formatCU(src, {options: const FormatterOptions()}) => |
| 563 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 610 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 564 | 611 |
| 565 String formatStatement(src, {options: const FormatterOptions()}) => | 612 String formatStatement(src, {options: const FormatterOptions()}) => |
| 566 new CodeFormatter(options).format(CodeKind.STATEMENT, src); | 613 new CodeFormatter(options).format(CodeKind.STATEMENT, src); |
| 567 | 614 |
| 568 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); | 615 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); |
| 569 | 616 |
| 570 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), | 617 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), |
| 571 equals(expected)); | 618 equals(expected)); |
| OLD | NEW |