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

Unified Diff: pkg/analyzer_experimental/test/services/formatter_test.dart

Issue 18346013: Formatter re-think/updates. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer_experimental/test/services/formatter_test.dart
===================================================================
--- pkg/analyzer_experimental/test/services/formatter_test.dart (revision 24755)
+++ pkg/analyzer_experimental/test/services/formatter_test.dart (working copy)
@@ -8,133 +8,10 @@
import 'package:analyzer_experimental/src/generated/scanner.dart';
import 'package:analyzer_experimental/src/services/formatter.dart';
import 'package:analyzer_experimental/src/services/formatter_impl.dart';
+import 'package:analyzer_experimental/src/services/writer.dart';
main() {
- /// Edit recorder tests
- group('edit recorder', () {
-
- test('countWhitespace', (){
- expect(newRecorder(' ').countWhitespace(), equals(3));
- expect(newRecorder('').countWhitespace(), equals(0));
- expect(newRecorder(' foo').countWhitespace(), equals(2));
- });
-
- test('isNewlineAt', (){
- expect(newRecorder('012\n').isNewlineAt(3), isTrue);
- expect(newRecorder('012\n3456').isNewlineAt(3), isTrue);
- expect(newRecorder('\n').isNewlineAt(0), isTrue);
- });
-
- test('space advances 1', (){
- var recorder = newRecorder(' foo');
- var startColumn = recorder.column;
- recorder.space();
- expect(recorder.column, equals(startColumn + 1));
- });
-
- test('space eats WS (1)', (){
- var recorder = newRecorder(' class')
- ..currentToken = new KeywordToken(Keyword.CLASS, 3)
- ..space()
- ..advanceToken('class');
- expect(doFormat(recorder), equals(' class'));
- });
-
- test('space eats WS (2)', (){
- var src = 'class A';
- var recorder = newRecorder(src);
- recorder..currentToken = (classKeyword(0)..setNext(identifier('A', 7)))
- ..advanceToken('class')
- ..space()
- ..advanceToken('A');
-
- expect(doFormat(recorder), equals('class A'));
- });
-
- test('advance string token', (){
- var recorder = newRecorder('class A')..currentToken = classKeyword(0);
- expect(recorder.column, equals(0));
- recorder.advanceToken('class');
- expect(recorder.column, equals(5));
- });
-
- test('advance string token (failure)', (){
- var recorder = newRecorder('class A')..currentToken = classKeyword(0);
- expect(() => recorder.advanceToken('static'),
- throwsA(new isInstanceOf<FormatterException>()));
- });
-
- test('advance indent', (){
- var recorder = newRecorder(' class A')..currentToken = classKeyword(0);
- recorder.advanceIndent();
- expect(doFormat(recorder), equals('class A'));
- });
-
- test('indent string', (){
- var recorder = newRecorder('');
- expect(recorder.getIndentString(0).length, equals(0));
- expect(recorder.getIndentString(5).length, equals(5));
- expect(recorder.getIndentString(50).length, equals(50));
- });
-
-
- test('newline', (){
- var recorder = newRecorder('class A { }');
- recorder..currentToken = chain([classKeyword(0),
- identifier('A', 6),
- openParen(8),
- closeParen(10)])
- ..advanceToken('class')
- ..space()
- ..advanceToken('A')
- ..space()
- ..advanceToken('{')
- ..newline();
-
- expect(doFormat(recorder)[9], equals(NEW_LINE));
- });
-
- test('newline eats trailing WS', (){
- var src = 'class A {';
- var recorder = newRecorder(src + ' ');
- recorder..currentToken = chain([classKeyword(0),
- identifier('A', 6),
- openParen(8)])
- ..advanceToken('class')
- ..space()
- ..advanceToken('A')
- ..space()
- ..advanceToken('{')
- ..newline();
-
- expect(doFormat(recorder).length, equals((src + NEW_LINE).length));
- });
-
-
- });
-
-
- /// Edit operations
- group('edit operations', () {
-
- test('replace same length', () {
- var edits = [new Edit(1, 2, 'oo'),
- new Edit(4, 5, 'bar')];
- expect(new EditOperation().apply(edits, 'fun house'), equals('foo bar'));
- });
-
- test('replace shorten', () {
- var edits = [new Edit(0, 2, 'a'),
- new Edit(2, 2, 'b'),
- new Edit(4, 2, 'c')];
- expect(new EditOperation().apply(edits, 'AaBbCc'), equals('abc'));
- });
-
-
- });
-
-
/// Formatter tests
group('formatter', () {
@@ -192,30 +69,151 @@
'}'
);
});
+//
+// test('CU (method indent - 2)', () {
+// expectCUFormatsTo(
+// 'class A {\n'
+// ' static bool x(){ return true; }\n'
+// ' }',
+// 'class A {\n'
+// ' static bool x() {\n'
+// ' return true;\n'
+// ' }\n'
+// '}'
+// );
+// });
+//
+// test('CU (method indent - 3)', () {
+// expectCUFormatsTo(
+// 'class A {\n'
+// ' int x() => 42 + 3 ; \n'
+// ' }',
+// 'class A {\n'
+// ' int x() => 42 + 3;\n'
+// '}'
+// );
+// });
+//
+// test('CU (method indent - 4)', () {
+// expectCUFormatsTo(
+// 'class A {\n'
+// ' int x() { \n'
+// 'if (true) {return 42;\n'
+// '} else { return false; }\n'
+// ' }'
+// '}',
+// 'class A {\n'
+// ' int x() {\n'
+// ' if (true) {\n'
+// ' return 42;\n'
+// ' } else {\n'
+// ' return false;\n'
+// ' }\n'
+// '}'
+// );
+// });
- test('CU (method indent - 2)', () {
- expectCUFormatsTo(
- 'class A {\n'
- ' static void x(){}\n'
- ' }',
- 'class A {\n'
- ' static void x() {\n'
- ' }\n'
- '}'
- );
- });
-
-
// test('initialIndent', () {
// var formatter = new CodeFormatter(
-// new FormatterOptions(initialIndentationLevel:2));
+// new FormatterOptions(initialIndentationLevel: 2));
// var formattedSource = formatter.format(CodeKind.STATEMENT, 'var x;');
// expect(formattedSource, startsWith(' '));
// });
});
+
+ /// Line tests
+ group('line', () {
+
+ test('space', () {
+ var line = new Line(indent: 0);
+ line.addSpaces(2);
+ expect(line.toString(), equals(' '));
+ });
+
+ test('initial indent', () {
+ var line = new Line(indent: 2);
+ expect(line.toString(), equals(' '));
+ });
+
+ test('initial indent (tabbed)', () {
+ var line = new Line(indent:1, useTabs: true);
+ expect(line.toString(), equals('\t'));
+ });
+
+ test('addToken', () {
+ var line = new Line();
+ line.addToken(new LineToken('foo'));
+ expect(line.toString(), equals('foo'));
+ });
+
+ test('addToken (2)', () {
+ var line = new Line(indent: 1);
+ line.addToken(new LineToken('foo'));
+ expect(line.toString(), equals(' foo'));
+ });
+
+ });
+
+
+ /// Writer tests
+ group('writer', () {
+
+ test('basic print', () {
+ var writer = new SourceWriter();
+ writer.print('foo');
+ writer.print(' ');
+ writer.print('bar');
+ expect(writer.toString(), equals('foo bar'));
+ });
+
+ test('newline', () {
+ var writer = new SourceWriter();
+ writer.print('foo');
+ writer.newline();
+ expect(writer.toString(), equals('foo\n'));
+ });
+
+ test('basic print (with indents)', () {
+ var writer = new SourceWriter();
+ writer.print('foo');
+ writer.indent();
+ writer.newline();
+ writer.print('bar');
+ writer.unindent();
+ writer.newline();
+ writer.print('baz');
+ expect(writer.toString(), equals('foo\n bar\nbaz'));
+ });
+
+ });
+
+
+ /// Helper method tests
+ group('helpers', () {
+
+ test('indentString', () {
+ expect(getIndentString(0), '');
+ expect(getIndentString(1), ' ');
+ expect(getIndentString(4), ' ');
+ });
+
+ test('indentString (tabbed)', () {
+ expect(getIndentString(0, useTabs: true), '');
+ expect(getIndentString(1, useTabs: true), '\t');
+ expect(getIndentString(3, useTabs: true), '\t\t\t');
+ });
+
+ test('repeat', () {
+ expect(repeat('x', 0), '');
+ expect(repeat('x', 1), 'x');
+ expect(repeat('x', 4), 'xxxx');
+ });
+
+ });
+
}
Token classKeyword(int offset) =>
@@ -237,12 +235,6 @@
return tokens[0];
}
-EditRecorder newRecorder(source) =>
- new EditRecorder(new FormatterOptions())..source = source;
-
-String doFormat(recorder) =>
- new EditOperation().apply(recorder.editStore.edits, recorder.source);
-
String formatCU(src, {options: const FormatterOptions()}) =>
new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src);

Powered by Google App Engine
This is Rietveld 408576698