• 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.substring, 'length').configurable === length_configurable());
27
28assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').enumerable === false);
29
30assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').writable === false);
31
32assert(String.prototype.substring.length === 2);
33
34assert(String.prototype.substring.call(new String()) === "");
35
36assert(String.prototype.substring.call({}) === "[object Object]");
37
38// check this is undefined
39try {
40  String.prototype.substring.call(undefined);
41  assert(false);
42} catch(e) {
43  assert(e instanceof TypeError);
44}
45
46// check this is null
47try {
48  String.prototype.substring.call(null);
49  assert(false);
50} catch(e) {
51  assert(e instanceof TypeError);
52}
53
54// simple checks
55assert("hello world!".substring(0, 11) === "hello world");
56
57assert("hello world!".substring(11, 0) === "hello world");
58
59assert("hello world!".substring(0, 12) === "hello world!");
60
61assert("hello world!".substring(12, 0) === "hello world!");
62
63// check NaN
64assert("hello world!".substring(NaN, 12) === "hello world!");
65
66// check NaN
67assert("hello world!".substring(2, NaN) === "he");
68
69// check end undefined
70assert("hello world!".substring(2, undefined) === "llo world!");
71
72// check negative
73assert("hello world!".substring(-1,8) === "hello wo");
74
75// check negative
76assert("hello\tworld!".substring(5,-8) === "hello");
77
78// check negative
79assert("hello world!".substring(-1,-8) === "");
80
81// check ranges
82assert("hello world!".substring(-1,10000) === "hello world!");
83
84assert("hello world!".substring(10000,1000000) === "");
85
86assert("hello world!".substring(100000,1) === "ello world!");
87
88// check both undefined
89assert("hello world!".substring(undefined, undefined) === "hello world!");
90
91var undef_var;
92assert("hello world!".substring(undef_var, undef_var) === "hello world!");
93
94// check integer conversion
95assert("hello world!".substring(undefined, 5) === "hello");
96
97assert("hello world!".substring(undefined, "bar") === "");
98
99assert("hello world!".substring(2, true) === "e");
100
101assert("hello world!".substring(2, false) === "he");
102
103assert("hello world!".substring(5, obj) === " world!");
104
105// check other objects
106var obj = { substring : String.prototype.substring }
107
108obj.toString = function() {
109    return "Iam";
110}
111assert(obj.substring(100000,1) === "am");
112
113obj.toString = function() {
114  throw new ReferenceError ("foo");
115};
116
117try {
118  assert(obj.substring(100000,1));
119  assert(false);
120} catch (e) {
121  assert(e.message === "foo");
122  assert(e instanceof ReferenceError);
123}
124
125// check coercible - undefined
126try {
127  assert(true.substring() === "");
128  assert(false);
129} catch (e) {
130  assert(e instanceof TypeError);
131}
132
133// check coercible - null
134try {
135  assert(String.prototype.substring.call(null, 0, 1) === "");
136  assert(false);
137} catch (e) {
138  assert(e instanceof TypeError);
139}
140
141// check coercible - Boolean
142assert(String.prototype.substring.call(true, 0, 1) === "t");
143
144// check coercible - Object
145var test_object = {firstName:"John", lastName:"Doe"};
146assert(String.prototype.substring.call(test_object, 0, 7) === "[object");
147
148// check coercible - Number
149assert(String.prototype.substring.call(123, 0, 3) === "123");
150