1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --harmony-arrow-functions
6
7// Arrow functions are like functions, except they throw when using the
8// "new" operator on them.
9assertEquals("function", typeof (() => {}));
10assertEquals(Function.prototype, Object.getPrototypeOf(() => {}));
11assertThrows(function() { new (() => {}); }, TypeError);
12assertFalse("prototype" in (() => {}));
13
14// Check the different syntax variations
15assertEquals(1, (() => 1)());
16assertEquals(2, (a => a + 1)(1));
17assertEquals(3, (() => { return 3; })());
18assertEquals(4, (a => { return a + 3; })(1));
19assertEquals(5, ((a, b) => a + b)(1, 4));
20assertEquals(6, ((a, b) => { return a + b; })(1, 5));
21
22// The following are tests from:
23// http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax
24
25// Empty arrow function returns undefined
26var empty = () => {};
27assertEquals(undefined, empty());
28
29// Single parameter case needs no parentheses around parameter list
30var identity = x => x;
31assertEquals(empty, identity(empty));
32
33// No need for parentheses even for lower-precedence expression body
34var square = x => x * x;
35assertEquals(9, square(3));
36
37// Parenthesize the body to return an object literal expression
38var key_maker = val => ({key: val});
39assertEquals(empty, key_maker(empty).key);
40
41// Statement body needs braces, must use 'return' explicitly if not void
42var evens = [0, 2, 4, 6, 8];
43assertEquals([1, 3, 5, 7, 9], evens.map(v => v + 1));
44
45var fives = [];
46[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(v => {
47  if (v % 5 === 0) fives.push(v);
48});
49assertEquals([5, 10], fives);
50