Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  test_utils_json.js

  Sprache: JAVA
 

/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */


const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

add_task(async function test_roundtrip() {
  _("Do a simple write of an array to json and read");
  await Utils.jsonSave("foo", {}, ["v1""v2"]);

  let foo = await Utils.jsonLoad("foo", {});
  Assert.equal(typeof foo, "object");
  Assert.equal(foo.length, 2);
  Assert.equal(foo[0], "v1");
  Assert.equal(foo[1], "v2");
});

add_task(async function test_string() {
  _("Try saving simple strings");
  await Utils.jsonSave("str", {}, "hi");

  let str = await Utils.jsonLoad("str", {});
  Assert.equal(typeof str, "string");
  Assert.equal(str.length, 2);
  Assert.equal(str[0], "h");
  Assert.equal(str[1], "i");
});

add_task(async function test_number() {
  _("Try saving a number");
  await Utils.jsonSave("num", {}, 42);

  let num = await Utils.jsonLoad("num", {});
  Assert.equal(typeof num, "number");
  Assert.equal(num, 42);
});

add_task(async function test_nonexistent_file() {
  _("Try loading a non-existent file.");
  let val = await Utils.jsonLoad("non-existent", {});
  Assert.equal(val, undefined);
});

add_task(async function test_save_logging() {
  _("Verify that writes are logged.");
  let trace;
  await Utils.jsonSave(
    "log",
    {
      _log: {
        trace(msg) {
          trace = msg;
        },
      },
    },
    "hi"
  );
  Assert.ok(!!trace);
});

add_task(async function test_load_logging() {
  _("Verify that reads and read errors are logged.");

  // Write a file with some invalid JSON
  let file = await IOUtils.getFile(PathUtils.profileDir, "weave""log.json");
  let fos = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
    Ci.nsIFileOutputStream
  );
  let flags =
    FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
  fos.init(file, flags, FileUtils.PERMS_FILE, fos.DEFER_OPEN);
  let stream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(
    Ci.nsIConverterOutputStream
  );
  stream.init(fos, "UTF-8");
  stream.writeString("invalid json!");
  stream.close();

  let trace, debug;
  let obj = {
    _log: {
      trace(msg) {
        trace = msg;
      },
      debug(msg) {
        debug = msg;
      },
    },
  };
  let val = await Utils.jsonLoad("log", obj);
  Assert.ok(!val);
  Assert.ok(!!trace);
  Assert.ok(!!debug);
});

Messung V0.5 in Prozent
C=91 H=94 G=92

¤ Dauer der Verarbeitung: 0.2 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Open Source Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Statistik
#Sources=141584
#Domains=752002