Home | History | Annotate | Download | only in Expressions
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          typeof_1.js
     24     ECMA Section:       11.4.3 typeof operator
     25     Description:        typeof evaluates unary expressions:
     26                         undefined   "undefined"
     27                         null        "object"
     28                         Boolean     "boolean"
     29                         Number      "number"
     30                         String      "string"
     31                         Object      "object" [native, doesn't implement Call]
     32                         Object      "function" [native, implements [Call]]
     33                         Object      implementation dependent
     34                                     [not sure how to test this]
     35     Author:             christine (at) netscape.com
     36     Date:               june 30, 1997
     37 
     38 */
     39 
     40     var SECTION = "11.4.3";
     41 
     42     var VERSION = "ECMA_1";
     43     startTest();
     44     var TITLE   = " The typeof operator";
     45 
     46     var testcases = new Array();
     47 
     48 
     49     testcases[testcases.length] = new TestCase( SECTION,     "typeof(void(0))",              "undefined",        typeof(void(0)) );
     50     testcases[testcases.length] = new TestCase( SECTION,     "typeof(null)",                 "object",           typeof(null) );
     51     testcases[testcases.length] = new TestCase( SECTION,     "typeof(true)",                 "boolean",          typeof(true) );
     52     testcases[testcases.length] = new TestCase( SECTION,     "typeof(false)",                "boolean",          typeof(false) );
     53     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Boolean())",        "object",           typeof(new Boolean()) );
     54     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Boolean(true))",    "object",           typeof(new Boolean(true)) );
     55     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Boolean())",            "boolean",          typeof(Boolean()) );
     56     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Boolean(false))",       "boolean",          typeof(Boolean(false)) );
     57     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Boolean(true))",        "boolean",          typeof(Boolean(true)) );
     58     testcases[testcases.length] = new TestCase( SECTION,     "typeof(NaN)",                  "number",           typeof(Number.NaN) );
     59     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Infinity)",             "number",           typeof(Number.POSITIVE_INFINITY) );
     60     testcases[testcases.length] = new TestCase( SECTION,     "typeof(-Infinity)",            "number",           typeof(Number.NEGATIVE_INFINITY) );
     61     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Math.PI)",              "number",           typeof(Math.PI) );
     62     testcases[testcases.length] = new TestCase( SECTION,     "typeof(0)",                    "number",           typeof(0) );
     63     testcases[testcases.length] = new TestCase( SECTION,     "typeof(1)",                    "number",           typeof(1) );
     64     testcases[testcases.length] = new TestCase( SECTION,     "typeof(-1)",                   "number",           typeof(-1) );
     65     testcases[testcases.length] = new TestCase( SECTION,     "typeof('0')",                  "string",           typeof("0") );
     66     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Number())",             "number",           typeof(Number()) );
     67     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Number(0))",            "number",           typeof(Number(0)) );
     68     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Number(1))",            "number",           typeof(Number(1)) );
     69     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Nubmer(-1))",           "number",           typeof(Number(-1)) );
     70     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Number())",         "object",           typeof(new Number()) );
     71     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Number(0))",        "object",           typeof(new Number(0)) );
     72     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Number(1))",        "object",           typeof(new Number(1)) );
     73 
     74     // Math does not implement [[Construct]] or [[Call]] so its type is object.
     75 
     76     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Math)",                 "object",         typeof(Math) );
     77 
     78     testcases[testcases.length] = new TestCase( SECTION,     "typeof(Number.prototype.toString)", "function",    typeof(Number.prototype.toString) );
     79 
     80     testcases[testcases.length] = new TestCase( SECTION,     "typeof('a string')",           "string",           typeof("a string") );
     81     testcases[testcases.length] = new TestCase( SECTION,     "typeof('')",                   "string",           typeof("") );
     82     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Date())",           "object",           typeof(new Date()) );
     83     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Array(1,2,3))",     "object",           typeof(new Array(1,2,3)) );
     84     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new String('string object'))",  "object",   typeof(new String("string object")) );
     85     testcases[testcases.length] = new TestCase( SECTION,     "typeof(String('string primitive'))",    "string",  typeof(String("string primitive")) );
     86     testcases[testcases.length] = new TestCase( SECTION,     "typeof(['array', 'of', 'strings'])",   "object",   typeof(["array", "of", "strings"]) );
     87     testcases[testcases.length] = new TestCase( SECTION,     "typeof(new Function())",                "function",     typeof( new Function() ) );
     88     testcases[testcases.length] = new TestCase( SECTION,     "typeof(parseInt)",                      "function",     typeof( parseInt ) );
     89     testcases[testcases.length] = new TestCase( SECTION,     "typeof(test)",                          "function",     typeof( test ) );
     90     testcases[testcases.length] = new TestCase( SECTION,     "typeof(String.fromCharCode)",           "function",     typeof( String.fromCharCode )  );
     91 
     92 
     93     writeHeaderToLog( SECTION + " "+ TITLE);
     94     test();
     95 
     96 
     97 function test() {
     98     for ( tc=0; tc < testcases.length; tc++ ) {
     99         testcases[tc].passed = writeTestCaseResult(
    100                             testcases[tc].expect,
    101                             testcases[tc].actual,
    102                             testcases[tc].description +" = "+
    103                             testcases[tc].actual );
    104 
    105         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    106     }
    107     stopTest();
    108     return ( testcases );
    109 }
    110