summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/class/className.js
blob: e72510e18d952b05d408fa06de625b3196f1ff96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
function testName(C, name, hasValue, hasGetter, hasSetter, isFunction=false) {
    if (isFunction) {
        assertEq(typeof C.name, "function");
    } else {
        assertEq(C.name, name);
    }

    var desc = Object.getOwnPropertyDescriptor(C, "name");
    if (!hasValue && !hasGetter && !hasSetter) {
        assertEq(desc, undefined);
        return;
    }

    if (hasValue) {
        assertEq("value" in desc, true);
        if (isFunction) {
            assertEq(typeof desc.value, "function");
        } else {
            assertEq(desc.value, name);
        }
        // FIXME: Methods defined in classes should not be enumerable
        // (bug 1144630).
        // assertEq(desc.enumerable, false);
        assertEq(desc.configurable, true);

        assertEq("get" in desc, false);
        assertEq("set" in desc, false);

        return;
    }

    assertEq("value" in desc, false);

    if (hasGetter) {
        assertEq("get" in desc, true);
        assertEq(desc.get(), name);
        // assertEq(desc.enumerable, false);
        assertEq(desc.configurable, true);
    } else {
        assertEq("get" in desc, true);
        assertEq(desc.get, undefined);
        // assertEq(desc.enumerable, false);
        assertEq(desc.configurable, true);
    }

    if (hasSetter) {
        assertEq("set" in desc, true);
        assertEq(typeof desc.set, "function");
        // assertEq(desc.enumerable, false);
        assertEq(desc.configurable, true);
    } else {
        assertEq("set" in desc, true);
        assertEq(desc.set, undefined);
        // assertEq(desc.enumerable, false);
        assertEq(desc.configurable, true);
    }
}

// ---- declaration ---

class Decl {
    constructor() {}
}
testName(Decl, "Decl", true, false, false);

class DeclWithFunc {
    constructor() {}
    static name() {}
}
testName(DeclWithFunc, "DeclWithFunc", true, false, false, true);

class DeclWithGetter {
    constructor() {}
    static get name() { return "base"; }
}
testName(DeclWithGetter, "base", false, true, false);

class DeclWithSetter {
    constructor() {}
    static set name(v) {}
}
testName(DeclWithSetter, undefined, false, false, true);

class DeclWithGetterSetter {
    constructor() {}
    static get name() { return "base"; }
    static set name(v) {}
}
testName(DeclWithGetterSetter, "base", false, true, true);

function prop() {
  return "name";
}
class DeclWithComputed {
    constructor() {}
    static get [prop()]() { return "base"; }
}
testName(DeclWithGetterSetter, "base", false, true, true);

class ExtendedDecl1 extends Decl {
    constructor() {}
}
testName(ExtendedDecl1, "ExtendedDecl1", true, false, false);
delete ExtendedDecl1.name;
testName(ExtendedDecl1, "Decl", false, false, false);

class ExtendedDecl2 extends DeclWithGetterSetter {
    constructor() {}
    static get name() { return "extend"; }
}
testName(ExtendedDecl2, "extend", false, true, false);
delete ExtendedDecl2.name;
testName(ExtendedDecl2, "base", false, false, false);

class ExtendedDecl3 extends DeclWithGetterSetter {
    constructor() {}
    static set name(v) {}
}
testName(ExtendedDecl3, undefined, false, false, true);
delete ExtendedDecl3.name;
testName(ExtendedDecl3, "base", false, false, false);

// ---- expression ---

let Expr = class Expr {
    constructor() {}
};
testName(Expr, "Expr", true, false, false);

let ExprWithGetter = class ExprWithGetter {
    constructor() {}
    static get name() { return "base"; }
};
testName(ExprWithGetter, "base", false, true, false);

let ExprWithSetter = class ExprWithSetter {
    constructor() {}
    static set name(v) {}
};
testName(ExprWithSetter, undefined, false, false, true);

let ExprWithGetterSetter = class ExprWithGetterSetter {
    constructor() {}
    static get name() { return "base"; }
    static set name(v) {}
};
testName(ExprWithGetterSetter, "base", false, true, true);


let ExtendedExpr1 = class ExtendedExpr1 extends Expr {
    constructor() {}
};
testName(ExtendedExpr1, "ExtendedExpr1", true, false, false);
delete ExtendedExpr1.name;
testName(ExtendedExpr1, "Expr", false, false, false);

let ExtendedExpr2 = class ExtendedExpr2 extends ExprWithGetterSetter {
    constructor() {}
    static get name() { return "extend"; }
};
testName(ExtendedExpr2, "extend", false, true, false);
delete ExtendedExpr2.name;
testName(ExtendedExpr2, "base", false, false, false);

let ExtendedExpr3 = class ExtendedExpr3 extends ExprWithGetterSetter {
    constructor() {}
    static set name(v) {}
};
testName(ExtendedExpr3, undefined, false, false, true);
delete ExtendedExpr3.name;
testName(ExtendedExpr3, "base", false, false, false);

// ---- anonymous ----

// Anonymous class expressions use the empty string as the default name property.
// Use property assignment to avoid setting name property.
let tmp = {};
let Anon = tmp.value = class {
    constructor() {}
};
testName(Anon, "", true, false, false);

let AnonDefault = tmp.value = class { };
testName(AnonDefault, "", true, false, false);

let AnonWithGetter = tmp.value = class {
    constructor() {}
    static get name() { return "base"; }
};
testName(AnonWithGetter, "base", false, true, false);

let AnonWithSetter = tmp.value = class {
    constructor() {}
    static set name(v) {}
};
testName(AnonWithSetter, undefined, false, false, true);

let AnonWithGetterSetter = tmp.value = class {
    constructor() {}
    static get name() { return "base"; }
    static set name(v) {}
};
testName(AnonWithGetterSetter, "base", false, true, true);


let ExtendedAnon1 = tmp.value = class extends Anon {
    constructor() {}
};
testName(ExtendedAnon1, "", true, false, false);

let ExtendedAnonDefault = tmp.value = class extends Anon { };
testName(ExtendedAnonDefault, "", true, false, false);

let ExtendedAnon2 = tmp.value = class extends AnonWithGetterSetter {
    constructor() {}
    static get name() { return "extend"; }
};
testName(ExtendedAnon2, "extend", false, true, false);
delete ExtendedAnon2.name;
testName(ExtendedAnon2, "base", false, false, false);

let ExtendedAnon3 = tmp.value = class extends AnonWithGetterSetter {
    constructor() {}
    static set name(v) {}
};
testName(ExtendedAnon3, undefined, false, false, true);
delete ExtendedAnon3.name;
testName(ExtendedAnon3, "base", false, false, false);

// ---- mixed ----

let ExtendedExpr4 = class ExtendedExpr4 extends Anon {
    constructor() {}
}
testName(ExtendedExpr4, "ExtendedExpr4", true, false, false);
delete ExtendedExpr4.name;
testName(ExtendedExpr4, "", false, false, false);

let ExtendedExpr5 = class ExtendedExpr5 extends AnonWithGetterSetter {
    constructor() {}
    static get name() { return "extend"; }
}
testName(ExtendedExpr5, "extend", false, true, false);
delete ExtendedExpr5.name;
testName(ExtendedExpr5, "base", false, false, false);

if (typeof reportCompare === "function")
    reportCompare(0, 0, "OK");