3
3
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4
4
//-------------------------------------------------------------------------------------------------------
5
5
6
- WScript . Echo ( "typeof (new String()) : " + typeof ( new String ( "" ) ) ) ;
7
- WScript . Echo ( "typeof () : " + typeof ( "" ) ) ;
6
+ WScript . LoadScriptFile ( "..\\UnitTestFramework\\UnitTestFramework.js" ) ;
8
7
9
- WScript . Echo ( "typeof (new Boolean(false)) : " + typeof ( new Boolean ( false ) ) ) ;
10
- WScript . Echo ( "typeof (false) : " + typeof ( false ) ) ;
8
+ var tests = [
9
+ {
10
+ name : "typeof of literals, built-in types and object wrappers" ,
11
+ body : function ( ) {
12
+ var arr = [ 42 ] ;
11
13
12
- WScript . Echo ( "typeof (new Number(0)) : " + typeof ( new Number ( 0 ) ) ) ;
13
- WScript . Echo ( "typeof (0) : " + typeof ( 0 ) ) ;
14
+ assert . areEqual ( "object" , typeof null , "typeof null" ) ;
15
+ assert . areEqual ( "undefined" , typeof undefined , "typeof undefined" ) ;
16
+ assert . areEqual ( "string" , typeof "" , "typeof empty string" ) ;
17
+ assert . areEqual ( "boolean" , typeof false , "typeof false" ) ;
18
+ assert . areEqual ( "number" , typeof 0 , "typeof 0" ) ;
19
+ assert . areEqual ( "number" , typeof 12345.678 , "typeof 12345.678" ) ;
20
+ assert . areEqual ( "object" , typeof { } , "typeof {}" ) ;
21
+ assert . areEqual ( "object" , typeof arr , "typeof array" ) ;
22
+ assert . areEqual ( "number" , typeof arr [ 0 ] , "typeof arr[0]" ) ;
23
+ assert . areEqual ( "undefined" , typeof arr [ 1 ] , "typeof arr[1]" ) ;
24
+ assert . areEqual ( "object" , typeof / a b c / , "typeof /abc/" ) ;
25
+ assert . areEqual ( "function" , typeof ( function ( ) { } ) , "typeof (function (){})" ) ;
26
+ assert . areEqual ( "function" , typeof ( ( ) => 42 ) , "typeof (() => 42)" ) ;
27
+ assert . areEqual ( "symbol" , typeof Symbol ( ) , "typeof Symbol()" ) ;
14
28
29
+ // built-in object and object wrappers
30
+ assert . areEqual ( "object" , typeof ( new String ( "" ) ) , "typeof empty string object wrapper" ) ;
31
+ assert . areEqual ( "object" , typeof ( new Boolean ( false ) ) , "typeof (new Boolean(false))" ) ;
32
+ assert . areEqual ( "object" , typeof ( new Number ( 0 ) ) , "typeof (new Number(0))" ) ;
33
+ assert . areEqual ( "object" , typeof ( new Number ( 12345.678 ) ) , "typeof (new Number(12345.678))" ) ;
34
+ assert . areEqual ( "object" , typeof ( new Object ( ) ) , "typeof (new Object())" ) ;
35
+ assert . areEqual ( "object" , typeof ( new Array ( ) ) , "typeof (new Array())" ) ;
36
+ assert . areEqual ( "object" , typeof ( new Date ( 123 ) ) , "typeof (new Date(123))" ) ;
37
+ }
38
+ } ,
39
+ {
40
+ name : "typeof of expressions" ,
41
+ body : function ( ) {
42
+ function f ( ) {
43
+ function g ( ) { }
44
+ return g ;
45
+ }
46
+ assert . areEqual ( "function" , typeof f ( ) , "typeof of function call" ) ;
47
+ assert . areEqual ( "number" , typeof eval ( 7 * 6 ) , "typeof of direct eval" ) ;
48
+ }
49
+ } ,
50
+ {
51
+ name : "typeof handling of undefined variables" ,
52
+ body : function ( ) {
53
+ assert . areEqual ( "undefined" , typeof x , "typeof of undeclaired var" ) ;
54
+ assert . areEqual ( "undefined" , typeof { } . x , "typeof {}.x" ) ;
15
55
16
- WScript . Echo ( "typeof (new Number(12345.678)) : " + typeof ( new Number ( 12345.678 ) ) ) ;
17
- WScript . Echo ( "typeof (12345.678) : " + typeof ( 12345.678 ) ) ;
56
+ assert . areEqual ( "undefined" , typeof hoisted , " typeof of hoisted var" ) ;
57
+ var hoisted = 42 ;
18
58
19
- function f ( ) {
20
- function g ( ) { }
21
- return g ;
22
- }
59
+ function inner ( ) { var scoped = 42 ; }
60
+ assert . areEqual ( "undefined" , typeof scoped , "typeof of var when it's out of scope" ) ;
23
61
24
- WScript . Echo ( "typeof function : " + typeof ( f ) ) ;
25
- WScript . Echo ( "typeof function returning function : " + typeof ( f ( ) ) ) ;
62
+ assert . throws ( ( ) => { typeof x . y ; } , ReferenceError , "typeof of property on undefined obj" , "'x' is not defined" ) ;
63
+ assert . throws ( ( ) => { typeof x [ 0 ] ; } , ReferenceError , "typeof of property on undefined obj" , "'x' is not defined" ) ;
64
+ assert . throws ( ( ) => { typeof ( ( ) => x ) ( ) ; } , ReferenceError , "reference error in the function invoked by typeof" , "'x' is not defined" ) ;
26
65
27
- function exc ( ) {
28
- try {
29
- WScript . Echo ( q ) ;
30
- }
31
- catch ( e ) {
32
- WScript . Echo ( "Caught JS error on undefined var" ) ;
33
- WScript . Echo ( typeof ( q ) ) ;
34
- }
35
- }
36
- exc ( ) ;
66
+ assert . throws ( ( ) => { typeof x_let ; } , ReferenceError , "typeof of 'let' variable in its dead zone" , "Use before declaration" ) ;
67
+ let x_let = 42 ;
37
68
38
- var x = { } ;
39
- WScript . Echo ( "typeof empty obj : " + typeof ( x ) ) ;
40
- WScript . Echo ( "typeof obj : " + typeof ( new Object ( ) ) ) ;
41
-
42
- var y = [ ] ;
43
- y [ 0 ] = 0 ;
44
- WScript . Echo ( "typeof array element with number : " + typeof ( y [ 0 ] ) ) ;
45
- WScript . Echo ( "typeof undef element array : " + typeof ( y [ 1 ] ) ) ;
46
- WScript . Echo ( "typeof array : " + typeof ( y ) ) ;
47
-
48
- var verr = new Error ( "aaa" ) ;
49
- WScript . Echo ( "typeof (err) : " + typeof ( verr ) ) ;
50
-
51
- var vDate = new Date ( 123 ) ;
52
- WScript . Echo ( "typeof ( new Date) : " + typeof ( vDate ) ) ;
53
-
54
- WScript . Echo ( "typeof (new Array()) : " + typeof ( new Array ( ) ) ) ;
55
-
56
- var regx = / a b c / ;
57
- WScript . Echo ( "typeof(regex) : " + typeof ( regx ) ) ;
58
-
59
- var s ;
60
- var map = { } ;
61
- function CEvent ( ) {
62
- do {
63
- } while ( typeof ( s = map . x ) != "undefined" ) ;
64
- }
65
- CEvent ( ) ;
69
+ assert . throws ( ( ) => { typeof x_const ; } , ReferenceError , "typeof of 'const' variable in its dead zone" , "Use before declaration" ) ;
70
+ const x_const = 42 ;
71
+ }
72
+ } ,
73
+ {
74
+ name : "typeof should propagate user exceptions" ,
75
+ body : function ( ) {
76
+ function foo ( ) { throw new Error ( "abc" ) ; }
77
+ assert . throws ( ( ) => { typeof foo ( ) } , Error , "exception raised from the invoked function" , "abc" ) ;
78
+
79
+ var obj = { get x ( ) { throw new Error ( "xyz" ) } } ;
80
+ assert . throws ( ( ) => { typeof obj . x } , Error , "exception raised from the getter" , "xyz" ) ;
81
+ }
82
+ } ,
83
+ {
84
+ name : "typeof should propagate stack overflow" ,
85
+ body : function ( ) {
86
+ var obj = { } ;
87
+ var handler = {
88
+ get : function ( ) {
89
+ return obj . x ;
90
+ }
91
+ } ;
92
+ obj = new Proxy ( obj , handler ) ;
93
+ assert . throws ( ( ) => { typeof obj . x } , Error , "recursive proxy should hit SO" , "Out of stack space" ) ;
94
+ }
95
+ } ,
96
+ ] ;
97
+
98
+ testRunner . runTests ( tests , { verbose : false /*so no need to provide baseline*/ } ) ;
0 commit comments