function parserForFile(filename) {
let parser = null; try {
let file = do_get_file(filename); Assert.ok(!!file);
parser = factory.createINIParser(file); Assert.ok(!!parser);
} catch (e) {
dump("INFO | caught error: " + e); // checkParserOutput will handle a null parser when it's expected.
} return parser;
}
function checkParserOutput(parser, expected) { // If the expected output is null, we expect the parser to have // failed (and vice-versa). if (!parser || !expected) { Assert.equal(parser, null); Assert.equal(expected, null); return;
}
let output = getParserOutput(parser); for (let section in expected) { Assert.ok(section in output); for (let key in expected[section]) { Assert.ok(key in output[section]); Assert.equal(output[section][key], expected[section][key]); delete output[section][key];
} for (let key in output[section]) { Assert.equal(key, "wasn't expecting this key!");
} delete output[section];
} for (let section in output) { Assert.equal(section, "wasn't expecting this section!");
}
}
function getParserOutput(parser) {
let output = {};
for (let section of parser.getSections()) { Assert.equal(false, section in output); // catch dupes
output[section] = {};
for (let key of parser.getKeys(section)) { Assert.equal(false, key in output[section]); // catch dupes
let value = parser.getString(section, key);
output[section][key] = value;
}
} return output;
}
// Test reading from a variety of files and strings. While we're at it, // write out each one and read it back to ensure that nothing changed. while (testnum < testdata.length) {
dump("\nINFO | test #" + ++testnum);
let filename = testdata[testnum - 1].filename;
dump(", filename " + filename + "\n");
let parser = parserForFile(filename);
checkParserOutput(parser, testdata[testnum - 1].reference); if (!parser) { continue;
} Assert.ok(parser instanceof Ci.nsIINIParserWriter); // write contents out to a new file
let newfilename = filename + ".new";
let newfile = do_get_file(filename);
newfile.leafName += ".new";
parser.writeFile(newfile); // read new file and make sure the contents are the same.
parser = parserForFile(newfilename);
checkParserOutput(parser, testdata[testnum - 1].reference); // cleanup after the test
newfile.remove(false);
// ensure that `writeString` works correctly Assert.ok(parser instanceof Ci.nsIINIParserWriter);
let formatted = parser.writeToString();
parser = factory.createINIParser(null); // re-parsing the formatted string is the easiest // way to verify correctness...
parser.initFromString(formatted);
checkParserOutput(parser, testdata[testnum - 1].reference);
}
dump("INFO | test #" + ++testnum + "\n");
// test writing to a new file. var newfile = do_get_file("data/");
newfile.append("nonexistent-file.ini"); if (newfile.exists()) {
newfile.remove(false);
} Assert.ok(!newfile.exists());
try { var parser = factory.createINIParser(newfile); Assert.ok(false, "Should have thrown an exception");
} catch (e) { Assert.equal(
e.result,
Cr.NS_ERROR_FILE_NOT_FOUND, "Caught a file not found exception"
);
}
parser = factory.createINIParser(); Assert.ok(!!parser); Assert.ok(parser instanceof Ci.nsIINIParserWriter);
checkParserOutput(parser, {});
parser.writeFile(newfile); Assert.ok(newfile.exists());
// test adding a new section and new key
parser.setString("section", "key", "value");
parser.setString("section", "key2", "");
parser.writeFile(newfile); Assert.ok(newfile.exists());
checkParserOutput(parser, { section: { key: "value", key2: "" } }); // read it in again, check for same data.
parser = parserForFile("data/nonexistent-file.ini");
checkParserOutput(parser, { section: { key: "value", key2: "" } }); // cleanup after the test
newfile.remove(false);
dump("INFO | test #" + ++testnum + "\n");
// test modifying a existing key's value (in an existing section)
parser = parserForFile("data/iniparser09.ini");
checkParserOutput(parser, { section1: { name1: "value1" } });
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 ist noch experimentell.