123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- const apiUtils = require("sdk/deprecated/api-utils");
- exports.testValidateOptionsEmpty = function (assert) {
- let val = apiUtils.validateOptions(null, {});
- assert.deepEqual(val, {});
- val = apiUtils.validateOptions(null, { foo: {} });
- assert.deepEqual(val, {});
- val = apiUtils.validateOptions({}, {});
- assert.deepEqual(val, {});
- val = apiUtils.validateOptions({}, { foo: {} });
- assert.deepEqual(val, {});
- };
- exports.testValidateOptionsNonempty = function (assert) {
- let val = apiUtils.validateOptions({ foo: 123 }, {});
- assert.deepEqual(val, {});
- val = apiUtils.validateOptions({ foo: 123, bar: 456 },
- { foo: {}, bar: {}, baz: {} });
- assert.deepEqual(val, { foo: 123, bar: 456 });
- };
- exports.testValidateOptionsMap = function (assert) {
- let val = apiUtils.validateOptions({ foo: 3, bar: 2 }, {
- foo: { map: function (v) v * v },
- bar: { map: function (v) undefined }
- });
- assert.deepEqual(val, { foo: 9, bar: undefined });
- };
- exports.testValidateOptionsMapException = function (assert) {
- let val = apiUtils.validateOptions({ foo: 3 }, {
- foo: { map: function () { throw new Error(); }}
- });
- assert.deepEqual(val, { foo: 3 });
- };
- exports.testValidateOptionsOk = function (assert) {
- let val = apiUtils.validateOptions({ foo: 3, bar: 2, baz: 1 }, {
- foo: { ok: function (v) v },
- bar: { ok: function (v) v }
- });
- assert.deepEqual(val, { foo: 3, bar: 2 });
- assert.throws(
- function () apiUtils.validateOptions({ foo: 2, bar: 2 }, {
- bar: { ok: function (v) v > 2 }
- }),
- /^The option "bar" is invalid/,
- "ok should raise exception on invalid option"
- );
- assert.throws(
- function () apiUtils.validateOptions(null, { foo: { ok: function (v) v }}),
- /^The option "foo" is invalid/,
- "ok should raise exception on invalid option"
- );
- };
- exports.testValidateOptionsIs = function (assert) {
- let opts = {
- array: [],
- boolean: true,
- func: function () {},
- nul: null,
- number: 1337,
- object: {},
- string: "foo",
- undef1: undefined
- };
- let requirements = {
- array: { is: ["array"] },
- boolean: { is: ["boolean"] },
- func: { is: ["function"] },
- nul: { is: ["null"] },
- number: { is: ["number"] },
- object: { is: ["object"] },
- string: { is: ["string"] },
- undef1: { is: ["undefined"] },
- undef2: { is: ["undefined"] }
- };
- let val = apiUtils.validateOptions(opts, requirements);
- assert.deepEqual(val, opts);
- assert.throws(
- function () apiUtils.validateOptions(null, {
- foo: { is: ["object", "number"] }
- }),
- /^The option "foo" must be one of the following types: object, number/,
- "Invalid type should raise exception"
- );
- };
- exports.testValidateOptionsIsWithExportedValue = function (assert) {
- let { string, number, boolean, object } = apiUtils;
- let opts = {
- boolean: true,
- number: 1337,
- object: {},
- string: "foo"
- };
- let requirements = {
- string: { is: string },
- number: { is: number },
- boolean: { is: boolean },
- object: { is: object }
- };
- let val = apiUtils.validateOptions(opts, requirements);
- assert.deepEqual(val, opts);
- // Test the types are optional by default
- val = apiUtils.validateOptions({foo: 'bar'}, requirements);
- assert.deepEqual(val, {});
- };
- exports.testValidateOptionsIsWithEither = function (assert) {
- let { string, number, boolean, either } = apiUtils;
- let text = { is: either(string, number) };
- let requirements = {
- text: text,
- boolOrText: { is: either(text, boolean) }
- };
- let val = apiUtils.validateOptions({text: 12}, requirements);
- assert.deepEqual(val, {text: 12});
- val = apiUtils.validateOptions({text: "12"}, requirements);
- assert.deepEqual(val, {text: "12"});
- val = apiUtils.validateOptions({boolOrText: true}, requirements);
- assert.deepEqual(val, {boolOrText: true});
- val = apiUtils.validateOptions({boolOrText: "true"}, requirements);
- assert.deepEqual(val, {boolOrText: "true"});
- val = apiUtils.validateOptions({boolOrText: 1}, requirements);
- assert.deepEqual(val, {boolOrText: 1});
- assert.throws(
- () => apiUtils.validateOptions({text: true}, requirements),
- /^The option "text" must be one of the following types/,
- "Invalid type should raise exception"
- );
- assert.throws(
- () => apiUtils.validateOptions({boolOrText: []}, requirements),
- /^The option "boolOrText" must be one of the following types/,
- "Invalid type should raise exception"
- );
- };
- exports.testValidateOptionsWithRequiredAndOptional = function (assert) {
- let { string, number, required, optional } = apiUtils;
- let opts = {
- number: 1337,
- string: "foo"
- };
- let requirements = {
- string: required(string),
- number: number
- };
- let val = apiUtils.validateOptions(opts, requirements);
- assert.deepEqual(val, opts);
- val = apiUtils.validateOptions({string: "foo"}, requirements);
- assert.deepEqual(val, {string: "foo"});
- assert.throws(
- () => apiUtils.validateOptions({number: 10}, requirements),
- /^The option "string" must be one of the following types/,
- "Invalid type should raise exception"
- );
- // Makes string optional
- requirements.string = optional(requirements.string);
- val = apiUtils.validateOptions({number: 10}, requirements),
- assert.deepEqual(val, {number: 10});
- };
- exports.testValidateOptionsWithExportedValue = function (assert) {
- let { string, number, boolean, object } = apiUtils;
- let opts = {
- boolean: true,
- number: 1337,
- object: {},
- string: "foo"
- };
- let requirements = {
- string: string,
- number: number,
- boolean: boolean,
- object: object
- };
- let val = apiUtils.validateOptions(opts, requirements);
- assert.deepEqual(val, opts);
- // Test the types are optional by default
- val = apiUtils.validateOptions({foo: 'bar'}, requirements);
- assert.deepEqual(val, {});
- };
- exports.testValidateOptionsMapIsOk = function (assert) {
- let [map, is, ok] = [false, false, false];
- let val = apiUtils.validateOptions({ foo: 1337 }, {
- foo: {
- map: function (v) v.toString(),
- is: ["string"],
- ok: function (v) v.length > 0
- }
- });
- assert.deepEqual(val, { foo: "1337" });
- let requirements = {
- foo: {
- is: ["object"],
- ok: function () assert.fail("is should have caused us to throw by now")
- }
- };
- assert.throws(
- function () apiUtils.validateOptions(null, requirements),
- /^The option "foo" must be one of the following types: object/,
- "is should be used before ok is called"
- );
- };
- exports.testValidateOptionsErrorMsg = function (assert) {
- assert.throws(
- function () apiUtils.validateOptions(null, {
- foo: { ok: function (v) v, msg: "foo!" }
- }),
- /^foo!/,
- "ok should raise exception with customized message"
- );
- };
- exports.testValidateMapWithMissingKey = function (assert) {
- let val = apiUtils.validateOptions({ }, {
- foo: {
- map: function (v) v || "bar"
- }
- });
- assert.deepEqual(val, { foo: "bar" });
- val = apiUtils.validateOptions({ }, {
- foo: {
- map: function (v) { throw "bar" }
- }
- });
- assert.deepEqual(val, { });
- };
- exports.testValidateMapWithMissingKeyAndThrown = function (assert) {
- let val = apiUtils.validateOptions({}, {
- bar: {
- map: function(v) { throw "bar" }
- },
- baz: {
- map: function(v) "foo"
- }
- });
- assert.deepEqual(val, { baz: "foo" });
- };
- exports.testAddIterator = function testAddIterator (assert) {
- let obj = {};
- let keys = ["foo", "bar", "baz"];
- let vals = [1, 2, 3];
- let keysVals = [["foo", 1], ["bar", 2], ["baz", 3]];
- apiUtils.addIterator(
- obj,
- function keysValsGen() {
- for each (let keyVal in keysVals)
- yield keyVal;
- }
- );
- let keysItr = [];
- for (let key in obj)
- keysItr.push(key);
- assert.equal(keysItr.length, keys.length,
- "the keys iterator returns the correct number of items");
- for (let i = 0; i < keys.length; i++)
- assert.equal(keysItr[i], keys[i], "the key is correct");
- let valsItr = [];
- for each (let val in obj)
- valsItr.push(val);
- assert.equal(valsItr.length, vals.length,
- "the vals iterator returns the correct number of items");
- for (let i = 0; i < vals.length; i++)
- assert.equal(valsItr[i], vals[i], "the val is correct");
- };
- require('test').run(exports);
|