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.9-1.js michael@0: ECMA Section: 15.5.4.9 String.prototype.substring( start ) michael@0: Description: michael@0: michael@0: 15.5.4.9 String.prototype.substring(start) 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 the end of the michael@0: string. The result is a string value, not a String object. michael@0: michael@0: If the argument is NaN or negative, it is replaced with zero; if the 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: When the substring method is called with one argument start, the following michael@0: 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.Compute the number of characters in Result(1). michael@0: 4.Compute min(max(Result(2), 0), Result(3)). michael@0: 5.Return a string whose length is the difference between Result(3) and Result(4), michael@0: containing characters from Result(1), namely the characters with indices Result(4) michael@0: through Result(3)1, in ascending order. 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.9-1"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: var TITLE = "String.prototype.substring( start )"; 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()", michael@0: "", michael@0: eval("var s = new String(''); s.substring()") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring()", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring()") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(NaN)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(NaN)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(-0.01)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(-0.01)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(s.length)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(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)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(s.length+1)") ); michael@0: michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(Infinity)", michael@0: "", michael@0: eval("var s = new String('this is a string object'); s.substring(Infinity)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String('this is a string object'); s.substring(-Infinity)", michael@0: "this is a string object", michael@0: eval("var s = new String('this is a string object'); s.substring(-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()", 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()") ); 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)", michael@0: ",2,3,4,5", michael@0: eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)") ); 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')", michael@0: "3,4,5", michael@0: eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new Array(); s.substring = String.prototype.substring; s.substring('4')", michael@0: "", michael@0: eval("var s = new Array(); s.substring = String.prototype.substring; s.substring('4')") ); 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)", michael@0: "Object]", michael@0: eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)") ); 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)", michael@0: "Function]", michael@0: eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)") ); 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(false)", michael@0: "NaN", michael@0: eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)") ); 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)", michael@0: "ject Math]", michael@0: eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)") ); 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())", michael@0: "false", michael@0: eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())") ); michael@0: michael@0: // this is a user defined object michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new MyObject( null ); obj.substring(0)", michael@0: "null", michael@0: eval( "var obj = new MyObject( null ); obj.substring(0)") ); michael@0: 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: }