test_assert.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {utils: Cu} = Components;
  6. Cu.import("chrome://marionette/content/assert.js");
  7. Cu.import("chrome://marionette/content/error.js");
  8. add_test(function test_session() {
  9. assert.session({sessionId: "foo"});
  10. for (let typ of [null, undefined, ""]) {
  11. Assert.throws(() => assert.session({sessionId: typ}), InvalidSessionIDError);
  12. }
  13. run_next_test();
  14. });
  15. add_test(function test_platforms() {
  16. // at least one will fail
  17. let raised;
  18. for (let fn of [assert.firefox, assert.fennec, assert.b2g, assert.mobile]) {
  19. try {
  20. fn();
  21. } catch (e) {
  22. raised = e;
  23. }
  24. }
  25. ok(raised instanceof UnsupportedOperationError);
  26. run_next_test();
  27. });
  28. add_test(function test_defined() {
  29. assert.defined({});
  30. Assert.throws(() => assert.defined(undefined), InvalidArgumentError);
  31. run_next_test();
  32. });
  33. add_test(function test_number() {
  34. assert.number(1);
  35. assert.number(0);
  36. assert.number(-1);
  37. assert.number(1.2);
  38. for (let i of ["foo", "1", {}, [], NaN, Infinity, undefined]) {
  39. Assert.throws(() => assert.number(i), InvalidArgumentError);
  40. }
  41. run_next_test();
  42. });
  43. add_test(function test_integer() {
  44. assert.integer(1);
  45. assert.integer(0);
  46. assert.integer(-1);
  47. Assert.throws(() => assert.integer("foo"), InvalidArgumentError);
  48. Assert.throws(() => assert.integer(1.2), InvalidArgumentError);
  49. run_next_test();
  50. });
  51. add_test(function test_positiveInteger() {
  52. assert.positiveInteger(1);
  53. assert.positiveInteger(0);
  54. Assert.throws(() => assert.positiveInteger(-1), InvalidArgumentError);
  55. Assert.throws(() => assert.positiveInteger("foo"), InvalidArgumentError);
  56. run_next_test();
  57. });
  58. add_test(function test_boolean() {
  59. assert.boolean(true);
  60. assert.boolean(false);
  61. Assert.throws(() => assert.boolean("false"), InvalidArgumentError);
  62. Assert.throws(() => assert.boolean(undefined), InvalidArgumentError);
  63. run_next_test();
  64. });
  65. add_test(function test_string() {
  66. assert.string("foo");
  67. assert.string(`bar`);
  68. Assert.throws(() => assert.string(42), InvalidArgumentError);
  69. run_next_test();
  70. });
  71. add_test(function test_object() {
  72. assert.object({});
  73. assert.object(new Object());
  74. for (let typ of [42, "foo", true, null, undefined]) {
  75. Assert.throws(() => assert.object(typ), InvalidArgumentError);
  76. }
  77. run_next_test();
  78. });
  79. add_test(function test_in() {
  80. assert.in("foo", {foo: 42});
  81. for (let typ of [{}, 42, true, null, undefined]) {
  82. Assert.throws(() => assert.in("foo", typ), InvalidArgumentError);
  83. }
  84. run_next_test();
  85. });
  86. add_test(function test_array() {
  87. assert.array([]);
  88. assert.array(new Array());
  89. Assert.throws(() => assert.array(42), InvalidArgumentError);
  90. Assert.throws(() => assert.array({}), InvalidArgumentError);
  91. run_next_test();
  92. });
  93. add_test(function test_that() {
  94. equal(1, assert.that(n => n + 1)(1));
  95. Assert.throws(() => assert.that(() => false)());
  96. Assert.throws(() => assert.that(val => val)(false));
  97. Assert.throws(() => assert.that(val => val, "foo", SessionNotCreatedError)(false),
  98. SessionNotCreatedError);
  99. run_next_test();
  100. });