| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
| 7 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
| 8 import 'mock_compiler.dart'; | 8 import 'mock_compiler.dart'; |
| 9 import 'package:compiler/src/js/js.dart' as jsAst; | 9 import 'package:compiler/src/js/js.dart' as jsAst; |
| 10 import 'package:compiler/src/js/js.dart' show js; | 10 import 'package:compiler/src/js/js.dart' show js; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 Expect.throws(action, doCheck); | 34 Expect.throws(action, doCheck); |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Function declaration and named function. | 38 // Function declaration and named function. |
| 39 const NAMED_FUNCTION_1 = r''' | 39 const NAMED_FUNCTION_1 = r''' |
| 40 function foo/*function declaration*/() { | 40 function foo/*function declaration*/() { |
| 41 return function harry/*named function*/() { return #; } | 41 return function harry/*named function*/() { return #; } |
| 42 }'''; | 42 }'''; |
| 43 | 43 |
| 44 const NAMED_FUNCTION_1_NAMED_HOLE = r''' |
| 45 function foo/*function declaration*/() { |
| 46 return function harry/*named function*/() { return #hole; } |
| 47 }'''; |
| 48 |
| 44 const NAMED_FUNCTION_1_ONE = r''' | 49 const NAMED_FUNCTION_1_ONE = r''' |
| 45 function foo() { | 50 function foo() { |
| 46 return function harry() { | 51 return function harry() { |
| 47 return 1; | 52 return 1; |
| 48 }; | 53 }; |
| 49 }'''; | 54 }'''; |
| 50 | 55 |
| 51 const MISC_1 = r''' | 56 const MISC_1 = r''' |
| 52 function foo() { | 57 function foo() { |
| 53 /a/; | 58 /a/; |
| 54 #; | 59 #; |
| 55 }'''; | 60 }'''; |
| 56 | 61 |
| 62 const MISC_1_NAMED_HOLE = r''' |
| 63 function foo() { |
| 64 /a/; |
| 65 #hole; |
| 66 }'''; |
| 67 |
| 57 const MISC_1_1 = r''' | 68 const MISC_1_1 = r''' |
| 58 function foo() { | 69 function foo() { |
| 59 /a/; | 70 /a/; |
| 60 1; | 71 1; |
| 61 2; | 72 2; |
| 62 }'''; | 73 }'''; |
| 63 | 74 |
| 64 | 75 |
| 65 | |
| 66 void main() { | 76 void main() { |
| 67 | 77 |
| 68 var eOne = js('1'); | 78 var eOne = js('1'); |
| 69 var eTrue = js('true'); | 79 var eTrue = js('true'); |
| 70 var eVar = js('x'); | 80 var eVar = js('x'); |
| 71 var block12 = js.statement('{ 1; 2; }'); | 81 var block12 = js.statement('{ 1; 2; }'); |
| 72 var seq1 = js('1, 2, 3'); | 82 var seq1 = js('1, 2, 3'); |
| 73 | 83 |
| 74 Expect.isTrue(eOne is jsAst.LiteralNumber); | 84 Expect.isTrue(eOne is jsAst.LiteralNumber); |
| 75 Expect.isTrue(eTrue is jsAst.LiteralBool); | 85 Expect.isTrue(eTrue is jsAst.LiteralBool); |
| 76 Expect.isTrue(block12 is jsAst.Block); | 86 Expect.isTrue(block12 is jsAst.Block); |
| 77 | 87 |
| 78 asyncTest(() => Future.wait([ | 88 asyncTest(() => Future.wait([ |
| 79 // Interpolated Expressions are upgraded to ExpressionStatements. | 89 // Interpolated Expressions are upgraded to ExpressionStatements. |
| 80 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'), | 90 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'), |
| 91 testStatement('{ #a; #b; }', {'a': eOne, 'b': eOne}, '{\n 1;\n 1;\n}'), |
| 81 | 92 |
| 82 // Interpolated sub-blocks are spliced. | 93 // Interpolated sub-blocks are spliced. |
| 83 testStatement('{ #; #; }', [block12, block12], | 94 testStatement('{ #; #; }', [block12, block12], |
| 84 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), | 95 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), |
| 96 testStatement('{ #a; #b; }', {'a': block12, 'b': block12}, |
| 97 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), |
| 85 | 98 |
| 86 // If-condition. Dart booleans are evaluated, JS Expression booleans are | 99 // If-condition. Dart booleans are evaluated, JS Expression booleans are |
| 87 // substituted. | 100 // substituted. |
| 88 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'), | 101 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'), |
| 89 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'), | 102 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'), |
| 90 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'), | 103 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'), |
| 91 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'), | 104 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'), |
| 92 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'), | 105 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'), |
| 93 testStatement('if (#) #;', [false, block12], ';'), | 106 testStatement('if (#) #;', [false, block12], ';'), |
| 94 testStatement('if (#) 3; else #;', [true, block12], '3;'), | 107 testStatement('if (#) 3; else #;', [true, block12], '3;'), |
| 95 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'), | 108 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'), |
| 109 testStatement('if (#a) #b', |
| 110 {'a': eOne, 'b': block12}, |
| 111 'if (1) {\n 1;\n 2;\n}'), |
| 112 testStatement('if (#a) #b;', |
| 113 {'a': eTrue, 'b': block12}, |
| 114 'if (true) {\n 1;\n 2;\n}'), |
| 115 testStatement('if (#a) #b;', |
| 116 {'a': eVar, 'b': block12}, |
| 117 'if (x) {\n 1;\n 2;\n}'), |
| 118 testStatement('if (#a) #b;', |
| 119 {'a': 'a', 'b': block12}, |
| 120 'if (a) {\n 1;\n 2;\n}'), |
| 121 testStatement('if (#a) #b;', |
| 122 {'a': true, 'b': block12}, |
| 123 '{\n 1;\n 2;\n}'), |
| 124 testStatement('if (#a) #b;', |
| 125 {'a': false, 'b': block12}, |
| 126 ';'), |
| 127 testStatement('if (#a) 3; else #b;', |
| 128 {'a': true, 'b': block12}, |
| 129 '3;'), |
| 130 testStatement('if (#a) 3; else #b;', |
| 131 {'a': false, 'b': block12}, |
| 132 '{\n 1;\n 2;\n}'), |
| 96 | 133 |
| 97 | 134 |
| 98 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE), | 135 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE), |
| 136 testStatement(NAMED_FUNCTION_1_NAMED_HOLE, |
| 137 {'hole': eOne}, |
| 138 NAMED_FUNCTION_1_ONE), |
| 99 | 139 |
| 100 testStatement(MISC_1, [block12], MISC_1_1), | 140 testStatement(MISC_1, [block12], MISC_1_1), |
| 141 testStatement(MISC_1_NAMED_HOLE, {'hole': block12}, MISC_1_1), |
| 101 | 142 |
| 102 // Argument list splicing. | 143 // Argument list splicing. |
| 103 testStatement('foo(#)', [[]], 'foo();'), | 144 testStatement('foo(#)', [[]], 'foo();'), |
| 104 testStatement('foo(#)', [[eOne]], 'foo(1);'), | 145 testStatement('foo(#)', [[eOne]], 'foo(1);'), |
| 105 testStatement('foo(#)', [eOne], 'foo(1);'), | 146 testStatement('foo(#)', [eOne], 'foo(1);'), |
| 106 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'), | 147 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'), |
| 148 testStatement('foo(#a)', {'a': []}, 'foo();'), |
| 149 testStatement('foo(#a)', {'a': [eOne]}, 'foo(1);'), |
| 150 testStatement('foo(#a)', {'a': eOne}, 'foo(1);'), |
| 151 testStatement('foo(#a)', {'a': [eTrue,eOne]}, 'foo(true, 1);'), |
| 107 | 152 |
| 108 testStatement('foo(2,#)', [[]], 'foo(2);'), | 153 testStatement('foo(2,#)', [[]], 'foo(2);'), |
| 109 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'), | 154 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'), |
| 110 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'), | 155 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'), |
| 111 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'), | 156 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'), |
| 157 testStatement('foo(2,#a)', {'a': []}, 'foo(2);'), |
| 158 testStatement('foo(2,#a)', {'a': [eOne]}, 'foo(2, 1);'), |
| 159 testStatement('foo(2,#a)', {'a': eOne}, 'foo(2, 1);'), |
| 160 testStatement('foo(2,#a)', {'a': [eTrue,eOne]}, 'foo(2, true, 1);'), |
| 112 | 161 |
| 113 testStatement('foo(#,3)', [[]], 'foo(3);'), | 162 testStatement('foo(#,3)', [[]], 'foo(3);'), |
| 114 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'), | 163 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'), |
| 115 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'), | 164 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'), |
| 116 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'), | 165 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'), |
| 166 testStatement('foo(#a,3)', {'a': []}, 'foo(3);'), |
| 167 testStatement('foo(#a,3)', {'a': [eOne]}, 'foo(1, 3);'), |
| 168 testStatement('foo(#a,3)', {'a': eOne}, 'foo(1, 3);'), |
| 169 testStatement('foo(#a,3)', {'a': [eTrue,eOne]}, 'foo(true, 1, 3);'), |
| 117 | 170 |
| 118 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'), | 171 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'), |
| 119 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'), | 172 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'), |
| 120 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'), | 173 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'), |
| 121 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'), | 174 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'), |
| 175 testStatement('foo(2,#a,3)', {'a': []}, 'foo(2, 3);'), |
| 176 testStatement('foo(2,#a,3)', {'a': [eOne]}, 'foo(2, 1, 3);'), |
| 177 testStatement('foo(2,#a,3)', {'a': eOne}, 'foo(2, 1, 3);'), |
| 178 testStatement('foo(2,#a,3)', {'a': [eTrue,eOne]}, 'foo(2, true, 1, 3);'), |
| 122 | 179 |
| 123 // Interpolated Literals | 180 // Interpolated Literals |
| 124 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'), | 181 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'), |
| 182 testStatement('a = {#a: 1}', {'a': eOne}, 'a = {1: 1};'), |
| 125 // Maybe we should make this work? | 183 // Maybe we should make this work? |
| 126 testError('a = {#: 1}', [1], 'is not a Literal: 1'), | 184 testError('a = {#: 1}', [1], 'is not a Literal: 1'), |
| 185 testError('a = {#a: 1}', {'a': 1}, 'is not a Literal: 1'), |
| 127 | 186 |
| 128 // Interpolated parameter splicing. | 187 // Interpolated parameter splicing. |
| 129 testStatement('function foo(#){}', [new jsAst.Parameter('x')], | 188 testStatement('function foo(#){}', [new jsAst.Parameter('x')], |
| 130 'function foo(x) {\n}'), | 189 'function foo(x) {\n}'), |
| 131 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'), | 190 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'), |
| 132 testStatement('function foo(#){}', [[]], 'function foo() {\n}'), | 191 testStatement('function foo(#){}', [[]], 'function foo() {\n}'), |
| 133 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'), | 192 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'), |
| 134 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'), | 193 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'), |
| 194 testStatement('function foo(#a){}', {'a': new jsAst.Parameter('x')}, |
| 195 'function foo(x) {\n}'), |
| 196 testStatement('function foo(#a){}', {'a': 'x'}, 'function foo(x) {\n}'), |
| 197 testStatement('function foo(#a){}', {'a': []}, 'function foo() {\n}'), |
| 198 testStatement('function foo(#a){}', {'a': ['x']}, 'function foo(x) {\n}'), |
| 199 testStatement('function foo(#a){}', |
| 200 {'a': ['x', 'y']}, |
| 201 'function foo(x, y) {\n}'), |
| 135 | 202 |
| 136 | 203 |
| 137 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'), | 204 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'), |
| 138 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'), | 205 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'), |
| 206 testStatement('a = #a.#b', {'a': eVar, 'b': eOne}, 'a = x[1];'), |
| 207 testStatement('a = #a.#b', {'a': eVar, 'b': 'foo'}, 'a = x.foo;'), |
| 139 | 208 |
| 140 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], | 209 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], |
| 141 'function f(x) {\n return x.foo;\n}'), | 210 'function f(x) {\n return x.foo;\n}'), |
| 211 testStatement('function f(#a) { return #b.#c; }', |
| 212 {'a': 'x', 'b': eVar, 'c': 'foo'}, |
| 213 'function f(x) {\n return x.foo;\n}'), |
| 142 | 214 |
| 143 testStatement('#.prototype.# = function(#) { return #.# };', | 215 testStatement('#.prototype.# = function(#) { return #.# };', |
| 144 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], | 216 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], |
| 145 'className.prototype.getterName = function(r, y) {\n' | 217 'className.prototype.getterName = function(r, y) {\n' |
| 146 ' return r.fieldName;\n' | 218 ' return r.fieldName;\n' |
| 147 '};'), | 219 '};'), |
| 220 testStatement('#a.prototype.#b = function(#c) { return #d.#e };', |
| 221 {'a': 'className', |
| 222 'b': 'getterName', |
| 223 'c': ['r', 'y'], |
| 224 'd': 'r', |
| 225 'e': 'fieldName'}, |
| 226 'className.prototype.getterName = function(r, y) {\n' |
| 227 ' return r.fieldName;\n' |
| 228 '};'), |
| 148 | 229 |
| 149 testStatement('function foo(r, #) { return #[r](#) }', | 230 testStatement('function foo(r, #) { return #[r](#) }', |
| 150 [['a', 'b'], 'g', ['b', 'a']], | 231 [['a', 'b'], 'g', ['b', 'a']], |
| 151 'function foo(r, a, b) {\n return g[r](b, a);\n}'), | 232 'function foo(r, a, b) {\n return g[r](b, a);\n}'), |
| 233 testStatement('function foo(r, #a) { return #b[r](#c) }', |
| 234 {'a': ['a', 'b'], 'b': 'g', 'c': ['b', 'a']}, |
| 235 'function foo(r, a, b) {\n return g[r](b, a);\n}'), |
| 152 | 236 |
| 153 // Sequence is printed flattened | 237 // Sequence is printed flattened |
| 154 testStatement('x = #', [seq1], 'x = (1, 2, 3);'), | 238 testStatement('x = #', [seq1], 'x = (1, 2, 3);'), |
| 155 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'), | 239 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'), |
| 156 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'), | 240 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'), |
| 157 testStatement( | 241 testStatement( |
| 158 'for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1], | 242 'for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1], |
| 159 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), | 243 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), |
| 244 testStatement('x = #a', {'a': seq1}, 'x = (1, 2, 3);'), |
| 245 testStatement('x = (#a, #b)', |
| 246 {'a': seq1, 'b': seq1}, |
| 247 'x = (1, 2, 3, 1, 2, 3);'), |
| 248 testStatement('x = #a, #b', |
| 249 {'a': seq1, 'b': seq1}, |
| 250 'x = (1, 2, 3), 1, 2, 3;'), |
| 251 testStatement( |
| 252 'for (i = 0, j = #a, k = 0; ; ++i, ++j, ++k){}', {'a': seq1}, |
| 253 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), |
| 254 |
| 255 // Use the same name several times. |
| 256 testStatement('#a.prototype.#a = function(#b) { return #c.#c };', |
| 257 {'a': 'name1_2', |
| 258 'b': ['r', 'y'], |
| 259 'c': 'name4_5'}, |
| 260 'name1_2.prototype.name1_2 = function(r, y) {\n' |
| 261 ' return name4_5.name4_5;\n' |
| 262 '};'), |
| 263 |
| 160 ])); | 264 ])); |
| 161 } | 265 } |
| OLD | NEW |