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: 03 September 2001
     21 *
     22 * SUMMARY: Double quotes should be escaped in Error.prototype.toSource()
     23 * See http://bugzilla.mozilla.org/show_bug.cgi?id=96284
     24 *
     25 * The real point here is this: we should be able to reconstruct an object
     26 * from its toSource() property. We'll test this on various types of objects.
     27 *
     28 * Method: define obj2 = eval(obj1.toSource()) and verify that
     29 * obj2.toSource() == obj1.toSource().
     30 */
     31 //-----------------------------------------------------------------------------
     32 var UBound = 0;
     33 var bug = 96284;
     34 var summary = 'Double quotes should be escaped in Error.prototype.toSource()';
     35 var status = '';
     36 var statusitems = [];
     37 var actual = '';
     38 var actualvalues = [];
     39 var expect= '';
     40 var expectedvalues = [];
     41 var obj1 = {};
     42 var obj2 = {};
     43 var cnTestString = '"This is a \" STUPID \" test string!!!"\\';
     44 
     45 
     46 // various NativeError objects -
     47 status = inSection(1);
     48 obj1 = Error(cnTestString);
     49 obj2 = eval(obj1.toSource());
     50 actual = obj2.toSource();
     51 expect = obj1.toSource();
     52 addThis();
     53 
     54 status = inSection(2);
     55 obj1 = EvalError(cnTestString);
     56 obj2 = eval(obj1.toSource());
     57 actual = obj2.toSource();
     58 expect = obj1.toSource();
     59 addThis();
     60 
     61 status = inSection(3);
     62 obj1 = RangeError(cnTestString);
     63 obj2 = eval(obj1.toSource());
     64 actual = obj2.toSource();
     65 expect = obj1.toSource();
     66 addThis();
     67 
     68 status = inSection(4);
     69 obj1 = ReferenceError(cnTestString);
     70 obj2 = eval(obj1.toSource());
     71 actual = obj2.toSource();
     72 expect = obj1.toSource();
     73 addThis();
     74 
     75 status = inSection(5);
     76 obj1 = SyntaxError(cnTestString);
     77 obj2 = eval(obj1.toSource());
     78 actual = obj2.toSource();
     79 expect = obj1.toSource();
     80 addThis();
     81 
     82 status = inSection(6);
     83 obj1 = TypeError(cnTestString);
     84 obj2 = eval(obj1.toSource());
     85 actual = obj2.toSource();
     86 expect = obj1.toSource();
     87 addThis();
     88 
     89 status = inSection(7);
     90 obj1 = URIError(cnTestString);
     91 obj2 = eval(obj1.toSource());
     92 actual = obj2.toSource();
     93 expect = obj1.toSource();
     94 addThis();
     95 
     96 
     97 // other types of objects -
     98 status = inSection(8);
     99 obj1 = new String(cnTestString);
    100 obj2 = eval(obj1.toSource());
    101 actual = obj2.toSource();
    102 expect = obj1.toSource();
    103 addThis();
    104 
    105 status = inSection(9);
    106 obj1 = {color:'red', texture:cnTestString, hasOwnProperty:42};
    107 obj2 = eval(obj1.toSource());
    108 actual = obj2.toSource();
    109 expect = obj1.toSource();
    110 addThis();
    111 
    112 status = inSection(10);
    113 obj1 = function(x) {function g(y){return y+1;} return g(x);};
    114 obj2 = eval(obj1.toSource());
    115 actual = obj2.toSource();
    116 expect = obj1.toSource();
    117 addThis();
    118 
    119 status = inSection(11);
    120 obj1 = new Number(eval('6'));
    121 obj2 = eval(obj1.toSource());
    122 actual = obj2.toSource();
    123 expect = obj1.toSource();
    124 addThis();
    125 
    126 status = inSection(12);
    127 obj1 = /ad;(lf)kj(2309\/\/)\/\//;
    128 obj2 = eval(obj1.toSource());
    129 actual = obj2.toSource();
    130 expect = obj1.toSource();
    131 addThis();
    132 
    133 
    134 
    135 //-----------------------------------------------------------------------------
    136 test();
    137 //-----------------------------------------------------------------------------
    138 
    139 
    140 function addThis()
    141 {
    142   statusitems[UBound] = status;
    143   actualvalues[UBound] = actual;
    144   expectedvalues[UBound] = expect;
    145   UBound++;
    146 }
    147 
    148 
    149 function test()
    150 {
    151   enterFunc ('test');
    152   printBugNumber (bug);
    153   printStatus (summary);
    154 
    155   for (var i = 0; i < UBound; i++)
    156   {
    157     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
    158   }
    159 
    160   exitFunc ('test');
    161 }
    162