| <!doctype html> |
| <html> |
| <head> |
| <script src="../../http/tests/inspector/resources/inspector-test.js"></script> |
| <script> |
| function test() |
| { |
| let suite = InspectorTest.createSyncSuite("InspectorTest.ExpectFunctions.Sync"); |
| |
| function toArray(a) { |
| return a instanceof Array && a.length ? a : [a]; |
| } |
| |
| function addTestCase({functionName, passingInputs, failingInputs}) { |
| let functionUnderTest = InspectorTest[functionName]; |
| InspectorTest.assert(typeof functionUnderTest === "function", "Unknown InspectorTest function: " + functionName); |
| |
| suite.addTestCase({ |
| name: `InspectorTest.${functionName}`, |
| test() { |
| function exerciseFunction(inputs, shouldPass) { |
| if (!inputs || !inputs.length) { |
| InspectorTest.fail("exerciseFunction called with no inputs."); |
| return; |
| } |
| |
| InspectorTest.log("Expected to " + (shouldPass ? "PASS" : "FAIL")); |
| for (let input of inputs.map(toArray)) |
| functionUnderTest.call(InspectorTest, ...input); |
| } |
| |
| exerciseFunction(passingInputs, true); |
| exerciseFunction(failingInputs, false); |
| return true; |
| } |
| }); |
| } |
| |
| function addInverseTestCase(functionName, testCase) { |
| addTestCase({functionName, passingInputs: testCase.failingInputs, failingInputs: testCase.passingInputs}) |
| } |
| |
| let expectThatTestCase = { |
| functionName: "expectThat", |
| passingInputs: [true, 1, "abc", {}, []], |
| failingInputs: [false, 0, "", null, undefined, NaN], |
| }; |
| addTestCase(expectThatTestCase); |
| addInverseTestCase("expectFalse", expectThatTestCase); |
| |
| let expectEmptyTestCase = { |
| functionName: "expectEmpty", |
| passingInputs: [null, "", {}, [], new Map, new Set], |
| failingInputs: ["test", {test: 1}, ["test"], new Map([["test", 1]]), new Set(["test"]), true, false, 1, undefined], |
| }; |
| addTestCase(expectEmptyTestCase); |
| |
| let expectNotEmptyTestCase = { |
| functionName: "expectNotEmpty", |
| passingInputs: ["test", {test: 1}, ["test"], new Map([["test", 1]]), new Set(["test"])], |
| failingInputs: [null, "", {}, [], new Map, new Set, true, false, 1, undefined], |
| }; |
| addTestCase(expectNotEmptyTestCase); |
| |
| let expectNullTestCase = { |
| functionName: "expectNull", |
| passingInputs: [null], |
| failingInputs: [true, false, 1, "", undefined, {}, []] |
| }; |
| addTestCase(expectNullTestCase); |
| addInverseTestCase("expectNotNull", expectNullTestCase); |
| |
| let object1 = {a: 1, b: 2}; |
| let object2 = {a: 1, b: 2}; |
| let customObject1 = new WI.SourceCode; |
| let customObject2 = new WI.Resource("foo.bar"); |
| let expectEqualTestCase = { |
| functionName: "expectEqual", |
| passingInputs: [ |
| [true, true], |
| [object1, object1], |
| [customObject1, customObject1], |
| [1.23, 1.23], |
| ["abc", "abc"], |
| [null, null], |
| [undefined, undefined], |
| ], |
| failingInputs: [ |
| [true, false], |
| [object1, object2], |
| [customObject1, customObject2], |
| [1.23, 4.56], |
| ["abc", "def"], |
| [null, undefined], |
| [NaN, NaN], |
| [{}, {}], |
| [[], []], |
| ] |
| }; |
| addTestCase(expectEqualTestCase); |
| addInverseTestCase("expectNotEqual", expectEqualTestCase); |
| |
| let expectShallowEqualTestCase = { |
| functionName: "expectShallowEqual", |
| passingInputs: [ |
| [{a: 1, b: 2}, {a: 1, b: 2}], |
| [{}, {}], |
| [[1, 2], [1, 2]], |
| [[{a: 1}, {a: 2}], [{a: 1}, {a: 2}]], |
| [[{}], [{}]], |
| [[], []], |
| ], |
| failingInputs: [ |
| [{a: 1, b: 2}, {a: 3, b: 4}], |
| [{}, []], |
| [[1, 2], [3, 4]], |
| [[{a: 1}, {a: 2}], [{a: 3}, {a: 4}]], |
| [[{a: 1}, {a: 2}], [{b: 1}, {b: 2}]], |
| ] |
| }; |
| addTestCase(expectShallowEqualTestCase); |
| addInverseTestCase("expectNotShallowEqual", expectShallowEqualTestCase); |
| |
| addTestCase({ |
| functionName: "expectEqualWithAccuracy", |
| passingInputs: [ |
| [0, 0, 0], |
| [0, 0, 1], |
| [0, 1, 1], |
| [1, 0, 1], |
| ], |
| failingInputs: [ |
| [0, 2, 1], |
| [2, 0, 1], |
| ] |
| }); |
| |
| addTestCase({ |
| functionName: "expectLessThan", |
| passingInputs: [ |
| [0, 1], |
| ["abc", "def"], |
| ], |
| failingInputs: [ |
| [0, 0], |
| [1, 0], |
| ["abc", "abc"], |
| ["def", "abc"], |
| ] |
| }); |
| |
| addTestCase({ |
| functionName: "expectLessThanOrEqual", |
| passingInputs: [ |
| [0, 1], |
| [0, 0], |
| ["abc", "def"], |
| ["abc", "abc"], |
| ], |
| failingInputs: [ |
| [1, 0], |
| ["def", "abc"], |
| ] |
| }); |
| |
| addTestCase({ |
| functionName: "expectGreaterThan", |
| passingInputs: [ |
| [1, 0], |
| ["def", "abc"], |
| ], |
| failingInputs: [ |
| [0, 0], |
| [0, 1], |
| ["abc", "abc"], |
| ["abc", "def"], |
| ] |
| }); |
| |
| addTestCase({ |
| functionName: "expectGreaterThanOrEqual", |
| passingInputs: [ |
| [1, 0], |
| [0, 0], |
| ["def", "abc"], |
| ["abc", "abc"], |
| ], |
| failingInputs: [ |
| [0, 1], |
| ["abc", "def"], |
| ] |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onload="runTest()"> |
| <p>Testing TestHarness.expect* family of functions.</p> |
| </body> |
| </html> |