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