|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.5.4.10-1.js |
|
9 ECMA Section: 15.5.4.10 String.prototype.substring( start, end ) |
|
10 Description: |
|
11 |
|
12 15.5.4.10 String.prototype.substring(start, end) |
|
13 |
|
14 Returns a substring of the result of converting this object to a string, |
|
15 starting from character position start and running to character position |
|
16 end of the string. The result is a string value, not a String object. |
|
17 |
|
18 If either argument is NaN or negative, it is replaced with zero; if either |
|
19 argument is larger than the length of the string, it is replaced with the |
|
20 length of the string. |
|
21 |
|
22 If start is larger than end, they are swapped. |
|
23 |
|
24 When the substring method is called with two arguments start and end, the |
|
25 following steps are taken: |
|
26 |
|
27 1. Call ToString, giving it the this value as its argument. |
|
28 2. Call ToInteger(start). |
|
29 3. Call ToInteger (end). |
|
30 4. Compute the number of characters in Result(1). |
|
31 5. Compute min(max(Result(2), 0), Result(4)). |
|
32 6. Compute min(max(Result(3), 0), Result(4)). |
|
33 7. Compute min(Result(5), Result(6)). |
|
34 8. Compute max(Result(5), Result(6)). |
|
35 9. Return a string whose length is the difference between Result(8) and |
|
36 Result(7), containing characters from Result(1), namely the characters |
|
37 with indices Result(7) through Result(8)1, in ascending order. |
|
38 |
|
39 Note that the substring function is intentionally generic; it does not require |
|
40 that its this value be a String object. Therefore it can be transferred to other |
|
41 kinds of objects for use as a method. |
|
42 |
|
43 Author: christine@netscape.com |
|
44 Date: 12 november 1997 |
|
45 */ |
|
46 |
|
47 var SECTION = "15.5.4.10-1"; |
|
48 var VERSION = "ECMA_1"; |
|
49 startTest(); |
|
50 var TITLE = "String.prototype.substring( start, end )"; |
|
51 |
|
52 writeHeaderToLog( SECTION + " "+ TITLE); |
|
53 |
|
54 new TestCase( SECTION, "String.prototype.substring.length", 2, String.prototype.substring.length ); |
|
55 new TestCase( SECTION, "delete String.prototype.substring.length", false, delete String.prototype.substring.length ); |
|
56 new TestCase( SECTION, "delete String.prototype.substring.length; String.prototype.substring.length", 2, eval("delete String.prototype.substring.length; String.prototype.substring.length") ); |
|
57 |
|
58 // test cases for when substring is called with no arguments. |
|
59 |
|
60 // this is a string object |
|
61 |
|
62 new TestCase( SECTION, |
|
63 "var s = new String('this is a string object'); typeof s.substring()", |
|
64 "string", |
|
65 eval("var s = new String('this is a string object'); typeof s.substring()") ); |
|
66 |
|
67 new TestCase( SECTION, |
|
68 "var s = new String(''); s.substring(1,0)", |
|
69 "", |
|
70 eval("var s = new String(''); s.substring(1,0)") ); |
|
71 |
|
72 new TestCase( SECTION, |
|
73 "var s = new String('this is a string object'); s.substring(true, false)", |
|
74 "t", |
|
75 eval("var s = new String('this is a string object'); s.substring(false, true)") ); |
|
76 |
|
77 new TestCase( SECTION, |
|
78 "var s = new String('this is a string object'); s.substring(NaN, Infinity)", |
|
79 "this is a string object", |
|
80 eval("var s = new String('this is a string object'); s.substring(NaN, Infinity)") ); |
|
81 |
|
82 |
|
83 new TestCase( SECTION, |
|
84 "var s = new String('this is a string object'); s.substring(Infinity, NaN)", |
|
85 "this is a string object", |
|
86 eval("var s = new String('this is a string object'); s.substring(Infinity, NaN)") ); |
|
87 |
|
88 |
|
89 new TestCase( SECTION, |
|
90 "var s = new String('this is a string object'); s.substring(Infinity, Infinity)", |
|
91 "", |
|
92 eval("var s = new String('this is a string object'); s.substring(Infinity, Infinity)") ); |
|
93 |
|
94 new TestCase( SECTION, |
|
95 "var s = new String('this is a string object'); s.substring(-0.01, 0)", |
|
96 "", |
|
97 eval("var s = new String('this is a string object'); s.substring(-0.01,0)") ); |
|
98 |
|
99 |
|
100 new TestCase( SECTION, |
|
101 "var s = new String('this is a string object'); s.substring(s.length, s.length)", |
|
102 "", |
|
103 eval("var s = new String('this is a string object'); s.substring(s.length, s.length)") ); |
|
104 |
|
105 new TestCase( SECTION, |
|
106 "var s = new String('this is a string object'); s.substring(s.length+1, 0)", |
|
107 "this is a string object", |
|
108 eval("var s = new String('this is a string object'); s.substring(s.length+1, 0)") ); |
|
109 |
|
110 |
|
111 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 // this is not a String object, start is not an integer |
|
117 |
|
118 |
|
119 new TestCase( SECTION, |
|
120 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)", |
|
121 "1,2,3,4,5", |
|
122 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)") ); |
|
123 |
|
124 new TestCase( SECTION, |
|
125 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)", |
|
126 "1", |
|
127 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)") ); |
|
128 |
|
129 new TestCase( SECTION, |
|
130 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')", |
|
131 "3", |
|
132 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')") ); |
|
133 |
|
134 |
|
135 // this is an object object |
|
136 new TestCase( SECTION, |
|
137 "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)", |
|
138 "[object ", |
|
139 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)") ); |
|
140 |
|
141 new TestCase( SECTION, |
|
142 "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,obj.toString().length)", |
|
143 "Object]", |
|
144 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8, obj.toString().length)") ); |
|
145 |
|
146 // this is a function object |
|
147 new TestCase( SECTION, |
|
148 "var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8, Infinity)", |
|
149 "Function]", |
|
150 eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8,Infinity)") ); |
|
151 // this is a number object |
|
152 new TestCase( SECTION, |
|
153 "var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)", |
|
154 "NaN", |
|
155 eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)") ); |
|
156 |
|
157 // this is the Math object |
|
158 new TestCase( SECTION, |
|
159 "var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)", |
|
160 "[ob", |
|
161 eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)") ); |
|
162 |
|
163 // this is a Boolean object |
|
164 |
|
165 new TestCase( SECTION, |
|
166 "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))", |
|
167 "f", |
|
168 eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))") ); |
|
169 |
|
170 // this is a user defined object |
|
171 |
|
172 new TestCase( SECTION, |
|
173 "var obj = new MyObject( void 0 ); obj.substring(0, 100)", |
|
174 "undefined", |
|
175 eval( "var obj = new MyObject( void 0 ); obj.substring(0,100)") ); |
|
176 |
|
177 test(); |
|
178 |
|
179 function MyObject( value ) { |
|
180 this.value = value; |
|
181 this.substring = String.prototype.substring; |
|
182 this.toString = new Function ( "return this.value+''" ); |
|
183 } |