| 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:unittest/compact_vm_config.dart'; | 10 import 'package:unittest/compact_vm_config.dart'; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 93 |
| 94 test('defaults to \\n if there are no newlines', () { | 94 test('defaults to \\n if there are no newlines', () { |
| 95 expect(new DartFormatter().format("var i =1;"), | 95 expect(new DartFormatter().format("var i =1;"), |
| 96 equals("var i = 1;\n")); | 96 equals("var i = 1;\n")); |
| 97 }); | 97 }); |
| 98 }); | 98 }); |
| 99 } | 99 } |
| 100 | 100 |
| 101 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. | 101 /// Run tests defined in "*.unit" and "*.stmt" files inside directory [name]. |
| 102 void testDirectory(String name) { | 102 void testDirectory(String name) { |
| 103 var indentPattern = new RegExp(r"^\(indent (\d+)\)\s*"); |
| 104 |
| 103 var dir = p.join(p.dirname(p.fromUri(Platform.script)), name); | 105 var dir = p.join(p.dirname(p.fromUri(Platform.script)), name); |
| 104 for (var entry in new Directory(dir).listSync()) { | 106 for (var entry in new Directory(dir).listSync()) { |
| 105 if (!entry.path.endsWith(".stmt") && !entry.path.endsWith(".unit")) { | 107 if (!entry.path.endsWith(".stmt") && !entry.path.endsWith(".unit")) { |
| 106 continue; | 108 continue; |
| 107 } | 109 } |
| 108 | 110 |
| 109 group("$name ${p.basename(entry.path)}", () { | 111 group("$name ${p.basename(entry.path)}", () { |
| 110 var lines = (entry as File).readAsLinesSync(); | 112 var lines = (entry as File).readAsLinesSync(); |
| 111 | 113 |
| 112 // The first line may have a "|" to indicate the page width. | 114 // The first line may have a "|" to indicate the page width. |
| 113 var pageWidth; | 115 var pageWidth; |
| 114 if (lines[0].endsWith("|")) { | 116 if (lines[0].endsWith("|")) { |
| 115 pageWidth = lines[0].indexOf("|"); | 117 pageWidth = lines[0].indexOf("|"); |
| 116 lines = lines.skip(1).toList(); | 118 lines = lines.skip(1).toList(); |
| 117 } | 119 } |
| 118 | 120 |
| 119 var i = 0; | 121 var i = 0; |
| 120 while (i < lines.length) { | 122 while (i < lines.length) { |
| 121 var description = lines[i++].replaceAll(">>>", "").trim(); | 123 var description = lines[i++].replaceAll(">>>", "").trim(); |
| 124 |
| 125 // Let the test specify a leading indentation. This is handy for |
| 126 // regression tests which often come from a chunk of nested code. |
| 127 var leadingIndent = 0; |
| 128 var indentMatch = indentPattern.firstMatch(description); |
| 129 if (indentMatch != null) { |
| 130 // The test specifies it in spaces, but the formatter expects levels. |
| 131 leadingIndent = int.parse(indentMatch[1]) ~/ 2; |
| 132 description = description.substring(indentMatch.end); |
| 133 } |
| 134 |
| 122 if (description == "") { | 135 if (description == "") { |
| 123 description = "line ${i + 1}"; | 136 description = "line ${i + 1}"; |
| 124 } else { | 137 } else { |
| 125 description = "line ${i + 1}: $description"; | 138 description = "line ${i + 1}: $description"; |
| 126 } | 139 } |
| 127 | 140 |
| 128 var input = ""; | 141 var input = ""; |
| 129 while (!lines[i].startsWith("<<<")) { | 142 while (!lines[i].startsWith("<<<")) { |
| 130 input += lines[i++] + "\n"; | 143 input += lines[i++] + "\n"; |
| 131 } | 144 } |
| 132 | 145 |
| 133 var expectedOutput = ""; | 146 var expectedOutput = ""; |
| 134 while (++i < lines.length && !lines[i].startsWith(">>>")) { | 147 while (++i < lines.length && !lines[i].startsWith(">>>")) { |
| 135 expectedOutput += lines[i] + "\n"; | 148 expectedOutput += lines[i] + "\n"; |
| 136 } | 149 } |
| 137 | 150 |
| 138 if (description.contains("SKIP")) { | |
| 139 print("SKIP: $description"); | |
| 140 continue; | |
| 141 } | |
| 142 | |
| 143 test(description, () { | 151 test(description, () { |
| 144 var formatter = new DartFormatter(pageWidth: pageWidth); | 152 var formatter = new DartFormatter( |
| 153 pageWidth: pageWidth, indent: leadingIndent); |
| 145 | 154 |
| 146 var result; | 155 var result; |
| 147 if (p.extension(entry.path) == ".stmt") { | 156 if (p.extension(entry.path) == ".stmt") { |
| 148 result = formatter.formatStatement(input) + "\n"; | 157 result = formatter.formatStatement(input) + "\n"; |
| 149 } else { | 158 } else { |
| 150 result = formatter.format(input); | 159 result = formatter.format(input); |
| 151 } | 160 } |
| 152 | 161 |
| 153 expect(result, equals(expectedOutput)); | 162 expect(result, equals(expectedOutput)); |
| 154 }); | 163 }); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 ' A() {\n' | 210 ' A() {\n' |
| 202 ' }\n' | 211 ' }\n' |
| 203 '}\n', | 212 '}\n', |
| 204 'class A {\n' | 213 'class A {\n' |
| 205 ' A();\n' | 214 ' A();\n' |
| 206 '}\n' | 215 '}\n' |
| 207 ); | 216 ); |
| 208 }); | 217 }); |
| 209 | 218 |
| 210 */ | 219 */ |
| OLD | NEW |