OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Library providing basic test framework functionality. | 6 * @fileoverview Library providing basic test framework functionality. |
7 */ | 7 */ |
8 | 8 |
9 /** | 9 /** |
10 * Namespace for |Test|. | 10 * Namespace for |Test|. |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1578 * @param {WhenTestDone} whenTestDone Call testDone() at the appropriate | 1578 * @param {WhenTestDone} whenTestDone Call testDone() at the appropriate |
1579 * time. | 1579 * time. |
1580 * @param {...Object} var_actions Actions to run. | 1580 * @param {...Object} var_actions Actions to run. |
1581 * @return {RunAllAction} Action for use in will. | 1581 * @return {RunAllAction} Action for use in will. |
1582 */ | 1582 */ |
1583 function runAllActionsAsync(whenTestDone) { | 1583 function runAllActionsAsync(whenTestDone) { |
1584 return new RunAllAction(true, whenTestDone, | 1584 return new RunAllAction(true, whenTestDone, |
1585 Array.prototype.slice.call(arguments, 1)); | 1585 Array.prototype.slice.call(arguments, 1)); |
1586 } | 1586 } |
1587 | 1587 |
| 1588 /** |
| 1589 * Syntactic sugar for use with will() on a Mock4JS.Mock. |
| 1590 * Creates an action for will() that invokes a callback that the tested code |
| 1591 * passes to a mocked function. |
| 1592 * @param {SaveMockArguments} savedArgs Arguments that will contain the |
| 1593 * callback once the mocked function is called. |
| 1594 * @param {number} callbackParameter Index of the callback parameter in |
| 1595 * |savedArgs|. |
| 1596 * @param {...Object} var_args Arguments to pass to the callback. |
| 1597 * @return {CallFunctionAction} Action for use in will(). |
| 1598 */ |
| 1599 function invokeCallback(savedArgs, callbackParameter, var_args) { |
| 1600 var callbackArguments = Array.prototype.slice.call(arguments, 2); |
| 1601 return callFunction(function() { |
| 1602 savedArgs.arguments[callbackParameter].apply(null, callbackArguments); |
| 1603 }); |
| 1604 } |
| 1605 |
| 1606 /** |
| 1607 * Mock4JS matcher object that matches the actual agrument and the expected |
| 1608 * value iff their JSON represenations are same. |
| 1609 * @param {Object} expectedValue Expected value. |
| 1610 * @constructor |
| 1611 */ |
| 1612 function MatchJSON(expectedValue) { |
| 1613 this.expectedValue_ = expectedValue; |
| 1614 } |
| 1615 |
| 1616 MatchJSON.prototype = { |
| 1617 /** |
| 1618 * Checks that JSON represenation of the actual and expected arguments are |
| 1619 * same. |
| 1620 * @param {Object} actualArgument The argument to match. |
| 1621 * @return {boolean} Result of the comparison. |
| 1622 */ |
| 1623 argumentMatches: function(actualArgument) { |
| 1624 return JSON.stringify(this.expectedValue_) === |
| 1625 JSON.stringify(actualArgument); |
| 1626 }, |
| 1627 |
| 1628 /** |
| 1629 * Describes the matcher. |
| 1630 * @return {string} Description of this Mock4JS matcher. |
| 1631 */ |
| 1632 describe: function() { |
| 1633 return 'eqJSON(' + JSON.stringify(this.expectedValue_) + ')'; |
| 1634 }, |
| 1635 }; |
| 1636 |
| 1637 /** |
| 1638 * Builds a MatchJSON agrument matcher for a given expected value. |
| 1639 * @param {Object} expectedValue Expected value. |
| 1640 * @return {MatchJSON} Resulting Mock4JS matcher. |
| 1641 */ |
| 1642 function eqJSON(expectedValue) { |
| 1643 return new MatchJSON(expectedValue); |
| 1644 } |
| 1645 |
1588 // Exports. | 1646 // Exports. |
1589 testing.Test = Test; | 1647 testing.Test = Test; |
1590 exports.testDone = testDone; | 1648 exports.testDone = testDone; |
1591 exports.assertTrue = assertTrue; | 1649 exports.assertTrue = assertTrue; |
1592 exports.assertFalse = assertFalse; | 1650 exports.assertFalse = assertFalse; |
1593 exports.assertGE = assertGE; | 1651 exports.assertGE = assertGE; |
1594 exports.assertGT = assertGT; | 1652 exports.assertGT = assertGT; |
1595 exports.assertEquals = assertEquals; | 1653 exports.assertEquals = assertEquals; |
1596 exports.assertLE = assertLE; | 1654 exports.assertLE = assertLE; |
1597 exports.assertLT = assertLT; | 1655 exports.assertLT = assertLT; |
1598 exports.assertNotEquals = assertNotEquals; | 1656 exports.assertNotEquals = assertNotEquals; |
1599 exports.assertNotReached = assertNotReached; | 1657 exports.assertNotReached = assertNotReached; |
1600 exports.assertAccessibilityOk = assertAccessibilityOk; | 1658 exports.assertAccessibilityOk = assertAccessibilityOk; |
1601 exports.callFunction = callFunction; | 1659 exports.callFunction = callFunction; |
1602 exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; | 1660 exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; |
1603 exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs; | 1661 exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs; |
| 1662 exports.eqJSON = eqJSON; |
1604 exports.expectTrue = createExpect(assertTrue); | 1663 exports.expectTrue = createExpect(assertTrue); |
1605 exports.expectFalse = createExpect(assertFalse); | 1664 exports.expectFalse = createExpect(assertFalse); |
1606 exports.expectGE = createExpect(assertGE); | 1665 exports.expectGE = createExpect(assertGE); |
1607 exports.expectGT = createExpect(assertGT); | 1666 exports.expectGT = createExpect(assertGT); |
1608 exports.expectEquals = createExpect(assertEquals); | 1667 exports.expectEquals = createExpect(assertEquals); |
1609 exports.expectLE = createExpect(assertLE); | 1668 exports.expectLE = createExpect(assertLE); |
1610 exports.expectLT = createExpect(assertLT); | 1669 exports.expectLT = createExpect(assertLT); |
1611 exports.expectNotEquals = createExpect(assertNotEquals); | 1670 exports.expectNotEquals = createExpect(assertNotEquals); |
1612 exports.expectNotReached = createExpect(assertNotReached); | 1671 exports.expectNotReached = createExpect(assertNotReached); |
1613 exports.expectAccessibilityOk = createExpect(assertAccessibilityOk); | 1672 exports.expectAccessibilityOk = createExpect(assertAccessibilityOk); |
| 1673 exports.invokeCallback = invokeCallback; |
1614 exports.preloadJavascriptLibraries = preloadJavascriptLibraries; | 1674 exports.preloadJavascriptLibraries = preloadJavascriptLibraries; |
1615 exports.registerMessageCallback = registerMessageCallback; | 1675 exports.registerMessageCallback = registerMessageCallback; |
1616 exports.registerMockGlobals = registerMockGlobals; | 1676 exports.registerMockGlobals = registerMockGlobals; |
1617 exports.registerMockMessageCallbacks = registerMockMessageCallbacks; | 1677 exports.registerMockMessageCallbacks = registerMockMessageCallbacks; |
1618 exports.resetTestState = resetTestState; | 1678 exports.resetTestState = resetTestState; |
1619 exports.runAccessibilityAudit = runAccessibilityAudit; | 1679 exports.runAccessibilityAudit = runAccessibilityAudit; |
1620 exports.runAllActions = runAllActions; | 1680 exports.runAllActions = runAllActions; |
1621 exports.runAllActionsAsync = runAllActionsAsync; | 1681 exports.runAllActionsAsync = runAllActionsAsync; |
1622 exports.runTest = runTest; | 1682 exports.runTest = runTest; |
1623 exports.runTestFunction = runTestFunction; | 1683 exports.runTestFunction = runTestFunction; |
1624 exports.SaveMockArguments = SaveMockArguments; | 1684 exports.SaveMockArguments = SaveMockArguments; |
1625 exports.DUMMY_URL = DUMMY_URL; | 1685 exports.DUMMY_URL = DUMMY_URL; |
1626 exports.TEST = TEST; | 1686 exports.TEST = TEST; |
1627 exports.TEST_F = TEST_F; | 1687 exports.TEST_F = TEST_F; |
1628 exports.RUNTIME_TEST_F = TEST_F; | 1688 exports.RUNTIME_TEST_F = TEST_F; |
1629 exports.GEN = GEN; | 1689 exports.GEN = GEN; |
1630 exports.GEN_INCLUDE = GEN_INCLUDE; | 1690 exports.GEN_INCLUDE = GEN_INCLUDE; |
1631 exports.WhenTestDone = WhenTestDone; | 1691 exports.WhenTestDone = WhenTestDone; |
1632 | 1692 |
1633 // Import the Mock4JS helpers. | 1693 // Import the Mock4JS helpers. |
1634 Mock4JS.addMockSupport(exports); | 1694 Mock4JS.addMockSupport(exports); |
1635 })(this); | 1695 })(this); |
OLD | NEW |