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

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

Issue 24371003: Formatter selection preservation support. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analyzer_experimental/lib/src/services/formatter_impl.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 754 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 }); 765 });
766 766
767 test('initialIndent', () { 767 test('initialIndent', () {
768 var formatter = new CodeFormatter( 768 var formatter = new CodeFormatter(
769 new FormatterOptions(initialIndentationLevel: 2)); 769 new FormatterOptions(initialIndentationLevel: 2));
770 var formattedSource = 770 var formattedSource =
771 formatter.format(CodeKind.STATEMENT, 'var x;').source; 771 formatter.format(CodeKind.STATEMENT, 'var x;').source;
772 expect(formattedSource, startsWith(' ')); 772 expect(formattedSource, startsWith(' '));
773 }); 773 });
774 774
775 test('selections', () {
776 expectSelectedPostFormat('class X {}', '}');
777 expectSelectedPostFormat('class X{}', '{');
778 expectSelectedPostFormat('class X{int y;}', ';');
779 expectSelectedPostFormat('class X{int y;}', '}');
780 expectSelectedPostFormat('class X {}', ' {');
781 });
782
775 }); 783 });
776 784
777 785
778 /// Token streams 786 /// Token streams
779 group('token streams', () { 787 group('token streams', () {
780 788
781 test('string tokens', () { 789 test('string tokens', () {
782 expectTokenizedEqual('class A{}', 'class A{ }'); 790 expectTokenizedEqual('class A{}', 'class A{ }');
783 expectTokenizedEqual('class A{}', 'class A{\n }\n'); 791 expectTokenizedEqual('class A{}', 'class A{\n }\n');
784 expectTokenizedEqual('class A {}', 'class A{ }'); 792 expectTokenizedEqual('class A {}', 'class A{ }');
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 Token closeParen(int offset) => 956 Token closeParen(int offset) =>
949 new StringToken(TokenType.CLOSE_PAREN, '}', offset); 957 new StringToken(TokenType.CLOSE_PAREN, '}', offset);
950 958
951 Token chain(List<Token> tokens) { 959 Token chain(List<Token> tokens) {
952 for (var i = 0; i < tokens.length - 1; ++i) { 960 for (var i = 0; i < tokens.length - 1; ++i) {
953 tokens[i].setNext(tokens[i + 1]); 961 tokens[i].setNext(tokens[i + 1]);
954 } 962 }
955 return tokens[0]; 963 return tokens[0];
956 } 964 }
957 965
958 String formatCU(src, {options: const FormatterOptions()}) => 966 FormattedSource formatCU(src, {options: const FormatterOptions(), selection}) =>
959 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src).source; 967 new CodeFormatter(options).format(
968 CodeKind.COMPILATION_UNIT, src, selection: selection);
960 969
961 String formatStatement(src, {options: const FormatterOptions()}) => 970 String formatStatement(src, {options: const FormatterOptions()}) =>
962 new CodeFormatter(options).format(CodeKind.STATEMENT, src).source; 971 new CodeFormatter(options).format(CodeKind.STATEMENT, src).source;
963 972
964 Token tokenize(String str) => new StringScanner(null, str, null).tokenize(); 973 Token tokenize(String str) => new StringScanner(null, str, null).tokenize();
965 974
975 expectSelectedPostFormat(src, token) {
976 var preOffset = src.indexOf(token);
977 var length = token.length;
978 var formatted = formatCU(src, selection: new Selection(preOffset, length));
979 var postOffset = formatted.selection.offset;
980 expect(formatted.source.substring(postOffset, postOffset + length),
981 equals(src.substring(preOffset, preOffset + length)));
982 }
966 983
967 expectTokenizedEqual(String s1, String s2) => 984 expectTokenizedEqual(String s1, String s2) =>
968 expectStreamsEqual(tokenize(s1), tokenize(s2)); 985 expectStreamsEqual(tokenize(s1), tokenize(s2));
969 986
970 expectTokenizedNotEqual(String s1, String s2) => 987 expectTokenizedNotEqual(String s1, String s2) =>
971 expect(()=> expectStreamsEqual(tokenize(s1), tokenize(s2)), 988 expect(()=> expectStreamsEqual(tokenize(s1), tokenize(s2)),
972 throwsA(new isInstanceOf<FormatterException>())); 989 throwsA(new isInstanceOf<FormatterException>()));
973 990
974 expectStreamsEqual(Token t1, Token t2) => 991 expectStreamsEqual(Token t1, Token t2) =>
975 new TokenStreamComparator(null, t1, t2).verifyEquals(); 992 new TokenStreamComparator(null, t1, t2).verifyEquals();
976 993
977 expectStreamsNotEqual(Token t1, Token t2) => 994 expectStreamsNotEqual(Token t1, Token t2) =>
978 expect(() => new TokenStreamComparator(null, t1, t2).verifyEquals(), 995 expect(() => new TokenStreamComparator(null, t1, t2).verifyEquals(),
979 throwsA(new isInstanceOf<FormatterException>())); 996 throwsA(new isInstanceOf<FormatterException>()));
980 997
981 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); 998 expectCUFormatsTo(src, expected) =>
999 expect(formatCU(src).source, equals(expected));
982 1000
983 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), 1001 expectStmtFormatsTo(src, expected) =>
984 equals(expected)); 1002 expect(formatStatement(src), equals(expected));
OLDNEW
« no previous file with comments | « pkg/analyzer_experimental/lib/src/services/formatter_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698