js/src/tests/ecma/String/15.5.4.10-1.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial