1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/Object/regress-90596-003.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,278 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Date: 28 August 2001 1.11 + * 1.12 + * SUMMARY: A [DontEnum] prop, if overridden, should appear in for-in loops. 1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596 1.14 + * 1.15 + * NOTE: some inefficiencies in the test are made for the sake of readability. 1.16 + * For example, we quote string values like "Hi" in lines like this: 1.17 + * 1.18 + * actual = enumerateThis(obj); 1.19 + * expect = '{prop:"Hi"}'; 1.20 + * 1.21 + * But enumerateThis(obj) gets literal value Hi for obj.prop, not 1.22 + * literal "Hi". We take care of all these details in the 1.23 + * compactThis(), sortThis() functions. Sorting properties 1.24 + * alphabetically is necessary for the test to work in Rhino. 1.25 + */ 1.26 +//----------------------------------------------------------------------------- 1.27 +var UBound = 0; 1.28 +var BUGNUMBER = 90596; 1.29 +var summary = '[DontEnum] props (if overridden) should appear in for-in loops'; 1.30 +var cnCOMMA = ','; 1.31 +var cnCOLON = ':'; 1.32 +var cnLBRACE = '{'; 1.33 +var cnRBRACE = '}'; 1.34 +var status = ''; 1.35 +var statusitems = []; 1.36 +var actual = ''; 1.37 +var actualvalues = []; 1.38 +var expect= ''; 1.39 +var expectedvalues = []; 1.40 +var obj = {}; 1.41 + 1.42 + 1.43 +status = inSection(1); 1.44 +obj = {toString:9}; 1.45 +actual = enumerateThis(obj); 1.46 +expect = '{toString:9}'; 1.47 +addThis(); 1.48 + 1.49 +status = inSection(2); 1.50 +obj = {hasOwnProperty:"Hi"}; 1.51 +actual = enumerateThis(obj); 1.52 +expect = '{hasOwnProperty:"Hi"}'; 1.53 +addThis(); 1.54 + 1.55 +status = inSection(3); 1.56 +obj = {toString:9, hasOwnProperty:"Hi"}; 1.57 +actual = enumerateThis(obj); 1.58 +expect = '{toString:9, hasOwnProperty:"Hi"}'; 1.59 +addThis(); 1.60 + 1.61 +status = inSection(4); 1.62 +obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}; 1.63 +actual = enumerateThis(obj); 1.64 +expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 1.65 +addThis(); 1.66 + 1.67 + 1.68 +// TRY THE SAME THING IN EVAL CODE 1.69 +var s = ''; 1.70 + 1.71 +status = inSection(5); 1.72 +s = 'obj = {toString:9}'; 1.73 +eval(s); 1.74 +actual = enumerateThis(obj); 1.75 +expect = '{toString:9}'; 1.76 +addThis(); 1.77 + 1.78 +status = inSection(6); 1.79 +s = 'obj = {hasOwnProperty:"Hi"}'; 1.80 +eval(s); 1.81 +actual = enumerateThis(obj); 1.82 +expect = '{hasOwnProperty:"Hi"}'; 1.83 +addThis(); 1.84 + 1.85 +status = inSection(7); 1.86 +s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; 1.87 +eval(s); 1.88 +actual = enumerateThis(obj); 1.89 +expect = '{toString:9, hasOwnProperty:"Hi"}'; 1.90 +addThis(); 1.91 + 1.92 +status = inSection(8); 1.93 +s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; 1.94 +eval(s); 1.95 +actual = enumerateThis(obj); 1.96 +expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 1.97 +addThis(); 1.98 + 1.99 + 1.100 +// TRY THE SAME THING IN FUNCTION CODE 1.101 +function A() 1.102 +{ 1.103 + status = inSection(9); 1.104 + var s = 'obj = {toString:9}'; 1.105 + eval(s); 1.106 + actual = enumerateThis(obj); 1.107 + expect = '{toString:9}'; 1.108 + addThis(); 1.109 +} 1.110 +A(); 1.111 + 1.112 +function B() 1.113 +{ 1.114 + status = inSection(10); 1.115 + var s = 'obj = {hasOwnProperty:"Hi"}'; 1.116 + eval(s); 1.117 + actual = enumerateThis(obj); 1.118 + expect = '{hasOwnProperty:"Hi"}'; 1.119 + addThis(); 1.120 +} 1.121 +B(); 1.122 + 1.123 +function C() 1.124 +{ 1.125 + status = inSection(11); 1.126 + var s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; 1.127 + eval(s); 1.128 + actual = enumerateThis(obj); 1.129 + expect = '{toString:9, hasOwnProperty:"Hi"}'; 1.130 + addThis(); 1.131 +} 1.132 +C(); 1.133 + 1.134 +function D() 1.135 +{ 1.136 + status = inSection(12); 1.137 + var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; 1.138 + eval(s); 1.139 + actual = enumerateThis(obj); 1.140 + expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; 1.141 + addThis(); 1.142 +} 1.143 +D(); 1.144 + 1.145 + 1.146 + 1.147 +//----------------------------------------------------------------------------- 1.148 +test(); 1.149 +//----------------------------------------------------------------------------- 1.150 + 1.151 + 1.152 + 1.153 +function enumerateThis(obj) 1.154 +{ 1.155 + var arr = new Array(); 1.156 + 1.157 + for (var prop in obj) 1.158 + { 1.159 + arr.push(prop + cnCOLON + obj[prop]); 1.160 + } 1.161 + 1.162 + var ret = addBraces(String(arr)); 1.163 + return ret; 1.164 +} 1.165 + 1.166 + 1.167 +function addBraces(text) 1.168 +{ 1.169 + return cnLBRACE + text + cnRBRACE; 1.170 +} 1.171 + 1.172 + 1.173 +/* 1.174 + * Sort properties alphabetically so the test will work in Rhino 1.175 + */ 1.176 +function addThis() 1.177 +{ 1.178 + statusitems[UBound] = status; 1.179 + actualvalues[UBound] = sortThis(actual); 1.180 + expectedvalues[UBound] = sortThis(expect); 1.181 + UBound++; 1.182 +} 1.183 + 1.184 + 1.185 +/* 1.186 + * Takes a string of the form '{"c", "b", "a", 2}' and returns '{2,a,b,c}' 1.187 + */ 1.188 +function sortThis(sList) 1.189 +{ 1.190 + sList = compactThis(sList); 1.191 + sList = stripBraces(sList); 1.192 + var arr = sList.split(cnCOMMA); 1.193 + arr = arr.sort(); 1.194 + var ret = String(arr); 1.195 + ret = addBraces(ret); 1.196 + return ret; 1.197 +} 1.198 + 1.199 + 1.200 +/* 1.201 + * Strips out any whitespace or quotes from the text - 1.202 + */ 1.203 +function compactThis(text) 1.204 +{ 1.205 + var charCode = 0; 1.206 + var ret = ''; 1.207 + 1.208 + for (var i=0; i<text.length; i++) 1.209 + { 1.210 + charCode = text.charCodeAt(i); 1.211 + 1.212 + if (!isWhiteSpace(charCode) && !isQuote(charCode)) 1.213 + ret += text.charAt(i); 1.214 + } 1.215 + 1.216 + return ret; 1.217 +} 1.218 + 1.219 + 1.220 +function isWhiteSpace(charCode) 1.221 +{ 1.222 + switch (charCode) 1.223 + { 1.224 + case (0x0009): 1.225 + case (0x000B): 1.226 + case (0x000C): 1.227 + case (0x0020): 1.228 + case (0x000A): // '\n' 1.229 + case (0x000D): // '\r' 1.230 + return true; 1.231 + break; 1.232 + 1.233 + default: 1.234 + return false; 1.235 + } 1.236 +} 1.237 + 1.238 + 1.239 +function isQuote(charCode) 1.240 +{ 1.241 + switch (charCode) 1.242 + { 1.243 + case (0x0027): // single quote 1.244 + case (0x0022): // double quote 1.245 + return true; 1.246 + break; 1.247 + 1.248 + default: 1.249 + return false; 1.250 + } 1.251 +} 1.252 + 1.253 + 1.254 +/* 1.255 + * strips off braces at beginning and end of text - 1.256 + */ 1.257 +function stripBraces(text) 1.258 +{ 1.259 + // remember to escape the braces... 1.260 + var arr = text.match(/^\{(.*)\}$/); 1.261 + 1.262 + // defend against a null match... 1.263 + if (arr != null && arr[1] != null) 1.264 + return arr[1]; 1.265 + return text; 1.266 +} 1.267 + 1.268 + 1.269 +function test() 1.270 +{ 1.271 + enterFunc ('test'); 1.272 + printBugNumber(BUGNUMBER); 1.273 + printStatus (summary); 1.274 + 1.275 + for (var i=0; i<UBound; i++) 1.276 + { 1.277 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.278 + } 1.279 + 1.280 + exitFunc ('test'); 1.281 +}