Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / tools / profiler / tests / xpcshell / test_assertion_helper.js
blobbaa4c34818fc97161f476880cdd010dfe7f4c2f2
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);
10 });
12 add_task(function test_objectContains() {
13 const fixture = {
14 foo: "foo",
15 bar: "bar",
18 Assert.objectContains(fixture, { foo: "foo" }, "Matches one property value");
19 Assert.objectContains(
20 fixture,
21 { foo: "foo", bar: "bar" },
22 "Matches both properties"
24 Assert.objectContainsOnly(
25 fixture,
26 { foo: "foo", bar: "bar" },
27 "Matches both properties"
29 Assert.throws(
30 () => Assert.objectContainsOnly(fixture, { foo: "foo" }),
31 /AssertionError/,
32 "Fails if some properties are missing"
34 Assert.throws(
35 () => Assert.objectContains(fixture, { foo: "bar" }),
36 /AssertionError/,
37 "Fails if the value for a present property is wrong"
39 Assert.throws(
40 () => Assert.objectContains(fixture, { hello: "world" }),
41 /AssertionError/,
42 "Fails if an expected property is missing"
44 Assert.throws(
45 () => Assert.objectContains(fixture, { foo: "foo", hello: "world" }),
46 /AssertionError/,
47 "Fails if some properties are present but others are missing"
49 });
51 add_task(function test_objectContains_expectations() {
52 const fixture = {
53 foo: "foo",
54 bar: "bar",
55 num: 42,
56 nested: {
57 nestedFoo: "nestedFoo",
58 nestedBar: "nestedBar",
62 Assert.objectContains(
63 fixture,
65 foo: Expect.stringMatches(/^fo/),
66 bar: Expect.stringContains("ar"),
67 num: Expect.number(),
68 nested: Expect.objectContainsOnly({
69 nestedFoo: Expect.stringMatches(/[Ff]oo/),
70 nestedBar: Expect.stringMatches(/[Bb]ar/),
71 }),
73 "Supports expectations"
75 Assert.objectContainsOnly(
76 fixture,
78 foo: Expect.stringMatches(/^fo/),
79 bar: Expect.stringContains("ar"),
80 num: Expect.number(),
81 nested: Expect.objectContains({
82 nestedFoo: Expect.stringMatches(/[Ff]oo/),
83 }),
85 "Supports expectations"
88 Assert.objectContains(fixture, {
89 num: val => Assert.greater(val, 40),
90 });
92 // Failed expectations
93 Assert.throws(
94 () =>
95 Assert.objectContains(fixture, {
96 foo: Expect.stringMatches(/bar/),
97 }),
98 /AssertionError/,
99 "Expect.stringMatches shouldn't match when the value is unexpected"
101 Assert.throws(
102 () =>
103 Assert.objectContains(fixture, {
104 foo: Expect.stringContains("bar"),
106 /AssertionError/,
107 "Expect.stringContains shouldn't match when the value is unexpected"
109 Assert.throws(
110 () =>
111 Assert.objectContains(fixture, {
112 foo: Expect.number(),
114 /AssertionError/,
115 "Expect.number shouldn't match when the value isn't a number"
117 Assert.throws(
118 () =>
119 Assert.objectContains(fixture, {
120 nested: Expect.objectContains({
121 nestedFoo: "bar",
124 /AssertionError/,
125 "Expect.objectContains should throw when the value is unexpected"
128 Assert.throws(
129 () =>
130 Assert.objectContains(fixture, {
131 num: val => Assert.less(val, 40),
133 /AssertionError/,
134 "Expect.objectContains should throw when a function assertion fails"
138 add_task(function test_type_expectations() {
139 const fixture = {
140 any: "foo",
141 string: "foo",
142 number: 42,
143 boolean: true,
144 bigint: 42n,
145 symbol: Symbol("foo"),
146 object: { foo: "foo" },
147 function1() {},
148 function2: () => {},
151 Assert.objectContains(fixture, {
152 any: Expect.any(),
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(),