summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/extensions/es5ish-defineGetter-defineSetter.js
blob: 681a157a8b3c5ea85a7eb5b7fa242e33b058f0c1 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

//-----------------------------------------------------------------------------
var BUGNUMBER = 715821;
var summary = "Implement __define[GS]etter__ using Object.defineProperty";

print(BUGNUMBER + ": " + summary);

/*************
 * UTILITIES *
 *************/

function s(desc)
{
  if (typeof desc === "undefined")
    return "<undefined>";
  assertEq(typeof desc, "object");
  assertEq(desc !== null, true);

  var str = "<enumerable: <" + desc.enumerable + ">, " +
            " configurable: <" + desc.configurable + ">,";

  if (desc.hasOwnProperty("value"))
  {
    return str +
           " value: <" + desc.value + ">," +
           " writable: <" + desc.writable + ">>";
  }

  return str +
         " get: <" + desc.get + ">," +
         " set: <" + desc.set + ">>";
}

function checkField(field, desc, expected)
{
  var present = desc.hasOwnProperty(field);
  assertEq(present, expected.hasOwnProperty(field),
           field + " presence mismatch (got " + s(desc) + ", expected " + s(expected) + ")");
  if (present)
  {
    assertEq(desc[field], expected[field],
             field + " value mismatch (got " + s(desc) + ", expected " + s(expected) + ")");
  }
}

function check(obj, prop, expected)
{
  var desc = Object.getOwnPropertyDescriptor(obj, prop);
  assertEq(typeof desc, typeof expected,
           "type mismatch (got " + s(desc) + ", expected " + s(expected) + ")");

  assertEq(desc.hasOwnProperty("get"), desc.hasOwnProperty("set"),
           "bad descriptor: " + s(desc));
  assertEq(desc.hasOwnProperty("value"), desc.hasOwnProperty("writable"),
           "bad descriptor: " + s(desc));

  assertEq(desc.hasOwnProperty("get"), !desc.hasOwnProperty("value"),
           "bad descriptor: " + s(desc));

  checkField("get", desc, expected);
  checkField("set", desc, expected);
  checkField("value", desc, expected);
  checkField("writable", desc, expected);
  checkField("enumerable", desc, expected);
  checkField("configurable", desc, expected);
}

function expectTypeError(f)
{
  try
  {
    f();
    throw new Error("no error thrown");
  }
  catch (e)
  {
    assertEq(e instanceof TypeError, true,
             "wrong error thrown: got " + e + ", not a TypeError");
  }
}

/**************
 * BEGIN TEST *
 **************/

// Adding a new getter, overwriting an existing one

function g1() { }
var gobj = {};
gobj.__defineGetter__("foo", g1);
check(gobj, "foo", { get: g1, set: undefined, enumerable: true, configurable: true });

