3
3
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4
4
//-------------------------------------------------------------------------------------------------------
5
5
6
- function write ( args )
7
- {
8
- WScript . Echo ( args ) ;
6
+ if ( this . WScript && this . WScript . LoadScriptFile ) { // Check for running in ch
7
+ this . WScript . LoadScriptFile ( "..\\UnitTestFramework\\UnitTestFramework.js" ) ;
9
8
}
10
9
11
- write ( "TestCase1" ) ;
12
- write ( Object . freeze . length ) ;
13
- write ( Object . isFrozen ( { } ) ) ;
14
-
15
- write ( "TestCase2 - freeze & add a property" ) ;
16
- var a = { x :20 , y :30 } ;
17
- Object . freeze ( a ) ;
18
- SafeCall ( function ( ) { a . z = 50 ; } ) ;
19
- write ( Object . getOwnPropertyNames ( a ) ) ;
20
- write ( Object . isFrozen ( a ) ) ;
21
-
22
- write ( "TestCase3 - freeze & delete a property" ) ;
23
- var a = { x :20 , y :30 } ;
24
- Object . freeze ( a ) ;
25
- SafeCall ( function ( ) { delete a . x ; } ) ;
26
- write ( Object . getOwnPropertyNames ( a ) ) ;
27
- write ( Object . isFrozen ( a ) ) ;
28
- write ( a . x ) ;
29
-
30
- write ( "TestCase4 - freeze & modify a property" ) ;
31
- var a = { x :20 , y :30 } ;
32
- Object . freeze ( a ) ;
33
- SafeCall ( function ( ) { a . x = 40 ; } ) ;
34
- SafeCall ( function ( ) { a . y = 60 ; } ) ;
35
- write ( Object . getOwnPropertyNames ( a ) ) ;
36
- write ( Object . isFrozen ( a ) ) ;
37
- write ( a . x ) ;
38
-
39
- function SafeCall ( f )
40
- {
41
- try
10
+ var tests = [
42
11
{
43
- f ( ) ;
44
- }
45
- catch ( e )
12
+ name : "Add, delete, modify properties after freezing" ,
13
+ body : function ( ) {
14
+ let a = { x : 42 } ;
15
+
16
+ Object . freeze ( a ) ;
17
+ assert . isFalse ( Object . isExtensible ( a ) ) ;
18
+ assert . isTrue ( Object . isSealed ( a ) ) ;
19
+ assert . isTrue ( Object . isFrozen ( a ) ) ;
20
+
21
+ // cannot add new properties
22
+ a . y = 17 ;
23
+ assert . isFalse ( a . hasOwnProperty ( 'y' ) ) ;
24
+ assert . throws ( function ( ) { 'use strict' ; a . y = 17 ; } , TypeError ,
25
+ "Should throw on creating a new property in frozen object in strict mode" ,
26
+ "Cannot create property for a non-extensible object" ) ;
27
+
28
+ // cannot delete properties
29
+ assert . isFalse ( delete a . x ) ;
30
+ assert . isTrue ( a . hasOwnProperty ( 'x' ) ) ;
31
+ assert . throws ( function ( ) { 'use strict' ; delete a . x ; } , TypeError ,
32
+ "Should throw on creating a new property in frozen object in strict mode" ,
33
+ "Calling delete on 'x' is not allowed in strict mode" ) ;
34
+
35
+ // cannot change prototype
36
+ let b = { } ;
37
+ assert . throws ( function ( ) { 'use strict' ; Object . setPrototypeOf ( a , b ) ; } , TypeError ,
38
+ "Should throw on creating a new property in sealed object in strict mode" ,
39
+ "Cannot create property for a non-extensible object" ) ;
40
+
41
+ // existing properties should be set to non-writable and non-configurable
42
+ let descr = Object . getOwnPropertyDescriptor ( a , 'x' ) ;
43
+ assert . isFalse ( descr . configurable ) ;
44
+ assert . isFalse ( descr . writable ) ;
45
+ }
46
+ } ,
47
+ {
48
+ name : "Add, delete, modify indexed elements of an array after freezing" ,
49
+ body : function ( ) {
50
+ let a = [ 42 ] ;
51
+ a [ 2 ] = 43 ;
52
+
53
+ Object . freeze ( a ) ;
54
+ assert . isFalse ( Object . isExtensible ( a ) ) ;
55
+ assert . isTrue ( Object . isSealed ( a ) ) ;
56
+ assert . isTrue ( Object . isFrozen ( a ) ) ;
57
+
58
+ // the array cannot be extended
59
+ a [ 3 ] = 17 ;
60
+ assert . areEqual ( 3 , a . length ) ;
61
+ assert . isFalse ( a . hasOwnProperty ( '3' ) )
62
+ assert . throws ( function ( ) { 'use strict' ; a [ 3 ] = 17 ; } , TypeError ,
63
+ "Should throw on creating a new property in frozen object in strict mode" ,
64
+ "Cannot create property for a non-extensible object" ) ;
65
+
66
+ // a hole cannot be filled
67
+ a [ 1 ] = 17 ;
68
+ assert . areEqual ( 3 , a . length ) ;
69
+ assert . isFalse ( a . hasOwnProperty ( '1' ) )
70
+ assert . throws ( function ( ) { 'use strict' ; a [ 1 ] = 17 ; } , TypeError ,
71
+ "Should throw on creating a new property in frozen object in strict mode" ,
72
+ "Cannot create property for a non-extensible object" ) ;
73
+
74
+ // existing elements cannot be deleted
75
+ assert . isFalse ( delete a [ 0 ] ) ;
76
+ assert . isTrue ( a . hasOwnProperty ( '0' ) ) ;
77
+ assert . throws ( function ( ) { 'use strict' ; delete a [ 0 ] ; } , TypeError ,
78
+ "Should throw on creating a new property in frozen object in strict mode" ,
79
+ "Calling delete on '0' is not allowed in strict mode" ) ;
80
+
81
+ // existing elements cannot be modified
82
+ let descr = Object . getOwnPropertyDescriptor ( a , '0' ) ;
83
+ assert . isFalse ( descr . configurable ) ;
84
+ assert . isFalse ( descr . writable ) ;
85
+ a [ 0 ] = 17 ;
86
+ assert . areEqual ( 42 , a [ 0 ] ) ;
87
+ assert . throws ( function ( ) { 'use strict' ; a [ 0 ] = 17 ; } , TypeError ,
88
+ "Should throw on creating a new property in frozen object in strict mode" ,
89
+ "Assignment to read-only properties is not allowed in strict mode" ) ;
90
+
91
+ // the special 'length' property also cannot be modified
92
+ let descr_len = Object . getOwnPropertyDescriptor ( a , 'length' ) ;
93
+ assert . isFalse ( descr_len . configurable ) ;
94
+ assert . isFalse ( descr_len . writable ) ;
95
+ }
96
+ } ,
46
97
{
47
- write ( "Exception: " + e . name ) ;
48
- }
49
- }
98
+ // what is the spec???
99
+ // v8 doesn't allow freezing typed arrays at all
100
+ name : "Add, delete, modify indexed elements of a typed array after freezing" ,
101
+ body : function ( ) {
102
+ let a = new Int8Array ( 1 ) ;
103
+ a [ 0 ] = 42 ;
104
+
105
+ Object . freeze ( a ) ; // should it throw?
106
+
107
+ assert . isFalse ( Object . isExtensible ( a ) ) ;
108
+ assert . isTrue ( Object . isSealed ( a ) ) ;
109
+ assert . isTrue ( Object . isFrozen ( a ) ) ; // should it return false?
110
+
111
+ // current behavior:
112
+ // even though the array is frozen it's ok to modify existing elements
113
+ a [ 0 ] = 17 ;
114
+ assert . areEqual ( 17 , a [ 0 ] ) ;
115
+ }
116
+ } ,
117
+ ] ;
118
+
119
+ testRunner . runTests ( tests , { verbose : false /*so no need to provide baseline*/ } ) ;
0 commit comments