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: 9.2.js 24 ECMA Section: 9.2 Type Conversion: ToBoolean 25 Description: rules for converting an argument to a boolean. 26 undefined false 27 Null false 28 Boolean input argument( no conversion ) 29 Number returns false for 0, -0, and NaN 30 otherwise return true 31 String return false if the string is empty 32 (length is 0) otherwise the result is 33 true 34 Object all return true 35 36 Author: christine (at) netscape.com 37 Date: 14 july 1997 38 */ 39 var SECTION = "9.2"; 40 var VERSION = "ECMA_1"; 41 startTest(); 42 var TITLE = "ToBoolean"; 43 44 writeHeaderToLog( SECTION + " "+ TITLE); 45 46 var testcases = new Array(); 47 48 // special cases here 49 50 testcases[tc++] = new TestCase( SECTION, "Boolean()", false, Boolean() ); 51 testcases[tc++] = new TestCase( SECTION, "Boolean(var x)", false, Boolean(eval("var x")) ); 52 testcases[tc++] = new TestCase( SECTION, "Boolean(void 0)", false, Boolean(void 0) ); 53 testcases[tc++] = new TestCase( SECTION, "Boolean(null)", false, Boolean(null) ); 54 testcases[tc++] = new TestCase( SECTION, "Boolean(false)", false, Boolean(false) ); 55 testcases[tc++] = new TestCase( SECTION, "Boolean(true)", true, Boolean(true) ); 56 testcases[tc++] = new TestCase( SECTION, "Boolean(0)", false, Boolean(0) ); 57 testcases[tc++] = new TestCase( SECTION, "Boolean(-0)", false, Boolean(-0) ); 58 testcases[tc++] = new TestCase( SECTION, "Boolean(NaN)", false, Boolean(Number.NaN) ); 59 testcases[tc++] = new TestCase( SECTION, "Boolean('')", false, Boolean("") ); 60 61 // normal test cases here 62 63 testcases[tc++] = new TestCase( SECTION, "Boolean(Infinity)", true, Boolean(Number.POSITIVE_INFINITY) ); 64 testcases[tc++] = new TestCase( SECTION, "Boolean(-Infinity)", true, Boolean(Number.NEGATIVE_INFINITY) ); 65 testcases[tc++] = new TestCase( SECTION, "Boolean(Math.PI)", true, Boolean(Math.PI) ); 66 testcases[tc++] = new TestCase( SECTION, "Boolean(1)", true, Boolean(1) ); 67 testcases[tc++] = new TestCase( SECTION, "Boolean(-1)", true, Boolean(-1) ); 68 testcases[tc++] = new TestCase( SECTION, "Boolean([tab])", true, Boolean("\t") ); 69 testcases[tc++] = new TestCase( SECTION, "Boolean('0')", true, Boolean("0") ); 70 testcases[tc++] = new TestCase( SECTION, "Boolean('string')", true, Boolean("string") ); 71 72 // ToBoolean (object) should always return true. 73 testcases[tc++] = new TestCase( SECTION, "Boolean(new String() )", true, Boolean(new String()) ); 74 testcases[tc++] = new TestCase( SECTION, "Boolean(new String('') )", true, Boolean(new String("")) ); 75 76 testcases[tc++] = new TestCase( SECTION, "Boolean(new Boolean(true))", true, Boolean(new Boolean(true)) ); 77 testcases[tc++] = new TestCase( SECTION, "Boolean(new Boolean(false))", true, Boolean(new Boolean(false)) ); 78 testcases[tc++] = new TestCase( SECTION, "Boolean(new Boolean() )", true, Boolean(new Boolean()) ); 79 80 testcases[tc++] = new TestCase( SECTION, "Boolean(new Array())", true, Boolean(new Array()) ); 81 82 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number())", true, Boolean(new Number()) ); 83 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(-0))", true, Boolean(new Number(-0)) ); 84 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(0))", true, Boolean(new Number(0)) ); 85 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(NaN))", true, Boolean(new Number(Number.NaN)) ); 86 87 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(-1))", true, Boolean(new Number(-1)) ); 88 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(Infinity))", true, Boolean(new Number(Number.POSITIVE_INFINITY)) ); 89 testcases[tc++] = new TestCase( SECTION, "Boolean(new Number(-Infinity))",true, Boolean(new Number(Number.NEGATIVE_INFINITY)) ); 90 91 testcases[tc++] = new TestCase( SECTION, "Boolean(new Object())", true, Boolean(new Object()) ); 92 testcases[tc++] = new TestCase( SECTION, "Boolean(new Function())", true, Boolean(new Function()) ); 93 testcases[tc++] = new TestCase( SECTION, "Boolean(new Date())", true, Boolean(new Date()) ); 94 testcases[tc++] = new TestCase( SECTION, "Boolean(new Date(0))", true, Boolean(new Date(0)) ); 95 testcases[tc++] = new TestCase( SECTION, "Boolean(Math)", true, Boolean(Math) ); 96 97 test(); 98 99 function test() { 100 for ( tc=0; tc < testcases.length; tc++ ) { 101 testcases[tc].passed = writeTestCaseResult( 102 testcases[tc].expect, 103 testcases[tc].actual, 104 testcases[tc].description +" = "+ 105 testcases[tc].actual ); 106 107 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 108 } 109 stopTest(); 110 return ( testcases ); 111 } 112