summaryrefslogtreecommitdiff
path: root/testing/marionette/test_assert.js
blob: c14f2852e33801052cad51bdbe0535bb68063807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {utils: Cu} = Components;

Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/error.js");

add_test(function test_session() {
  assert.session({sessionId: "foo"});
  for (let typ of [null, undefined, ""]) {
    Assert.throws(() => assert.session({sessionId: typ}), InvalidSessionIDError);
  }

  run_next_test();
});

add_test(function test_platforms() {
  // at least one will fail
  let raised;
  for (let fn of [assert.firefox, assert.fennec, assert.b2g, assert.mobile]) {
    try {
      fn();
    } catch (e) {
      raised = e;
    }
  }
  ok(raised instanceof UnsupportedOperationError);

  run_next_test();
});

add_test(function test_defined() {
  assert.defined({});
  Assert.throws(() => assert.defined(undefined), InvalidArgumentError);

  run_next_test();
});

add_test(function test_number() {
  assert.number(1);
  assert.number(0);
  assert.number(-1);
  assert.number(1.2);
  for (let i of ["foo", "1", {}, [], NaN, Infinity, undefined]) {
    Assert.throws(() => assert.number(i), InvalidArgumentError);
  }
  run_next_test();
});

add_test(function test_integer() {
  assert.integer(1);
  assert.integer(0);
  assert.integer(-1);
  Assert.throws(() => assert.integer("foo"), InvalidArgumentError);
  Assert.throws(() => assert.integer(1.2), InvalidArgumentError);

  run_next_test();
});

add_test(function test_positiveInteger() {
  assert.positiveInteger(1);
  assert.positiveInteger(0);
  Assert.throws(() => assert.positiveInteger(-1), InvalidArgumentError);
  Assert.throws(() => assert.positiveInteger("foo"), InvalidArgumentError);

  run_next_test();
});

add_test(function test_boolean() {
  assert.boolean(true);
  assert.boolean(false);
  Assert.throws(() => assert.boolean("false"), InvalidArgumentError);
  Assert.throws(() => assert.boolean(undefined), InvalidArgumentError);

  run_next_test();
});

add_test(function test_string() {
  assert.string("foo");
  assert.string(`bar`);
  Assert.throws(() => assert.string(42), InvalidArgumentError);

  run_next_test();
});

add_test(function test_object() {
  assert.object({});
  assert.object(new Object());
  for (let typ of [42, "foo", true, null, undefined]) {
    Assert.throws(() => assert.object(typ), InvalidArgumentError);
  }

  run_next_test();
});

add_test(function test_in() {
  assert.in("foo", {foo: 42});
  for (let typ of [{}, 42, true, null, undefined]) {
    Assert.throws(() => assert.in("foo", typ), InvalidArgumentError);
  }

  run_next_test();
});

add_test(function test_array() {
  assert.array([]);
  assert.array(new Array());
  Assert.throws(() => assert.array(42), InvalidArgumentError);
  Assert.throws(() => assert.array({}), InvalidArgumentError);

  run_next_test();
});

add_test(function test_that() {
  equal(1, assert.that(n => n + 1)(1));
  Assert.throws(() => assert.that(() => false)());
  Assert.throws(() => assert.that(val => val)(false));
  Assert.throws(() => assert.that(val => val, "foo", SessionNotCreatedError)(false),
      SessionNotCreatedError);

  run_next_test();
});