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