Debian Codegraph @

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