summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-api-utils.js
blob: 12f2bf44f33d0a5b83228982c2507a507ba68176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* 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: v => v * v },
    bar: { map: 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: v => v },
    bar: { ok: v => v }
  });
  assert.deepEqual(val, { foo: 3, bar: 2 });

  assert.throws(
    () => apiUtils.validateOptions({ foo: 2, bar: 2 }, {
      bar: { ok: v => v > 2 }
    }),
    /^The option "bar" is invalid/,
    "ok should raise exception on invalid option"
  );

  assert.throws(
    () => apiUtils.validateOptions(null, { foo: { ok: 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(
    () => 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: v => v.toString(),
      is: ["string"],
      ok: v => v.length > 0
    }
  });
  assert.deepEqual(val, { foo: "1337" });

  let requirements = {
    foo: {
      is: ["object"],
      ok: () => assert.fail("is should have caused us to throw by now")
    }
  };
  assert.throws(
    () => 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(
    () => apiUtils.validateOptions(null, {
      foo: { ok: v => v, msg: "foo!" }
    }),
    /^foo!/,
    "ok should raise exception with customized message"
  );
};

exports.testValidateMapWithMissingKey = function (assert) {
  let val = apiUtils.validateOptions({ }, {
    foo: {
      map: v => v || "bar"
    }
  });
  assert.deepEqual(val, { foo: "bar" });

  val = apiUtils.validateOptions({ }, {
    foo: {
      map: v => { throw "bar" }
    }
  });
  assert.deepEqual(val, { });
};

exports.testValidateMapWithMissingKeyAndThrown = function (assert) {
  let val = apiUtils.validateOptions({}, {
    bar: {
      map: function(v) { throw "bar" }
    },
    baz: {
      map: 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 (let keyVal of 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("sdk/test").run(exports);