addon-sdk/source/test/commonjs-test-adapter/asserts.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/commonjs-test-adapter/asserts.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     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 +const AssertBase = require("sdk/test/assert").Assert;
    1.11 +
    1.12 +/**
    1.13 + * Generates custom assertion constructors that may be bundled with a test
    1.14 + * suite.
    1.15 + * @params {String}
    1.16 + *    names of assertion function to be added to the generated Assert.
    1.17 + */
    1.18 +function Assert() {
    1.19 +  let assertDescriptor = {};
    1.20 +  Array.forEach(arguments, function(name) {
    1.21 +    assertDescriptor[name] = { value: function(message) {
    1.22 +      this.pass(message);
    1.23 +    }}
    1.24 +  });
    1.25 +
    1.26 +  return function Assert() {
    1.27 +    return Object.create(AssertBase.apply(null, arguments), assertDescriptor);
    1.28 +  };
    1.29 +}
    1.30 +
    1.31 +exports["test suite"] = {
    1.32 +  Assert: Assert("foo"),
    1.33 +  "test that custom assertor is passed to test function": function(assert) {
    1.34 +    assert.ok("foo" in assert, "custom assertion function `foo` is defined");
    1.35 +    assert.foo("custom assertion function `foo` is called");
    1.36 +  },
    1.37 +  "test sub suite": {
    1.38 +    "test that `Assert` is inherited by sub suits": function(assert) {
    1.39 +      assert.ok("foo" in assert, "assertion function `foo` is not defined");
    1.40 +    },
    1.41 +    "test sub sub suite": {
    1.42 +      Assert: Assert("bar"),
    1.43 +      "test that custom assertor is passed to test function": function(assert) {
    1.44 +        assert.ok("bar" in assert,
    1.45 +                  "custom assertion function `bar` is defined");
    1.46 +        assert.bar("custom assertion function `bar` is called");
    1.47 +      },
    1.48 +      "test that `Assert` is not inherited by sub sub suits": function(assert) {
    1.49 +        assert.ok(!("foo" in assert),
    1.50 +                  "assertion function `foo` is not defined");
    1.51 +      }
    1.52 +    }
    1.53 +  }
    1.54 +};
    1.55 +
    1.56 +if (module == require.main)
    1.57 +  require("test").run(exports);

mercurial