1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-type.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,92 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict" 1.9 + 1.10 +var utils = require("sdk/lang/type"); 1.11 + 1.12 +exports["test function"] = function (assert) { 1.13 + assert.ok(utils.isFunction(function(){}), "value is function"); 1.14 + assert.ok(utils.isFunction(Object), "Object is function"); 1.15 + assert.ok(utils.isFunction(new Function("")), "Genertaed value is function"); 1.16 + assert.ok(!utils.isFunction({}), "object is not a function"); 1.17 + assert.ok(!utils.isFunction(4), "number is not a function"); 1.18 +}; 1.19 + 1.20 +exports["test atoms"] = function (assert) { 1.21 + assert.ok(utils.isPrimitive(2), "number is primitive"); 1.22 + assert.ok(utils.isPrimitive(NaN), "`NaN` is primitve"); 1.23 + assert.ok(utils.isPrimitive(undefined), "`undefined` is primitive"); 1.24 + assert.ok(utils.isPrimitive(null), "`null` is primitive"); 1.25 + assert.ok(utils.isPrimitive(Infinity), "`Infinity` is primitive"); 1.26 + assert.ok(utils.isPrimitive("foo"), "strings are primitive"); 1.27 + assert.ok(utils.isPrimitive(true) && utils.isPrimitive(false), 1.28 + "booleans are primitive"); 1.29 +}; 1.30 + 1.31 +exports["test object"] = function (assert) { 1.32 + assert.ok(utils.isObject({}), "`{}` is object"); 1.33 + assert.ok(!utils.isObject(null), "`null` is not an object"); 1.34 + assert.ok(!utils.isObject(Object), "functions is not an object"); 1.35 +}; 1.36 + 1.37 +exports["test flat objects"] = function (assert) { 1.38 + assert.ok(utils.isFlat({}), "`{}` is a flat object"); 1.39 + assert.ok(!utils.isFlat([]), "`[]` is not a flat object"); 1.40 + assert.ok(!utils.isFlat(new function() {}), "derived objects are not flat"); 1.41 + assert.ok(utils.isFlat(Object.prototype), "Object.prototype is flat"); 1.42 +}; 1.43 + 1.44 +exports["test json atoms"] = function (assert) { 1.45 + assert.ok(utils.isJSON(null), "`null` is JSON"); 1.46 + assert.ok(utils.isJSON(undefined), "`undefined` is JSON"); 1.47 + assert.ok(utils.isJSON(NaN), "`NaN` is JSON"); 1.48 + assert.ok(utils.isJSON(Infinity), "`Infinity` is JSON"); 1.49 + assert.ok(utils.isJSON(true) && utils.isJSON(false), "booleans are JSON"); 1.50 + assert.ok(utils.isJSON(4), utils.isJSON(0), "numbers are JSON"); 1.51 + assert.ok(utils.isJSON("foo bar"), "strings are JSON"); 1.52 +}; 1.53 + 1.54 +exports["test instanceOf"] = function (assert) { 1.55 + assert.ok(utils.instanceOf(assert, Object), 1.56 + "assert is object from other sandbox"); 1.57 + assert.ok(utils.instanceOf(new Date(), Date), "instance of date"); 1.58 + assert.ok(!utils.instanceOf(null, Object), "null is not an instance"); 1.59 +}; 1.60 + 1.61 +exports["test json"] = function (assert) { 1.62 + assert.ok(!utils.isJSON(function(){}), "functions are not json"); 1.63 + assert.ok(utils.isJSON({}), "`{}` is JSON"); 1.64 + assert.ok(utils.isJSON({ 1.65 + a: "foo", 1.66 + b: 3, 1.67 + c: undefined, 1.68 + d: null, 1.69 + e: { 1.70 + f: { 1.71 + g: "bar", 1.72 + p: [{}, "oueou", 56] 1.73 + }, 1.74 + q: { nan: NaN, infinity: Infinity }, 1.75 + "non standard name": "still works" 1.76 + } 1.77 + }), "JSON can contain nested objects"); 1.78 + 1.79 + var foo = {}; 1.80 + var bar = { foo: foo }; 1.81 + foo.bar = bar; 1.82 + assert.ok(!utils.isJSON(foo), "recursive objects are not json"); 1.83 + 1.84 + 1.85 + assert.ok(!utils.isJSON({ get foo() { return 5 } }), 1.86 + "json can not have getter"); 1.87 + 1.88 + assert.ok(!utils.isJSON({ foo: "bar", baz: function () {} }), 1.89 + "json can not contain functions"); 1.90 + 1.91 + assert.ok(!utils.isJSON(Object.create({})), 1.92 + "json must be direct descendant of `Object.prototype`"); 1.93 +}; 1.94 + 1.95 +require("test").run(exports);