Source/WebInspectorUI:
authormattbaker@apple.com <mattbaker@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 12 Sep 2016 22:36:57 +0000 (22:36 +0000)
committermattbaker@apple.com <mattbaker@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 12 Sep 2016 22:36:57 +0000 (22:36 +0000)
Web Inspector: Improve clarity of inspector tests by adding TestHarness.expect* functions similar to XCTest
https://bugs.webkit.org/show_bug.cgi?id=161278
<rdar://problem/28039741>

Reviewed by Joseph Pecoraro.

Under the hood they all call TestHarness.expectThat.

* UserInterface/Test/TestHarness.js:
(TestHarness.prototype.expectFalse):
(TestHarness.prototype.expectNull):
(TestHarness.prototype.expectNotNull):
(TestHarness.prototype.expectEqual):
(TestHarness.prototype.expectNotEqual):
(TestHarness.prototype.expectShallowEqual):
(TestHarness.prototype.expectNotShallowEqual):
(TestHarness.prototype.expectEqualWithAccuracy):
(TestHarness.prototype.expectLessThan):
(TestHarness.prototype.expectLessThanOrEqual):
(TestHarness.prototype.expectGreaterThan):
(TestHarness.prototype.expectGreaterThanOrEqual):

LayoutTests:
Web Inspector: Add TestHarness assertions/expectations to provide additional semantics similar to XCTest
https://bugs.webkit.org/show_bug.cgi?id=161278
<rdar://problem/28039741>

Reviewed by Joseph Pecoraro.

Add test to verify that the TestHarness.expect* family of functions
trivially work. Since it isn't possible to verify the TestPage results
directly without introducing additional complexity, simply log baseline
"PASS" and "FAIL" results for each function.

* inspector/unit-tests/test-harness-expect-functions-expected.txt: Added.
* inspector/unit-tests/test-harness-expect-functions.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@205834 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/unit-tests/test-harness-expect-functions-expected.txt [new file with mode: 0644]
LayoutTests/inspector/unit-tests/test-harness-expect-functions.html [new file with mode: 0644]
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Test/TestHarness.js

index baf912f4d4c30e5ca25e1ce1cf557aad7f27ecf7..160e51a97e9936e8ce2a0247e166cfdbc4866af9 100644 (file)
@@ -1,3 +1,19 @@
+2016-09-12  Matt Baker  <mattbaker@apple.com>
+
+        Web Inspector: Add TestHarness assertions/expectations to provide additional semantics similar to XCTest
+        https://bugs.webkit.org/show_bug.cgi?id=161278
+        <rdar://problem/28039741>
+
+        Reviewed by Joseph Pecoraro.
+
+        Add test to verify that the TestHarness.expect* family of functions
+        trivially work. Since it isn't possible to verify the TestPage results
+        directly without introducing additional complexity, simply log baseline
+        "PASS" and "FAIL" results for each function.
+
+        * inspector/unit-tests/test-harness-expect-functions-expected.txt: Added.
+        * inspector/unit-tests/test-harness-expect-functions.html: Added.
+
 2016-09-12  Chris Dumez  <cdumez@apple.com>
 
         window.performance object resets script-applied properties
