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: 15.5.4.9-1.js 24 ECMA Section: 15.5.4.9 String.prototype.substring( start ) 25 Description: 26 27 15.5.4.9 String.prototype.substring(start) 28 29 Returns a substring of the result of converting this object to a string, 30 starting from character position start and running to the end of the 31 string. The result is a string value, not a String object. 32 33 If the argument is NaN or negative, it is replaced with zero; if the 34 argument is larger than the length of the string, it is replaced with the 35 length of the string. 36 37 When the substring method is called with one argument start, the following 38 steps are taken: 39 40 1.Call ToString, giving it the this value as its argument. 41 2.Call ToInteger(start). 42 3.Compute the number of characters in Result(1). 43 4.Compute min(max(Result(2), 0), Result(3)). 44 5.Return a string whose length is the difference between Result(3) and Result(4), 45 containing characters from Result(1), namely the characters with indices Result(4) 46 through Result(3)1, in ascending order. 47 48 Author: christine (at) netscape.com 49 Date: 12 november 1997 50 */ 51 52 var SECTION = "15.5.4.9-1"; 53 var VERSION = "ECMA_1"; 54 startTest(); 55 var TITLE = "String.prototype.substring( start )"; 56 57 writeHeaderToLog( SECTION + " "+ TITLE); 58 59 var testcases = getTestCases(); 60 test(); 61 62 function getTestCases() { 63 var array = new Array(); 64 var item = 0; 65 66 array[item++] = new TestCase( SECTION, "String.prototype.substring.length", 2, String.prototype.substring.length ); 67 array[item++] = new TestCase( SECTION, "delete String.prototype.substring.length", false, delete String.prototype.substring.length ); 68 array[item++] = new TestCase( SECTION, "delete String.prototype.substring.length; String.prototype.substring.length", 2, eval("delete String.prototype.substring.length; String.prototype.substring.length") ); 69 70 // test cases for when substring is called with no arguments. 71 72 // this is a string object 73 74 array[item++] = new TestCase( SECTION, 75 "var s = new String('this is a string object'); typeof s.substring()", 76 "string", 77 eval("var s = new String('this is a string object'); typeof s.substring()") ); 78 79 array[item++] = new TestCase( SECTION, 80 "var s = new String(''); s.substring()", 81 "", 82 eval("var s = new String(''); s.substring()") ); 83 84 85 array[item++] = new TestCase( SECTION, 86 "var s = new String('this is a string object'); s.substring()", 87 "this is a string object", 88 eval("var s = new String('this is a string object'); s.substring()") ); 89 90 array[item++] = new TestCase( SECTION, 91 "var s = new String('this is a string object'); s.substring(NaN)", 92 "this is a string object", 93 eval("var s = new String('this is a string object'); s.substring(NaN)") ); 94 95 96 array[item++] = new TestCase( SECTION, 97 "var s = new String('this is a string object'); s.substring(-0.01)", 98 "this is a string object", 99 eval("var s = new String('this is a string object'); s.substring(-0.01)") ); 100 101 102 array[item++] = new TestCase( SECTION, 103 "var s = new String('this is a string object'); s.substring(s.length)", 104 "", 105 eval("var s = new String('this is a string object'); s.substring(s.length)") ); 106 107 array[item++] = new TestCase( SECTION, 108 "var s = new String('this is a string object'); s.substring(s.length+1)", 109 "", 110 eval("var s = new String('this is a string object'); s.substring(s.length+1)") ); 111 112 113 array[item++] = new TestCase( SECTION, 114 "var s = new String('this is a string object'); s.substring(Infinity)", 115 "", 116 eval("var s = new String('this is a string object'); s.substring(Infinity)") ); 117 118 array[item++] = new TestCase( SECTION, 119 "var s = new String('this is a string object'); s.substring(-Infinity)", 120 "this is a string object", 121 eval("var s = new String('this is a string object'); s.substring(-Infinity)") ); 122 123 // this is not a String object, start is not an integer 124 125 126 array[item++] = new TestCase( SECTION, 127 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()", 128 "1,2,3,4,5", 129 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()") ); 130 131 array[item++] = new TestCase( SECTION, 132 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)", 133 ",2,3,4,5", 134 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)") ); 135 136 array[item++] = new TestCase( SECTION, 137 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')", 138 "3,4,5", 139 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')") ); 140 141 array[item++] = new TestCase( SECTION, 142 "var s = new Array(); s.substring = String.prototype.substring; s.substring('4')", 143 "", 144 eval("var s = new Array(); s.substring = String.prototype.substring; s.substring('4')") ); 145 146 // this is an object object 147 array[item++] = new TestCase( SECTION, 148 "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)", 149 "Object]", 150 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)") ); 151 152 // this is a function object 153 array[item++] = new TestCase( SECTION, 154 "var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)", 155 "Function]", 156 eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)") ); 157 // this is a number object 158 array[item++] = new TestCase( SECTION, 159 "var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)", 160 "NaN", 161 eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)") ); 162 163 // this is the Math object 164 array[item++] = new TestCase( SECTION, 165 "var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)", 166 "ject Math]", 167 eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)") ); 168 169 // this is a Boolean object 170 171 array[item++] = new TestCase( SECTION, 172 "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())", 173 "false", 174 eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())") ); 175 176 // this is a user defined object 177 178 array[item++] = new TestCase( SECTION, 179 "var obj = new MyObject( null ); obj.substring(0)", 180 "null", 181 eval( "var obj = new MyObject( null ); obj.substring(0)") ); 182 183 return array; 184 } 185 function test() { 186 for ( tc=0; tc < testcases.length; tc++ ) { 187 testcases[tc].passed = writeTestCaseResult( 188 testcases[tc].expect, 189 testcases[tc].actual, 190 testcases[tc].description +" = "+ 191 testcases[tc].actual ); 192 193 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 194 } 195 stopTest(); 196 return ( testcases ); 197 } 198 function MyObject( value ) { 199 this.value = value; 200 this.substring = String.prototype.substring; 201 this.toString = new Function ( "return this.value+''" ); 202 }