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 15assert ([].join() === ""); 16assert ([1].join() === "1"); 17assert ([1, 2].join() === "1,2"); 18 19 20assert ([].join('--') === ""); 21assert ([1].join("--") === "1"); 22assert ([1, 2].join('--') === "1--2"); 23 24assert ([1,2,3].join({toString: function() { return "--"; }}) === "1--2--3"); 25 26 27// Join should use 'length' to as the number of elements int the array. 28var lst = [1,2,3,4]; 29lst.length = 3; 30assert (lst.join() === [1,2,3].join()); 31 32// Checking behavior when unable to get length. 33var obj = {}; 34Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } }); 35obj.join = Array.prototype.join; 36 37try { 38 obj.join(); 39 // Should not be reached. 40 assert (false); 41} catch (e) { 42 assert (e.message === "foo"); 43 assert (e instanceof ReferenceError); 44} 45 46// Check join argument fail. 47try { 48 [1,2,3].join({toString: function() { throw new ReferenceError ("foo"); }}); 49 // Should not be reached. 50 assert (false); 51} catch (e) { 52 assert (e.message === "foo"); 53 assert (e instanceof ReferenceError); 54} 55 56// Check single join element fail. 57try { 58 [1, 2, {toString: function() { throw new ReferenceError ("foo"); }}, 4].join(); 59 // Should not be reached. 60 assert (false); 61} catch (e) { 62 assert (e.message === "foo"); 63 assert (e instanceof ReferenceError); 64} 65 66// Check join on different object. 67var obj_2 = {}; 68obj_2.length = 3; 69obj_2[0] = 1; 70obj_2[1] = 2; 71obj_2[2] = 3; 72obj_2[3] = 4; 73 74obj_2.join = Array.prototype.join; 75 76assert (obj_2.join() === "1,2,3"); 77 78/* ES v5.1 15.4.4.5.7. 79 Checking behavior when an element throws error */ 80try { 81 var f = function () { throw new TypeError("ooo");}; 82 var arr = [0, 1, 2, 3]; 83 Object.defineProperty(arr, '0', { 'get' : f }); 84 Array.prototype.join.call(arr); 85 assert(false); 86} catch (e) { 87 assert(e instanceof TypeError); 88 assert(e.message == "ooo"); 89} 90