michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 15.5.4.10-1.js michael@0: ECMA Section: 15.5.4.10 String.prototype.substring( start, end ) michael@0: Description: michael@0: michael@0: 15.5.4.10 String.prototype.substring(start, end) michael@0: michael@0: Returns a substring of the result of converting this object to a string, michael@0: starting from character position start and running to character position michael@0: end of the string. The result is a string value, not a String object. michael@0: michael@0: If either argument is NaN or negative, it is replaced with zero; if either michael@0: argument is larger than the length of the string, it is replaced with the michael@0: length of the string. michael@0: michael@0: If start is larger than end, they are swapped. michael@0: michael@0: When the substring method is called with two arguments start and end, the michael@0: following steps are taken: michael@0: michael@0: 1. Call ToString, giving it the this value as its argument. michael@0: 2. Call ToInteger(start). michael@0: 3. Call ToInteger (end). michael@0: 4. Compute the number of characters in Result(1). michael@0: 5. Compute min(max(Result(2), 0), Result(4)). michael@0: 6. Compute min(max(Result(3), 0), Result(4)). michael@0: 7. Compute min(Result(5), Result(6)). michael@0: 8. Compute max(Result(5), Result(6)). michael@0: 9. Return a string whose length is the difference between Result(8) and michael@0: Result(7), containing characters from Result(1), namely the characters michael@0: with indices Result(7) through Result(8)1, in ascending order. michael@0: michael@0: Note that the substring function is intentionally generic; it does not require michael@0: that its this value be a String object. Therefore it can be transferred to other michael@0: kinds of objects for use as a method. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: michael@0: var SECTION = "15.5.4.10-1"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: var TITLE = "String.prototype.substring( start, end )"; michael@0: michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: new TestCase( SECTION, "String.prototype.substring.length", 2, String.prototype.substring.length ); michael@0: new TestCase( SECTION, "delete String.prototype.substring.length", false, delete String.prototype.substring.length ); michael@0: 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: michael@0: // test cases for when substring is called with no arguments. michael@0: michael@0: // this is a string object michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); typeof s.substring()", michael@0: "string", michael@0: eval("var s = new String('this is a string object'); typeof s.substring()") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String(''); s.substring(1,0)", michael@0: "", michael@0: eval("var s = new String(''); s.substring(1,0)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(true, false)", michael@0: "t", michael@0: eval("var s = new String('this is a string object'); s.substring(false, true)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(NaN, Infinity)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(NaN, Infinity)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(Infinity, NaN)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(Infinity, NaN)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(Infinity, Infinity)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(Infinity, Infinity)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(-0.01, 0)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(-0.01,0)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(s.length, s.length)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(s.length, s.length)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(s.length+1, 0)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(s.length+1, 0)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(-Infinity, -Infinity)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(-Infinity, -Infinity)") ); michael@0: michael@0: // this is not a String object, start is not an integer michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)", michael@0: "1,2,3,4,5", michael@0: eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)", michael@0: "1", michael@0: eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')", michael@0: "3", michael@0: eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')") ); michael@0: michael@0: michael@0: // this is an object object michael@0: new TestCase( SECTION, michael@0: "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)", michael@0: "[object ", michael@0: eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,obj.toString().length)", michael@0: "Object]", michael@0: eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8, obj.toString().length)") ); michael@0: michael@0: // this is a function object michael@0: new TestCase( SECTION, michael@0: "var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8, Infinity)", michael@0: "Function]", michael@0: eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8,Infinity)") ); michael@0: // this is a number object michael@0: new TestCase( SECTION, michael@0: "var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)", michael@0: "NaN", michael@0: eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)") ); michael@0: michael@0: // this is the Math object michael@0: new TestCase( SECTION, michael@0: "var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)", michael@0: "[ob", michael@0: eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)") ); michael@0: michael@0: // this is a Boolean object michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))", michael@0: "f", michael@0: eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))") ); michael@0: michael@0: // this is a user defined object michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new MyObject( void 0 ); obj.substring(0, 100)", michael@0: "undefined", michael@0: eval( "var obj = new MyObject( void 0 ); obj.substring(0,100)") ); michael@0: michael@0: test(); michael@0: michael@0: function MyObject( value ) { michael@0: this.value = value; michael@0: this.substring = String.prototype.substring; michael@0: this.toString = new Function ( "return this.value+''" ); michael@0: }