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// 1. 16var simple_obj = {a: 1, b: 2, c: 3, d: 4}; 17for (var prop_of_simple_obj in simple_obj) { 18 simple_obj[prop_of_simple_obj] += 4; 19} 20 21assert(simple_obj.a === 5 22 && simple_obj.b === 6 23 && simple_obj.c === 7 24 && simple_obj.d === 8); 25 26// 2. 27for 28 ( 29 var 30 prop_of_simple_obj in simple_obj 31 ) { 32 simple_obj[prop_of_simple_obj] -= 4; 33} 34 35assert(simple_obj.a === 1 36 && simple_obj.b === 2 37 && simple_obj.c === 3 38 && simple_obj.d === 4); 39 40// 3. 41function test() { 42 var cnt = 0; 43 44 for (var prop_of_simple_obj in simple_obj) { 45 if (prop_of_simple_obj === 'b') 46 continue; 47 48 cnt++; 49 50 simple_obj[prop_of_simple_obj] += 4; 51 } 52 53 return cnt; 54} 55 56var ret_val = test(); 57 58assert((simple_obj.a === 5 59 && simple_obj.b === 2 60 && simple_obj.c === 7 61 && simple_obj.d == 8) 62 && ret_val === 3); 63 64// 4. 65var array_obj = new Array(1, 2, 3, 4, 5, 6, 7); 66var prop_of_array_obj; 67 68array_obj.eight = 8; 69 70for (prop_of_array_obj in array_obj) { 71 array_obj[prop_of_array_obj] += 1; 72} 73 74assert(array_obj[0] === 2 75 && array_obj[1] === 3 76 && array_obj[2] === 4 77 && array_obj[3] === 5 78 && array_obj[4] === 6 79 && array_obj[5] === 7 80 && array_obj[6] === 8 81 && array_obj['eight'] === 9); 82 83// 5. 84var null_obj = null; 85for (var prop_of_null_obj in null_obj) { 86 assert(false); 87} 88 89// 6. 90var empty_object = {}; 91for (var prop_of_empty_object in empty_object) { 92 assert(false); 93} 94 95// 7. 96for (var i in undefined) { 97 assert(false); 98} 99 100// 8. 101var base_obj = {base_prop: "base"}; 102 103function constr() { 104 this.derived_prop = "derived"; 105} 106 107constr.prototype = base_obj; 108 109var derived_obj = new constr(); 110 111for (var prop_of_derived_obj in derived_obj) { 112 derived_obj[prop_of_derived_obj] += "A"; 113} 114 115assert(derived_obj.base_prop === "baseA" && derived_obj.derived_prop === "derivedA"); 116 117// 9. 118log = {}; 119count = 0; 120 121for (i in {q : 1}) 122{ 123 log [i] = true; 124 count++; 125} 126 127assert (count == 1 && 'q' in log); 128 129// 10. 130log = {}; 131count = 0; 132 133for (i in {q : 1, p : 2, get f() { ; }, set f (v) { ; }, get t () { }, set c (v) {}}) 134{ 135 log [i] = true; 136 count++; 137} 138 139assert (count == 5 140 && 'q' in log 141 && 'p' in log 142 && 'f' in log 143 && 't' in log 144 && 'c' in log); 145 146// 11. 147log = {}; 148count = 0; 149 150var a = []; 151a[5] = 5; 152for (var x in a) 153{ 154 log[x] = true; 155 count++; 156} 157 158assert (count == 1 159 && '5' in log); 160 161// 12. 162log = {}; 163count = 0; 164 165q = { c : 3, d : 4 }; 166 167function p_constructor () 168{ 169 this.a = 1; 170 this.b = 2; 171 172 return this; 173} 174 175p_constructor.prototype = q; 176p = new p_constructor (); 177 178Object.defineProperty (p, 'h', { value : 5, enumerable : false, configurable : true }); 179Object.defineProperty (q, 'h', { value : 6, enumerable : true, configurable : true }); 180 181for (var i in p) 182{ 183 log[i] = true; 184 count++; 185} 186 187assert (count == 4 188 && 'a' in log 189 && 'b' in log 190 && 'c' in log 191 && 'd' in log); 192 193// 13. 194log = {}; 195count = 0; 196 197function f() 198{ 199 var tmp = { a: 1, b: 2, c: 3, d: 4 }; 200 201 return tmp; 202} 203 204for (var i in f()) 205{ 206 log[i] = true; 207 count++; 208} 209 210assert (count == 4 211 && 'a' in log 212 && 'b' in log 213 && 'c' in log 214 && 'd' in log); 215 216// 14. 217log = {}; 218count = 0; 219 220b = 'prop'; 221c = { prop : 1 }; 222Boolean.prototype.boolean_prototype_prop = 1; 223 224for (a in b in c) 225{ 226 log[a] = true; 227 count++; 228} 229 230assert (count == 1 231 && 'boolean_prototype_prop' in log); 232 233// 15. 234log = {}; 235count = 0; 236 237for (a in 'prop' in { prop : 1 }) 238{ 239 log[a] = true; 240 count++; 241} 242 243assert (count == 1 244 && 'boolean_prototype_prop' in log); 245 246// 16. 247a = 'str'; 248b = {}; 249for ((a in b) ; ; ) 250{ 251 break; 252} 253 254// 17. 255log = {}; 256count = 0; 257 258var base_obj = { base_prop1: "base1", base_prop2: "base2" }; 259 260function l () { 261 this.derived_prop1 = "derived1"; 262 this.derived_prop2 = "derived2"; 263} 264 265l.prototype = base_obj; 266 267var derived_obj = new l(); 268 269for (var prop_of_derived_obj in derived_obj) { 270 delete derived_obj.derived_prop1; 271 delete derived_obj.derived_prop2; 272 delete base_obj.base_prop1; 273 delete base_obj.base_prop2; 274 275 log[prop_of_derived_obj] = true; 276 count++; 277} 278 279assert(count == 1 280 && ('base_prop1' in log 281 || 'base_prop2' in log 282 || 'derived_prop1' in log 283 || 'derived_prop2' in log)); 284 285try { 286 /* This form is a SyntaxError even in ES5.1. */ 287 eval("for (a = b in {}) ;"); 288 assert(false); 289} catch (e) { 290 assert(e instanceof SyntaxError); 291} 292