services/sync/tests/unit/test_utils_json.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

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://gre/modules/FileUtils.jsm");
     5 Cu.import("resource://services-sync/util.js");
     7 function run_test() {
     8   initTestLogging();
     9   run_next_test();
    10 }
    12 add_test(function test_roundtrip() {
    13   _("Do a simple write of an array to json and read");
    14   Utils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function(error) {
    15     do_check_eq(error, null);
    17     Utils.jsonLoad("foo", {}, ensureThrows(function(val) {
    18       let foo = val;
    19       do_check_eq(typeof foo, "object");
    20       do_check_eq(foo.length, 2);
    21       do_check_eq(foo[0], "v1");
    22       do_check_eq(foo[1], "v2");
    23       run_next_test();
    24     }));
    25   }));
    26 });
    28 add_test(function test_string() {
    29   _("Try saving simple strings");
    30   Utils.jsonSave("str", {}, "hi", ensureThrows(function(error) {
    31     do_check_eq(error, null);
    33     Utils.jsonLoad("str", {}, ensureThrows(function(val) {
    34       let str = val;
    35       do_check_eq(typeof str, "string");
    36       do_check_eq(str.length, 2);
    37       do_check_eq(str[0], "h");
    38       do_check_eq(str[1], "i");
    39       run_next_test();
    40     }));
    41   }));
    42 });
    44 add_test(function test_number() {
    45   _("Try saving a number");
    46   Utils.jsonSave("num", {}, 42, ensureThrows(function(error) {
    47     do_check_eq(error, null);
    49     Utils.jsonLoad("num", {}, ensureThrows(function(val) {
    50       let num = val;
    51       do_check_eq(typeof num, "number");
    52       do_check_eq(num, 42);
    53       run_next_test();
    54     }));
    55   }));
    56 });
    58 add_test(function test_nonexistent_file() {
    59   _("Try loading a non-existent file.");
    60   Utils.jsonLoad("non-existent", {}, ensureThrows(function(val) {
    61     do_check_eq(val, undefined);
    62     run_next_test();
    63   }));
    64 });
    66 add_test(function test_save_logging() {
    67   _("Verify that writes are logged.");
    68   let trace;
    69   Utils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}},
    70                        "hi", ensureThrows(function () {
    71     do_check_true(!!trace);
    72     run_next_test();
    73   }));
    74 });
    76 add_test(function test_load_logging() {
    77   _("Verify that reads and read errors are logged.");
    79   // Write a file with some invalid JSON
    80   let filePath = "weave/log.json";
    81   let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
    82   let fos = Cc["@mozilla.org/network/file-output-stream;1"]
    83               .createInstance(Ci.nsIFileOutputStream);
    84   let flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE
    85               | FileUtils.MODE_TRUNCATE;
    86   fos.init(file, flags, FileUtils.PERMS_FILE, fos.DEFER_OPEN);
    87   let stream = Cc["@mozilla.org/intl/converter-output-stream;1"]
    88                  .createInstance(Ci.nsIConverterOutputStream);
    89   stream.init(fos, "UTF-8", 4096, 0x0000);
    90   stream.writeString("invalid json!");
    91   stream.close();
    93   let trace, debug;
    94   let obj = {
    95     _log: {
    96       trace: function(msg) {
    97         trace = msg;
    98       },
    99       debug: function(msg) {
   100         debug = msg;
   101       }
   102     }
   103   };
   104   Utils.jsonLoad("log", obj, ensureThrows(function(val) {
   105     do_check_true(!val);
   106     do_check_true(!!trace);
   107     do_check_true(!!debug);
   108     run_next_test();
   109   }));
   110 });
   112 add_task(function* test_undefined_callback() {
   113   yield Utils.jsonSave("foo", {}, ["v1", "v2"]);
   114 });

mercurial