c19341bf1f3c2731d697c8c101642ecc0a6770a4
[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("InspectorTestExpectFunctions");
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 WebInspector.CSSStyleDeclaration;
62     let customObject2 = new WebInspector.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             [[], []],
95         ],
96         failingInputs: [
97             [{a: 1, b: 2}, {a: 3, b: 4}],
98             [{}, []],
99         ]
100     };
101     addTestCase(expectShallowEqualTestCase);
102     addInverseTestCase("expectNotShallowEqual", expectShallowEqualTestCase);
103
104     addTestCase({
105         functionName: "expectEqualWithAccuracy",
106         passingInputs: [
107             [0, 0, 0],
108             [0, 0, 1],
109             [0, 1, 1],
110             [1, 0, 1],
111         ],
112         failingInputs: [
113             [0, 2, 1],
114             [2, 0, 1],
115         ]
116     });
117
118     addTestCase({
119         functionName: "expectLessThan",
120         passingInputs: [
121             [0, 1],
122             ["abc", "def"],
123         ],
124         failingInputs: [
125             [0, 0],
126             [1, 0],
127             ["abc", "abc"],
128             ["def", "abc"],
129         ]
130     });
131
132     addTestCase({
133         functionName: "expectLessThanOrEqual",
134         passingInputs: [
135             [0, 1],
136             [0, 0],
137             ["abc", "def"],
138             ["abc", "abc"],
139         ],
140         failingInputs: [
141             [1, 0],
142             ["def", "abc"],
143         ]
144     });
145
146     addTestCase({
147         functionName: "expectGreaterThan",
148         passingInputs: [
149             [1, 0],
150             ["def", "abc"],
151         ],
152         failingInputs: [
153             [0, 0],
154             [0, 1],
155             ["abc", "abc"],
156             ["abc", "def"],
157         ]
158     });
159
160     addTestCase({
161         functionName: "expectGreaterThanOrEqual",
162         passingInputs: [
163             [1, 0],
164             [0, 0],
165             ["def", "abc"],
166             ["abc", "abc"],
167         ],
168         failingInputs: [
169             [0, 1],
170             ["abc", "def"],
171         ]
172     });
173
174     suite.runTestCasesAndFinish();
175 }
176 </script>
177 </head>
178 <body onload="runTest()">
179     <p>Testing TestHarness.expect* family of functions.</p>
180 </body>
181 </html>