1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 File Name: 15.5.4.9-1.js
42 ECMA Section: 15.5.4.9 String.prototype.substring( start )
45 15.5.4.9 String.prototype.substring(start)
47 Returns a substring of the result of converting this object to a string,
48 starting from character position start and running to the end of the
49 string. The result is a string value, not a String object.
51 If the argument is NaN or negative, it is replaced with zero; if the
52 argument is larger than the length of the string, it is replaced with the
55 When the substring method is called with one argument start, the following
58 1.Call ToString, giving it the this value as its argument.
59 2.Call ToInteger(start).
60 3.Compute the number of characters in Result(1).
61 4.Compute min(max(Result(2), 0), Result(3)).
62 5.Return a string whose length is the difference between Result(3) and Result(4),
63 containing characters from Result(1), namely the characters with indices Result(4)
64 through Result(3)1, in ascending order.
66 Author: christine@netscape.com
67 Date: 12 november 1997
70 var SECTION = "15.5.4.9-1";
71 var VERSION = "ECMA_1";
73 var TITLE = "String.prototype.substring( start )";
75 writeHeaderToLog( SECTION + " "+ TITLE);
77 new TestCase( SECTION, "String.prototype.substring.length", 2, String.prototype.substring.length );
78 new TestCase( SECTION, "delete String.prototype.substring.length", false, delete String.prototype.substring.length );
79 new TestCase( SECTION, "delete String.prototype.substring.length; String.prototype.substring.length", 2, eval("delete String.prototype.substring.length; String.prototype.substring.length") );
81 // test cases for when substring is called with no arguments.
83 // this is a string object
85 new TestCase( SECTION,
86 "var s = new String('this is a string object'); typeof s.substring()",
88 eval("var s = new String('this is a string object'); typeof s.substring()") );
90 new TestCase( SECTION,
91 "var s = new String(''); s.substring()",
93 eval("var s = new String(''); s.substring()") );
96 new TestCase( SECTION,
97 "var s = new String('this is a string object'); s.substring()",
98 "this is a string object",
99 eval("var s = new String('this is a string object'); s.substring()") );
101 new TestCase( SECTION,
102 "var s = new String('this is a string object'); s.substring(NaN)",
103 "this is a string object",
104 eval("var s = new String('this is a string object'); s.substring(NaN)") );
107 new TestCase( SECTION,
108 "var s = new String('this is a string object'); s.substring(-0.01)",
109 "this is a string object",
110 eval("var s = new String('this is a string object'); s.substring(-0.01)") );
113 new TestCase( SECTION,
114 "var s = new String('this is a string object'); s.substring(s.length)",
116 eval("var s = new String('this is a string object'); s.substring(s.length)") );
118 new TestCase( SECTION,
119 "var s = new String('this is a string object'); s.substring(s.length+1)",
121 eval("var s = new String('this is a string object'); s.substring(s.length+1)") );
124 new TestCase( SECTION,
125 "var s = new String('this is a string object'); s.substring(Infinity)",
127 eval("var s = new String('this is a string object'); s.substring(Infinity)") );
129 new TestCase( SECTION,
130 "var s = new String('this is a string object'); s.substring(-Infinity)",
131 "this is a string object",
132 eval("var s = new String('this is a string object'); s.substring(-Infinity)") );
134 // this is not a String object, start is not an integer
137 new TestCase( SECTION,
138 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()",
140 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()") );
142 new TestCase( SECTION,
143 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)",
145 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)") );
147 new TestCase( SECTION,
148 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')",
150 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')") );
152 new TestCase( SECTION,
153 "var s = new Array(); s.substring = String.prototype.substring; s.substring('4')",
155 eval("var s = new Array(); s.substring = String.prototype.substring; s.substring('4')") );
157 // this is an object object
158 new TestCase( SECTION,
159 "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)",
161 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)") );
163 // this is a function object
164 new TestCase( SECTION,
165 "var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)",
167 eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)") );
168 // this is a number object
169 new TestCase( SECTION,
170 "var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)",
172 eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)") );
174 // this is the Math object
175 new TestCase( SECTION,
176 "var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)",
178 eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)") );
180 // this is a Boolean object
182 new TestCase( SECTION,
183 "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())",
185 eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())") );
187 // this is a user defined object
189 new TestCase( SECTION,
190 "var obj = new MyObject( null ); obj.substring(0)",
192 eval( "var obj = new MyObject( null ); obj.substring(0)") );
197 function MyObject( value ) {
199 this.substring = String.prototype.substring;
200 this.toString = new Function ( "return this.value+''" );