Web Inspector: add InspectorTest.expectException() and use it
[WebKit-https.git] / LayoutTests / inspector / unit-tests / test-harness-expect-functions.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     let suite = InspectorTest.createSyncSuite("InspectorTest.ExpectFunctions.Sync");
9
10     function toArray(a) {
11         return a instanceof Array && a.length ? a : [a];
12     }
13
14     function addTestCase({functionName, passingInputs, failingInputs}) {
15         let functionUnderTest = InspectorTest[functionName];
16         InspectorTest.assert(typeof functionUnderTest === "function", "Unknown InspectorTest function: " + functionName);
17
18         suite.addTestCase({
19             name: `InspectorTest.${functionName}`,
20             test() {
21                 function exerciseFunction(inputs, shouldPass) {
22                     if (!inputs || !inputs.length) {
23                         InspectorTest.fail("exerciseFunction called with no inputs.");
24                         return;
25                     }
26
27                     InspectorTest.log("Expected to " + (shouldPass ? "PASS" : "FAIL"));
28                     for (let input of inputs.map(toArray))
29                         functionUnderTest.call(InspectorTest, ...input);
30                 }
31
32                 exerciseFunction(passingInputs, true);
33                 exerciseFunction(failingInputs, false);
34                 return true;
35             }
36         });
37     }
38
39     function addInverseTestCase(functionName, testCase) {
40         addTestCase({functionName, passingInputs: testCase.failingInputs, failingInputs: testCase.passingInputs})
41     }
42
43     let expectThatTestCase = {
44         functionName: "expectThat",
45         passingInputs: [true, 1, "abc", {}, []],
46         failingInputs: [false, 0, "", null, undefined, NaN],
47     };
48     addTestCase(expectThatTestCase);
49     addInverseTestCase("expectFalse", expectThatTestCase);
50
51     let expectNullTestCase = {
52         functionName: "expectNull",
53         passingInputs: [null],
54         failingInputs: [true, false, 1, "", undefined, {}, []]
55     };
56     addTestCase(expectNullTestCase);
57     addInverseTestCase("expectNotNull", expectNullTestCase);
58
59     let object1 = {a: 1, b: 2};
60     let object2 = {a: 1, b: 2};
61     let customObject1 = new WI.CSSStyleDeclaration;
62     let customObject2 = new WI.CSSRule;
63     let expectEqualTestCase = {
64         functionName: "expectEqual",
65         passingInputs: [
66             [true, true],
67             [object1, object1],
68             [customObject1, customObject1],
69             [1.23, 1.23],
70             ["abc", "abc"],
71             [null, null],
72             [undefined, undefined],
73         ],
74         failingInputs: [
75             [true, false],
76             [object1, object2],
77             [customObject1, customObject2],
78             [1.23, 4.56],
79             ["abc", "def"],
80             [null, undefined],
81             [NaN, NaN],
82             [{}, {}],
83             [[], []],
84         ]
85     };
86     addTestCase(expectEqualTestCase);
87     addInverseTestCase("expectNotEqual", expectEqualTestCase);
88
89     let expectShallowEqualTestCase = {
90         functionName: "expectShallowEqual",
91         passingInputs: [
92             [{a: 1, b: 2}, {a: 1, b: 2}],
93             [{}, {}],
94             [[1, 2], [1, 2]],
95             [[{a: 1}, {a: 2}], [{a: 1}, {a: 2}]],
96             [[{}], [{}]],
97             [[], []],
98         ],
99         failingInputs: [
100             [{a: 1, b: 2}, {a: 3, b: 4}],
101             [{}, []],
102             [[1, 2], [3, 4]],
103             [[{a: 1}, {a: 2}], [{a: 3}, {a: 4}]],
104             [[{a: 1}, {a: 2}], [{b: 1}, {b: 2}]],
105         ]
106     };
107     addTestCase(expectShallowEqualTestCase);
108     addInverseTestCase("expectNotShallowEqual", expectShallowEqualTestCase);
109
110     addTestCase({
111         functionName: "expectEqualWithAccuracy",
112         passingInputs: [
113             [0, 0, 0],
114             [0, 0, 1],
115             [0, 1, 1],
116             [1, 0, 1],
117         ],
118         failingInputs: [
119             [0, 2, 1],
120             [2, 0, 1],
121         ]
122     });
123
124     addTestCase({
125         functionName: "expectLessThan",
126         passingInputs: [
127             [0, 1],
128             ["abc", "def"],
129         ],
130         failingInputs: [
131             [0, 0],
132             [1, 0],
133             ["abc", "abc"],
134             ["def", "abc"],
135         ]
136     });
137
138     addTestCase({
139         functionName: "expectLessThanOrEqual",
140         passingInputs: [
141             [0, 1],
142             [0, 0],
143             ["abc", "def"],
144             ["abc", "abc"],
145         ],
146         failingInputs: [
147             [1, 0],
148             ["def", "abc"],
149         ]
150     });
151
152     addTestCase({
153         functionName: "expectGreaterThan",
154         passingInputs: [
155             [1, 0],
156             ["def", "abc"],
157         ],
158         failingInputs: [
159             [0, 0],
160             [0, 1],
161             ["abc", "abc"],
162             ["abc", "def"],
163         ]
164     });
165
166     addTestCase({
167         functionName: "expectGreaterThanOrEqual",
168         passingInputs: [
169             [1, 0],
170             [0, 0],
171             ["def", "abc"],
172             ["abc", "abc"],
173         ],
174         failingInputs: [
175             [0, 1],
176             ["abc", "def"],
177         ]
178     });
179
180     suite.runTestCasesAndFinish();
181 }
182 </script>
183 </head>
184 <body onload="runTest()">
185     <p>Testing TestHarness.expect* family of functions.</p>
186 </body>
187 </html>