| 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 |
| 11 import 'package:js_ast/js_ast.dart'; | 11 import 'package:js_ast/js_ast.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 enum TestMode { | 14 enum TestMode { |
| 15 INPUT, | 15 INPUT, |
| 16 NONE, | 16 NONE, |
| 17 ENTER, | 17 ENTER, |
| 18 DELIMITER, | 18 DELIMITER, |
| 19 EXIT, | 19 EXIT, |
| 20 } | 20 } |
| 21 | 21 |
| 22 class TestCase { | 22 class TestCase { |
| 23 final Map<TestMode, String> data; | 23 final Map<TestMode, String> data; |
| 24 | 24 |
| 25 /// Map from template names to the inserted values. | 25 /// Map from template names to the inserted values. |
| 26 final Map<String, String> environment; | 26 final Map<String, String> environment; |
| 27 | 27 |
| 28 const TestCase( | 28 const TestCase(this.data, [this.environment = const {}]); |
| 29 this.data, | |
| 30 [this.environment = const {}]); | |
| 31 } | 29 } |
| 32 | 30 |
| 33 const List<TestCase> DATA = const <TestCase>[ | 31 const List<TestCase> DATA = const <TestCase>[ |
| 34 const TestCase(const { | 32 const TestCase(const { |
| 35 TestMode.NONE: """ | 33 TestMode.NONE: """ |
| 36 function(a, b) { | 34 function(a, b) { |
| 37 return null; | 35 return null; |
| 38 }""", | 36 }""", |
| 39 TestMode.ENTER: """ | 37 TestMode.ENTER: """ |
| 40 @0function(@1a, @2b) @3{ | 38 @0function(@1a, @2b) @3{ |
| 41 @4return @5null; | 39 @4return @5null; |
| 42 }""", | 40 }""", |
| 43 TestMode.DELIMITER: """ | 41 TestMode.DELIMITER: """ |
| 44 function(a, b) { | 42 function(a, b) { |
| 45 return null; | 43 return null; |
| 46 @0}""", | 44 @0}""", |
| 47 TestMode.EXIT: """ | 45 TestMode.EXIT: """ |
| 48 function(a@1, b@2) { | 46 function(a@1, b@2) { |
| 49 return null@5; | 47 return null@5; |
| 50 @4}@3@0""" | 48 @4}@3@0""" |
| 51 }), | 49 }), |
| 52 | |
| 53 const TestCase(const { | 50 const TestCase(const { |
| 54 TestMode.NONE: """ | 51 TestMode.NONE: """ |
| 55 function() { | 52 function() { |
| 56 if (true) { | 53 if (true) { |
| 57 foo1(); | 54 foo1(); |
| 58 foo2(); | 55 foo2(); |
| 59 } else if (false) { | 56 } else if (false) { |
| 60 bar1(); | 57 bar1(); |
| 61 bar2(); | 58 bar2(); |
| 62 } | 59 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 @8 }@4 else if (false@12) { | 98 @8 }@4 else if (false@12) { |
| 102 bar1@16()@15; | 99 bar1@16()@15; |
| 103 @14 bar2@19()@18; | 100 @14 bar2@19()@18; |
| 104 @17 }@13 | 101 @17 }@13 |
| 105 @11@2 while (false@21) { | 102 @11@2 while (false@21) { |
| 106 baz3@25()@24; | 103 baz3@25()@24; |
| 107 @23 baz4@28()@27; | 104 @23 baz4@28()@27; |
| 108 @26 }@22 | 105 @26 }@22 |
| 109 @20}@1@0""", | 106 @20}@1@0""", |
| 110 }), | 107 }), |
| 111 | |
| 112 const TestCase(const { | 108 const TestCase(const { |
| 113 TestMode.NONE: """ | 109 TestMode.NONE: """ |
| 114 function() { | 110 function() { |
| 115 function foo() { | 111 function foo() { |
| 116 } | 112 } |
| 117 }""", | 113 }""", |
| 118 TestMode.ENTER: """ | 114 TestMode.ENTER: """ |
| 119 @0function() @1{ | 115 @0function() @1{ |
| 120 @2@3function @4foo() @5{ | 116 @2@3function @4foo() @5{ |
| 121 } | 117 } |
| 122 }""", | 118 }""", |
| 123 TestMode.DELIMITER: """ | 119 TestMode.DELIMITER: """ |
| 124 function() { | 120 function() { |
| 125 function foo() { | 121 function foo() { |
| 126 @3} | 122 @3} |
| 127 @0}""", | 123 @0}""", |
| 128 TestMode.EXIT: """ | 124 TestMode.EXIT: """ |
| 129 function() { | 125 function() { |
| 130 function foo@4() { | 126 function foo@4() { |
| 131 }@5@3 | 127 }@5@3 |
| 132 @2}@1@0""" | 128 @2}@1@0""" |
| 133 }), | 129 }), |
| 134 | |
| 135 const TestCase(const { | 130 const TestCase(const { |
| 136 TestMode.INPUT: """ | 131 TestMode.INPUT: """ |
| 137 function() { | 132 function() { |
| 138 a['b']; | 133 a['b']; |
| 139 [1,, 2]; | 134 [1,, 2]; |
| 140 }""", | 135 }""", |
| 141 TestMode.NONE: """ | 136 TestMode.NONE: """ |
| 142 function() { | 137 function() { |
| 143 a.b; | 138 a.b; |
| 144 [1,, 2]; | 139 [1,, 2]; |
| 145 }""", | 140 }""", |
| 146 TestMode.ENTER: """ | 141 TestMode.ENTER: """ |
| 147 @0function() @1{ | 142 @0function() @1{ |
| 148 @2@3@4a.@5b; | 143 @2@3@4a.@5b; |
| 149 @6@7[@81,@9, @102]; | 144 @6@7[@81,@9, @102]; |
| 150 }""", | 145 }""", |
| 151 TestMode.DELIMITER: """ | 146 TestMode.DELIMITER: """ |
| 152 function() { | 147 function() { |
| 153 a.b; | 148 a.b; |
| 154 [1,, 2]; | 149 [1,, 2]; |
| 155 @0}""", | 150 @0}""", |
| 156 TestMode.EXIT: """ | 151 TestMode.EXIT: """ |
| 157 function() { | 152 function() { |
| 158 a@4.b@5@3; | 153 a@4.b@5@3; |
| 159 @2 [1@8,,@9 2@10]@7; | 154 @2 [1@8,,@9 2@10]@7; |
| 160 @6}@1@0""", | 155 @6}@1@0""", |
| 161 }), | 156 }), |
| 162 | |
| 163 const TestCase(const { | 157 const TestCase(const { |
| 164 TestMode.INPUT: "a.#nameTemplate = #nameTemplate", | 158 TestMode.INPUT: "a.#nameTemplate = #nameTemplate", |
| 165 TestMode.NONE: "a.nameValue = nameValue", | 159 TestMode.NONE: "a.nameValue = nameValue", |
| 166 TestMode.ENTER: "@0@1@2a.@3nameValue = @3nameValue", | 160 TestMode.ENTER: "@0@1@2a.@3nameValue = @3nameValue", |
| 167 TestMode.DELIMITER: "a.nameValue = nameValue", | 161 TestMode.DELIMITER: "a.nameValue = nameValue", |
| 168 TestMode.EXIT: "a@2.nameValue@3@1 = nameValue@3@0", | 162 TestMode.EXIT: "a@2.nameValue@3@1 = nameValue@3@0", |
| 169 }, const {'nameTemplate': 'nameValue'}), | 163 }, const { |
| 164 'nameTemplate': 'nameValue' |
| 165 }), |
| 170 ]; | 166 ]; |
| 171 | 167 |
| 172 class FixedName extends Name { | 168 class FixedName extends Name { |
| 173 final String name; | 169 final String name; |
| 174 String get key => name; | 170 String get key => name; |
| 175 | 171 |
| 176 FixedName(this.name); | 172 FixedName(this.name); |
| 177 | 173 |
| 178 @override | 174 @override |
| 179 int compareTo(other) => 0; | 175 int compareTo(other) => 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 210 |
| 215 String tag(int value) => '@$value'; | 211 String tag(int value) => '@$value'; |
| 216 | 212 |
| 217 void enterNode(Node node, int startPosition) { | 213 void enterNode(Node node, int startPosition) { |
| 218 int value = id(node); | 214 int value = id(node); |
| 219 if (mode == TestMode.ENTER) { | 215 if (mode == TestMode.ENTER) { |
| 220 tagMap.putIfAbsent(startPosition, () => []).add(tag(value)); | 216 tagMap.putIfAbsent(startPosition, () => []).add(tag(value)); |
| 221 } | 217 } |
| 222 } | 218 } |
| 223 | 219 |
| 224 void exitNode(Node node, | 220 void exitNode( |
| 225 int startPosition, | 221 Node node, int startPosition, int endPosition, int delimiterPosition) { |
| 226 int endPosition, | |
| 227 int delimiterPosition) { | |
| 228 int value = id(node); | 222 int value = id(node); |
| 229 if (mode == TestMode.DELIMITER && delimiterPosition != null) { | 223 if (mode == TestMode.DELIMITER && delimiterPosition != null) { |
| 230 tagMap.putIfAbsent(delimiterPosition, () => []).add(tag(value)); | 224 tagMap.putIfAbsent(delimiterPosition, () => []).add(tag(value)); |
| 231 } else if (mode == TestMode.EXIT) { | 225 } else if (mode == TestMode.EXIT) { |
| 232 tagMap.putIfAbsent(endPosition, () => []).add(tag(value)); | 226 tagMap.putIfAbsent(endPosition, () => []).add(tag(value)); |
| 233 } | 227 } |
| 234 } | 228 } |
| 235 | 229 |
| 236 String getText() { | 230 String getText() { |
| 237 String text = super.getText(); | 231 String text = super.getText(); |
| 238 int offset = 0; | 232 int offset = 0; |
| 239 StringBuffer sb = new StringBuffer(); | 233 StringBuffer sb = new StringBuffer(); |
| 240 for (int position in tagMap.keys.toList()..sort()) { | 234 for (int position in tagMap.keys.toList()..sort()) { |
| 241 if (offset < position) { | 235 if (offset < position) { |
| 242 sb.write(text.substring(offset, position)); | 236 sb.write(text.substring(offset, position)); |
| 243 } | 237 } |
| 244 tagMap[position].forEach((String tag) => sb.write(tag)); | 238 tagMap[position].forEach((String tag) => sb.write(tag)); |
| 245 offset = position; | 239 offset = position; |
| 246 } | 240 } |
| 247 if (offset < text.length) { | 241 if (offset < text.length) { |
| 248 sb.write(text.substring(offset)); | 242 sb.write(text.substring(offset)); |
| 249 } | 243 } |
| 250 return sb.toString(); | 244 return sb.toString(); |
| 251 } | 245 } |
| 252 } | 246 } |
| 253 | 247 |
| 254 void main() { | 248 void main() { |
| 255 DATA.forEach(check); | 249 DATA.forEach(check); |
| 256 } | 250 } |
| OLD | NEW |