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

Unified Diff: test/mjsunit/harmony/arrow-functions.js

Issue 382893003: Implement basic code generation for arrow functions (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/arrow-functions.js
diff --git a/test/mjsunit/harmony/arrow-functions.js b/test/mjsunit/harmony/arrow-functions.js
new file mode 100644
index 0000000000000000000000000000000000000000..043bcf077c63cccb7f9951991a91f228110ae296
--- /dev/null
+++ b/test/mjsunit/harmony/arrow-functions.js
@@ -0,0 +1,47 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-arrow-functions
+
+// Arrow functions are like functions, except they throw when using the
+// "new" operator on them.
+assertEquals(typeof (() => {}), "function");
rossberg 2014/07/14 13:55:46 Nit: expected value should go left (similarly belo
+assertEquals(Object.getPrototypeOf(() => {}), Function.prototype);
+assertThrows("new (() => {})", TypeError);
+
+// Check the different syntax variations
+assertEquals(1, (() => 1)());
+assertEquals(2, (a => a + 1)(1));
+assertEquals(3, (() => { return 3; })());
+assertEquals(4, (a => { return a + 3; })(1));
+assertEquals(5, ((a, b) => a + b)(1, 4));
+assertEquals(6, ((a, b) => { return a + b; })(1, 5));
+
+// The following are tests from:
+// http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax
+
+// Empty arrow function returns undefined
+var empty = () => {};
+assertEquals(empty(), undefined);
+
+// Single parameter case needs no parentheses around parameter list
+var identity = x => x;
+assertEquals(identity(empty), empty);
+
+// No need for parentheses even for lower-precedence expression body
+var square = x => x * x;
+assertEquals(square(3), 9);
+
+// Parenthesize the body to return an object literal expression
+var key_maker = val => ({key: val});
+assertEquals(key_maker(empty).key, empty);
+
+// Statement body needs braces, must use 'return' explicitly if not void
+var evens = [0, 2, 4, 6, 8];
+var odds = evens.map(v => v + 1);
+assertEquals(odds, [1, 3, 5, 7, 9]);
+
+var fives = [];
+[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(v => { if (v % 5 === 0) fives.push(v); });
rossberg 2014/07/14 13:55:46 Nit: line length
+assertEquals(fives, [5, 10]);

Powered by Google App Engine
This is Rietveld 408576698