]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.9-1.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 File Name: 15.5.4.9-1.js
24 ECMA Section: 15.5.4.9 String.prototype.substring( start )
27 15.5.4.9 String.prototype.substring(start)
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.
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
37 When the substring method is called with one argument start, the following
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.
48 Author: christine@netscape.com
49 Date: 12 november 1997
52 var SECTION
= "15.5.4.9-1";
53 var VERSION
= "ECMA_1";
55 var TITLE
= "String.prototype.substring( start )";
57 writeHeaderToLog( SECTION
+ " "+ TITLE
);
59 var testcases
= getTestCases();
62 function getTestCases() {
63 var array
= new Array();
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") );
70 // test cases for when substring is called with no arguments.
72 // this is a string object
74 array
[item
++] = new TestCase( SECTION
,
75 "var s = new String('this is a string object'); typeof s.substring()",
77 eval("var s = new String('this is a string object'); typeof s.substring()") );
79 array
[item
++] = new TestCase( SECTION
,
80 "var s = new String(''); s.substring()",
82 eval("var s = new String(''); s.substring()") );
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()") );
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)") );
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)") );
102 array
[item
++] = new TestCase( SECTION
,
103 "var s = new String('this is a string object'); s.substring(s.length)",
105 eval("var s = new String('this is a string object'); s.substring(s.length)") );
107 array
[item
++] = new TestCase( SECTION
,
108 "var s = new String('this is a string object'); s.substring(s.length+1)",
110 eval("var s = new String('this is a string object'); s.substring(s.length+1)") );
113 array
[item
++] = new TestCase( SECTION
,
114 "var s = new String('this is a string object'); s.substring(Infinity)",
116 eval("var s = new String('this is a string object'); s.substring(Infinity)") );
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)") );
123 // this is not a String object, start is not an integer
126 array
[item
++] = new TestCase( SECTION
,
127 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()",
129 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()") );
131 array
[item
++] = new TestCase( SECTION
,
132 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)",
134 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)") );
136 array
[item
++] = new TestCase( SECTION
,
137 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')",
139 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')") );
141 array
[item
++] = new TestCase( SECTION
,
142 "var s = new Array(); s.substring = String.prototype.substring; s.substring('4')",
144 eval("var s = new Array(); s.substring = String.prototype.substring; s.substring('4')") );
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)",
150 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)") );
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)",
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)",
161 eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)") );
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)",
167 eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)") );
169 // this is a Boolean object
171 array
[item
++] = new TestCase( SECTION
,
172 "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())",
174 eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())") );
176 // this is a user defined object
178 array
[item
++] = new TestCase( SECTION
,
179 "var obj = new MyObject( null ); obj.substring(0)",
181 eval( "var obj = new MyObject( null ); obj.substring(0)") );
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
);
193 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
196 return ( testcases
);
198 function MyObject( value
) {
200 this.substring
= String
.prototype.substring
;
201 this.toString
= new Function ( "return this.value+''" );