blob: c1b375a4112685c615ff8b4125de9a8ec9b1fb87 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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// Arrow functions are like functions, except they throw when using the
6// "new" operator on them.
7assertEquals("function", typeof (() => {}));
8assertEquals(Function.prototype, Object.getPrototypeOf(() => {}));
9assertThrows(function() { new (() => {}); }, TypeError);
10assertFalse("prototype" in (() => {}));
11
12// Check the different syntax variations
13assertEquals(1, (() => 1)());
14assertEquals(2, (a => a + 1)(1));
15assertEquals(3, (() => { return 3; })());
16assertEquals(4, (a => { return a + 3; })(1));
17assertEquals(5, ((a, b) => a + b)(1, 4));
18assertEquals(6, ((a, b) => { return a + b; })(1, 5));
19
20// The following are tests from:
21// http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax
22
23// Empty arrow function returns undefined
24var empty = () => {};
25assertEquals(undefined, empty());
26
27// Single parameter case needs no parentheses around parameter list
28var identity = x => x;
29assertEquals(empty, identity(empty));
30
31// No need for parentheses even for lower-precedence expression body
32var square = x => x * x;
33assertEquals(9, square(3));
34
35// Parenthesize the body to return an object literal expression
36var key_maker = val => ({key: val});
37assertEquals(empty, key_maker(empty).key);
38
39// Statement body needs braces, must use 'return' explicitly if not void
40var evens = [0, 2, 4, 6, 8];
41assertEquals([1, 3, 5, 7, 9], evens.map(v => v + 1));
42
43var fives = [];
44[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(v => {
45 if (v % 5 === 0) fives.push(v);
46});
47assertEquals([5, 10], fives);
48
49(function testRestrictedFunctionPropertiesStrict() {
50 var arrowFn = () => { "use strict"; };
51 assertFalse(arrowFn.hasOwnProperty("arguments"));
52 assertThrows(function() { return arrowFn.arguments; }, TypeError);
53 assertThrows(function() { arrowFn.arguments = {}; }, TypeError);
54
55 assertFalse(arrowFn.hasOwnProperty("caller"));
56 assertThrows(function() { return arrowFn.caller; }, TypeError);
57 assertThrows(function() { arrowFn.caller = {}; }, TypeError);
58})();
59
60
61(function testRestrictedFunctionPropertiesSloppy() {
62 var arrowFn = () => {};
63 assertFalse(arrowFn.hasOwnProperty("arguments"));
64 assertThrows(function() { return arrowFn.arguments; }, TypeError);
65 assertThrows(function() { arrowFn.arguments = {}; }, TypeError);
66
67 assertFalse(arrowFn.hasOwnProperty("caller"));
68 assertThrows(function() { return arrowFn.caller; }, TypeError);
69 assertThrows(function() { arrowFn.caller = {}; }, TypeError);
70})();
71
72
73// v8:4474
74(function testConciseBodyReturnsRegexp() {
75 var arrow1 = () => /foo/
76 var arrow2 = () => /foo/;
77 var arrow3 = () => /foo/i
78 var arrow4 = () => /foo/i;
79 assertEquals(arrow1.toString(), "() => /foo/");
80 assertEquals(arrow2.toString(), "() => /foo/");
81 assertEquals(arrow3.toString(), "() => /foo/i");
82 assertEquals(arrow4.toString(), "() => /foo/i");
83});