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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial