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