| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Note: This test relies on LF line endings in the source file. | 5 // Note: This test relies on LF line endings in the source file. |
| 6 | 6 |
| 7 // Test that JS printer callbacks occur when expected. | 7 // Test that JS printer callbacks occur when expected. |
| 8 | 8 |
| 9 library js_ast.printer.callback_test; | 9 library js_ast.printer.callback_test; |
| 10 | 10 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 }, | 98 }, |
| 99 ]; | 99 ]; |
| 100 | 100 |
| 101 void check(Map<TestMode, String> map) { | 101 void check(Map<TestMode, String> map) { |
| 102 String code = map[TestMode.NONE]; | 102 String code = map[TestMode.NONE]; |
| 103 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions(); | 103 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions(); |
| 104 Node node = js.parseForeignJS(code).instantiate({}); | 104 Node node = js.parseForeignJS(code).instantiate({}); |
| 105 map.forEach((TestMode mode, String expectedOutput) { | 105 map.forEach((TestMode mode, String expectedOutput) { |
| 106 Context context = new Context(mode); | 106 Context context = new Context(mode); |
| 107 new Printer(options, context).visit(node); | 107 new Printer(options, context).visit(node); |
| 108 expect(context.getText(), equals(expectedOutput), | 108 expect(context.getText(), |
| 109 equals(canonicalizeLineEndings(expectedOutput)), |
| 109 reason: "Unexpected output for $code in $mode"); | 110 reason: "Unexpected output for $code in $mode"); |
| 110 }); | 111 }); |
| 111 } | 112 } |
| 112 | 113 |
| 114 // The string literals may end in \r\n or \n depending on filesystem. |
| 115 canonicalizeLineEndings(String s) => s.replaceAll('\r\n', '\n'); |
| 116 |
| 113 class Context extends SimpleJavaScriptPrintingContext { | 117 class Context extends SimpleJavaScriptPrintingContext { |
| 114 final TestMode mode; | 118 final TestMode mode; |
| 115 final Map<Node, int> idMap = {}; | 119 final Map<Node, int> idMap = {}; |
| 116 final Map<int, List<String>> tagMap = {}; | 120 final Map<int, List<String>> tagMap = {}; |
| 117 | 121 |
| 118 Context(this.mode); | 122 Context(this.mode); |
| 119 | 123 |
| 120 int id(Node node) => idMap.putIfAbsent(node, () => idMap.length); | 124 int id(Node node) => idMap.putIfAbsent(node, () => idMap.length); |
| 121 | 125 |
| 122 String tag(int value) => '@$value'; | 126 String tag(int value) => '@$value'; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 if (offset < text.length) { | 158 if (offset < text.length) { |
| 155 sb.write(text.substring(offset)); | 159 sb.write(text.substring(offset)); |
| 156 } | 160 } |
| 157 return sb.toString(); | 161 return sb.toString(); |
| 158 } | 162 } |
| 159 } | 163 } |
| 160 | 164 |
| 161 void main() { | 165 void main() { |
| 162 DATA.forEach(check); | 166 DATA.forEach(check); |
| 163 } | 167 } |
| OLD | NEW |