Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: tests/compiler/dart2js/js_template_test.dart

Issue 237583014: JS templates (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: mostly switched to templates Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
9 js, Template;
10
11 /*
12 void testExpression(String expression, [String expect = ""]) {
13 jsAst.Node node = js(expression);
14 MockCompiler compiler = new MockCompiler();
15 String jsText =
16 jsAst.prettyPrint(node,
17 compiler,
18 allowVariableMinification: false).getText();
19 if (expect == "") {
20 Expect.stringEquals(expression, jsText);
21 } else {
22 Expect.stringEquals(expect, jsText);
23 }
24 }
25
26 void testError(String expression, [String expect = ""]) {
27 bool doCheck(exception) {
28 Expect.isTrue(exception.toString().contains(expect));
29 return true;
30 }
31 Expect.throws(() => js(expression), doCheck);
32 }
33 */
34
35 void testStatement(String statement, List arguments, String expect) {
36 var template = new Template.statement(statement, forceCopy: true);
37 jsAst.Node node = template.instantiate(arguments);
38 MockCompiler compiler = new MockCompiler();
39 String jsText =
40 jsAst.prettyPrint(node, compiler, allowVariableMinification: false)
41 .getText();
42
43 Expect.stringEquals(expect.trim(), jsText.trim());
44 }
45
46 void testError(String statement, List arguments, [String expect = ""]) {
47 bool doCheck(exception) {
48 String message = '$exception';
49 Expect.isTrue(message.contains(expect),
50 '"$message" contains "$expect"');
51 return true;
52 }
53 void action() {
54 var template = new Template.statement(statement, forceCopy: true);
55 jsAst.Node node = template.instantiate(arguments);
56 }
57 Expect.throws(action, doCheck);
58 }
59
60 // Function declaration and named function.
61 const NAMED_FUNCTION_1 = r'''
62 function foo/*function declaration*/() {
63 return function harry/*named function*/() { return #; }
64 }''';
65
66 const NAMED_FUNCTION_1_ONE = r'''
67 function foo() {
68 return function harry() {
69 return 1;
70 };
71 }''';
72
73 const MISC_1 = r'''
74 function foo() {
75 /a/;
76 #;
77 }''';
78
79 const MISC_1_1 = r'''
80 function foo() {
81 /a/;
82 1;
83 2;
84 }''';
85
86
87
88 void main() {
89
90 var eOne = js('1');
91 var eTrue = js('true');
92 var eVar = js('x');
93 var block12 = js.statement('{ 1; 2; }');
94
95 Expect.isTrue(eOne is jsAst.LiteralNumber);
96 Expect.isTrue(eTrue is jsAst.LiteralBool);
97 Expect.isTrue(block12 is jsAst.Block);
98
99 // Interpolated Expressions are upgraded to ExpressionStatements.
100 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}');
101
102 // Interpolated sub-blocks are spliced.
103 testStatement('{ #; #; }', [block12, block12],
104 '{\n 1;\n 2;\n 1;\n 2;\n}\n');
105
106 // If-condition. Dart booleans are evaluated, JS Expression booleans are
107 // substituted.
108 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}');
109 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}');
110 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}');
111 testStatement('if (#) #;', [false, block12], ';');
112 testStatement('if (#) 3; else #;', [true, block12], '3;');
113 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}');
114
115
116 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE);
117
118 testStatement(MISC_1, [block12], MISC_1_1);
119
120 // Argument list splicing.
121 testStatement('foo(#)', [[]], 'foo();');
122 testStatement('foo(#)', [[eOne]], 'foo(1);');
123 testStatement('foo(#)', [eOne], 'foo(1);');
124 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);');
125
126 testStatement('foo(2,#)', [[]], 'foo(2);');
127 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);');
128 testStatement('foo(2,#)', [eOne], 'foo(2, 1);');
129 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);');
130
131 testStatement('foo(#,3)', [[]], 'foo(3);');
132 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);');
133 testStatement('foo(#,3)', [eOne], 'foo(1, 3);');
134 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);');
135
136 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);');
137 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);');
138 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);');
139 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);');
140
141 // Interpolated Literals
142 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};');
143 // Maybe we should make this work?
144 testError('a = {#: 1}', [1], 'is not a Literal: 1');
145
146 // Interpolated parameter splicing.
147 testStatement('function foo(#){}', [new jsAst.Parameter('x')],
148 'function foo(x) {\n}');
149 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}');
150 testStatement('function foo(#){}', [[]], 'function foo() {\n}');
151 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}');
152 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}');
153
154
155 testStatement('a = #.#', [eVar,eOne], 'a = x[1];');
156 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;');
157
158 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'],
159 'function f(x) {\n return x.foo;\n}');
160
161 testStatement('#.prototype.# = function(#) { return #.# };',
162 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'],
163 'className.prototype.getterName = function(r, y) {\n'
164 ' return r.fieldName;\n'
165 '};');
166
167 testStatement('function foo(r, #) { return #[r](#) }',
168 [['a', 'b'], 'g', ['b', 'a']],
169 'function foo(r, a, b) {\n return g[r](b, a);\n}');
170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698