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

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

Issue 23480055: Formatter sanity-checking (via token stream verification). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 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
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 '}\n', 403 '}\n',
404 '/// Copyright info\n' 404 '/// Copyright info\n'
405 '\n' 405 '\n'
406 'library foo;\n' 406 'library foo;\n'
407 '/// Class comment\n' 407 '/// Class comment\n'
408 '//TODO: implement\n' 408 '//TODO: implement\n'
409 'class C {\n' 409 'class C {\n'
410 '}\n' 410 '}\n'
411 ); 411 );
412 }); 412 });
413 413
414 test('CU - mixed comments', () { 414 test('CU - mixed comments', () {
415 expectCUFormatsTo( 415 expectCUFormatsTo(
416 'library foo;\n' 416 'library foo;\n'
417 '\n' 417 '\n'
418 '\n' 418 '\n'
419 '/* Comment 1 */\n' 419 '/* Comment 1 */\n'
420 '\n' 420 '\n'
421 '// Comment 2\n' 421 '// Comment 2\n'
422 '\n' 422 '\n'
423 '/* Comment 3 */', 423 '/* Comment 3 */',
424 'library foo;\n' 424 'library foo;\n'
425 '\n' 425 '\n'
426 '\n' 426 '\n'
427 '/* Comment 1 */\n' 427 '/* Comment 1 */\n'
428 '\n' 428 '\n'
429 '// Comment 2\n' 429 '// Comment 2\n'
430 '\n' 430 '\n'
431 '/* Comment 3 */' 431 '/* Comment 3 */'
432 ); 432 );
433 }); 433 });
434 434
435 test('CU - comments (EOF)', () { 435 test('CU - comments (EOF)', () {
436 expectCUFormatsTo( 436 expectCUFormatsTo(
437 'library foo; //zamm', 437 'library foo; //zamm',
438 'library foo; //zamm\n' //<-- note extra NEWLINE 438 'library foo; //zamm\n' //<-- note extra NEWLINE
439 ); 439 );
440 }); 440 });
441 441
442 test('CU - comments (0)', () { 442 test('CU - comments (0)', () {
443 expectCUFormatsTo( 443 expectCUFormatsTo(
444 'library foo; //zamm\n' 444 'library foo; //zamm\n'
445 '\n' 445 '\n'
446 'class A {\n' 446 'class A {\n'
447 '}\n', 447 '}\n',
448 'library foo; //zamm\n' 448 'library foo; //zamm\n'
449 '\n' 449 '\n'
450 'class A {\n' 450 'class A {\n'
451 '}\n' 451 '}\n'
452 ); 452 );
453 }); 453 });
454 454
455 test('CU - comments (1)', () { 455 test('CU - comments (1)', () {
456 expectCUFormatsTo( 456 expectCUFormatsTo(
457 '/* foo */ /* bar */\n', 457 '/* foo */ /* bar */\n',
458 '/* foo */ /* bar */\n' 458 '/* foo */ /* bar */\n'
459 ); 459 );
460 }); 460 });
461 461
462 test('CU - comments (2)', () { 462 test('CU - comments (2)', () {
463 expectCUFormatsTo( 463 expectCUFormatsTo(
464 '/** foo */ /** bar */\n', 464 '/** foo */ /** bar */\n',
465 '/** foo */\n' 465 '/** foo */\n'
466 '/** bar */\n' 466 '/** bar */\n'
467 ); 467 );
468 }); 468 });
469 469
470 test('CU - comments (3)', () { 470 test('CU - comments (3)', () {
471 expectCUFormatsTo( 471 expectCUFormatsTo(
472 'var x; //x\n', 472 'var x; //x\n',
473 'var x; //x\n' 473 'var x; //x\n'
474 ); 474 );
475 }); 475 });
476 476
477 test('CU - comments (4)', () { 477 test('CU - comments (4)', () {
478 expectCUFormatsTo( 478 expectCUFormatsTo(
479 'class X { //X!\n' 479 'class X { //X!\n'
480 '}', 480 '}',
481 'class X { //X!\n' 481 'class X { //X!\n'
482 '}' 482 '}'
483 ); 483 );
484 }); 484 });
485 485
486 test('CU - comments (5)', () { 486 test('CU - comments (5)', () {
487 expectCUFormatsTo( 487 expectCUFormatsTo(
488 '//comment one\n\n' 488 '//comment one\n\n'
489 '//comment two\n\n', 489 '//comment two\n\n',
490 '//comment one\n\n' 490 '//comment one\n\n'
491 '//comment two\n\n' 491 '//comment two\n\n'
492 ); 492 );
493 }); 493 });
494 494
495 test('CU - comments (6)', () { 495 test('CU - comments (6)', () {
496 expectCUFormatsTo( 496 expectCUFormatsTo(
497 'var x; //x\n', 497 'var x; //x\n',
498 'var x; //x\n' 498 'var x; //x\n'
499 ); 499 );
500 }); 500 });
501 501
502 test('CU - comments (6)', () { 502 test('CU - comments (6)', () {
503 expectCUFormatsTo( 503 expectCUFormatsTo(
504 'var /* int */ x; //x\n', 504 'var /* int */ x; //x\n',
505 'var /* int */ x; //x\n' 505 'var /* int */ x; //x\n'
506 ); 506 );
507 }); 507 });
508 508
509 test('CU - comments (7)', () { 509 test('CU - comments (7)', () {
510 expectCUFormatsTo( 510 expectCUFormatsTo(
511 'library foo;\n' 511 'library foo;\n'
512 '\n' 512 '\n'
513 '/// Docs\n' 513 '/// Docs\n'
514 '/// spanning\n' 514 '/// spanning\n'
515 '/// lines.\n' 515 '/// lines.\n'
516 'class A {\n' 516 'class A {\n'
517 '}\n' 517 '}\n'
518 '\n' 518 '\n'
519 '/// ... and\n' 519 '/// ... and\n'
520 '\n' 520 '\n'
521 '/// Dangling ones too\n' 521 '/// Dangling ones too\n'
522 'int x;\n', 522 'int x;\n',
523 'library foo;\n' 523 'library foo;\n'
524 '\n' 524 '\n'
525 '/// Docs\n' 525 '/// Docs\n'
526 '/// spanning\n' 526 '/// spanning\n'
527 '/// lines.\n' 527 '/// lines.\n'
528 'class A {\n' 528 'class A {\n'
529 '}\n' 529 '}\n'
530 '\n' 530 '\n'
531 '/// ... and\n' 531 '/// ... and\n'
532 '\n' 532 '\n'
533 '/// Dangling ones too\n' 533 '/// Dangling ones too\n'
534 'int x;\n' 534 'int x;\n'
535 ); 535 );
536 }); 536 });
537 537
538 538
539 test('CU - constructor', () { 539 test('CU - constructor', () {
540 expectCUFormatsTo( 540 expectCUFormatsTo(
541 'class A {\n' 541 'class A {\n'
542 ' const _a;\n' 542 ' const _a;\n'
543 ' A();\n' 543 ' A();\n'
544 ' int a() => _a;\n' 544 ' int a() => _a;\n'
545 '}\n', 545 '}\n',
546 'class A {\n' 546 'class A {\n'
547 ' const _a;\n' 547 ' const _a;\n'
548 ' A();\n' 548 ' A();\n'
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 test('initialIndent', () { 743 test('initialIndent', () {
744 var formatter = new CodeFormatter( 744 var formatter = new CodeFormatter(
745 new FormatterOptions(initialIndentationLevel: 2)); 745 new FormatterOptions(initialIndentationLevel: 2));
746 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;'); 746 var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;');
747 expect(formattedSource, startsWith(' ')); 747 expect(formattedSource, startsWith(' '));
748 }); 748 });
749 749
750 }); 750 });
751 751
752 752
753 /// Token streams
754 group('token streams', () {
755
756 test('string tokens', () {
757 expectTokenizedEqual('class A{}', 'class A{ }');
Brian Wilkerson 2013/09/09 22:21:47 In all of the equal cases the strings are identica
pquitslund 2013/09/10 18:24:13 Added some. Thanks!
758 });
759
760 test('string tokens - w/ comments', () {
761 expectTokenizedEqual('//foo\nint bar;', '//foo\nint bar;');
762 expectTokenizedNotEqual('int bar;', '//foo\nint bar;');
763 expectTokenizedNotEqual('//foo\nint bar;', 'int bar;');
764 });
765
766 test('INDEX', () {
767 /// '[' ']' => '[]'
768 var t1 = openSqBracket()..setNext(closeSqBracket()..setNext(eof()));
769 var t2 = index()..setNext(eof());
770 expectStreamsEqual(t1, t2);
771 });
772
773 test('GT_GT', () {
774 /// '>' '>' => '>>'
775 var t1 = gt()..setNext(gt()..setNext(eof()));
776 var t2 = gt_gt()..setNext(eof());
777 expectStreamsEqual(t1, t2);
778 });
779
780 test('t1 < t2', () {
781 var t1 = string('foo')..setNext(eof());
782 var t2 = string('foo')..setNext(string('bar')..setNext(eof()));
783 expectStreamsNotEqual(t1, t2);
784 });
785
786 test('t1 > t2', () {
787 var t1 = string('foo')..setNext(string('bar')..setNext(eof()));
788 var t2 = string('foo')..setNext(eof());
789 expectStreamsNotEqual(t1, t2);
790 });
791
792 });
793
794
753 /// Line tests 795 /// Line tests
754 group('line', () { 796 group('line', () {
755 797
756 test('space', () { 798 test('space', () {
757 var line = new Line(indent: 0); 799 var line = new Line(indent: 0);
758 line.addSpaces(2); 800 line.addSpaces(2);
759 expect(line.toString(), equals(' ')); 801 expect(line.toString(), equals(' '));
760 }); 802 });
761 803
762 test('initial indent', () { 804 test('initial indent', () {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 test('repeat', () { 888 test('repeat', () {
847 expect(repeat('x', 0), ''); 889 expect(repeat('x', 0), '');
848 expect(repeat('x', 1), 'x'); 890 expect(repeat('x', 1), 'x');
849 expect(repeat('x', 4), 'xxxx'); 891 expect(repeat('x', 4), 'xxxx');
850 }); 892 });
851 893
852 }); 894 });
853 895
854 } 896 }
855 897
856 Token classKeyword(int offset) => 898 Token closeSqBracket() => new Token(TokenType.CLOSE_SQUARE_BRACKET, 0);
857 new KeywordToken(Keyword.CLASS, offset); 899
900 Token eof() => new Token(TokenType.EOF, 0);
901
902 Token gt() => new Token(TokenType.GT, 0);
903
904 Token gt_gt() => new Token(TokenType.GT_GT, 0);
905
906 Token index() => new Token(TokenType.INDEX, 0);
907
908 Token openSqBracket() => new BeginToken(TokenType.OPEN_SQUARE_BRACKET, 0);
909
910 Token string(String lexeme) => new StringToken(TokenType.STRING, lexeme, 0);
Brian Wilkerson 2013/09/09 22:21:47 There is a TokenFactory class in the engine tests
pquitslund 2013/09/10 18:24:13 I'll look. Thanks!
911
912 Token classKeyword(int offset) => new KeywordToken(Keyword.CLASS, offset);
858 913
859 Token identifier(String value, int offset) => 914 Token identifier(String value, int offset) =>
860 new StringToken(TokenType.IDENTIFIER, value, offset); 915 new StringToken(TokenType.IDENTIFIER, value, offset);
861 916
862 Token openParen(int offset) => 917 Token openParen(int offset) =>
863 new StringToken(TokenType.OPEN_PAREN, '{', offset); 918 new StringToken(TokenType.OPEN_PAREN, '{', offset);
864 919
865 Token closeParen(int offset) => 920 Token closeParen(int offset) =>
866 new StringToken(TokenType.CLOSE_PAREN, '}', offset); 921 new StringToken(TokenType.CLOSE_PAREN, '}', offset);
867 922
868 Token chain(List<Token> tokens) { 923 Token chain(List<Token> tokens) {
869 for (var i = 0; i < tokens.length - 1; ++i) { 924 for (var i = 0; i < tokens.length - 1; ++i) {
870 tokens[i].setNext(tokens[i + 1]); 925 tokens[i].setNext(tokens[i + 1]);
871 } 926 }
872 return tokens[0]; 927 return tokens[0];
873 } 928 }
874 929
875 String formatCU(src, {options: const FormatterOptions()}) => 930 String formatCU(src, {options: const FormatterOptions()}) =>
876 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); 931 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src);
877 932
878 String formatStatement(src, {options: const FormatterOptions()}) => 933 String formatStatement(src, {options: const FormatterOptions()}) =>
879 new CodeFormatter(options).format(CodeKind.STATEMENT, src); 934 new CodeFormatter(options).format(CodeKind.STATEMENT, src);
880 935
936 Token tokenize(String str) => new StringScanner(null, str, null).tokenize();
937
938
939 expectTokenizedEqual(String s1, String s2) =>
940 expectStreamsEqual(tokenize(s1), tokenize(s2));
941
942 expectTokenizedNotEqual(String s1, String s2) =>
943 expect(()=> expectStreamsEqual(tokenize(s1), tokenize(s2)),
944 throwsA(new isInstanceOf<FormatterException>()));
945
946 expectStreamsEqual(Token t1, Token t2) =>
947 new TokenStreamComparator(null, t1, t2).verifyEquals();
948
949 expectStreamsNotEqual(Token t1, Token t2) =>
950 expect(() => new TokenStreamComparator(null, t1, t2).verifyEquals(),
951 throwsA(new isInstanceOf<FormatterException>()));
952
881 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); 953 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected));
882 954
883 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), 955 expectStmtFormatsTo(src, expected) => expect(formatStatement(src),
884 equals(expected)); 956 equals(expected));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698