addon-sdk/source/test/test-type.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:d7ee41420b01
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
5 "use strict"
6
7 var utils = require("sdk/lang/type");
8
9 exports["test function"] = function (assert) {
10 assert.ok(utils.isFunction(function(){}), "value is function");
11 assert.ok(utils.isFunction(Object), "Object is function");
12 assert.ok(utils.isFunction(new Function("")), "Genertaed value is function");
13 assert.ok(!utils.isFunction({}), "object is not a function");
14 assert.ok(!utils.isFunction(4), "number is not a function");
15 };
16
17 exports["test atoms"] = function (assert) {
18 assert.ok(utils.isPrimitive(2), "number is primitive");
19 assert.ok(utils.isPrimitive(NaN), "`NaN` is primitve");
20 assert.ok(utils.isPrimitive(undefined), "`undefined` is primitive");
21 assert.ok(utils.isPrimitive(null), "`null` is primitive");
22 assert.ok(utils.isPrimitive(Infinity), "`Infinity` is primitive");
23 assert.ok(utils.isPrimitive("foo"), "strings are primitive");
24 assert.ok(utils.isPrimitive(true) && utils.isPrimitive(false),
25 "booleans are primitive");
26 };
27
28 exports["test object"] = function (assert) {
29 assert.ok(utils.isObject({}), "`{}` is object");
30 assert.ok(!utils.isObject(null), "`null` is not an object");
31 assert.ok(!utils.isObject(Object), "functions is not an object");
32 };
33
34 exports["test flat objects"] = function (assert) {
35 assert.ok(utils.isFlat({}), "`{}` is a flat object");
36 assert.ok(!utils.isFlat([]), "`[]` is not a flat object");
37 assert.ok(!utils.isFlat(new function() {}), "derived objects are not flat");
38 assert.ok(utils.isFlat(Object.prototype), "Object.prototype is flat");
39 };
40
41 exports["test json atoms"] = function (assert) {
42 assert.ok(utils.isJSON(null), "`null` is JSON");
43 assert.ok(utils.isJSON(undefined), "`undefined` is JSON");
44 assert.ok(utils.isJSON(NaN), "`NaN` is JSON");
45 assert.ok(utils.isJSON(Infinity), "`Infinity` is JSON");
46 assert.ok(utils.isJSON(true) && utils.isJSON(false), "booleans are JSON");
47 assert.ok(utils.isJSON(4), utils.isJSON(0), "numbers are JSON");
48 assert.ok(utils.isJSON("foo bar"), "strings are JSON");
49 };
50
51 exports["test instanceOf"] = function (assert) {
52 assert.ok(utils.instanceOf(assert, Object),
53 "assert is object from other sandbox");
54 assert.ok(utils.instanceOf(new Date(), Date), "instance of date");
55 assert.ok(!utils.instanceOf(null, Object), "null is not an instance");
56 };
57
58 exports["test json"] = function (assert) {
59 assert.ok(!utils.isJSON(function(){}), "functions are not json");
60 assert.ok(utils.isJSON({}), "`{}` is JSON");
61 assert.ok(utils.isJSON({
62 a: "foo",
63 b: 3,
64 c: undefined,
65 d: null,
66 e: {
67 f: {
68 g: "bar",
69 p: [{}, "oueou", 56]
70 },
71 q: { nan: NaN, infinity: Infinity },
72 "non standard name": "still works"
73 }
74 }), "JSON can contain nested objects");
75
76 var foo = {};
77 var bar = { foo: foo };
78 foo.bar = bar;
79 assert.ok(!utils.isJSON(foo), "recursive objects are not json");
80
81
82 assert.ok(!utils.isJSON({ get foo() { return 5 } }),
83 "json can not have getter");
84
85 assert.ok(!utils.isJSON({ foo: "bar", baz: function () {} }),
86 "json can not contain functions");
87
88 assert.ok(!utils.isJSON(Object.create({})),
89 "json must be direct descendant of `Object.prototype`");
90 };
91
92 require("test").run(exports);

mercurial