Chromium Code Reviews| 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()); | |
|
floitsch
2014/04/22 16:11:18
for simplicity I would just remove all whitespace
sra1
2014/04/23 02:33:50
I will leave it alone for now, but replacing multi
| |
| 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 (#) #;', [true, block12], '{\n 1;\n 2;\n}'); | |
| 84 testStatement('if (#) #;', [false, block12], ';'); | |
| 85 testStatement('if (#) 3; else #;', [true, block12], '3;'); | |
| 86 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'); | |
| 87 | |
| 88 | |
| 89 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE); | |
| 90 | |
| 91 testStatement(MISC_1, [block12], MISC_1_1); | |
| 92 | |
| 93 // Argument list splicing. | |
| 94 testStatement('foo(#)', [[]], 'foo();'); | |
| 95 testStatement('foo(#)', [[eOne]], 'foo(1);'); | |
| 96 testStatement('foo(#)', [eOne], 'foo(1);'); | |
| 97 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'); | |
| 98 | |
| 99 testStatement('foo(2,#)', [[]], 'foo(2);'); | |
| 100 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'); | |
| 101 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'); | |
| 102 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'); | |
| 103 | |
| 104 testStatement('foo(#,3)', [[]], 'foo(3);'); | |
| 105 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'); | |
| 106 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'); | |
| 107 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'); | |
| 108 | |
| 109 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'); | |
| 110 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'); | |
| 111 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'); | |
| 112 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'); | |
| 113 | |
| 114 // Interpolated Literals | |
| 115 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'); | |
| 116 // Maybe we should make this work? | |
|
floitsch
2014/04/22 16:11:18
I don't think it will be necessary. but we will se
sra1
2014/04/23 02:33:50
I agree. It was not necessary.
| |
| 117 testError('a = {#: 1}', [1], 'is not a Literal: 1'); | |
| 118 | |
| 119 // Interpolated parameter splicing. | |
| 120 testStatement('function foo(#){}', [new jsAst.Parameter('x')], | |
| 121 'function foo(x) {\n}'); | |
| 122 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'); | |
| 123 testStatement('function foo(#){}', [[]], 'function foo() {\n}'); | |
| 124 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'); | |
| 125 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'); | |
| 126 | |
| 127 | |
| 128 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'); | |
| 129 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'); | |
| 130 | |
| 131 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], | |
| 132 'function f(x) {\n return x.foo;\n}'); | |
| 133 | |
| 134 testStatement('#.prototype.# = function(#) { return #.# };', | |
| 135 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], | |
| 136 'className.prototype.getterName = function(r, y) {\n' | |
| 137 ' return r.fieldName;\n' | |
| 138 '};'); | |
| 139 | |
| 140 testStatement('function foo(r, #) { return #[r](#) }', | |
| 141 [['a', 'b'], 'g', ['b', 'a']], | |
| 142 'function foo(r, a, b) {\n return g[r](b, a);\n}'); | |
| 143 } | |
| OLD | NEW |