diff --git a/LayoutTests/inspector/unit-tests/test-harness-expect-functions-expected.txt b/LayoutTests/inspector/unit-tests/test-harness-expect-functions-expected.txt
new file mode 100644 (file)
index 0000000..8cb8747
--- /dev/null
@@ -0,0 +1,162 @@
+Testing TestHarness.expect* family of functions.
+
+
+== Running test suite: InspectorTestExpectFunctions
+-- Running test case: InspectorTest.expectThat
+Expected to PASS
+PASS: expectThat(true)
+PASS: expectThat(1)
+PASS: expectThat("abc")
+PASS: expectThat({})
+PASS: expectThat([])
+Expected to FAIL
+FAIL: expectThat(false)
+FAIL: expectThat(0)
+FAIL: expectThat("")
+FAIL: expectThat(null)
+FAIL: expectThat(undefined)
+FAIL: expectThat(NaN)
+
+-- Running test case: InspectorTest.expectFalse
+Expected to PASS
+PASS: expectFalse(false)
+PASS: expectFalse(0)
+PASS: expectFalse("")
+PASS: expectFalse(null)
+PASS: expectFalse(undefined)
+PASS: expectFalse(NaN)
+Expected to FAIL
+FAIL: expectFalse(true)
+FAIL: expectFalse(1)
+FAIL: expectFalse("abc")
+FAIL: expectFalse({})
+FAIL: expectFalse([])
+
+-- Running test case: InspectorTest.expectNull
+Expected to PASS
+PASS: expectNull(null)
+Expected to FAIL
+FAIL: expectNull(true)
+FAIL: expectNull(false)
+FAIL: expectNull(1)
+FAIL: expectNull("")
+FAIL: expectNull(undefined)
+FAIL: expectNull({})
+FAIL: expectNull([])
+
+-- Running test case: InspectorTest.expectNotNull
+Expected to PASS
+PASS: expectNotNull(true)
+PASS: expectNotNull(false)
+PASS: expectNotNull(1)
+PASS: expectNotNull("")
+PASS: expectNotNull(undefined)
+PASS: expectNotNull({})
+PASS: expectNotNull([])
+Expected to FAIL
+FAIL: expectNotNull(null)
+
+-- Running test case: InspectorTest.expectEqual
+Expected to PASS
+PASS: expectEqual(true, true)
+PASS: expectEqual({"a":1,"b":2}, {"a":1,"b":2})
+PASS: expectEqual(1.23, 1.23)
+PASS: expectEqual("abc", "abc")
+PASS: expectEqual(null, null)
+PASS: expectEqual(undefined, undefined)
+Expected to FAIL
+FAIL: expectEqual(true, false)
+FAIL: expectEqual({"a":1,"b":2}, {"c":3,"d":4})
+FAIL: expectEqual(1.23, 4.56)
+FAIL: expectEqual("abc", "def")
+FAIL: expectEqual(null, undefined)
+FAIL: expectEqual(NaN, NaN)
+FAIL: expectEqual({}, {})
+FAIL: expectEqual([], [])
+
+-- Running test case: InspectorTest.expectNotEqual
+Expected to PASS
+PASS: expectNotEqual(true, false)
+PASS: expectNotEqual({"a":1,"b":2}, {"c":3,"d":4})
+PASS: expectNotEqual(1.23, 4.56)
+PASS: expectNotEqual("abc", "def")
+PASS: expectNotEqual(null, undefined)
+PASS: expectNotEqual(NaN, NaN)
+PASS: expectNotEqual({}, {})
+PASS: expectNotEqual([], [])
+Expected to FAIL
+FAIL: expectNotEqual(true, true)
+FAIL: expectNotEqual({"a":1,"b":2}, {"a":1,"b":2})
+FAIL: expectNotEqual(1.23, 1.23)
+FAIL: expectNotEqual("abc", "abc")
+FAIL: expectNotEqual(null, null)
+FAIL: expectNotEqual(undefined, undefined)
+
+-- Running test case: InspectorTest.expectShallowEqual
+Expected to PASS
+PASS: expectShallowEqual({"a":1,"b":2}, {"a":1,"b":2})
+PASS: expectShallowEqual({}, {})
+PASS: expectShallowEqual([], [])
+Expected to FAIL
+FAIL: expectShallowEqual({"a":1,"b":2}, {"a":3,"b":4})
+FAIL: expectShallowEqual({}, [])
+
+-- Running test case: InspectorTest.expectNotShallowEqual
+Expected to PASS
+PASS: expectNotShallowEqual({"a":1,"b":2}, {"a":3,"b":4})
+PASS: expectNotShallowEqual({}, [])
+Expected to FAIL
+FAIL: expectNotShallowEqual({"a":1,"b":2}, {"a":1,"b":2})
+FAIL: expectNotShallowEqual({}, {})
+FAIL: expectNotShallowEqual([], [])
+
+-- Running test case: InspectorTest.expectEqualWithAccuracy
+Expected to PASS
+PASS: expectEqualWithAccuracy(0, 0, 0)
+PASS: expectEqualWithAccuracy(0, 0, 1)
+PASS: expectEqualWithAccuracy(0, 1, 1)
+PASS: expectEqualWithAccuracy(1, 0, 1)
+Expected to FAIL
+FAIL: expectEqualWithAccuracy(0, 2, 1)
+FAIL: expectEqualWithAccuracy(2, 0, 1)
+
+-- Running test case: InspectorTest.expectLessThan
+Expected to PASS
+PASS: expectLessThan(0, 1)
+PASS: expectLessThan("abc", "def")
+Expected to FAIL
+FAIL: expectLessThan(0, 0)
+FAIL: expectLessThan(1, 0)
+FAIL: expectLessThan("abc", "abc")
+FAIL: expectLessThan("def", "abc")
+
+-- Running test case: InspectorTest.expectLessThanOrEqual
+Expected to PASS
+PASS: expectLessThanOrEqual(0, 1)
+PASS: expectLessThanOrEqual(0, 0)
+PASS: expectLessThanOrEqual("abc", "def")
+PASS: expectLessThanOrEqual("abc", "abc")
+Expected to FAIL
+FAIL: expectLessThanOrEqual(1, 0)
+FAIL: expectLessThanOrEqual("def", "abc")
+
+-- Running test case: InspectorTest.expectGreaterThan
+Expected to PASS
+PASS: expectGreaterThan(1, 0)
+PASS: expectGreaterThan("def", "abc")
+Expected to FAIL
+FAIL: expectGreaterThan(0, 0)
+FAIL: expectGreaterThan(0, 1)
+FAIL: expectGreaterThan("abc", "abc")
+FAIL: expectGreaterThan("abc", "def")
+
+-- Running test case: InspectorTest.expectGreaterThanOrEqual
+Expected to PASS
+PASS: expectGreaterThanOrEqual(1, 0)
+PASS: expectGreaterThanOrEqual(0, 0)
+PASS: expectGreaterThanOrEqual("def", "abc")
+PASS: expectGreaterThanOrEqual("abc", "abc")
+Expected to FAIL
+FAIL: expectGreaterThanOrEqual(0, 1)
+FAIL: expectGreaterThanOrEqual("abc", "def")
+
diff --git a/LayoutTests/inspector/unit-tests/test-harness-expect-functions.html b/LayoutTests/inspector/unit-tests/test-harness-expect-functions.html
new file mode 100644 (file)
index 0000000..c60ff69
--- /dev/null
@@ -0,0 +1,190 @@
+<!doctype html>
+<html>
+<head>
+<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
+<script>
+function test()
+{
+    console.assert(false, "FAIL ASSERTION!!!!!!!");
+
+    let suite = InspectorTest.createSyncSuite("InspectorTestExpectFunctions");
+
+    function toArray(a) {
+        return a instanceof Array && a.length ? a : [a];
+    }
+
+    function stringifyArguments(args) {
+        return args.map((a) => {
+            if (typeof a === "number")
+                return a;
+            // Append empty string so `undefined` is displayed correctly.
+            return JSON.stringify(a) + "";
+        }).join(", ");
+    }
+
+    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)) {
+                        let argumentsString = stringifyArguments(input);
+                        functionUnderTest.call(InspectorTest, ...input, `${functionName}(${argumentsString})`);
+                    }
+                }
+
+                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 expectNullTestCase = {
+        functionName: "expectNull",
+        passingInputs: [null],
+        failingInputs: [true, false, 1, "", undefined, {}, []]
+    };
+    addTestCase(expectNullTestCase);
+    addInverseTestCase("expectNotNull", expectNullTestCase);
+
+    let object1 = {a: 1, b: 2};
+    let object2 = {c: 3, d: 4};
+    let expectEqualTestCase = {
+        functionName: "expectEqual",
+        passingInputs: [
+            [true, true],
+            [object1, object1],
+            [1.23, 1.23],
+            ["abc", "abc"],
+            [null, null],
+            [undefined, undefined],
+        ],
+        failingInputs: [
+            [true, false],
+            [object1, object2],
+            [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}],
+            [{}, {}],
+            [[], []],
+        ],
+        failingInputs: [
+            [{a: 1, b: 2}, {a: 3, b: 4}],
+            [{}, []],
+        ]
+    };
+    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>
index e13b183220a1e796aa8b8053bbc616955d2eb8a1..6a730d1b4fa3217cecf0a689935962345fbaec6d 100644 (file)
@@ -1,3 +1,27 @@
+2016-09-12  Matt Baker  <mattbaker@apple.com>
+
+        Web Inspector: Improve clarity of inspector tests by adding TestHarness.expect* functions similar to XCTest
+        https://bugs.webkit.org/show_bug.cgi?id=161278
+        <rdar://problem/28039741>
+
+        Reviewed by Joseph Pecoraro.
+
+        Under the hood they all call TestHarness.expectThat.
+
+        * UserInterface/Test/TestHarness.js:
+        (TestHarness.prototype.expectFalse):
+        (TestHarness.prototype.expectNull):
+        (TestHarness.prototype.expectNotNull):
+        (TestHarness.prototype.expectEqual):
+        (TestHarness.prototype.expectNotEqual):
+        (TestHarness.prototype.expectShallowEqual):
+        (TestHarness.prototype.expectNotShallowEqual):
+        (TestHarness.prototype.expectEqualWithAccuracy):
+        (TestHarness.prototype.expectLessThan):
+        (TestHarness.prototype.expectLessThanOrEqual):
+        (TestHarness.prototype.expectGreaterThan):
+        (TestHarness.prototype.expectGreaterThanOrEqual):
+
 2016-09-12  Nikita Vasilyev  <nvasilyev@apple.com>
 
         REGRESSION (r205223): Web Inspector: Debugger popover title and code aren't horizontally aligned
