summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/Object/mutation-prevention-methods.js
blob: 01b4c19cbef1e70ccbf80e2c84688e11afa6893f (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
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 * Contributor:
 *   Jeff Walden <jwalden+code@mit.edu>
 */

var gTestfile = 'mutation-prevention-methods.js';
//-----------------------------------------------------------------------------
var BUGNUMBER = 492849;
var summary = 'Object.is{Sealed,Frozen}, Object.{seal,freeze}';

print(BUGNUMBER + ": " + summary);

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

// Empty object

var o1 = {};

assertEq(Object.isExtensible(o1), true);
assertEq(Object.isSealed(o1), false);
assertEq(Object.isFrozen(o1), false);

Object.preventExtensions(o1);

// An non-extensible empty object has no properties, so it is vacuously sealed
// and frozen.
assertEq(Object.isExtensible(o1), false);
assertEq(Object.isSealed(o1), true);
assertEq(Object.isFrozen(o1), true);


// Object with a data property

var o2 = { 1: 2 };

assertEq(Object.isExtensible(o2), true);
assertEq(Object.isSealed(o2), false);
assertEq(Object.isFrozen(o2), false);

Object.preventExtensions(o2);

assertEq(Object.isExtensible(o2), false);
assertEq(Object.isSealed(o2), false);
assertEq(Object.isFrozen(o2), false);

Object.seal(o2);

assertEq(Object.isExtensible(o2), false);
assertEq(Object.isSealed(o2), true);
assertEq(Object.isFrozen(o2), false);

assertEq(o2[1], 2);

var desc;

desc = Object.getOwnPropertyDescriptor(o2, "1");
assertEq(typeof desc, "object");
assertEq(desc.enumerable, true);
assertEq(desc.configurable, false);
assertEq(desc.value, 2);
assertEq(desc.writable, true);

o2[1] = 17;

assertEq(o2[1], 17);

desc = Object.getOwnPropertyDescriptor(o2, "1");
assertEq(typeof desc, "object");
assertEq(desc.enumerable, true);
assertEq(desc.configurable, false);
assertEq(desc.value, 17);
assertEq(desc.writable, true);

Object.freeze(o2);

assertEq(o2[1], 17);

desc = Object.getOwnPropertyDescriptor(o2, "1");
assertEq(typeof desc, "object");
assertEq(desc.enumerable, true);
assertEq(desc.configurable, false);
assertEq(desc.value, 17);
assertEq(desc.writable, false);


// Object with an accessor property

var o3 = { get foo() { return 17; } };

assertEq(Object.isExtensible(o3), true);
assertEq(Object.isSealed(o3), false);
assertEq(Object.isFrozen(o3), false);

Object.preventExtensions(o3);

assertEq(Object.isExtensible(o3), false);
assertEq(Object.isSealed(o3), false);
assertEq(Object.isFrozen(o3), false);

Object.seal(o3);

// An accessor property in a sealed object is unchanged if that object is
// frozen, so a sealed object containing only accessor properties is also
// vacuously frozen.
assertEq(Object.isExtensible(o3), false);
assertEq(Object.isSealed(o3), true);
assertEq(Object.isFrozen(o3), true);

Object.freeze(o3);

assertEq(Object.isExtensible(o3), false);
assertEq(Object.isSealed(o3), true);
assertEq(Object.isFrozen(o3), true);


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

reportCompare(true, true);

print("All tests passed!");