| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart_style.test.formatter_test; | 5 library dart_style.test.formatter_test; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
| 10 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| 11 | 11 |
| 12 import 'package:dart_style/dart_style.dart'; | 12 import 'package:dart_style/dart_style.dart'; |
| 13 | 13 |
| 14 void main() { | 14 void main() { |
| 15 testDirectory("comments"); | 15 testDirectory("comments"); |
| 16 testDirectory("regression"); | 16 testDirectory("regression"); |
| 17 testDirectory("selections"); | 17 testDirectory("selections"); |
| 18 testDirectory("splitting"); | 18 testDirectory("splitting"); |
| 19 testDirectory("whitespace"); | 19 testDirectory("whitespace"); |
| 20 | 20 |
| 21 test("throws a FormatterException on failed parse", () { | 21 test("throws a FormatterException on failed parse", () { |
| 22 var formatter = new DartFormatter(); | 22 var formatter = new DartFormatter(); |
| 23 expect(() => formatter.format('wat?!'), | 23 expect(() => formatter.format('wat?!'), |
| 24 throwsA(new isInstanceOf<FormatterException>())); | 24 throwsA(new isInstanceOf<FormatterException>())); |
| 25 }); | 25 }); |
| 26 | 26 |
| 27 test("FormatterException describes parse errors", () { | 27 test("FormatterException describes parse errors", () { |
| 28 try { | 28 try { |
| 29 new DartFormatter().format(""" | 29 new DartFormatter().format(""" |
| 30 | 30 |
| 31 var a = some error; | 31 var a = some error; |
| 32 | 32 |
| 33 var b = another one; | 33 var b = another one; |
| 34 """, uri: "my_file.dart"); | 34 """, uri: "my_file.dart"); |
| 35 | 35 |
| 36 fail("Should throw."); | 36 fail("Should throw."); |
| 37 } on FormatterException catch (err) { | 37 } on FormatterException catch (err) { |
| 38 var message = err.message(); | 38 var message = err.message(); |
| 39 expect(message, contains("my_file.dart")); | 39 expect(message, contains("my_file.dart")); |
| 40 expect(message, contains("line 2")); | 40 expect(message, contains("line 2")); |
| 41 expect(message, contains("line 4")); | 41 expect(message, contains("line 4")); |
| 42 } | 42 } |
| 43 }); | 43 }); |
| 44 | 44 |
| 45 test("adds newline to unit", () { | 45 test("adds newline to unit", () { |
| 46 expect(new DartFormatter().format("var x = 1;"), | 46 expect(new DartFormatter().format("var x = 1;"), equals("var x = 1;\n")); |
| 47 equals("var x = 1;\n")); | |
| 48 }); | 47 }); |
| 49 | 48 |
| 50 test("adds newline to unit after trailing comment", () { | 49 test("adds newline to unit after trailing comment", () { |
| 51 expect(new DartFormatter().format("library foo; //zamm"), | 50 expect(new DartFormatter().format("library foo; //zamm"), |
| 52 equals("library foo; //zamm\n")); | 51 equals("library foo; //zamm\n")); |
| 53 }); | 52 }); |
| 54 | 53 |
| 55 test("removes extra newlines", () { | 54 test("removes extra newlines", () { |
| 56 expect(new DartFormatter().format("var x = 1;\n\n\n"), | 55 expect( |
| 57 equals("var x = 1;\n")); | 56 new DartFormatter().format("var x = 1;\n\n\n"), equals("var x = 1;\n")); |
| 58 }); | 57 }); |
| 59 | 58 |
| 60 test("does not add newline to statement", () { | 59 test("does not add newline to statement", () { |
| 61 expect(new DartFormatter().formatStatement("var x = 1;"), | 60 expect(new DartFormatter().formatStatement("var x = 1;"), |
| 62 equals("var x = 1;")); | 61 equals("var x = 1;")); |
| 63 }); | 62 }); |
| 64 | 63 |
| 65 test('preserves initial indent', () { | 64 test('preserves initial indent', () { |
| 66 var formatter = new DartFormatter(indent: 3); | 65 var formatter = new DartFormatter(indent: 3); |
| 67 expect(formatter.formatStatement('if (foo) {bar;}'), equals( | 66 expect( |
| 68 ' if (foo) {\n' | 67 formatter.formatStatement('if (foo) {bar;}'), equals(' if (foo) {\n' |
| 69 ' bar;\n' | 68 ' bar;\n' |
| 70 ' }')); | 69 ' }')); |
| 71 }); | 70 }); |
| 72 | 71 |
| 73 group('line endings', () { | 72 group('line endings', () { |
| 74 test('uses given line ending', () { | 73 test('uses given line ending', () { |
| 75 expect(new DartFormatter(lineEnding: "%").format("var i = 1;"), | 74 expect(new DartFormatter(lineEnding: "%").format("var i = 1;"), |
| 76 equals("var i = 1;%")); | 75 equals("var i = 1;%")); |
| 77 }); | 76 }); |
| 78 | 77 |
| 79 test('infers \\r\\n if the first newline uses that', () { | 78 test('infers \\r\\n if the first newline uses that', () { |
| 80 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"), | 79 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"), |
| 81 equals("var i = 1;\r\n")); | 80 equals("var i = 1;\r\n")); |
| 82 }); | 81 }); |
| 83 | 82 |
| 84 test('infers \\n if the first newline uses that', () { | 83 test('infers \\n if the first newline uses that', () { |
| 85 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"), | 84 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"), |
| 86 equals("var i = 1;\n")); | 85 equals("var i = 1;\n")); |
| 87 }); | 86 }); |
| 88 | 87 |
| 89 test('defaults to \\n if there are no newlines', () { | 88 test('defaults to \\n if there are no newlines', () { |
| 90 expect(new DartFormatter().format("var i =1;"), | 89 expect(new DartFormatter().format("var i =1;"), equals("var i = 1;\n")); |
| 91 equals("var i = 1;\n")); | |
| 92 }); | 90 }); |
| 93 | 91 |
| 94 test('handles Windows line endings in multiline strings', () { | 92 test('handles Windows line endings in multiline strings', () { |
| 95 expect(new DartFormatter(lineEnding: "\r\n").formatStatement( | 93 expect( |
| 96 ' """first\r\n' | 94 new DartFormatter(lineEnding: "\r\n").formatStatement(' """first\r\n' |
| 97 'second\r\n' | 95 'second\r\n' |
| 98 'third""" ;'), equals( | 96 'third""" ;'), equals('"""first\r\n' |
| 99 '"""first\r\n' | 97 'second\r\n' |
| 100 'second\r\n' | 98 'third""";')); |
| 101 'third""";')); | |
| 102 }); | 99 }); |
| 103 }); | 100 }); |
| 104 } | 101 } |
| 105 | 102 |
| 106 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. | 103 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. |
| 107 void testDirectory(String name) { | 104 void testDirectory(String name) { |
| 108 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*"); | 105 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*"); |
| 109 | 106 |
| 110 var dir = p.join('test', name); | 107 var dir = p.join('test', name); |
| 111 for (var entry in new Directory(dir).listSync()) { | 108 for (var entry in new Directory(dir).listSync()) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 145 } |
| 149 | 146 |
| 150 var expectedOutput = ""; | 147 var expectedOutput = ""; |
| 151 while (++i < lines.length && !lines[i].startsWith(">>>")) { | 148 while (++i < lines.length && !lines[i].startsWith(">>>")) { |
| 152 expectedOutput += lines[i] + "\n"; | 149 expectedOutput += lines[i] + "\n"; |
| 153 } | 150 } |
| 154 | 151 |
| 155 test(description, () { | 152 test(description, () { |
| 156 var isCompilationUnit = p.extension(entry.path) == ".unit"; | 153 var isCompilationUnit = p.extension(entry.path) == ".unit"; |
| 157 | 154 |
| 158 var inputCode = _extractSelection(input, | 155 var inputCode = |
| 159 isCompilationUnit: isCompilationUnit); | 156 _extractSelection(input, isCompilationUnit: isCompilationUnit); |
| 160 | 157 |
| 161 var expected = _extractSelection(expectedOutput, | 158 var expected = _extractSelection(expectedOutput, |
| 162 isCompilationUnit: isCompilationUnit); | 159 isCompilationUnit: isCompilationUnit); |
| 163 | 160 |
| 164 var formatter = new DartFormatter( | 161 var formatter = |
| 165 pageWidth: pageWidth, indent: leadingIndent); | 162 new DartFormatter(pageWidth: pageWidth, indent: leadingIndent); |
| 166 | 163 |
| 167 var actual = formatter.formatSource(inputCode); | 164 var actual = formatter.formatSource(inputCode); |
| 168 | 165 |
| 169 // The test files always put a newline at the end of the expectation. | 166 // The test files always put a newline at the end of the expectation. |
| 170 // Statements from the formatter (correctly) don't have that, so add | 167 // Statements from the formatter (correctly) don't have that, so add |
| 171 // one to line up with the expected result. | 168 // one to line up with the expected result. |
| 172 var actualText = actual.text; | 169 var actualText = actual.text; |
| 173 if (!isCompilationUnit) actualText += "\n"; | 170 if (!isCompilationUnit) actualText += "\n"; |
| 174 | 171 |
| 175 expect(actualText, equals(expected.text)); | 172 expect(actualText, equals(expected.text)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 189 source = source.replaceAll("‹", ""); | 186 source = source.replaceAll("‹", ""); |
| 190 | 187 |
| 191 var end = source.indexOf("›"); | 188 var end = source.indexOf("›"); |
| 192 source = source.replaceAll("›", ""); | 189 source = source.replaceAll("›", ""); |
| 193 | 190 |
| 194 return new SourceCode(source, | 191 return new SourceCode(source, |
| 195 isCompilationUnit: isCompilationUnit, | 192 isCompilationUnit: isCompilationUnit, |
| 196 selectionStart: start == -1 ? null : start, | 193 selectionStart: start == -1 ? null : start, |
| 197 selectionLength: end == -1 ? null : end - start); | 194 selectionLength: end == -1 ? null : end - start); |
| 198 } | 195 } |
| OLD | NEW |