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 // Test that JS printer callbacks occur when expected. | 5 // Test that JS printer callbacks occur when expected. |
6 | 6 |
7 library js_ast.printer.callback_test; | 7 library js_ast.printer.callback_test; |
8 | 8 |
9 import 'package:js_ast/js_ast.dart'; | 9 import 'package:js_ast/js_ast.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
11 | 11 |
12 enum TestMode { | 12 enum TestMode { |
| 13 INPUT, |
13 NONE, | 14 NONE, |
14 ENTER, | 15 ENTER, |
15 DELIMITER, | 16 DELIMITER, |
16 EXIT, | 17 EXIT, |
17 } | 18 } |
18 | 19 |
19 const DATA = const [ | 20 const DATA = const [ |
20 const { | 21 const { |
21 TestMode.NONE: """ | 22 TestMode.NONE: """ |
22 function(a, b) { | 23 function(a, b) { |
(...skipping 12 matching lines...) Expand all Loading... |
35 return null@5; | 36 return null@5; |
36 @4}@3@0""" | 37 @4}@3@0""" |
37 }, | 38 }, |
38 | 39 |
39 const { | 40 const { |
40 TestMode.NONE: """ | 41 TestMode.NONE: """ |
41 function() { | 42 function() { |
42 if (true) { | 43 if (true) { |
43 foo1(); | 44 foo1(); |
44 foo2(); | 45 foo2(); |
45 } else { | 46 } else if (false) { |
46 bar1(); | 47 bar1(); |
47 bar2(); | 48 bar2(); |
48 } | 49 } |
49 while (false) { | 50 while (false) { |
50 baz3(); | 51 baz3(); |
51 baz4(); | 52 baz4(); |
52 } | 53 } |
53 }""", | 54 }""", |
54 TestMode.ENTER: """ | 55 TestMode.ENTER: """ |
55 @0function() @1{ | 56 @0function() @1{ |
56 @2if (@3true) @4{ | 57 @2if (@3true) @4{ |
57 @5@6@7foo1(); | 58 @5@6@7foo1(); |
58 @8@9@10foo2(); | 59 @8@9@10foo2(); |
59 } else @11{ | 60 } else @11if (@12false) @13{ |
60 @12@13@14bar1(); | 61 @14@15@16bar1(); |
61 @15@16@17bar2(); | 62 @17@18@19bar2(); |
62 } | 63 } |
63 @18while (@19false) @20{ | 64 @20while (@21false) @22{ |
64 @21@22@23baz3(); | 65 @23@24@25baz3(); |
65 @24@25@26baz4(); | 66 @26@27@28baz4(); |
66 } | 67 } |
67 }""", | 68 }""", |
68 TestMode.DELIMITER: """ | 69 TestMode.DELIMITER: """ |
69 function() { | 70 function() { |
70 if (true) { | 71 if (true) { |
71 foo1(); | 72 foo1(); |
72 foo2(); | 73 foo2(); |
73 } else { | 74 } else if (false) { |
74 bar1(); | 75 bar1(); |
75 bar2(); | 76 bar2(); |
76 } | 77 } |
77 while (false) { | 78 while (false) { |
78 baz3(); | 79 baz3(); |
79 baz4(); | 80 baz4(); |
80 } | 81 } |
81 @0}""", | 82 @0}""", |
82 TestMode.EXIT: """ | 83 TestMode.EXIT: """ |
83 function() { | 84 function() { |
84 if (true@3) { | 85 if (true@3) { |
85 foo1@7()@6; | 86 foo1@7()@6; |
86 @5 foo2@10()@9; | 87 @5 foo2@10()@9; |
87 @8 }@4 else { | 88 @8 }@4 else if (false@12) { |
88 bar1@14()@13; | 89 bar1@16()@15; |
89 @12 bar2@17()@16; | 90 @14 bar2@19()@18; |
90 @15 }@11 | 91 @17 }@13 |
91 @2 while (false@19) { | 92 @11@2 while (false@21) { |
92 baz3@23()@22; | 93 baz3@25()@24; |
93 @21 baz4@26()@25; | 94 @23 baz4@28()@27; |
94 @24 }@20 | 95 @26 }@22 |
95 @18}@1@0""", | 96 @20}@1@0""", |
| 97 }, |
| 98 |
| 99 const { |
| 100 TestMode.NONE: """ |
| 101 function() { |
| 102 function foo() { |
| 103 } |
| 104 }""", |
| 105 TestMode.ENTER: """ |
| 106 @0function() @1{ |
| 107 @2@3function @4foo() @5{ |
| 108 } |
| 109 }""", |
| 110 TestMode.DELIMITER: """ |
| 111 function() { |
| 112 function foo() { |
| 113 @3} |
| 114 @0}""", |
| 115 TestMode.EXIT: """ |
| 116 function() { |
| 117 function foo@4() { |
| 118 }@5@3 |
| 119 @2}@1@0""" |
| 120 }, |
| 121 |
| 122 const { |
| 123 TestMode.INPUT: """ |
| 124 function() { |
| 125 a['b']; |
| 126 [1,, 2]; |
| 127 }""", |
| 128 TestMode.NONE: """ |
| 129 function() { |
| 130 a.b; |
| 131 [1,, 2]; |
| 132 }""", |
| 133 TestMode.ENTER: """ |
| 134 @0function() @1{ |
| 135 @2@3@4a.@5b; |
| 136 @6@7[@81,@9, @102]; |
| 137 }""", |
| 138 TestMode.DELIMITER: """ |
| 139 function() { |
| 140 a.b; |
| 141 [1,, 2]; |
| 142 @0}""", |
| 143 TestMode.EXIT: """ |
| 144 function() { |
| 145 a@4.b@5@3; |
| 146 @2 [1@8,,@9 2@10]@7; |
| 147 @6}@1@0""", |
96 }, | 148 }, |
97 ]; | 149 ]; |
98 | 150 |
99 void check(Map<TestMode, String> map) { | 151 void check(Map<TestMode, String> map) { |
100 String code = map[TestMode.NONE]; | 152 String code = map[TestMode.INPUT]; |
| 153 if (code == null) { |
| 154 // Input is the same as output. |
| 155 code = map[TestMode.NONE]; |
| 156 } |
101 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions(); | 157 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions(); |
102 Node node = js.parseForeignJS(code).instantiate({}); | 158 Node node = js.parseForeignJS(code).instantiate({}); |
103 map.forEach((TestMode mode, String expectedOutput) { | 159 map.forEach((TestMode mode, String expectedOutput) { |
| 160 if (mode == TestMode.INPUT) return; |
104 Context context = new Context(mode); | 161 Context context = new Context(mode); |
105 new Printer(options, context).visit(node); | 162 new Printer(options, context).visit(node); |
106 expect(context.getText(), equals(expectedOutput), | 163 expect(context.getText(), equals(expectedOutput), |
107 reason: "Unexpected output for $code in $mode"); | 164 reason: "Unexpected output for $code in $mode"); |
108 }); | 165 }); |
109 } | 166 } |
110 | 167 |
111 class Context extends SimpleJavaScriptPrintingContext { | 168 class Context extends SimpleJavaScriptPrintingContext { |
112 final TestMode mode; | 169 final TestMode mode; |
113 final Map<Node, int> idMap = {}; | 170 final Map<Node, int> idMap = {}; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 if (offset < text.length) { | 209 if (offset < text.length) { |
153 sb.write(text.substring(offset)); | 210 sb.write(text.substring(offset)); |
154 } | 211 } |
155 return sb.toString(); | 212 return sb.toString(); |
156 } | 213 } |
157 } | 214 } |
158 | 215 |
159 void main() { | 216 void main() { |
160 DATA.forEach(check); | 217 DATA.forEach(check); |
161 } | 218 } |
OLD | NEW |