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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial