Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.10-1.js

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