1// Copyright 2015 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-function-name
6
7(function testVariableDeclarationsFunction() {
8  'use strict';
9  var a = function(){};
10  assertEquals('a', a.name);
11  let b = () => {};
12  assertEquals('b', b.name);
13  const c = ((function(){}));
14  assertEquals('c', c.name);
15
16  var x = function(){}, y = () => {}, z = function withName() {};
17  assertEquals('x', x.name);
18  assertEquals('y', y.name);
19  assertEquals('withName', z.name);
20})();
21
22(function testVariableDeclarationsClass() {
23  'use strict';
24  var a = class {};
25  assertEquals('a', a.name);
26  let b = ((class {}));
27  assertEquals('b', b.name);
28  // Should not overwrite name property.
29  const c = class { static name() { } }
30  assertEquals('function', typeof c.name);
31
32  var x = class {}, y = class NamedClass {};
33  assertEquals('x', x.name);
34  assertEquals('NamedClass', y.name);
35})();
36
37(function testObjectProperties() {
38  'use strict';
39  var obj = {
40    a: function() {},
41    b: () => {},
42    c() { },
43    get d() { },
44    set d(val) { },
45    x: function withName() { },
46    y: class { },
47    z: class ClassName { },
48    42: function() {},
49    4.2: function() {},
50    __proto__: function() {},
51  };
52
53  assertEquals('a', obj.a.name);
54  assertEquals('b', obj.b.name);
55  assertEquals('c', obj.c.name);
56  var dDescriptor = Object.getOwnPropertyDescriptor(obj, 'd');
57  assertEquals('get d', dDescriptor.get.name);
58  assertEquals('set d', dDescriptor.set.name);
59  assertEquals('withName', obj.x.name);
60  assertEquals('y', obj.y.name);
61  assertEquals('ClassName', obj.z.name);
62  assertEquals('42', obj[42].name);
63  assertEquals('4.2', obj[4.2].name);
64  assertEquals('', obj.__proto__.name);
65})();
66
67(function testClassProperties() {
68  'use strict';
69  class C {
70    a() { }
71    static b() { }
72    get c() { }
73    set c(val) { }
74    42() { }
75    static 43() { }
76    get 44() { }
77    set 44(val) { }
78  };
79
80  assertEquals('a', C.prototype.a.name);
81  assertEquals('b', C.b.name);
82  var descriptor = Object.getOwnPropertyDescriptor(C.prototype, 'c');
83  assertEquals('get c', descriptor.get.name);
84  assertEquals('set c', descriptor.set.name);
85  assertEquals('42', C.prototype[42].name);
86  assertEquals('43', C[43].name);
87  var descriptor = Object.getOwnPropertyDescriptor(C.prototype, '44');
88  assertEquals('get 44', descriptor.get.name);
89  assertEquals('set 44', descriptor.set.name);
90})();
91
92// TODO(adamk): Make computed property names work.
93(function testComputedProperties() {
94  'use strict';
95  var a = 'a';
96  var sym1 = Symbol('1');
97  var sym2 = Symbol('2');
98  var obj = {
99    [a]: function() {},
100    [sym1]: function() {},
101    [sym2]: function withName() {},
102  };
103
104  // Should be 'a'
105  assertEquals('', obj[a].name);
106  // Should be '[1]'
107  assertEquals('', obj[sym1].name);
108  assertEquals('withName', obj[sym2].name);
109
110  class C {
111    [a]() { }
112    [sym1]() { }
113    static [sym2]() { }
114  }
115
116  // Should be 'a'
117  assertEquals('', C.prototype[a].name);
118  // Should be '[1]'
119  assertEquals('', C.prototype[sym1].name);
120  // Should be '[2]'
121  assertEquals('', C[sym2].name);
122})();
123
124
125(function testAssignment() {
126  var basicFn, arrowFn, generatorFn, classLit;
127
128  basicFn = function() { return true; };
129  assertEquals('basicFn', basicFn.name);
130  var basicFn2 = basicFn;
131  assertEquals('basicFn', basicFn2.name);
132  basicFn = function functionWithName() { };
133  assertEquals("functionWithName", basicFn.name);
134
135  arrowFn = x => x;
136  assertEquals('arrowFn', arrowFn.name);
137  var arrowFn2 = arrowFn;
138  assertEquals('arrowFn', arrowFn2.name);
139
140  generatorFn = function*() { yield true; };
141  assertEquals('generatorFn', generatorFn.name);
142  var generatorFn2 = generatorFn;
143  assertEquals('generatorFn', generatorFn2.name);
144  generatorFn = function* generatorWithName() { };
145  assertEquals("generatorWithName", generatorFn.name);
146
147  classLit = class { constructor() {} };
148  assertEquals('classLit', classLit.name);
149  var classLit2 = classLit;
150  assertEquals('classLit', classLit2.name);
151  classLit = class classWithName { constructor() {} };
152  assertEquals('classWithName', classLit.name);
153  classLit = class { constructor() {} static name() {} };
154  assertEquals('function', typeof classLit.name);
155  classLit = class { constructor() {} static get name() { return true; } };
156  assertTrue(classLit.name);
157  classLit = class { constructor() {} static ['name']() {} };
158  assertEquals('function', typeof classLit.name);
159  classLit = class { constructor() {} static get ['name']() { return true; } };
160  assertTrue(classLit.name);
161})();
162