mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-08 16:58:38 +09:00
import FIREFOX_52_6_0esr_RELEASE from mozilla-esr52 hg repo
This commit is contained in:
commit
dcd9973243
150858 changed files with 23884658 additions and 0 deletions
143
dom/json/test/unit/test_encode.js
Normal file
143
dom/json/test/unit/test_encode.js
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
var Ci = Components.interfaces;
|
||||
var Cc = Components.classes;
|
||||
|
||||
var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
||||
|
||||
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
|
||||
var workingDir = dirSvc.get("TmpD", Ci.nsIFile);
|
||||
|
||||
var outputName = "json-test-output";
|
||||
var outputDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
||||
outputDir.initWithFile(workingDir);
|
||||
outputDir.append(outputName);
|
||||
|
||||
if (!outputDir.exists()) {
|
||||
outputDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o777);
|
||||
} else if (!outputDir.isDirectory()) {
|
||||
do_throw(outputName + " is not a directory?")
|
||||
}
|
||||
|
||||
function testStringEncode()
|
||||
{
|
||||
var obj1 = {a:1};
|
||||
var obj2 = {foo:"bar"};
|
||||
do_check_eq(nativeJSON.encode(obj1), '{"a":1}');
|
||||
do_check_eq(nativeJSON.encode(obj2), '{"foo":"bar"}');
|
||||
|
||||
do_check_eq(nativeJSON.encode(), null);
|
||||
|
||||
// useless roots are dropped
|
||||
do_check_eq(nativeJSON.encode(null), null);
|
||||
do_check_eq(nativeJSON.encode(""), null);
|
||||
do_check_eq(nativeJSON.encode(undefined), null);
|
||||
do_check_eq(nativeJSON.encode(5), null);
|
||||
do_check_eq(nativeJSON.encode(function(){}), null);
|
||||
do_check_eq(nativeJSON.encode(dump), null);
|
||||
|
||||
// All other testing should occur in js/src/tests/ecma_5/JSON/ using
|
||||
// the otherwise-exactly-identical JSON.stringify.
|
||||
}
|
||||
|
||||
function testToJSON() {
|
||||
var obj1 = {a:1};
|
||||
var obj2 = {foo:"bar"};
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => obj1}), '{"a":1}');
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => obj2}), '{"foo":"bar"}');
|
||||
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => null}), null);
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => ""}), null);
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => undefined }), null);
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => 5}), null);
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => function(){}}), null);
|
||||
do_check_eq(nativeJSON.encode({toJSON: () => dump}), null);
|
||||
}
|
||||
|
||||
function testThrowingToJSON() {
|
||||
var obj1 = {
|
||||
"b": 1,
|
||||
"c": 2,
|
||||
toJSON: function() { throw("uh oh"); }
|
||||
};
|
||||
try {
|
||||
var y = nativeJSON.encode(obj1);
|
||||
throw "didn't throw";
|
||||
} catch (ex) {
|
||||
do_check_eq(ex, "uh oh");
|
||||
}
|
||||
|
||||
var obj2 = {
|
||||
"b": 1,
|
||||
"c": 2,
|
||||
get toJSON() { throw("crash and burn"); }
|
||||
};
|
||||
try {
|
||||
var y = nativeJSON.encode(obj2);
|
||||
throw "didn't throw";
|
||||
} catch (ex) {
|
||||
do_check_eq(ex, "crash and burn");
|
||||
}
|
||||
}
|
||||
|
||||
function testOutputStreams() {
|
||||
function writeToFile(obj, charset, writeBOM) {
|
||||
var jsonFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
||||
jsonFile.initWithFile(outputDir);
|
||||
jsonFile.append("test.json");
|
||||
jsonFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
|
||||
var stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
|
||||
try {
|
||||
stream.init(jsonFile, 0x04 | 0x08 | 0x20, 0o600, 0); // write, create, truncate
|
||||
nativeJSON.encodeToStream(stream, charset, writeBOM, obj);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
return jsonFile;
|
||||
}
|
||||
|
||||
var pairs = [
|
||||
["{}", {}],
|
||||
['{"foo":"bar"}', {"foo":"bar"}],
|
||||
['{"null":null}', {"null":null}],
|
||||
['{"five":5}', {"five":5}],
|
||||
['{"true":true}', {"true":true}],
|
||||
['{"x":{"y":"z"}}', {"x":{"y":"z"}}],
|
||||
['{"w":{"x":{"y":"z"}}}', {"w":{"x":{"y":"z"}}}],
|
||||
["[]", []],
|
||||
['[1,2,3]', [1,2,3]],
|
||||
['[1,null,3]',[1,,3]],
|
||||
];
|
||||
for (var i = 0; i < pairs.length; i++)
|
||||
{
|
||||
var pair = pairs[i];
|
||||
if (pair[1] && (typeof pair[1] == "object")) {
|
||||
var utf8File = writeToFile(pair[1], "UTF-8", false);
|
||||
var utf16LEFile = writeToFile(pair[1], "UTF-16LE", false);
|
||||
var utf16BEFile = writeToFile(pair[1], "UTF-16BE", false);
|
||||
|
||||
// all ascii with no BOMs, so this will work
|
||||
do_check_eq(utf16LEFile.fileSize / 2, utf8File.fileSize);
|
||||
do_check_eq(utf16LEFile.fileSize, utf16BEFile.fileSize);
|
||||
}
|
||||
}
|
||||
|
||||
// check BOMs
|
||||
// the clone() calls are there to work around -- bug 410005
|
||||
var f = writeToFile({},"UTF-8", true).clone();
|
||||
do_check_eq(f.fileSize, 5);
|
||||
var f = writeToFile({},"UTF-16LE", true).clone();
|
||||
do_check_eq(f.fileSize, 6);
|
||||
var f = writeToFile({},"UTF-16BE", true).clone();
|
||||
do_check_eq(f.fileSize, 6);
|
||||
|
||||
outputDir.remove(true);
|
||||
}
|
||||
|
||||
function run_test()
|
||||
{
|
||||
testStringEncode();
|
||||
testToJSON();
|
||||
testThrowingToJSON();
|
||||
|
||||
testOutputStreams();
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue