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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
add_task(function setup() {
// With the default reporter, an assertion doesn't throw if it fails, it
// merely report the result to the reporter and then go on. But in this test
// we want that a failure really throws, so that we can actually assert that
// it throws in case of failures!
// That's why we disable the default repoter here.
// I noticed that this line needs to be in an add_task (or possibly run_test)
// function. If put outside this will crash the test.
Assert.setReporter(null);
});
add_task(function test_objectContains() {
const fixture = {
foo: "foo",
bar: "bar",
};
Assert.objectContains(fixture, { foo: "foo" }, "Matches one property value");
Assert.objectContains(
fixture,
{ foo: "foo", bar: "bar" },
"Matches both properties"
);
Assert.objectContainsOnly(
fixture,
{ foo: "foo", bar: "bar" },
"Matches both properties"
);
Assert.throws(
() => Assert.objectContainsOnly(fixture, { foo: "foo" }),
/AssertionError/,
"Fails if some properties are missing"
);
Assert.throws(
() => Assert.objectContains(fixture, { foo: "bar" }),
/AssertionError/,
"Fails if the value for a present property is wrong"
);
Assert.throws(
() => Assert.objectContains(fixture, { hello: "world" }),
/AssertionError/,
"Fails if an expected property is missing"
);
Assert.throws(
() => Assert.objectContains(fixture, { foo: "foo", hello: "world" }),
/AssertionError/,
"Fails if some properties are present but others are missing"
);
});
add_task(function test_objectContains_expectations() {
const fixture = {
foo: "foo",
bar: "bar",
num: 42,
nested: {
nestedFoo: "nestedFoo",
nestedBar: "nestedBar",
},
};
Assert.objectContains(
fixture,
{
foo: Expect.stringMatches(/^fo/),
bar: Expect.stringContains("ar"),
num: Expect.number(),
nested: Expect.objectContainsOnly({
nestedFoo: Expect.stringMatches(/[Ff]oo/),
nestedBar: Expect.stringMatches(/[Bb]ar/),
}),
},
"Supports expectations"
);
Assert.objectContainsOnly(
fixture,
{
foo: Expect.stringMatches(/^fo/),
bar: Expect.stringContains("ar"),
num: Expect.number(),
nested: Expect.objectContains({
nestedFoo: Expect.stringMatches(/[Ff]oo/),
}),
},
"Supports expectations"
);
Assert.objectContains(fixture, {
num: val => Assert.greater(val, 40),
});
// Failed expectations
Assert.throws(
() =>
Assert.objectContains(fixture, {
foo: Expect.stringMatches(/bar/),
}),
/AssertionError/,
"Expect.stringMatches shouldn't match when the value is unexpected"
);
Assert.throws(
() =>
Assert.objectContains(fixture, {
foo: Expect.stringContains("bar"),
}),
/AssertionError/,
"Expect.stringContains shouldn't match when the value is unexpected"
);
Assert.throws(
() =>
Assert.objectContains(fixture, {
foo: Expect.number(),
}),
/AssertionError/,
"Expect.number shouldn't match when the value isn't a number"
);
Assert.throws(
() =>
Assert.objectContains(fixture, {
nested: Expect.objectContains({
nestedFoo: "bar",
}),
}),
/AssertionError/,
"Expect.objectContains should throw when the value is unexpected"
);
Assert.throws(
() =>
Assert.objectContains(fixture, {
num: val => Assert.less(val, 40),
}),
/AssertionError/,
"Expect.objectContains should throw when a function assertion fails"
);
});
add_task(function test_type_expectations() {
const fixture = {
any: "foo",
string: "foo",
number: 42,
boolean: true,
bigint: 42n,
symbol: Symbol("foo"),
object: { foo: "foo" },
function1() {},
function2: () => {},
};
Assert.objectContains(fixture, {
any: Expect.any(),
string: Expect.string(),
number: Expect.number(),
boolean: Expect.boolean(),
bigint: Expect.bigint(),
symbol: Expect.symbol(),
object: Expect.object(),
function1: Expect.function(),
function2: Expect.function(),
});
});
|