function g2() { }
gobj.__defineGetter__("foo", g2);
check(gobj, "foo", { get: g2, set: undefined, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a new setter, overwriting an existing one

function s1() { }
var sobj = {};
sobj.__defineSetter__("bar", s1);
check(sobj, "bar", { get: undefined, set: s1, enumerable: true, configurable: true });

function s2() { }
sobj.__defineSetter__("bar", s2);
check(sobj, "bar", { get: undefined, set: s2, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a new getter, then adding a setter
// Changing an existing accessor's enumerability, then "null"-changing the accessor
// Changing an accessor's configurability, then "null"-changing and real-changing the accessor

function g3() { }
var gsobj = {};
gsobj.__defineGetter__("baz", g3);
check(gsobj, "baz", { get: g3, set: undefined, enumerable: true, configurable: true });

function s3() { }
gsobj.__defineSetter__("baz", s3);
check(gsobj, "baz", { get: g3, set: s3, enumerable: true, configurable: true });

Object.defineProperty(gsobj, "baz", { enumerable: false });
check(gsobj, "baz", { get: g3, set: s3, enumerable: false, configurable: true });

gsobj.__defineGetter__("baz", g3);
check(gsobj, "baz", { get: g3, set: s3, enumerable: true, configurable: true });

Object.defineProperty(gsobj, "baz", { enumerable: false });
check(gsobj, "baz", { get: g3, set: s3, enumerable: false, configurable: true });

gsobj.__defineSetter__("baz", s3);
check(gsobj, "baz", { get: g3, set: s3, enumerable: true, configurable: true });

Object.defineProperty(gsobj, "baz", { configurable: false });
expectTypeError(function() { gsobj.__defineSetter__("baz", s2); });
expectTypeError(function() { gsobj.__defineSetter__("baz", s3); });
check(gsobj, "baz", { get: g3, set: s3, enumerable: true, configurable: false });

/******************************************************************************/

// Adding a new setter, then adding a getter
// Changing an existing accessor's enumerability, then "null"-changing the accessor
// Changing an accessor's configurability, then "null"-changing and real-changing the accessor

function s4() { }
var sgobj = {};
sgobj.__defineSetter__("baz", s4);
check(sgobj, "baz", { get: undefined, set: s4, enumerable: true, configurable: true });

function g4() { }
sgobj.__defineGetter__("baz", g4);
check(sgobj, "baz", { get: g4, set: s4, enumerable: true, configurable: true });

Object.defineProperty(sgobj, "baz", { enumerable: false });
check(sgobj, "baz", { get: g4, set: s4, enumerable: false, configurable: true });

sgobj.__defineSetter__("baz", s4);
check(sgobj, "baz", { get: g4, set: s4, enumerable: true, configurable: true });

Object.defineProperty(sgobj, "baz", { enumerable: false });
check(sgobj, "baz", { get: g4, set: s4, enumerable: false, configurable: true });

sgobj.__defineSetter__("baz", s4);
check(sgobj, "baz", { get: g4, set: s4, enumerable: true, configurable: true });

Object.defineProperty(sgobj, "baz", { configurable: false });
expectTypeError(function() { sgobj.__defineGetter__("baz", g3); });
expectTypeError(function() { sgobj.__defineSetter__("baz", s4); });
check(sgobj, "baz", { get: g4, set: s4, enumerable: true, configurable: false });

/******************************************************************************/

// Adding a getter over a writable data property

function g5() { }
var gover = { quux: 17 };
check(gover, "quux", { value: 17, writable: true, enumerable: true, configurable: true });

gover.__defineGetter__("quux", g5);
check(gover, "quux", { get: g5, set: undefined, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a setter over a writable data property

function s5() { }
var sover = { quux: 17 };
check(sover, "quux", { value: 17, writable: true, enumerable: true, configurable: true });

sover.__defineSetter__("quux", s5);
check(sover, "quux", { get: undefined, set: s5, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a getter over a non-writable data property

function g6() { }
var gnover = { eit: 17 };
check(gnover, "eit", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(gnover, "eit", { writable: false });
check(gnover, "eit", { value: 17, writable: false, enumerable: true, configurable: true });

gnover.__defineGetter__("eit", g6);
check(gnover, "eit", { get: g6, set: undefined, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a setter over a non-writable data property

function s6() { }
var snover = { eit: 17 };
check(snover, "eit", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(snover, "eit", { writable: false });
check(snover, "eit", { value: 17, writable: false, enumerable: true, configurable: true });

snover.__defineSetter__("eit", s6);
check(snover, "eit", { get: undefined, set: s6, enumerable: true, configurable: true });

/******************************************************************************/

// Adding a getter over a non-configurable, writable data property

function g7() { }
var gncover = { moo: 17 };
check(gncover, "moo", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(gncover, "moo", { configurable: false });
check(gncover, "moo", { value: 17, writable: true, enumerable: true, configurable: false });

expectTypeError(function() { gncover.__defineGetter__("moo", g7); });
check(gncover, "moo", { value: 17, writable: true, enumerable: true, configurable: false });

/******************************************************************************/

// Adding a setter over a non-configurable, writable data property

function s7() { }
var sncover = { moo: 17 };
check(sncover, "moo", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(sncover, "moo", { configurable: false });
check(sncover, "moo", { value: 17, writable: true, enumerable: true, configurable: false });

expectTypeError(function() { sncover.__defineSetter__("moo", s7); });
check(sncover, "moo", { value: 17, writable: true, enumerable: true, configurable: false });

/******************************************************************************/

// Adding a getter over a non-configurable, non-writable data property

function g8() { }
var gncwover = { fwoosh: 17 };
check(gncwover, "fwoosh", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(gncwover, "fwoosh", { writable: false, configurable: false });
check(gncwover, "fwoosh", { value: 17, writable: false, enumerable: true, configurable: false });

expectTypeError(function() { gncwover.__defineGetter__("fwoosh", g7); });
check(gncwover, "fwoosh", { value: 17, writable: false, enumerable: true, configurable: false });

/******************************************************************************/

// Adding a setter over a non-configurable, non-writable data property

function s8() { }
var sncwover = { fwoosh: 17 };
check(sncwover, "fwoosh", { value: 17, writable: true, enumerable: true, configurable: true });
Object.defineProperty(sncwover, "fwoosh", { writable: false, configurable: false });
check(sncwover, "fwoosh", { value: 17, writable: false, enumerable: true, configurable: false });

expectTypeError(function() { sncwover.__defineSetter__("fwoosh", s7); });
check(sncwover, "fwoosh", { value: 17, writable: false, enumerable: true, configurable: false });

/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("Tests complete");