• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// check properties
16
17function length_configurable()
18{
19  function is_es51() {
20    return (typeof g === "function");
21    { function g() {} }
22  }
23  return is_es51() ? false : true;
24}
25
26assert(Object.getOwnPropertyDescriptor(String.prototype.substr, 'length').configurable === length_configurable());
27
28assert(Object.getOwnPropertyDescriptor(String.prototype.substr, 'length').enumerable === false);
29
30assert(Object.getOwnPropertyDescriptor(String.prototype.substr, 'length').writable === false);
31
32assert(String.prototype.substr.length === 2);
33
34assert(String.prototype.substr.call(new String()) === "");
35
36assert(String.prototype.substr.call({}) === "[object Object]");
37
38// check this is undefined
39try {
40  String.prototype.substr.call(undefined);
41  assert(false);
42} catch(e) {
43  assert(e instanceof TypeError);
44}
45
46// check this is null
47try {
48  String.prototype.substr.call(null);
49  assert(false);
50} catch(e) {
51  assert(e instanceof TypeError);
52}
53
54// simple checks
55assert("Hello world!".substr(0, 11) === "Hello world");
56
57assert("Hello world!".substr(11, 0) === "");
58
59assert("Hello world!".substr(0, 12) === "Hello world!");
60
61assert("Hello world!".substr(12, 0) === "");
62// check NaN
63assert("Hello world!".substr(NaN, 12) === "Hello world!");
64
65// check NaN
66assert("Hello world!".substr(2, NaN) === "");
67
68// check end undefined
69assert("Hello world!".substr(2, undefined) === "llo world!");
70
71// check negative
72assert("Hello world!".substr(-1,8) === "!");
73
74// check negative
75assert("Hello\tworld!".substr(5,-8) === "");
76
77// check negative
78assert("Hello world!".substr(-1,-8) === "");
79
80// check ranges
81assert("Hello world!".substr(-1,10000) === "!");
82
83assert("Hello world!".substr(10000,1000000) === "");
84
85assert("Hello world!".substr(100000,1) === "");
86
87// check both undefined
88assert("Hello world!".substr(undefined, undefined) === "Hello world!");
89
90var undef_var;
91assert("Hello world!".substr(undef_var, undef_var) === "Hello world!");
92
93// check integer conversion
94assert("Hello world!".substr(undefined, 5) === "Hello");
95
96assert("Hello world!".substr(undefined, "bar") === "");
97
98assert("Hello world!".substr(2, true) === "l");
99
100assert("Hello world!".substr(2, false) === "");
101
102assert("Hello world!".substr(5, obj) === " world!");
103
104// check other objects
105var obj = { substr : String.prototype.substr }
106
107obj.toString = function() {
108    return "Iam";
109}
110assert(obj.substr(0,1) === "I");
111
112obj.toString = function() {
113  throw new ReferenceError ("foo");
114};
115
116try {
117  assert(obj.substr(100000,1));
118  assert(false);
119} catch (e) {
120  assert(e.message === "foo");
121  assert(e instanceof ReferenceError);
122}
123
124// check coercible - undefined
125try {
126  assert(true.substr() === "");
127  assert(false);
128} catch (e) {
129  assert(e instanceof TypeError);
130}
131
132// check coercible - null
133try {
134  assert(String.prototype.substr.call(null, 0, 1) === "");
135  assert(false);
136} catch (e) {
137  assert(e instanceof TypeError);
138}
139
140// check coercible - Boolean
141assert(String.prototype.substr.call(true, 0, 1) === "t");
142
143// check coercible - Object
144var test_object = {firstName:"John", lastName:"Doe"};
145assert(String.prototype.substr.call(test_object, 0, 7) === "[object");
146
147// check coercible - Number
148assert(String.prototype.substr.call(123, 0, 3) === "123");
149