| 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 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
| 7 import 'package:async_helper/async_helper.dart'; |
| 6 import 'mock_compiler.dart'; | 8 import 'mock_compiler.dart'; |
| 7 import '../../../sdk/lib/_internal/compiler/implementation/js/js.dart' as jsAst; | 9 import '../../../sdk/lib/_internal/compiler/implementation/js/js.dart' as jsAst; |
| 8 import '../../../sdk/lib/_internal/compiler/implementation/js/js.dart' show js; | 10 import '../../../sdk/lib/_internal/compiler/implementation/js/js.dart' show js; |
| 9 | 11 |
| 10 | 12 |
| 11 void testStatement(String statement, List arguments, String expect) { | 13 Future testStatement(String statement, List arguments, String expect) { |
| 12 jsAst.Node node = js.statement(statement, arguments); | 14 jsAst.Node node = js.statement(statement, arguments); |
| 13 MockCompiler compiler = new MockCompiler(); | 15 return MockCompiler.create((MockCompiler compiler) { |
| 14 String jsText = | 16 String jsText = |
| 15 jsAst.prettyPrint(node, compiler, allowVariableMinification: false) | 17 jsAst.prettyPrint(node, compiler, allowVariableMinification: false) |
| 16 .getText(); | 18 .getText(); |
| 17 | 19 |
| 18 Expect.stringEquals(expect.trim(), jsText.trim()); | 20 Expect.stringEquals(expect.trim(), jsText.trim()); |
| 21 }); |
| 19 } | 22 } |
| 20 | 23 |
| 21 void testError(String statement, List arguments, [String expect = ""]) { | 24 Future testError(String statement, List arguments, [String expect = ""]) { |
| 22 bool doCheck(exception) { | 25 return new Future.sync(() { |
| 23 String message = '$exception'; | 26 bool doCheck(exception) { |
| 24 Expect.isTrue(message.contains(expect), '"$message" contains "$expect"'); | 27 String message = '$exception'; |
| 25 return true; | 28 Expect.isTrue(message.contains(expect), '"$message" contains "$expect"'); |
| 26 } | 29 return true; |
| 27 void action() { | 30 } |
| 28 jsAst.Node node = js.statement(statement, arguments); | 31 void action() { |
| 29 } | 32 jsAst.Node node = js.statement(statement, arguments); |
| 30 Expect.throws(action, doCheck); | 33 } |
| 34 Expect.throws(action, doCheck); |
| 35 }); |
| 31 } | 36 } |
| 32 | 37 |
| 33 // Function declaration and named function. | 38 // Function declaration and named function. |
| 34 const NAMED_FUNCTION_1 = r''' | 39 const NAMED_FUNCTION_1 = r''' |
| 35 function foo/*function declaration*/() { | 40 function foo/*function declaration*/() { |
| 36 return function harry/*named function*/() { return #; } | 41 return function harry/*named function*/() { return #; } |
| 37 }'''; | 42 }'''; |
| 38 | 43 |
| 39 const NAMED_FUNCTION_1_ONE = r''' | 44 const NAMED_FUNCTION_1_ONE = r''' |
| 40 function foo() { | 45 function foo() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 57 }'''; | 62 }'''; |
| 58 | 63 |
| 59 | 64 |
| 60 | 65 |
| 61 void main() { | 66 void main() { |
| 62 | 67 |
| 63 var eOne = js('1'); | 68 var eOne = js('1'); |
| 64 var eTrue = js('true'); | 69 var eTrue = js('true'); |
| 65 var eVar = js('x'); | 70 var eVar = js('x'); |
| 66 var block12 = js.statement('{ 1; 2; }'); | 71 var block12 = js.statement('{ 1; 2; }'); |
| 72 var seq1 = js('1, 2, 3'); |
| 67 | 73 |
| 68 Expect.isTrue(eOne is jsAst.LiteralNumber); | 74 Expect.isTrue(eOne is jsAst.LiteralNumber); |
| 69 Expect.isTrue(eTrue is jsAst.LiteralBool); | 75 Expect.isTrue(eTrue is jsAst.LiteralBool); |
| 70 Expect.isTrue(block12 is jsAst.Block); | 76 Expect.isTrue(block12 is jsAst.Block); |
| 71 | 77 |
| 72 // Interpolated Expressions are upgraded to ExpressionStatements. | 78 asyncTest(() => Future.wait([ |
| 73 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'); | 79 // Interpolated Expressions are upgraded to ExpressionStatements. |
| 80 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'), |
| 74 | 81 |
| 75 // Interpolated sub-blocks are spliced. | 82 // Interpolated sub-blocks are spliced. |
| 76 testStatement('{ #; #; }', [block12, block12], | 83 testStatement('{ #; #; }', [block12, block12], |
| 77 '{\n 1;\n 2;\n 1;\n 2;\n}\n'); | 84 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), |
| 78 | 85 |
| 79 // If-condition. Dart booleans are evaluated, JS Expression booleans are | 86 // If-condition. Dart booleans are evaluated, JS Expression booleans are |
| 80 // substituted. | 87 // substituted. |
| 81 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'); | 88 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'), |
| 82 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'); | 89 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'), |
| 83 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'); | 90 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'), |
| 84 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'); | 91 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'), |
| 85 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'); | 92 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'), |
| 86 testStatement('if (#) #;', [false, block12], ';'); | 93 testStatement('if (#) #;', [false, block12], ';'), |
| 87 testStatement('if (#) 3; else #;', [true, block12], '3;'); | 94 testStatement('if (#) 3; else #;', [true, block12], '3;'), |
| 88 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'); | 95 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'), |
| 89 | 96 |
| 90 | 97 |
| 91 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE); | 98 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE), |
| 92 | 99 |
| 93 testStatement(MISC_1, [block12], MISC_1_1); | 100 testStatement(MISC_1, [block12], MISC_1_1), |
| 94 | 101 |
| 95 // Argument list splicing. | 102 // Argument list splicing. |
| 96 testStatement('foo(#)', [[]], 'foo();'); | 103 testStatement('foo(#)', [[]], 'foo();'), |
| 97 testStatement('foo(#)', [[eOne]], 'foo(1);'); | 104 testStatement('foo(#)', [[eOne]], 'foo(1);'), |
| 98 testStatement('foo(#)', [eOne], 'foo(1);'); | 105 testStatement('foo(#)', [eOne], 'foo(1);'), |
| 99 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'); | 106 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'), |
| 100 | 107 |
| 101 testStatement('foo(2,#)', [[]], 'foo(2);'); | 108 testStatement('foo(2,#)', [[]], 'foo(2);'), |
| 102 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'); | 109 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'), |
| 103 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'); | 110 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'), |
| 104 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'); | 111 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'), |
| 105 | 112 |
| 106 testStatement('foo(#,3)', [[]], 'foo(3);'); | 113 testStatement('foo(#,3)', [[]], 'foo(3);'), |
| 107 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'); | 114 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'), |
| 108 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'); | 115 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'), |
| 109 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'); | 116 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'), |
| 110 | 117 |
| 111 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'); | 118 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'), |
| 112 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'); | 119 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'), |
| 113 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'); | 120 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'), |
| 114 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'); | 121 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'), |
| 115 | 122 |
| 116 // Interpolated Literals | 123 // Interpolated Literals |
| 117 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'); | 124 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'), |
| 118 // Maybe we should make this work? | 125 // Maybe we should make this work? |
| 119 testError('a = {#: 1}', [1], 'is not a Literal: 1'); | 126 testError('a = {#: 1}', [1], 'is not a Literal: 1'), |
| 120 | 127 |
| 121 // Interpolated parameter splicing. | 128 // Interpolated parameter splicing. |
| 122 testStatement('function foo(#){}', [new jsAst.Parameter('x')], | 129 testStatement('function foo(#){}', [new jsAst.Parameter('x')], |
| 123 'function foo(x) {\n}'); | 130 'function foo(x) {\n}'), |
| 124 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'); | 131 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'), |
| 125 testStatement('function foo(#){}', [[]], 'function foo() {\n}'); | 132 testStatement('function foo(#){}', [[]], 'function foo() {\n}'), |
| 126 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'); | 133 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'), |
| 127 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'); | 134 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'), |
| 128 | 135 |
| 129 | 136 |
| 130 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'); | 137 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'), |
| 131 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'); | 138 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'), |
| 132 | 139 |
| 133 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], | 140 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], |
| 134 'function f(x) {\n return x.foo;\n}'); | 141 'function f(x) {\n return x.foo;\n}'), |
| 135 | 142 |
| 136 testStatement('#.prototype.# = function(#) { return #.# };', | 143 testStatement('#.prototype.# = function(#) { return #.# };', |
| 137 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], | 144 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], |
| 138 'className.prototype.getterName = function(r, y) {\n' | 145 'className.prototype.getterName = function(r, y) {\n' |
| 139 ' return r.fieldName;\n' | 146 ' return r.fieldName;\n' |
| 140 '};'); | 147 '};'), |
| 141 | 148 |
| 142 testStatement('function foo(r, #) { return #[r](#) }', | 149 testStatement('function foo(r, #) { return #[r](#) }', |
| 143 [['a', 'b'], 'g', ['b', 'a']], | 150 [['a', 'b'], 'g', ['b', 'a']], |
| 144 'function foo(r, a, b) {\n return g[r](b, a);\n}'); | 151 'function foo(r, a, b) {\n return g[r](b, a);\n}'), |
| 145 | 152 |
| 146 // Sequence is printed flattened | 153 // Sequence is printed flattened |
| 147 var seq1 = js('1, 2, 3'); | 154 testStatement('x = #', [seq1], 'x = (1, 2, 3);'), |
| 148 testStatement('x = #', [seq1], 'x = (1, 2, 3);'); | 155 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'), |
| 149 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'); | 156 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'), |
| 150 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'); | 157 testStatement( |
| 151 testStatement( | 158 'for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1], |
| 152 'for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1], | 159 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), |
| 153 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'); | 160 ])); |
| 154 } | 161 } |
| OLD | NEW |