dom/src/json/test/unit/test_encode.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 var Ci = Components.interfaces;
     2 var Cc = Components.classes;
     4 var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
     6 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
     7 var workingDir = dirSvc.get("TmpD", Ci.nsIFile);
     9 var outputName = "json-test-output";
    10 var outputDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    11 outputDir.initWithFile(workingDir);
    12 outputDir.append(outputName);
    14 if (!outputDir.exists()) {
    15   outputDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0777);
    16 } else if (!outputDir.isDirectory()) {
    17   do_throw(outputName + " is not a directory?")
    18 }
    20 function testStringEncode()
    21 {
    22   var obj1 = {a:1};
    23   var obj2 = {foo:"bar"};
    24   do_check_eq(nativeJSON.encode(obj1), '{"a":1}');
    25   do_check_eq(nativeJSON.encode(obj2), '{"foo":"bar"}');
    27   do_check_eq(nativeJSON.encode(), null);
    29   // useless roots are dropped
    30   do_check_eq(nativeJSON.encode(null), null);
    31   do_check_eq(nativeJSON.encode(""), null);
    32   do_check_eq(nativeJSON.encode(undefined), null);
    33   do_check_eq(nativeJSON.encode(5), null);
    34   do_check_eq(nativeJSON.encode(function(){}), null);
    35   do_check_eq(nativeJSON.encode(dump), null);
    37   // All other testing should occur in js/src/tests/ecma_5/JSON/ using
    38   // the otherwise-exactly-identical JSON.stringify.
    39 }
    41 function testToJSON() {
    42   var obj1 = {a:1};
    43   var obj2 = {foo:"bar"};
    44   do_check_eq(nativeJSON.encode({toJSON: function() obj1}), '{"a":1}');
    45   do_check_eq(nativeJSON.encode({toJSON: function() obj2}), '{"foo":"bar"}');
    47   do_check_eq(nativeJSON.encode({toJSON: function() null}), null);
    48   do_check_eq(nativeJSON.encode({toJSON: function() ""}), null);
    49   do_check_eq(nativeJSON.encode({toJSON: function() undefined }), null);
    50   do_check_eq(nativeJSON.encode({toJSON: function() 5}), null);
    51   do_check_eq(nativeJSON.encode({toJSON: function() function(){}}), null);
    52   do_check_eq(nativeJSON.encode({toJSON: function() dump}), null);
    53 }
    55 function testThrowingToJSON() {
    56   var obj1 = {
    57     "b": 1,
    58     "c": 2,
    59     toJSON: function() { throw("uh oh"); }
    60   };
    61   try {
    62     var y = nativeJSON.encode(obj1);
    63     throw "didn't throw";
    64   } catch (ex) {
    65     do_check_eq(ex, "uh oh");
    66   }
    68   var obj2 = {
    69     "b": 1,
    70     "c": 2,
    71     get toJSON() { throw("crash and burn"); }
    72   };
    73   try {
    74     var y = nativeJSON.encode(obj2);
    75     throw "didn't throw";
    76   } catch (ex) {
    77     do_check_eq(ex, "crash and burn");
    78   }
    79 }
    81 function testOutputStreams() {
    82   function writeToFile(obj, charset, writeBOM) {
    83     var jsonFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    84     jsonFile.initWithFile(outputDir);
    85     jsonFile.append("test.json");
    86     jsonFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
    87     var stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
    88     try {
    89       stream.init(jsonFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate
    90       nativeJSON.encodeToStream(stream, charset, writeBOM, obj);
    91     } finally {
    92       stream.close();
    93     }
    94     return jsonFile;
    95   }
    97   var pairs = [
    98     ["{}", {}],
    99     ['{"foo":"bar"}', {"foo":"bar"}],
   100     ['{"null":null}', {"null":null}],
   101     ['{"five":5}', {"five":5}],
   102     ['{"true":true}', {"true":true}],
   103     ['{"x":{"y":"z"}}', {"x":{"y":"z"}}],
   104     ['{"w":{"x":{"y":"z"}}}', {"w":{"x":{"y":"z"}}}],
   105     ["[]", []],
   106     ['[1,2,3]', [1,2,3]],
   107     ['[1,null,3]',[1,,3]],
   108   ];
   109   for (var i = 0; i < pairs.length; i++)
   110   {
   111     var pair = pairs[i];
   112     if (pair[1] && (typeof pair[1] == "object")) {
   113       var utf8File = writeToFile(pair[1], "UTF-8", false);
   114       var utf16LEFile = writeToFile(pair[1], "UTF-16LE", false);
   115       var utf16BEFile = writeToFile(pair[1], "UTF-16BE", false);
   117       // all ascii with no BOMs, so this will work
   118       do_check_eq(utf16LEFile.fileSize / 2, utf8File.fileSize);
   119       do_check_eq(utf16LEFile.fileSize, utf16BEFile.fileSize);
   120     }
   121   }
   123   // check BOMs
   124   // the clone() calls are there to work around -- bug 410005
   125   var f = writeToFile({},"UTF-8", true).clone();
   126   do_check_eq(f.fileSize, 5);
   127   var f = writeToFile({},"UTF-16LE", true).clone();
   128   do_check_eq(f.fileSize, 6);
   129   var f = writeToFile({},"UTF-16BE", true).clone();
   130   do_check_eq(f.fileSize, 6);
   132   outputDir.remove(true);
   133 }
   135 function run_test()
   136 {
   137   testStringEncode();
   138   testToJSON();
   139   testThrowingToJSON();
   141   testOutputStreams();
   143 }

mercurial