1 add_task(function setup() {
2 // With the default reporter, an assertion doesn't throw if it fails, it
3 // merely report the result to the reporter and then go on. But in this test
4 // we want that a failure really throws, so that we can actually assert that
5 // it throws in case of failures!
6 // That's why we disable the default repoter here.
7 // I noticed that this line needs to be in an add_task (or possibly run_test)
8 // function. If put outside this will crash the test.
9 Assert
.setReporter(null);
12 add_task(function test_objectContains() {
18 Assert
.objectContains(fixture
, { foo
: "foo" }, "Matches one property value");
19 Assert
.objectContains(
21 { foo
: "foo", bar
: "bar" },
22 "Matches both properties"
24 Assert
.objectContainsOnly(
26 { foo
: "foo", bar
: "bar" },
27 "Matches both properties"
30 () => Assert
.objectContainsOnly(fixture
, { foo
: "foo" }),
32 "Fails if some properties are missing"
35 () => Assert
.objectContains(fixture
, { foo
: "bar" }),
37 "Fails if the value for a present property is wrong"
40 () => Assert
.objectContains(fixture
, { hello
: "world" }),
42 "Fails if an expected property is missing"
45 () => Assert
.objectContains(fixture
, { foo
: "foo", hello
: "world" }),
47 "Fails if some properties are present but others are missing"
51 add_task(function test_objectContains_expectations() {
57 nestedFoo
: "nestedFoo",
58 nestedBar
: "nestedBar",
62 Assert
.objectContains(
65 foo
: Expect
.stringMatches(/^fo/),
66 bar
: Expect
.stringContains("ar"),
68 nested
: Expect
.objectContainsOnly({
69 nestedFoo
: Expect
.stringMatches(/[Ff]oo/),
70 nestedBar
: Expect
.stringMatches(/[Bb]ar/),
73 "Supports expectations"
75 Assert
.objectContainsOnly(
78 foo
: Expect
.stringMatches(/^fo/),
79 bar
: Expect
.stringContains("ar"),
81 nested
: Expect
.objectContains({
82 nestedFoo
: Expect
.stringMatches(/[Ff]oo/),
85 "Supports expectations"
88 Assert
.objectContains(fixture
, {
89 num
: val
=> Assert
.greater(val
, 40),
92 // Failed expectations
95 Assert
.objectContains(fixture
, {
96 foo
: Expect
.stringMatches(/bar/),
99 "Expect.stringMatches shouldn't match when the value is unexpected"
103 Assert
.objectContains(fixture
, {
104 foo
: Expect
.stringContains("bar"),
107 "Expect.stringContains shouldn't match when the value is unexpected"
111 Assert
.objectContains(fixture
, {
112 foo
: Expect
.number(),
115 "Expect.number shouldn't match when the value isn't a number"
119 Assert
.objectContains(fixture
, {
120 nested
: Expect
.objectContains({
125 "Expect.objectContains should throw when the value is unexpected"
130 Assert
.objectContains(fixture
, {
131 num
: val
=> Assert
.less(val
, 40),
134 "Expect.objectContains should throw when a function assertion fails"
138 add_task(function test_type_expectations() {
145 symbol
: Symbol("foo"),
146 object
: { foo
: "foo" },
151 Assert
.objectContains(fixture
, {
153 string
: Expect
.string(),
154 number
: Expect
.number(),
155 boolean: Expect
.boolean(),
156 bigint
: Expect
.bigint(),
157 symbol
: Expect
.symbol(),
158 object
: Expect
.object(),
159 function1
: Expect
.function(),
160 function2
: Expect
.function(),