1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/src/json/test/unit/test_encode.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 1.4 +var Ci = Components.interfaces; 1.5 +var Cc = Components.classes; 1.6 + 1.7 +var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON); 1.8 + 1.9 +var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); 1.10 +var workingDir = dirSvc.get("TmpD", Ci.nsIFile); 1.11 + 1.12 +var outputName = "json-test-output"; 1.13 +var outputDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); 1.14 +outputDir.initWithFile(workingDir); 1.15 +outputDir.append(outputName); 1.16 + 1.17 +if (!outputDir.exists()) { 1.18 + outputDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0777); 1.19 +} else if (!outputDir.isDirectory()) { 1.20 + do_throw(outputName + " is not a directory?") 1.21 +} 1.22 + 1.23 +function testStringEncode() 1.24 +{ 1.25 + var obj1 = {a:1}; 1.26 + var obj2 = {foo:"bar"}; 1.27 + do_check_eq(nativeJSON.encode(obj1), '{"a":1}'); 1.28 + do_check_eq(nativeJSON.encode(obj2), '{"foo":"bar"}'); 1.29 + 1.30 + do_check_eq(nativeJSON.encode(), null); 1.31 + 1.32 + // useless roots are dropped 1.33 + do_check_eq(nativeJSON.encode(null), null); 1.34 + do_check_eq(nativeJSON.encode(""), null); 1.35 + do_check_eq(nativeJSON.encode(undefined), null); 1.36 + do_check_eq(nativeJSON.encode(5), null); 1.37 + do_check_eq(nativeJSON.encode(function(){}), null); 1.38 + do_check_eq(nativeJSON.encode(dump), null); 1.39 + 1.40 + // All other testing should occur in js/src/tests/ecma_5/JSON/ using 1.41 + // the otherwise-exactly-identical JSON.stringify. 1.42 +} 1.43 + 1.44 +function testToJSON() { 1.45 + var obj1 = {a:1}; 1.46 + var obj2 = {foo:"bar"}; 1.47 + do_check_eq(nativeJSON.encode({toJSON: function() obj1}), '{"a":1}'); 1.48 + do_check_eq(nativeJSON.encode({toJSON: function() obj2}), '{"foo":"bar"}'); 1.49 + 1.50 + do_check_eq(nativeJSON.encode({toJSON: function() null}), null); 1.51 + do_check_eq(nativeJSON.encode({toJSON: function() ""}), null); 1.52 + do_check_eq(nativeJSON.encode({toJSON: function() undefined }), null); 1.53 + do_check_eq(nativeJSON.encode({toJSON: function() 5}), null); 1.54 + do_check_eq(nativeJSON.encode({toJSON: function() function(){}}), null); 1.55 + do_check_eq(nativeJSON.encode({toJSON: function() dump}), null); 1.56 +} 1.57 + 1.58 +function testThrowingToJSON() { 1.59 + var obj1 = { 1.60 + "b": 1, 1.61 + "c": 2, 1.62 + toJSON: function() { throw("uh oh"); } 1.63 + }; 1.64 + try { 1.65 + var y = nativeJSON.encode(obj1); 1.66 + throw "didn't throw"; 1.67 + } catch (ex) { 1.68 + do_check_eq(ex, "uh oh"); 1.69 + } 1.70 + 1.71 + var obj2 = { 1.72 + "b": 1, 1.73 + "c": 2, 1.74 + get toJSON() { throw("crash and burn"); } 1.75 + }; 1.76 + try { 1.77 + var y = nativeJSON.encode(obj2); 1.78 + throw "didn't throw"; 1.79 + } catch (ex) { 1.80 + do_check_eq(ex, "crash and burn"); 1.81 + } 1.82 +} 1.83 + 1.84 +function testOutputStreams() { 1.85 + function writeToFile(obj, charset, writeBOM) { 1.86 + var jsonFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); 1.87 + jsonFile.initWithFile(outputDir); 1.88 + jsonFile.append("test.json"); 1.89 + jsonFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); 1.90 + var stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream); 1.91 + try { 1.92 + stream.init(jsonFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate 1.93 + nativeJSON.encodeToStream(stream, charset, writeBOM, obj); 1.94 + } finally { 1.95 + stream.close(); 1.96 + } 1.97 + return jsonFile; 1.98 + } 1.99 + 1.100 + var pairs = [ 1.101 + ["{}", {}], 1.102 + ['{"foo":"bar"}', {"foo":"bar"}], 1.103 + ['{"null":null}', {"null":null}], 1.104 + ['{"five":5}', {"five":5}], 1.105 + ['{"true":true}', {"true":true}], 1.106 + ['{"x":{"y":"z"}}', {"x":{"y":"z"}}], 1.107 + ['{"w":{"x":{"y":"z"}}}', {"w":{"x":{"y":"z"}}}], 1.108 + ["[]", []], 1.109 + ['[1,2,3]', [1,2,3]], 1.110 + ['[1,null,3]',[1,,3]], 1.111 + ]; 1.112 + for (var i = 0; i < pairs.length; i++) 1.113 + { 1.114 + var pair = pairs[i]; 1.115 + if (pair[1] && (typeof pair[1] == "object")) { 1.116 + var utf8File = writeToFile(pair[1], "UTF-8", false); 1.117 + var utf16LEFile = writeToFile(pair[1], "UTF-16LE", false); 1.118 + var utf16BEFile = writeToFile(pair[1], "UTF-16BE", false); 1.119 + 1.120 + // all ascii with no BOMs, so this will work 1.121 + do_check_eq(utf16LEFile.fileSize / 2, utf8File.fileSize); 1.122 + do_check_eq(utf16LEFile.fileSize, utf16BEFile.fileSize); 1.123 + } 1.124 + } 1.125 + 1.126 + // check BOMs 1.127 + // the clone() calls are there to work around -- bug 410005 1.128 + var f = writeToFile({},"UTF-8", true).clone(); 1.129 + do_check_eq(f.fileSize, 5); 1.130 + var f = writeToFile({},"UTF-16LE", true).clone(); 1.131 + do_check_eq(f.fileSize, 6); 1.132 + var f = writeToFile({},"UTF-16BE", true).clone(); 1.133 + do_check_eq(f.fileSize, 6); 1.134 + 1.135 + outputDir.remove(true); 1.136 +} 1.137 + 1.138 +function run_test() 1.139 +{ 1.140 + testStringEncode(); 1.141 + testToJSON(); 1.142 + testThrowingToJSON(); 1.143 + 1.144 + testOutputStreams(); 1.145 + 1.146 +}