| OLD | NEW |
| (Empty) |
| 1 description( | |
| 2 "Test some array functions on non-array objects." | |
| 3 ); | |
| 4 | |
| 5 function properties(object, extraName1, extraName2, extraName3) | |
| 6 { | |
| 7 var string = ""; | |
| 8 | |
| 9 // destructive, lists properties | |
| 10 var names = []; | |
| 11 var enumerables = {}; | |
| 12 for (propertyName in object) { | |
| 13 names.push(propertyName); | |
| 14 enumerables[propertyName] = 1; | |
| 15 } | |
| 16 names.push("length"); | |
| 17 names.push(extraName1); | |
| 18 names.push(extraName2); | |
| 19 names.push(extraName3); | |
| 20 names.sort(); | |
| 21 | |
| 22 var propertyStrings = []; | |
| 23 for (i = 0; i < names.length; ++i) { | |
| 24 var name = names[i]; | |
| 25 if (name == names[i - 1]) | |
| 26 continue; | |
| 27 if (!(name in object)) | |
| 28 continue; | |
| 29 | |
| 30 var propertyString = name + ":" + object[name]; | |
| 31 | |
| 32 var flags = []; | |
| 33 if (!object.hasOwnProperty(name)) | |
| 34 flags.push("FromPrototype"); | |
| 35 if (!enumerables[name]) | |
| 36 flags.push("DontEnum"); | |
| 37 if (name != "length") { | |
| 38 try { | |
| 39 object[name] = "ReadOnlyTestValue"; | |
| 40 } catch (e) { | |
| 41 } | |
| 42 if (object[name] != "ReadOnlyTestValue") | |
| 43 flags.push("ReadOnly"); | |
| 44 } | |
| 45 delete object[name]; | |
| 46 if (object.hasOwnProperty(name)) | |
| 47 flags.push("DontDelete"); | |
| 48 | |
| 49 flags.sort(); | |
| 50 | |
| 51 if (flags.length) | |
| 52 propertyString += "(" + flags.join(", ") + ")"; | |
| 53 | |
| 54 propertyStrings.push(propertyString); | |
| 55 } | |
| 56 return propertyStrings.join(", "); | |
| 57 } | |
| 58 | |
| 59 var x; | |
| 60 | |
| 61 var oneItemPrototype = { length:1, 0:'a' }; | |
| 62 function OneItemConstructor() | |
| 63 { | |
| 64 } | |
| 65 OneItemConstructor.prototype = oneItemPrototype; | |
| 66 | |
| 67 var twoItemPrototype = { length:2, 0:'b', 1:'a' }; | |
| 68 function TwoItemConstructor() | |
| 69 { | |
| 70 } | |
| 71 TwoItemConstructor.prototype = twoItemPrototype; | |
| 72 | |
| 73 shouldBe("properties(['b', 'a'])", "'0:b, 1:a, length:2(DontDelete, DontEnum)'")
; | |
| 74 shouldBe("properties({ length:2, 0:'b', 1:'a' })", "'0:b, 1:a, length:2'"); | |
| 75 | |
| 76 shouldBe("properties(new OneItemConstructor)", "'0:a(FromPrototype), length:1(Fr
omPrototype)'"); | |
| 77 shouldBe("properties(new TwoItemConstructor)", "'0:b(FromPrototype), 1:a(FromPro
totype), length:2(FromPrototype)'"); | |
| 78 | |
| 79 shouldBe("Array.prototype.toString.call({})", '"' + ({}).toString() + '"'); | |
| 80 shouldBe("Array.prototype.toString.call(new Date)", '"' + Object.prototype.toStr
ing.call(new Date) + '"'); | |
| 81 shouldBe("Array.prototype.toString.call({sort: function() { return 'sort' }})",
'"' + Object.prototype.toString.call({}) + '"'); | |
| 82 shouldBe("Array.prototype.toString.call({join: function() { return 'join' }})",
'"join"'); | |
| 83 shouldBe("Array.prototype.toString.call({__proto__: Array.prototype, 0: 'a', 1:
'b', 2: 'c', length: 3})", '"a,b,c"'); | |
| 84 shouldBe("({__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', length: 3}).toSt
ring()", '"a,b,c"'); | |
| 85 shouldBe("Array.prototype.toString.call({__proto__: Array.prototype, 0: 'a', 1:
'b', 2: 'c', length: 3, join: function() { return 'join' }})", '"join"'); | |
| 86 shouldBe("({__proto__: Array.prototype, 0: 'a', 1: 'b', 2: 'c', length: 3, join:
function() { return 'join' }}).toString()", '"join"'); | |
| 87 Number.prototype.join = function() { return "Number.prototype.join:" + this; } | |
| 88 shouldBe("Array.prototype.toString.call(42)", '"Number.prototype.join:42"'); | |
| 89 var arrayJoin = Array.prototype.join; | |
| 90 Array.prototype.join = function() { return 'array-join' }; | |
| 91 shouldBe("[0, 1, 2].toString()", '"array-join"'); | |
| 92 Array.prototype.join = arrayJoin; | |
| 93 | |
| 94 shouldThrow("Array.prototype.toLocaleString.call({})"); | |
| 95 | |
| 96 shouldBe("Array.prototype.concat.call(x = { length:2, 0:'b', 1:'a' })", "[x]"); | |
| 97 | |
| 98 shouldBe("Array.prototype.join.call({})", "''"); | |
| 99 shouldBe("Array.prototype.join.call(['b', 'a'])", "'b,a'"); | |
| 100 shouldBe("Array.prototype.join.call({ length:2, 0:'b', 1:'a' })", "'b,a'"); | |
| 101 shouldBe("Array.prototype.join.call(new TwoItemConstructor)", "'b,a'"); | |
| 102 | |
| 103 shouldBe("Array.prototype.pop.call({})", "undefined"); | |
| 104 shouldBe("Array.prototype.pop.call({ length:2, 0:'b', 1:'a' })", "'a'"); | |
| 105 shouldBe("Array.prototype.pop.call({ length:2, 0:'b', 1:'a' })", "'a'"); | |
| 106 shouldBe("Array.prototype.pop.call(new TwoItemConstructor)", "'a'"); | |
| 107 shouldBe("Array.prototype.pop.call(x = {}); properties(x)", "'length:0'"); | |
| 108 shouldBe("Array.prototype.pop.call(x = ['b', 'a']); properties(x)", "'0:b, lengt
h:1(DontDelete, DontEnum)'"); | |
| 109 shouldBe("Array.prototype.pop.call(x = { length:2, 0:'b', 1:'a' }); properties(x
)", "'0:b, length:1'"); | |
| 110 shouldBe("Array.prototype.pop.call(x = new TwoItemConstructor); properties(x)",
"'0:b(FromPrototype), 1:a(FromPrototype), length:1'"); | |
| 111 | |
| 112 shouldBe("Array.prototype.push.call({})", "0"); | |
| 113 shouldBe("Array.prototype.push.call(['b', 'a'])", "2"); | |
| 114 shouldBe("Array.prototype.push.call({ length:2, 0:'b', 1:'a' })", "2"); | |
| 115 shouldBe("Array.prototype.push.call(new TwoItemConstructor)", "2"); | |
| 116 shouldBe("Array.prototype.push.call(x = {}); properties(x)", "'length:0'"); | |
| 117 shouldBe("Array.prototype.push.call(x = ['b', 'a']); properties(x)", "'0:b, 1:a,
length:2(DontDelete, DontEnum)'"); | |
| 118 shouldBe("Array.prototype.push.call(x = { length:2, 0:'b', 1:'a' }); properties(
x)", "'0:b, 1:a, length:2'"); | |
| 119 shouldBe("Array.prototype.push.call(x = new TwoItemConstructor); properties(x)",
"'0:b(FromPrototype), 1:a(FromPrototype), length:2'"); | |
| 120 shouldBe("Array.prototype.push.call({}, 'c')", "1"); | |
| 121 shouldBe("Array.prototype.push.call(['b', 'a'], 'c')", "3"); | |
| 122 shouldBe("Array.prototype.push.call({ length:2, 0:'b', 1:'a' }, 'c')", "3"); | |
| 123 shouldBe("Array.prototype.push.call(new TwoItemConstructor, 'c')", "3"); | |
| 124 shouldBe("Array.prototype.push.call(x = {}, 'c'); properties(x)", "'0:c, length:
1'"); | |
| 125 shouldBe("Array.prototype.push.call(x = ['b', 'a'], 'c'); properties(x)", "'0:b,
1:a, 2:c, length:3(DontDelete, DontEnum)'"); | |
| 126 shouldBe("Array.prototype.push.call(x = { length:2, 0:'b', 1:'a' }, 'c'); proper
ties(x)", "'0:b, 1:a, 2:c, length:3'"); | |
| 127 shouldBe("Array.prototype.push.call(x = new TwoItemConstructor, 'c'); properties
(x)", "'0:b(FromPrototype), 1:a(FromPrototype), 2:c, length:3'"); | |
| 128 | |
| 129 shouldBe("properties(Array.prototype.reverse.call({}))", "''"); | |
| 130 shouldBe("properties(Array.prototype.reverse.call(['b', 'a']))", "'0:a, 1:b, len
gth:2(DontDelete, DontEnum)'"); | |
| 131 shouldBe("properties(Array.prototype.reverse.call({ length:2, 0:'b', 1:'a' }))",
"'0:a, 1:b, length:2'"); | |
| 132 shouldBe("properties(Array.prototype.reverse.call(new OneItemConstructor))", "'0
:a(FromPrototype), length:1(FromPrototype)'"); | |
| 133 shouldBe("properties(Array.prototype.reverse.call(new TwoItemConstructor))", "'0
:a, 1:b, length:2(FromPrototype)'"); | |
| 134 | |
| 135 shouldBe("Array.prototype.shift.call({})", "undefined"); | |
| 136 shouldBe("Array.prototype.shift.call(['b', 'a'])", "'b'"); | |
| 137 shouldBe("Array.prototype.shift.call({ length:2, 0:'b', 1:'a' })", "'b'"); | |
| 138 shouldBe("Array.prototype.shift.call(new TwoItemConstructor)", "'b'"); | |
| 139 shouldBe("Array.prototype.shift.call(x = {}); properties(x)", "'length:0'"); | |
| 140 shouldBe("Array.prototype.shift.call(x = ['b', 'a']); properties(x)", "'0:a, len
gth:1(DontDelete, DontEnum)'"); | |
| 141 shouldBe("Array.prototype.shift.call(x = { length:2, 0:'b', 1:'a' }); properties
(x)", "'0:a, length:1'"); | |
| 142 shouldBe("Array.prototype.shift.call(x = new TwoItemConstructor); properties(x)"
, "'0:a, 1:a(FromPrototype), length:1'"); | |
| 143 | |
| 144 shouldBe("Array.prototype.slice.call({}, 0, 1)", "[]"); | |
| 145 shouldBe("Array.prototype.slice.call(['b', 'a'], 0, 1)", "['b']"); | |
| 146 shouldBe("Array.prototype.slice.call({ length:2, 0:'b', 1:'a' }, 0, 1)", "['b']"
); | |
| 147 shouldBe("Array.prototype.slice.call(new TwoItemConstructor, 0, 1)", "['b']"); | |
| 148 | |
| 149 shouldBe("properties(Array.prototype.sort.call({}))", "''"); | |
| 150 shouldBe("properties(Array.prototype.sort.call(['b', 'a']))", "'0:a, 1:b, length
:2(DontDelete, DontEnum)'"); | |
| 151 shouldBe("properties(Array.prototype.sort.call({ length:2, 0:'b', 1:'a' }))", "'
0:a, 1:b, length:2'"); | |
| 152 shouldBe("properties(Array.prototype.sort.call(new OneItemConstructor))", "'0:a(
FromPrototype), length:1(FromPrototype)'"); | |
| 153 shouldBe("properties(Array.prototype.sort.call(new TwoItemConstructor))", "'0:a,
1:b, length:2(FromPrototype)'"); | |
| 154 | |
| 155 shouldBe("Array.prototype.splice.call({}, 0, 1)", "[]"); | |
| 156 shouldBe("Array.prototype.splice.call(['b', 'a'], 0, 1)", "['b']"); | |
| 157 shouldBe("Array.prototype.splice.call({ length:2, 0:'b', 1:'a' }, 0, 1)", "['b']
"); | |
| 158 shouldBe("Array.prototype.splice.call(new TwoItemConstructor, 0, 1)", "['b']"); | |
| 159 shouldBe("Array.prototype.splice.call(x = {}, 0, 1); properties(x)", "'length:0'
"); | |
| 160 shouldBe("Array.prototype.splice.call(x = ['b', 'a'], 0, 1); properties(x)", "'0
:a, length:1(DontDelete, DontEnum)'"); | |
| 161 shouldBe("Array.prototype.splice.call(x = { length:2, 0:'b', 1:'a' }, 0, 1); pro
perties(x)", "'0:a, length:1'"); | |
| 162 shouldBe("Array.prototype.splice.call(x = new TwoItemConstructor, 0, 1); propert
ies(x)", "'0:a, 1:a(FromPrototype), length:1'"); | |
| 163 | |
| 164 shouldBe("Array.prototype.unshift.call({})", "0"); | |
| 165 shouldBe("Array.prototype.unshift.call(['b', 'a'])", "2"); | |
| 166 shouldBe("Array.prototype.unshift.call({ length:2, 0:'b', 1:'a' })", "2"); | |
| 167 shouldBe("Array.prototype.unshift.call(new TwoItemConstructor)", "2"); | |
| 168 shouldBe("Array.prototype.unshift.call(x = {}); properties(x)", "'length:0'"); | |
| 169 shouldBe("Array.prototype.unshift.call(x = ['b', 'a']); properties(x)", "'0:b, 1
:a, length:2(DontDelete, DontEnum)'"); | |
| 170 shouldBe("Array.prototype.unshift.call(x = { length:2, 0:'b', 1:'a' }); properti
es(x)", "'0:b, 1:a, length:2'"); | |
| 171 shouldBe("Array.prototype.unshift.call(x = new TwoItemConstructor); properties(x
)", "'0:b(FromPrototype), 1:a(FromPrototype), length:2'"); | |
| 172 shouldBe("Array.prototype.unshift.call({}, 'c')", "1"); | |
| 173 shouldBe("Array.prototype.unshift.call(['b', 'a'], 'c')", "3"); | |
| 174 shouldBe("Array.prototype.unshift.call({ length:2, 0:'b', 1:'a' }, 'c')", "3"); | |
| 175 shouldBe("Array.prototype.unshift.call(new TwoItemConstructor, 'c')", "3"); | |
| 176 shouldBe("Array.prototype.unshift.call(x = {}, 'c'); properties(x)", "'0:c, leng
th:1'"); | |
| 177 shouldBe("Array.prototype.unshift.call(x = ['b', 'a'], 'c'); properties(x)", "'0
:c, 1:b, 2:a, length:3(DontDelete, DontEnum)'"); | |
| 178 shouldBe("Array.prototype.unshift.call(x = { length:2, 0:'b', 1:'a' }, 'c'); pro
perties(x)", "'0:c, 1:b, 2:a, length:3'"); | |
| 179 shouldBe("Array.prototype.unshift.call(x = new TwoItemConstructor, 'c'); propert
ies(x)", "'0:c, 1:b, 2:a, length:3'"); | |
| 180 | |
| 181 // FIXME: Add tests for every, forEach, some, indexOf, lastIndexOf, filter, and
map | |
| OLD | NEW |