index 5668b2e2aa3f6daf99c85a88204672f0f84dde43..d4ff40d69b7dd980ee99870676cf2ba2f6f83dc3 100644 (file)
@@ -99,6 +99,68 @@ TestHarness = class TestHarness extends WebInspector.Object
             this.fail(message);
     }
 
+    expectFalse(expression, message)
+    {
+        this.expectThat(!expression, message);
+    }
+
+    expectNull(expression, message)
+    {
+        this.expectThat(expression === null, message);
+    }
+
+    expectNotNull(expression, message)
+    {
+        this.expectThat(expression !== null, message);
+    }
+
+    expectEqual(expression1, expression2, message)
+    {
+        this.expectThat(expression1 === expression2, message);
+    }
+
+    expectNotEqual(expression1, expression2, message)
+    {
+        this.expectThat(expression1 !== expression2, message);
+    }
+
+    expectShallowEqual(expression1, expression2, message)
+    {
+        this.expectThat(Object.shallowEqual(expression1, expression2), message);
+    }
+
+    expectNotShallowEqual(expression1, expression2, message)
+    {
+        this.expectThat(!Object.shallowEqual(expression1, expression2), message);
+    }
+
+    expectEqualWithAccuracy(expression1, expression2, accuracy, message)
+    {
+        console.assert(typeof expression1 === "number");
+        console.assert(typeof expression2 === "number");
+        this.expectThat(Math.abs(expression1 - expression2) <= accuracy, message);
+    }
+
+    expectLessThan(expression1, expression2, message)
+    {
+        this.expectThat(expression1 < expression2, message);
+    }
+
+    expectLessThanOrEqual(expression1, expression2, message)
+    {
+        this.expectThat(expression1 <= expression2, message);
+    }
+
+    expectGreaterThan(expression1, expression2, message)
+    {
+        this.expectThat(expression1 > expression2, message);
+    }
+
+    expectGreaterThanOrEqual(expression1, expression2, message)
+    {
+        this.expectThat(expression1 >= expression2, message);
+    }
+
     pass(message)
     {
         let stringifiedMessage = TestHarness.messageAsString(message);