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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     5 "use strict"
     7 var utils = require("sdk/lang/type");
     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 };
    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 };
    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 };
    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 };
    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 };
    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 };
    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");
    76   var foo = {};
    77   var bar = { foo: foo };
    78   foo.bar = bar;
    79   assert.ok(!utils.isJSON(foo), "recursive objects are not json");
    82   assert.ok(!utils.isJSON({ get foo() { return 5 } }),
    83             "json can not have getter");
    85   assert.ok(!utils.isJSON({ foo: "bar", baz: function () {} }),
    86             "json can not contain functions");
    88   assert.ok(!utils.isJSON(Object.create({})),
    89             "json must be direct descendant of `Object.prototype`");
    90 };
    92 require("test").run(exports);

mercurial