Home | History | Annotate | Download | only in webkit
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions
      6 // are met:
      7 // 1.  Redistributions of source code must retain the above copyright
      8 //     notice, this list of conditions and the following disclaimer.
      9 // 2.  Redistributions in binary form must reproduce the above copyright
     10 //     notice, this list of conditions and the following disclaimer in the
     11 //     documentation and/or other materials provided with the distribution.
     12 //
     13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23 
     24 description("Test to ensure correct behaviour of replacer functions in JSON.stringify");
     25 
     26 var object = {0:0, 1:1, 2:2, 3:undefined};
     27 var array = [0, 1, 2, undefined];
     28 function returnUndefined(){}
     29 function returnObjectFor1(k, v) {
     30     if (k == "1")
     31         return {};
     32     return v;
     33 }
     34 function returnArrayFor1(k, v) {
     35     if (k == "1")
     36         return [];
     37     return v;
     38 }
     39 function returnUndefinedFor1(k, v) {
     40     if (k == "1")
     41         return undefined;
     42     return v;
     43 }
     44 function returnNullFor1(k, v) {
     45     if (k == "1")
     46         return null;
     47     return v;
     48 }
     49 function returnCycleObjectFor1(k, v) {
     50     if (k == "1")
     51         return object;
     52     return v;
     53 }
     54 function returnCycleArrayFor1(k, v) {
     55     if (k == "1")
     56         return array;
     57     return v;
     58 }
     59 function returnFunctionFor1(k, v) {
     60     if (k == "1")
     61         return function(){};
     62     return v;
     63 }
     64 function returnStringForUndefined(k, v) {
     65     if (v === undefined)
     66         return "undefined value";
     67     return v;
     68 }
     69 
     70 shouldBeUndefined("JSON.stringify(object, returnUndefined)");
     71 shouldBeUndefined("JSON.stringify(array, returnUndefined)");
     72 
     73 shouldBe("JSON.stringify(object, returnObjectFor1)", '\'{"0":0,"1":{},"2":2}\'');
     74 shouldBe("JSON.stringify(array, returnObjectFor1)", '\'[0,{},2,null]\'');
     75 
     76 shouldBe("JSON.stringify(object, returnArrayFor1)", '\'{"0":0,"1":[],"2":2}\'');
     77 shouldBe("JSON.stringify(array, returnArrayFor1)", '\'[0,[],2,null]\'');
     78 
     79 shouldBe("JSON.stringify(object, returnUndefinedFor1)", '\'{"0":0,"2":2}\'');
     80 shouldBe("JSON.stringify(array, returnUndefinedFor1)", '\'[0,null,2,null]\'');
     81 
     82 shouldBe("JSON.stringify(object, returnFunctionFor1)", '\'{"0":0,"2":2}\'');
     83 shouldBe("JSON.stringify(array, returnFunctionFor1)", '\'[0,null,2,null]\'');
     84 
     85 shouldBe("JSON.stringify(object, returnNullFor1)", '\'{"0":0,"1":null,"2":2}\'');
     86 shouldBe("JSON.stringify(array, returnNullFor1)", '\'[0,null,2,null]\'');
     87 
     88 shouldBe("JSON.stringify(object, returnStringForUndefined)", '\'{"0":0,"1":1,"2":2,"3":"undefined value"}\'');
     89 shouldBe("JSON.stringify(array, returnStringForUndefined)", '\'[0,1,2,"undefined value"]\'');
     90 
     91 shouldThrow("JSON.stringify(object, returnCycleObjectFor1)");
     92 shouldThrow("JSON.stringify(array, returnCycleObjectFor1)");
     93 
     94 shouldThrow("JSON.stringify(object, returnCycleArrayFor1)");
     95 shouldThrow("JSON.stringify(array, returnCycleArrayFor1)");
     96