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