asserts.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. "use strict";
  5. const AssertBase = require("sdk/test/assert").Assert;
  6. /**
  7. * Generates custom assertion constructors that may be bundled with a test
  8. * suite.
  9. * @params {String}
  10. * names of assertion function to be added to the generated Assert.
  11. */
  12. function Assert() {
  13. let assertDescriptor = {};
  14. Array.forEach(arguments, function(name) {
  15. assertDescriptor[name] = { value: function(message) {
  16. this.pass(message);
  17. }}
  18. });
  19. return function Assert() {
  20. return Object.create(AssertBase.apply(null, arguments), assertDescriptor);
  21. };
  22. }
  23. exports["test suite"] = {
  24. Assert: Assert("foo"),
  25. "test that custom assertor is passed to test function": function(assert) {
  26. assert.ok("foo" in assert, "custom assertion function `foo` is defined");
  27. assert.foo("custom assertion function `foo` is called");
  28. },
  29. "test sub suite": {
  30. "test that `Assert` is inherited by sub suits": function(assert) {
  31. assert.ok("foo" in assert, "assertion function `foo` is not defined");
  32. },
  33. "test sub sub suite": {
  34. Assert: Assert("bar"),
  35. "test that custom assertor is passed to test function": function(assert) {
  36. assert.ok("bar" in assert,
  37. "custom assertion function `bar` is defined");
  38. assert.bar("custom assertion function `bar` is called");
  39. },
  40. "test that `Assert` is not inherited by sub sub suits": function(assert) {
  41. assert.ok(!("foo" in assert),
  42. "assertion function `foo` is not defined");
  43. }
  44. }
  45. }
  46. };
  47. if (module == require.main)
  48. require("test").run(exports);