regress-90596-003.js (5522B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * Date: 28 August 2001 8 * 9 * SUMMARY: A [DontEnum] prop, if overridden, should appear in for-in loops. 10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596 11 * 12 * NOTE: some inefficiencies in the test are made for the sake of readability. 13 * For example, we quote string values like "Hi" in lines like this: 14 * 15 * actual = enumerateThis(obj); 16 * expect = '{prop:"Hi"}'; 17 * 18 * But enumerateThis(obj) gets literal value Hi for obj.prop, not 19 * literal "Hi". We take care of all these details in the 20 * compactThis(), sortThis() functions. Sorting properties 21 * alphabetically is necessary for the test to work in Rhino. 22 */ 23 //----------------------------------------------------------------------------- 24 var UBound = 0; 25 var BUGNUMBER = 90596; 26 var summary = '[DontEnum] props (if overridden) should appear in for-in loops'; 27 var cnCOMMA = ','; 28 var cnCOLON = ':'; 29 var cnLBRACE = '{'; 30 var cnRBRACE = '}'; 31 var status = ''; 32 var statusitems = []; 33 var actual = ''; 34 var actualvalues = []; 35 var expect= ''; 36 var expectedvalues = []; 37 var obj = {}; 38 39 40 status = inSection(1); 41 obj = {toString:9}; 42 actual = enumerateThis(obj); 43 expect = '{toString:9}'; 44 addThis(); 45 46 status = inSection(2); 47 obj = {hasOwnProperty:"Hi"}; 48 actual = enumerateThis(obj); 49 expect = '{hasOwnProperty:"Hi"}'; 50 addThis(); 51 52 status = inSection(3); 53 obj = {toString:9, hasOwnProperty:"Hi"}; 54 actual = enumerateThis(obj); 55 expect = '{toString:9, hasOwnProperty:"Hi"}'; 56 addThis(); 57 58 status = inSection(4); 59 obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}; 60 actual = enumerateThis(obj); 61 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 62 addThis(); 63 64 65 // TRY THE SAME THING IN EVAL CODE 66 var s = ''; 67 68 status = inSection(5); 69 s = 'obj = {toString:9}'; 70 eval(s); 71 actual = enumerateThis(obj); 72 expect = '{toString:9}'; 73 addThis(); 74 75 status = inSection(6); 76 s = 'obj = {hasOwnProperty:"Hi"}'; 77 eval(s); 78 actual = enumerateThis(obj); 79 expect = '{hasOwnProperty:"Hi"}'; 80 addThis(); 81 82 status = inSection(7); 83 s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; 84 eval(s); 85 actual = enumerateThis(obj); 86 expect = '{toString:9, hasOwnProperty:"Hi"}'; 87 addThis(); 88 89 status = inSection(8); 90 s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; 91 eval(s); 92 actual = enumerateThis(obj); 93 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 94 addThis(); 95 96 97 // TRY THE SAME THING IN FUNCTION CODE 98 function A() 99 { 100 status = inSection(9); 101 var s = 'obj = {toString:9}'; 102 eval(s); 103 actual = enumerateThis(obj); 104 expect = '{toString:9}'; 105 addThis(); 106 } 107 A(); 108 109 function B() 110 { 111 status = inSection(10); 112 var s = 'obj = {hasOwnProperty:"Hi"}'; 113 eval(s); 114 actual = enumerateThis(obj); 115 expect = '{hasOwnProperty:"Hi"}'; 116 addThis(); 117 } 118 B(); 119 120 function C() 121 { 122 status = inSection(11); 123 var s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; 124 eval(s); 125 actual = enumerateThis(obj); 126 expect = '{toString:9, hasOwnProperty:"Hi"}'; 127 addThis(); 128 } 129 C(); 130 131 function D() 132 { 133 status = inSection(12); 134 var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; 135 eval(s); 136 actual = enumerateThis(obj); 137 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 138 addThis(); 139 } 140 D(); 141 142 143 144 //----------------------------------------------------------------------------- 145 test(); 146 //----------------------------------------------------------------------------- 147 148 149 150 function enumerateThis(obj) 151 { 152 var arr = new Array(); 153 154 for (var prop in obj) 155 { 156 arr.push(prop + cnCOLON + obj[prop]); 157 } 158 159 var ret = addBraces(String(arr)); 160 return ret; 161 } 162 163 164 function addBraces(text) 165 { 166 return cnLBRACE + text + cnRBRACE; 167 } 168 169 170 /* 171 * Sort properties alphabetically so the test will work in Rhino 172 */ 173 function addThis() 174 { 175 statusitems[UBound] = status; 176 actualvalues[UBound] = sortThis(actual); 177 expectedvalues[UBound] = sortThis(expect); 178 UBound++; 179 } 180 181 182 /* 183 * Takes a string of the form '{"c", "b", "a", 2}' and returns '{2,a,b,c}' 184 */ 185 function sortThis(sList) 186 { 187 sList = compactThis(sList); 188 sList = stripBraces(sList); 189 var arr = sList.split(cnCOMMA); 190 arr = arr.sort(); 191 var ret = String(arr); 192 ret = addBraces(ret); 193 return ret; 194 } 195 196 197 /* 198 * Strips out any whitespace or quotes from the text - 199 */ 200 function compactThis(text) 201 { 202 var charCode = 0; 203 var ret = ''; 204 205 for (var i=0; i<text.length; i++) 206 { 207 charCode = text.charCodeAt(i); 208 209 if (!isWhiteSpace(charCode) && !isQuote(charCode)) 210 ret += text.charAt(i); 211 } 212 213 return ret; 214 } 215 216 217 function isWhiteSpace(charCode) 218 { 219 switch (charCode) 220 { 221 case (0x0009): 222 case (0x000B): 223 case (0x000C): 224 case (0x0020): 225 case (0x000A): // '\n' 226 case (0x000D): // '\r' 227 return true; 228 break; 229 230 default: 231 return false; 232 } 233 } 234 235 236 function isQuote(charCode) 237 { 238 switch (charCode) 239 { 240 case (0x0027): // single quote 241 case (0x0022): // double quote 242 return true; 243 break; 244 245 default: 246 return false; 247 } 248 } 249 250 251 /* 252 * strips off braces at beginning and end of text - 253 */ 254 function stripBraces(text) 255 { 256 // remember to escape the braces... 257 var arr = text.match(/^\{(.*)\}$/); 258 259 // defend against a null match... 260 if (arr != null && arr[1] != null) 261 return arr[1]; 262 return text; 263 } 264 265 266 function test() 267 { 268 printBugNumber(BUGNUMBER); 269 printStatus (summary); 270 271 for (var i=0; i<UBound; i++) 272 { 273 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 274 } 275 }