test-api-utils.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const apiUtils = require("sdk/deprecated/api-utils");
  5. exports.testValidateOptionsEmpty = function (assert) {
  6. let val = apiUtils.validateOptions(null, {});
  7. assert.deepEqual(val, {});
  8. val = apiUtils.validateOptions(null, { foo: {} });
  9. assert.deepEqual(val, {});
  10. val = apiUtils.validateOptions({}, {});
  11. assert.deepEqual(val, {});
  12. val = apiUtils.validateOptions({}, { foo: {} });
  13. assert.deepEqual(val, {});
  14. };
  15. exports.testValidateOptionsNonempty = function (assert) {
  16. let val = apiUtils.validateOptions({ foo: 123 }, {});
  17. assert.deepEqual(val, {});
  18. val = apiUtils.validateOptions({ foo: 123, bar: 456 },
  19. { foo: {}, bar: {}, baz: {} });
  20. assert.deepEqual(val, { foo: 123, bar: 456 });
  21. };
  22. exports.testValidateOptionsMap = function (assert) {
  23. let val = apiUtils.validateOptions({ foo: 3, bar: 2 }, {
  24. foo: { map: function (v) v * v },
  25. bar: { map: function (v) undefined }
  26. });
  27. assert.deepEqual(val, { foo: 9, bar: undefined });
  28. };
  29. exports.testValidateOptionsMapException = function (assert) {
  30. let val = apiUtils.validateOptions({ foo: 3 }, {
  31. foo: { map: function () { throw new Error(); }}
  32. });
  33. assert.deepEqual(val, { foo: 3 });
  34. };
  35. exports.testValidateOptionsOk = function (assert) {
  36. let val = apiUtils.validateOptions({ foo: 3, bar: 2, baz: 1 }, {
  37. foo: { ok: function (v) v },
  38. bar: { ok: function (v) v }
  39. });
  40. assert.deepEqual(val, { foo: 3, bar: 2 });
  41. assert.throws(
  42. function () apiUtils.validateOptions({ foo: 2, bar: 2 }, {
  43. bar: { ok: function (v) v > 2 }
  44. }),
  45. /^The option "bar" is invalid/,
  46. "ok should raise exception on invalid option"
  47. );
  48. assert.throws(
  49. function () apiUtils.validateOptions(null, { foo: { ok: function (v) v }}),
  50. /^The option "foo" is invalid/,
  51. "ok should raise exception on invalid option"
  52. );
  53. };
  54. exports.testValidateOptionsIs = function (assert) {
  55. let opts = {
  56. array: [],
  57. boolean: true,
  58. func: function () {},
  59. nul: null,
  60. number: 1337,
  61. object: {},
  62. string: "foo",
  63. undef1: undefined
  64. };
  65. let requirements = {
  66. array: { is: ["array"] },
  67. boolean: { is: ["boolean"] },
  68. func: { is: ["function"] },
  69. nul: { is: ["null"] },
  70. number: { is: ["number"] },
  71. object: { is: ["object"] },
  72. string: { is: ["string"] },
  73. undef1: { is: ["undefined"] },
  74. undef2: { is: ["undefined"] }
  75. };
  76. let val = apiUtils.validateOptions(opts, requirements);
  77. assert.deepEqual(val, opts);
  78. assert.throws(
  79. function () apiUtils.validateOptions(null, {
  80. foo: { is: ["object", "number"] }
  81. }),
  82. /^The option "foo" must be one of the following types: object, number/,
  83. "Invalid type should raise exception"
  84. );
  85. };
  86. exports.testValidateOptionsIsWithExportedValue = function (assert) {
  87. let { string, number, boolean, object } = apiUtils;
  88. let opts = {
  89. boolean: true,
  90. number: 1337,
  91. object: {},
  92. string: "foo"
  93. };
  94. let requirements = {
  95. string: { is: string },
  96. number: { is: number },
  97. boolean: { is: boolean },
  98. object: { is: object }
  99. };
  100. let val = apiUtils.validateOptions(opts, requirements);
  101. assert.deepEqual(val, opts);
  102. // Test the types are optional by default
  103. val = apiUtils.validateOptions({foo: 'bar'}, requirements);
  104. assert.deepEqual(val, {});
  105. };
  106. exports.testValidateOptionsIsWithEither = function (assert) {
  107. let { string, number, boolean, either } = apiUtils;
  108. let text = { is: either(string, number) };
  109. let requirements = {
  110. text: text,
  111. boolOrText: { is: either(text, boolean) }
  112. };
  113. let val = apiUtils.validateOptions({text: 12}, requirements);
  114. assert.deepEqual(val, {text: 12});
  115. val = apiUtils.validateOptions({text: "12"}, requirements);
  116. assert.deepEqual(val, {text: "12"});
  117. val = apiUtils.validateOptions({boolOrText: true}, requirements);
  118. assert.deepEqual(val, {boolOrText: true});
  119. val = apiUtils.validateOptions({boolOrText: "true"}, requirements);
  120. assert.deepEqual(val, {boolOrText: "true"});
  121. val = apiUtils.validateOptions({boolOrText: 1}, requirements);
  122. assert.deepEqual(val, {boolOrText: 1});
  123. assert.throws(
  124. () => apiUtils.validateOptions({text: true}, requirements),
  125. /^The option "text" must be one of the following types/,
  126. "Invalid type should raise exception"
  127. );
  128. assert.throws(
  129. () => apiUtils.validateOptions({boolOrText: []}, requirements),
  130. /^The option "boolOrText" must be one of the following types/,
  131. "Invalid type should raise exception"
  132. );
  133. };
  134. exports.testValidateOptionsWithRequiredAndOptional = function (assert) {
  135. let { string, number, required, optional } = apiUtils;
  136. let opts = {
  137. number: 1337,
  138. string: "foo"
  139. };
  140. let requirements = {
  141. string: required(string),
  142. number: number
  143. };
  144. let val = apiUtils.validateOptions(opts, requirements);
  145. assert.deepEqual(val, opts);
  146. val = apiUtils.validateOptions({string: "foo"}, requirements);
  147. assert.deepEqual(val, {string: "foo"});
  148. assert.throws(
  149. () => apiUtils.validateOptions({number: 10}, requirements),
  150. /^The option "string" must be one of the following types/,
  151. "Invalid type should raise exception"
  152. );
  153. // Makes string optional
  154. requirements.string = optional(requirements.string);
  155. val = apiUtils.validateOptions({number: 10}, requirements),
  156. assert.deepEqual(val, {number: 10});
  157. };
  158. exports.testValidateOptionsWithExportedValue = function (assert) {
  159. let { string, number, boolean, object } = apiUtils;
  160. let opts = {
  161. boolean: true,
  162. number: 1337,
  163. object: {},
  164. string: "foo"
  165. };
  166. let requirements = {
  167. string: string,
  168. number: number,
  169. boolean: boolean,
  170. object: object
  171. };
  172. let val = apiUtils.validateOptions(opts, requirements);
  173. assert.deepEqual(val, opts);
  174. // Test the types are optional by default
  175. val = apiUtils.validateOptions({foo: 'bar'}, requirements);
  176. assert.deepEqual(val, {});
  177. };
  178. exports.testValidateOptionsMapIsOk = function (assert) {
  179. let [map, is, ok] = [false, false, false];
  180. let val = apiUtils.validateOptions({ foo: 1337 }, {
  181. foo: {
  182. map: function (v) v.toString(),
  183. is: ["string"],
  184. ok: function (v) v.length > 0
  185. }
  186. });
  187. assert.deepEqual(val, { foo: "1337" });
  188. let requirements = {
  189. foo: {
  190. is: ["object"],
  191. ok: function () assert.fail("is should have caused us to throw by now")
  192. }
  193. };
  194. assert.throws(
  195. function () apiUtils.validateOptions(null, requirements),
  196. /^The option "foo" must be one of the following types: object/,
  197. "is should be used before ok is called"
  198. );
  199. };
  200. exports.testValidateOptionsErrorMsg = function (assert) {
  201. assert.throws(
  202. function () apiUtils.validateOptions(null, {
  203. foo: { ok: function (v) v, msg: "foo!" }
  204. }),
  205. /^foo!/,
  206. "ok should raise exception with customized message"
  207. );
  208. };
  209. exports.testValidateMapWithMissingKey = function (assert) {
  210. let val = apiUtils.validateOptions({ }, {
  211. foo: {
  212. map: function (v) v || "bar"
  213. }
  214. });
  215. assert.deepEqual(val, { foo: "bar" });
  216. val = apiUtils.validateOptions({ }, {
  217. foo: {
  218. map: function (v) { throw "bar" }
  219. }
  220. });
  221. assert.deepEqual(val, { });
  222. };
  223. exports.testValidateMapWithMissingKeyAndThrown = function (assert) {
  224. let val = apiUtils.validateOptions({}, {
  225. bar: {
  226. map: function(v) { throw "bar" }
  227. },
  228. baz: {
  229. map: function(v) "foo"
  230. }
  231. });
  232. assert.deepEqual(val, { baz: "foo" });
  233. };
  234. exports.testAddIterator = function testAddIterator (assert) {
  235. let obj = {};
  236. let keys = ["foo", "bar", "baz"];
  237. let vals = [1, 2, 3];
  238. let keysVals = [["foo", 1], ["bar", 2], ["baz", 3]];
  239. apiUtils.addIterator(
  240. obj,
  241. function keysValsGen() {
  242. for each (let keyVal in keysVals)
  243. yield keyVal;
  244. }
  245. );
  246. let keysItr = [];
  247. for (let key in obj)
  248. keysItr.push(key);
  249. assert.equal(keysItr.length, keys.length,
  250. "the keys iterator returns the correct number of items");
  251. for (let i = 0; i < keys.length; i++)
  252. assert.equal(keysItr[i], keys[i], "the key is correct");
  253. let valsItr = [];
  254. for each (let val in obj)
  255. valsItr.push(val);
  256. assert.equal(valsItr.length, vals.length,
  257. "the vals iterator returns the correct number of items");
  258. for (let i = 0; i < vals.length; i++)
  259. assert.equal(valsItr[i], vals[i], "the val is correct");
  260. };
  261. require('test').run(exports);