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 @TestOn("vm") | 5 @TestOn("vm") |
6 library dart_style.test.formatter_test; | 6 library dart_style.test.formatter_test; |
7 | 7 |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 try { | 32 try { |
33 new DartFormatter().format("library"); | 33 new DartFormatter().format("library"); |
34 } on FormatterException catch (err) { | 34 } on FormatterException catch (err) { |
35 var message = err.message(); | 35 var message = err.message(); |
36 expect(message, contains("Could not format")); | 36 expect(message, contains("Could not format")); |
37 } | 37 } |
38 }); | 38 }); |
39 | 39 |
40 test("FormatterException describes parse errors", () { | 40 test("FormatterException describes parse errors", () { |
41 try { | 41 try { |
42 new DartFormatter().format(""" | 42 new DartFormatter().format( |
| 43 """ |
43 | 44 |
44 var a = some error; | 45 var a = some error; |
45 | 46 |
46 var b = another one; | 47 var b = another one; |
47 """, uri: "my_file.dart"); | 48 """, |
| 49 uri: "my_file.dart"); |
48 | 50 |
49 fail("Should throw."); | 51 fail("Should throw."); |
50 } on FormatterException catch (err) { | 52 } on FormatterException catch (err) { |
51 var message = err.message(); | 53 var message = err.message(); |
52 expect(message, contains("my_file.dart")); | 54 expect(message, contains("my_file.dart")); |
53 expect(message, contains("line 2")); | 55 expect(message, contains("line 2")); |
54 expect(message, contains("line 4")); | 56 expect(message, contains("line 4")); |
55 } | 57 } |
56 }); | 58 }); |
57 | 59 |
(...skipping 25 matching lines...) Expand all Loading... |
83 expect(err, new isInstanceOf<FormatterException>()); | 85 expect(err, new isInstanceOf<FormatterException>()); |
84 var message = err.message(); | 86 var message = err.message(); |
85 expect(message, contains("Unexpected token")); | 87 expect(message, contains("Unexpected token")); |
86 expect(message, contains("column 11")); | 88 expect(message, contains("column 11")); |
87 } | 89 } |
88 }); | 90 }); |
89 | 91 |
90 test('preserves initial indent', () { | 92 test('preserves initial indent', () { |
91 var formatter = new DartFormatter(indent: 3); | 93 var formatter = new DartFormatter(indent: 3); |
92 expect( | 94 expect( |
93 formatter.formatStatement('if (foo) {bar;}'), equals(' if (foo) {\n' | 95 formatter.formatStatement('if (foo) {bar;}'), |
| 96 equals(' if (foo) {\n' |
94 ' bar;\n' | 97 ' bar;\n' |
95 ' }')); | 98 ' }')); |
96 }); | 99 }); |
97 | 100 |
98 group('line endings', () { | 101 group('line endings', () { |
99 test('uses given line ending', () { | 102 test('uses given line ending', () { |
100 expect(new DartFormatter(lineEnding: "%").format("var i = 1;"), | 103 expect(new DartFormatter(lineEnding: "%").format("var i = 1;"), |
101 equals("var i = 1;%")); | 104 equals("var i = 1;%")); |
102 }); | 105 }); |
103 | 106 |
104 test('infers \\r\\n if the first newline uses that', () { | 107 test('infers \\r\\n if the first newline uses that', () { |
105 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"), | 108 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"), |
106 equals("var i = 1;\r\n")); | 109 equals("var i = 1;\r\n")); |
107 }); | 110 }); |
108 | 111 |
109 test('infers \\n if the first newline uses that', () { | 112 test('infers \\n if the first newline uses that', () { |
110 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"), | 113 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"), |
111 equals("var i = 1;\n")); | 114 equals("var i = 1;\n")); |
112 }); | 115 }); |
113 | 116 |
114 test('defaults to \\n if there are no newlines', () { | 117 test('defaults to \\n if there are no newlines', () { |
115 expect(new DartFormatter().format("var i =1;"), equals("var i = 1;\n")); | 118 expect(new DartFormatter().format("var i =1;"), equals("var i = 1;\n")); |
116 }); | 119 }); |
117 | 120 |
118 test('handles Windows line endings in multiline strings', () { | 121 test('handles Windows line endings in multiline strings', () { |
119 expect( | 122 expect( |
120 new DartFormatter(lineEnding: "\r\n").formatStatement(' """first\r\n' | 123 new DartFormatter(lineEnding: "\r\n").formatStatement(' """first\r\n' |
121 'second\r\n' | 124 'second\r\n' |
122 'third""" ;'), equals('"""first\r\n' | 125 'third""" ;'), |
| 126 equals('"""first\r\n' |
123 'second\r\n' | 127 'second\r\n' |
124 'third""";')); | 128 'third""";')); |
125 }); | 129 }); |
126 }); | 130 }); |
127 } | 131 } |
128 | 132 |
129 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. | 133 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. |
130 void testDirectory(String name) { | 134 void testDirectory(String name) { |
131 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*"); | 135 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*"); |
132 | 136 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 source = source.replaceAll("‹", ""); | 224 source = source.replaceAll("‹", ""); |
221 | 225 |
222 var end = source.indexOf("›"); | 226 var end = source.indexOf("›"); |
223 source = source.replaceAll("›", ""); | 227 source = source.replaceAll("›", ""); |
224 | 228 |
225 return new SourceCode(source, | 229 return new SourceCode(source, |
226 isCompilationUnit: isCompilationUnit, | 230 isCompilationUnit: isCompilationUnit, |
227 selectionStart: start == -1 ? null : start, | 231 selectionStart: start == -1 ? null : start, |
228 selectionLength: end == -1 ? null : end - start); | 232 selectionLength: end == -1 ? null : end - start); |
229 } | 233 } |
OLD | NEW |