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

Side by Side Diff: tests/language/src/FunctionSyntaxTest.dart

Issue 10248007: test rename overhaul: step 8 - language tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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
« no previous file with comments | « tests/language/src/FunctionLiteralsTest.dart ('k') | tests/language/src/FunctionTest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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 // Tests function statement and expression syntax.
6
7 class FunctionSyntaxTest {
8
9 static void testMain() {
10 testNestedFunctions();
11 testFunctionExpressions();
12 testPrecedence();
13 testInitializers();
14 testFunctionParameter();
15 testFunctionIdentifierExpression();
16 testFunctionIdentifierStatement();
17 }
18
19 static void testNestedFunctions() {
20 // No types - braces.
21 nb0() { return 42; }
22 nb1(a) { return a; }
23 nb2(a, b) { return a + b; }
24 Expect.equals(42, nb0());
25 Expect.equals(87, nb1(87));
26 Expect.equals(1 + 2, nb2(1, 2));
27
28 // No types - arrows.
29 na0() => 42;
30 na1(a) => a;
31 na2(a, b) => a + b;
32 Expect.equals(42, na0());
33 Expect.equals(87, na1(87));
34 Expect.equals(1 + 2, na2(1, 2));
35
36 // Return type - braces.
37 int rb0() { return 42; }
38 int rb1(a) { return a; }
39 int rb2(a, b) { return a + b; }
40 Expect.equals(42, rb0());
41 Expect.equals(87, rb1(87));
42 Expect.equals(1 + 2, rb2(1, 2));
43
44 // Return type - arrows.
45 int ra0() => 42;
46 int ra1(a) => a;
47 int ra2(a, b) => a + b;
48 Expect.equals(42, ra0());
49 Expect.equals(87, ra1(87));
50 Expect.equals(1 + 2, ra2(1, 2));
51
52 // Fully typed - braces.
53 int fb1(int a) { return a; }
54 int fb2(int a, int b) { return a + b; }
55 Expect.equals(42, rb0());
56 Expect.equals(87, rb1(87));
57 Expect.equals(1 + 2, rb2(1, 2));
58
59 // Fully typed - arrows.
60 int fa1(int a) => a;
61 int fa2(int a, int b) => a + b;
62 Expect.equals(42, ra0());
63 Expect.equals(87, ra1(87));
64 Expect.equals(1 + 2, ra2(1, 2));
65
66 // Generic types - braces.
67 List<int> gb0() { return [42]; }
68 List<int> gb1(List<int> a) { return a; }
69 Expect.equals(42, gb0()[0]);
70 Expect.equals(87, gb1([87])[0]);
71
72 // Generic types - arrows.
73 List<int> ga0() => [42];
74 List<int> ga1(List<int> a) => a;
75 Expect.equals(42, ga0()[0]);
76 Expect.equals(87, ga1([87])[0]);
77 }
78
79 static void testFunctionExpressions() {
80 eval0(fn) => fn();
81 eval1(fn, a) => fn(a);
82 eval2(fn, a, b) => fn(a, b);
83
84 // No types - braces.
85 Expect.equals(42, eval0(() { return 42; }));
86 Expect.equals(87, eval1((a) { return a; }, 87));
87 Expect.equals(1 + 2, eval2((a, b) { return a + b; }, 1, 2));
88 Expect.equals(42, eval0(nb0() { return 42; }));
89 Expect.equals(87, eval1(nb1(a) { return a; }, 87));
90 Expect.equals(1 + 2, eval2(nb2(a, b) { return a + b; }, 1, 2));
91
92 // No types - arrows.
93 Expect.equals(42, eval0(() => 42));
94 Expect.equals(87, eval1((a) => a, 87));
95 Expect.equals(1 + 2, eval2((a, b) => a + b, 1, 2));
96 Expect.equals(42, eval0(na0() => 42));
97 Expect.equals(87, eval1(na1(a) => a, 87));
98 Expect.equals(1 + 2, eval2(na2(a, b) => a + b, 1, 2));
99
100 // Return type - braces.
101 Expect.equals(42, eval0(int rb0() { return 42; }));
102 Expect.equals(87, eval1(int rb1(a) { return a; }, 87));
103 Expect.equals(1 + 2, eval2(int rb2(a, b) { return a + b; }, 1, 2));
104
105 // Return type - arrows.
106 Expect.equals(42, eval0(int ra0() => 42));
107 Expect.equals(87, eval1(int ra1(a) => a, 87));
108 Expect.equals(1 + 2, eval2(int ra2(a, b) => a + b, 1, 2));
109
110 // Argument types - braces.
111 Expect.equals(42, eval0(() { return 42; }));
112 Expect.equals(87, eval1((int a) { return a; }, 87));
113 Expect.equals(1 + 2, eval2((int a, int b) { return a + b; }, 1, 2));
114 Expect.equals(42, eval0( ab0() { return 42; }));
115 Expect.equals(87, eval1(ab1(int a) { return a; }, 87));
116 Expect.equals(1 + 2, eval2(ab2(int a, int b) { return a + b; }, 1, 2));
117
118 // Argument types - arrows.
119 Expect.equals(42, eval0(() => 42));
120 Expect.equals(87, eval1((int a) => a, 87));
121 Expect.equals(1 + 2, eval2((int a, int b) => a + b, 1, 2));
122 Expect.equals(42, eval0(aa0() => 42));
123 Expect.equals(87, eval1(aa1(int a) => a, 87));
124 Expect.equals(1 + 2, eval2(aa2(int a, int b) => a + b, 1, 2));
125
126 // Fully typed - braces.
127 Expect.equals(87, eval1(int fb1(int a) { return a; }, 87));
128 Expect.equals(1 + 2, eval2(int fb2(int a, int b) { return a + b; }, 1, 2));
129
130 // Fully typed - arrows.
131 Expect.equals(87, eval1(int fa1(int a) => a, 87));
132 Expect.equals(1 + 2, eval2(int fa2(int a, int b) => a + b, 1, 2));
133
134 // Generic types - braces.
135 Expect.equals(42, eval0(List<int> gb0() { return [42]; })[0]);
136 Expect.equals(87, eval1(List<int> gb1(List<int> a) { return a; }, [87])[0]);
137
138 // Generic types - arrows.
139 Expect.equals(42, eval0(List<int> ga0() => [42])[0]);
140 Expect.equals(87, eval1(List<int> ga1(List<int> a) => a, [87])[0]);
141 }
142
143 static void testPrecedence() {
144 expectEvaluatesTo(value, fn) { Expect.equals(value, fn()); }
145
146 // Assignment.
147 var x;
148 expectEvaluatesTo(42, ()=> x = 42);
149 Expect.equals(42, x);
150 x = 1;
151 expectEvaluatesTo(100, ()=> x += 99);
152 Expect.equals(100, x);
153 x = 1;
154 expectEvaluatesTo(87, ()=> x *= 87);
155 Expect.equals(87, x);
156
157 // Conditional.
158 expectEvaluatesTo(42, ()=> true ? 42 : 87);
159 expectEvaluatesTo(87, ()=> false ? 42 : 87);
160
161 // Logical or.
162 expectEvaluatesTo(true, ()=> true || true);
163 expectEvaluatesTo(true, ()=> true || false);
164 expectEvaluatesTo(true, ()=> false || true);
165 expectEvaluatesTo(false, ()=> false || false);
166
167 // Logical and.
168 expectEvaluatesTo(true, ()=> true && true);
169 expectEvaluatesTo(false, ()=> true && false);
170 expectEvaluatesTo(false, ()=> false && true);
171 expectEvaluatesTo(false, ()=> false && false);
172
173 // Bitwise operations.
174 expectEvaluatesTo(3, ()=> 1 | 2);
175 expectEvaluatesTo(2, ()=> 3 ^ 1);
176 expectEvaluatesTo(1, ()=> 3 & 1);
177
178 // Equality.
179 expectEvaluatesTo(true, ()=> 1 == 1);
180 expectEvaluatesTo(false, ()=> 1 != 1);
181 expectEvaluatesTo(true, ()=> 1 === 1);
182 expectEvaluatesTo(false, ()=> 1 !== 1);
183
184 // Relational.
185 expectEvaluatesTo(true, ()=> 1 <= 1);
186 expectEvaluatesTo(false, ()=> 1 < 1);
187 expectEvaluatesTo(false, ()=> 1 > 1);
188 expectEvaluatesTo(true, ()=> 1 >= 1);
189
190 // Is.
191 expectEvaluatesTo(true, ()=> 1 is int);
192 expectEvaluatesTo(true, ()=> 1.0 is double);
193
194 // Shift.
195 expectEvaluatesTo(2, ()=> 1 << 1);
196 expectEvaluatesTo(1, ()=> 2 >> 1);
197
198 // Additive.
199 expectEvaluatesTo(2, ()=> 1 + 1);
200 expectEvaluatesTo(1, ()=> 2 - 1);
201
202 // Multiplicative.
203 expectEvaluatesTo(2, ()=> 1 * 2);
204 expectEvaluatesTo(2.0, ()=> 4 / 2);
205 expectEvaluatesTo(2, ()=> 4 ~/ 2);
206 expectEvaluatesTo(0, ()=> 4 % 2);
207
208 // Negate.
209 expectEvaluatesTo(-3, ()=> ~2);
210 expectEvaluatesTo(false, ()=> !true);
211
212 // Postfix / prefix.
213 var y = 0;
214 expectEvaluatesTo(0, ()=> y++);
215 expectEvaluatesTo(2, ()=> ++y);
216 expectEvaluatesTo(1, ()=> --y);
217 expectEvaluatesTo(1, ()=> y--);
218 Expect.equals(0, y);
219
220 // Selector.
221 fn() => 42;
222 var list = [87];
223 expectEvaluatesTo(42, ()=> fn());
224 expectEvaluatesTo(1, ()=> list.length);
225 expectEvaluatesTo(87, ()=> list[0]);
226 expectEvaluatesTo(87, ()=> list.removeLast());
227 }
228
229 static void testInitializers() {
230 Expect.equals(42, (new C.cb0().fn)());
231 Expect.equals(43, (new C.ca0().fn)());
232 Expect.equals(44, (new C.cb1().fn)());
233 Expect.equals(45, (new C.ca1().fn)());
234 Expect.equals(46, (new C.cb2().fn)());
235 Expect.equals(47, (new C.ca2().fn)());
236 Expect.equals(48, (new C.cb3().fn)());
237 Expect.equals(49, (new C.ca3().fn)());
238
239 Expect.equals(52, (new C.nb0().fn)());
240 Expect.equals(53, (new C.na0().fn)());
241 Expect.equals(54, (new C.nb1().fn)());
242 Expect.equals(55, (new C.na1().fn)());
243 Expect.equals(56, (new C.nb2().fn)());
244 Expect.equals(57, (new C.na2().fn)());
245 Expect.equals(58, (new C.nb3().fn)());
246 Expect.equals(59, (new C.na3().fn)());
247
248 Expect.equals(62, (new C.rb0().fn)());
249 Expect.equals(63, (new C.ra0().fn)());
250 Expect.equals(64, (new C.rb1().fn)());
251 Expect.equals(65, (new C.ra1().fn)());
252 Expect.equals(66, (new C.rb2().fn)());
253 Expect.equals(67, (new C.ra2().fn)());
254 Expect.equals(68, (new C.rb3().fn)());
255 Expect.equals(69, (new C.ra3().fn)());
256 }
257
258 static void testFunctionParameter() {
259 f0(fn()) => fn();
260 Expect.equals(42, f0(()=> 42));
261
262 f1(int fn()) => fn();
263 Expect.equals(87, f1(()=> 87));
264
265 f2(fn(a)) => fn(42);
266 Expect.equals(43, f2((a)=> a + 1));
267
268 f3(fn(int a)) => fn(42);
269 Expect.equals(44, f3((int a)=> a + 2));
270 }
271
272 static void testFunctionIdentifierExpression() {
273 Expect.equals(87, (function() => 87)());
274 }
275
276 static void testFunctionIdentifierStatement() {
277 function() => 42;
278 Expect.equals(42, function());
279 Expect.equals(true, function is Function);
280 }
281
282 }
283
284
285 class C {
286
287 C.cb0() : fn = (() { return 42; }) { }
288 C.ca0() : fn = (() => 43) { }
289
290 C.cb1() : fn = wrap(() { return 44; }) { }
291 C.ca1() : fn = wrap(()=> 45) { }
292
293 C.cb2() : fn = [() { return 46; }][0] { }
294 C.ca2() : fn = [() => 47][0] { }
295
296 C.cb3() : fn = {'x': () { return 48; }}['x'] { }
297 C.ca3() : fn = {'x': () => 49}['x'] { }
298
299 C.nb0() : fn = (f() { return 52; }) { }
300 C.na0() : fn = (f() => 53) { }
301
302 C.nb1() : fn = wrap(f() { return 54; }) { }
303 C.na1() : fn = wrap(f()=> 55) { }
304
305 C.nb2() : fn = [f() { return 56; }][0] { }
306 C.na2() : fn = [f() => 57][0] { }
307
308 C.nb3() : fn = {'x': f() { return 58; }}['x'] { }
309 C.na3() : fn = {'x': f() => 59}['x'] { }
310
311 C.rb0() : fn = (int _() { return 62; }) { }
312 C.ra0() : fn = (int _() => 63) { }
313
314 C.rb1() : fn = wrap(int _() { return 64; }) { }
315 C.ra1() : fn = wrap(int _()=> 65) { }
316
317 C.rb2() : fn = [int _() { return 66; }][0] { }
318 C.ra2() : fn = [int _() => 67][0] { }
319
320 C.rb3() : fn = {'x': int _() { return 68; }}['x'] { }
321 C.ra3() : fn = {'x': int _() => 69}['x'] { }
322
323 static wrap(fn) { return fn; }
324
325 final fn;
326
327 }
328
329 main() {
330 FunctionSyntaxTest.testMain();
331 }
OLDNEW
« no previous file with comments | « tests/language/src/FunctionLiteralsTest.dart ('k') | tests/language/src/FunctionTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698