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