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 toSource().
     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 * Sorting properties alphabetically is done for definiteness in comparisons.
     27 */
     28 //-----------------------------------------------------------------------------
     29 var UBound = 0;
     30 var bug = 90596;
     31 var summary = 'A [DontEnum] prop, if overridden, should appear in toSource()';
     32 var cnCOMMA = ',';
     33 var cnLBRACE = '{';
     34 var cnRBRACE = '}';
     35 var cnLPAREN = '(';
     36 var cnRPAREN = ')';
     37 var status = '';
     38 var statusitems = [];
     39 var actual = '';
     40 var actualvalues = [];
     41 var expect= '';
     42 var expectedvalues = [];
     43 var obj = {};
     44 
     45 
     46 status = inSection(1);
     47 obj = {toString:9};
     48 actual = obj.toSource();
     49 expect = '({toString:9})';
     50 addThis();
     51 
     52 status = inSection(2);
     53 obj = {hasOwnProperty:"Hi"};
     54 actual = obj.toSource();
     55 expect = '({hasOwnProperty:"Hi"})';
     56 addThis();
     57 
     58 status = inSection(3);
     59 obj = {toString:9, hasOwnProperty:"Hi"};
     60 actual = obj.toSource();
     61 expect = '({toString:9, hasOwnProperty:"Hi"})';
     62 addThis();
     63 
     64 status = inSection(4);
     65 obj = {prop1:1, toString:9, hasOwnProperty:"Hi"};
     66 actual = obj.toSource();
     67 expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
     68 addThis();
     69 
     70 
     71 // TRY THE SAME THING IN EVAL CODE
     72 var s = '';
     73 
     74 status = inSection(5);
     75 s = 'obj = {toString:9}';
     76 eval(s);
     77 actual = obj.toSource();
     78 expect = '({toString:9})';
     79 addThis();
     80 
     81 status = inSection(6);
     82 s = 'obj = {hasOwnProperty:"Hi"}';
     83 eval(s);
     84 actual = obj.toSource();
     85 expect = '({hasOwnProperty:"Hi"})';
     86 addThis();
     87 
     88 status = inSection(7);
     89 s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
     90 eval(s);
     91 actual = obj.toSource();
     92 expect = '({toString:9, hasOwnProperty:"Hi"})';
     93 addThis();
     94 
     95 status = inSection(8);
     96 s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
     97 eval(s);
     98 actual = obj.toSource();
     99 expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
    100 addThis();
    101 
    102 
    103 // TRY THE SAME THING IN FUNCTION CODE
    104 function A()
    105 {
    106   status = inSection(9);
    107   var s = 'obj = {toString:9}';
    108   eval(s);
    109   actual = obj.toSource();
    110   expect = '({toString:9})';
    111   addThis();
    112 }
    113 A();
    114 
    115 function B()
    116 {
    117   status = inSection(10);
    118   var s = 'obj = {hasOwnProperty:"Hi"}';
    119   eval(s);
    120   actual = obj.toSource();
    121   expect = '({hasOwnProperty:"Hi"})';
    122   addThis();
    123 }
    124 B();
    125 
    126 function C()
    127 {
    128   status = inSection(11);
    129   var s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
    130   eval(s);
    131   actual = obj.toSource();
    132   expect = '({toString:9, hasOwnProperty:"Hi"})';
    133   addThis();
    134 }
    135 C();
    136 
    137 function D()
    138 {
    139   status = inSection(12);
    140   var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
    141   eval(s);
    142   actual = obj.toSource();
    143   expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
    144   addThis();
    145 }
    146 D();
    147 
    148 
    149 
    150 //-----------------------------------------------------------------------------
    151 test();
    152 //-----------------------------------------------------------------------------
    153 
    154 
    155 
    156 /*
    157  * Sort properties alphabetically -
    158  */
    159 function addThis()
    160 {
    161   statusitems[UBound] = status;
    162   actualvalues[UBound] = sortThis(actual);
    163   expectedvalues[UBound] = sortThis(expect);
    164   UBound++;
    165 }
    166 
    167 
    168 /*
    169  * Takes string of form '({"c", "b", "a", 2})' and returns '({"a","b","c",2})'
    170  */
    171 function sortThis(sList)
    172 {
    173   sList = compactThis(sList);
    174   sList = stripParens(sList);
    175   sList = stripBraces(sList);
    176   var arr = sList.split(cnCOMMA);
    177   arr = arr.sort();
    178   var ret = String(arr);
    179   ret = addBraces(ret);
    180   ret = addParens(ret);
    181   return ret;
    182 }
    183 
    184 
    185 /*
    186  * Strips out any whitespace from the text -
    187  */
    188 function compactThis(text)
    189 {
    190   var charCode = 0;
    191   var ret = '';
    192 
    193   for (var i=0; i<text.length; i++)
    194   {
    195     charCode = text.charCodeAt(i);
    196 
    197     if (!isWhiteSpace(charCode))
    198       ret += text.charAt(i);
    199   }
    200 
    201   return ret;
    202 }
    203 
    204 
    205 function isWhiteSpace(charCode)
    206 {
    207   switch (charCode)
    208   {
    209     case (0x0009):
    210     case (0x000B):
    211     case (0x000C):
    212     case (0x0020):
    213     case (0x000A):  // '\n'
    214     case (0x000D):  // '\r'
    215       return true;
    216       break;
    217 
    218     default:
    219       return false;
    220   }
    221 }
    222 
    223 
    224 /*
    225  * strips off parens at beginning and end of text -
    226  */
    227 function stripParens(text)
    228 {
    229   // remember to escape the parens...
    230   var arr = text.match(/^\((.*)\)$/);
    231 
    232   // defend against a null match...
    233   if (arr != null && arr[1] != null)
    234     return arr[1];
    235   return text;
    236 }
    237 
    238 
    239 /*
    240  * strips off braces at beginning and end of text -
    241  */
    242 function stripBraces(text)
    243 {
    244   // remember to escape the braces...
    245   var arr = text.match(/^\{(.*)\}$/);
    246 
    247   // defend against a null match...
    248   if (arr != null && arr[1] != null)
    249     return arr[1];
    250   return text;
    251 }
    252 
    253 
    254 function addBraces(text)
    255 {
    256   return cnLBRACE + text + cnRBRACE;
    257 }
    258 
    259 
    260 function addParens(text)
    261 {
    262   return cnLPAREN + text + cnRPAREN;
    263 }
    264 
    265 
    266 function test()
    267 {
    268   enterFunc ('test');
    269   printBugNumber (bug);
    270   printStatus (summary);
    271 
    272   for (var i=0; i<UBound; i++)
    273   {
    274     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
    275   }
    276 
    277   exitFunc ('test');
    278 }
    279