| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 * Create a mock function that records function calls and validates against | 6 * Create a mock function that records function calls and validates against |
| 7 * expectations. | 7 * expectations. |
| 8 * @constructor. | 8 * @constructor. |
| 9 */ | 9 */ |
| 10 function MockMethod() { | 10 function MockMethod() { |
| 11 var fn = function() { | 11 var fn = function() { |
| 12 var args = Array.prototype.slice.call(arguments); | 12 var args = Array.prototype.slice.call(arguments); |
| 13 var callbacks = |
| 14 args.filter(function(arg) { return (typeof arg == 'function'); }); |
| 15 |
| 16 if (callbacks.length > 1) { |
| 17 console.error('Only support mocking function with at most one callback.'); |
| 18 return; |
| 19 } |
| 20 |
| 13 fn.recordCall(args); | 21 fn.recordCall(args); |
| 14 return this.returnValue; | 22 if (callbacks.length == 1) { |
| 23 callbacks[0].apply(undefined, fn.callbackData); |
| 24 return; |
| 25 } |
| 26 return fn.returnValue; |
| 15 }; | 27 }; |
| 16 | 28 |
| 17 /** | 29 /** |
| 18 * List of signatures for fucntion calls. | 30 * List of signatures for fucntion calls. |
| 19 * @type {!Array.<!Array>} | 31 * @type {!Array.<!Array>} |
| 20 * @private | 32 * @private |
| 21 */ | 33 */ |
| 22 fn.calls_ = []; | 34 fn.calls_ = []; |
| 23 | 35 |
| 24 /** | 36 /** |
| 25 * List of expected call signatures. | 37 * List of expected call signatures. |
| 26 * @type {!Array.<!Array>} | 38 * @type {!Array.<!Array>} |
| 27 * @private | 39 * @private |
| 28 */ | 40 */ |
| 29 fn.expectations_ = []; | 41 fn.expectations_ = []; |
| 30 | 42 |
| 31 /** | 43 /** |
| 32 * Value returned from call to function. | 44 * Value returned from call to function. |
| 33 * @type {*} | 45 * @type {*} |
| 34 */ | 46 */ |
| 35 fn.returnValue = undefined; | 47 fn.returnValue = undefined; |
| 36 | 48 |
| 49 /** |
| 50 * List of arguments for callback function. |
| 51 * @type {!Array.<!Array>} |
| 52 */ |
| 53 fn.callbackData = []; |
| 54 |
| 37 fn.__proto__ = MockMethod.prototype; | 55 fn.__proto__ = MockMethod.prototype; |
| 38 return fn; | 56 return fn; |
| 39 } | 57 } |
| 40 | 58 |
| 41 MockMethod.prototype = { | 59 MockMethod.prototype = { |
| 42 /** | 60 /** |
| 43 * Adds an expected call signature. | 61 * Adds an expected call signature. |
| 44 * @param {...} var_args Expected arguments for the function call. | 62 * @param {...} var_args Expected arguments for the function call. |
| 45 */ | 63 */ |
| 46 addExpectation: function() { | 64 addExpectation: function() { |
| 47 var args = Array.prototype.slice.call(arguments); | 65 var args = Array.prototype.slice.call(arguments); |
| 48 this.expectations_.push(args); | 66 this.expectations_.push(args.filter(this.notFunction_)); |
| 49 }, | 67 }, |
| 50 | 68 |
| 51 /** | 69 /** |
| 52 * Adds a call signature. | 70 * Adds a call signature. |
| 53 * @param {!Array} args. | 71 * @param {!Array} args. |
| 54 */ | 72 */ |
| 55 recordCall: function(args) { | 73 recordCall: function(args) { |
| 56 this.calls_.push(args); | 74 this.calls_.push(args.filter(this.notFunction_)); |
| 57 }, | 75 }, |
| 58 | 76 |
| 59 /** | 77 /** |
| 60 * Verifies that the function is called the expected number of times and with | 78 * Verifies that the function is called the expected number of times and with |
| 61 * the correct signature for each call. | 79 * the correct signature for each call. |
| 62 */ | 80 */ |
| 63 verifyMock: function() { | 81 verifyMock: function() { |
| 64 var errorMessage = 'Number of method calls did not match expectation.'; | 82 var errorMessage = 'Number of method calls did not match expectation.'; |
| 65 if (this.functionName) | 83 if (this.functionName) |
| 66 errorMessage = 'Error in ' + this.functionName + ':\n' + errorMessage; | 84 errorMessage = 'Error in ' + this.functionName + ':\n' + errorMessage; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 * Override if strict equality is not required. | 95 * Override if strict equality is not required. |
| 78 * @param {number} index Canonical index of the function call. Unused in the | 96 * @param {number} index Canonical index of the function call. Unused in the |
| 79 * base implementation, but provides context that may be useful for | 97 * base implementation, but provides context that may be useful for |
| 80 * overrides. | 98 * overrides. |
| 81 * @param {!Array} expected The expected arguments. | 99 * @param {!Array} expected The expected arguments. |
| 82 * @parma {!Array} observed The observed arguments. | 100 * @parma {!Array} observed The observed arguments. |
| 83 */ | 101 */ |
| 84 validateCall: function(index, expected, observed) { | 102 validateCall: function(index, expected, observed) { |
| 85 assertDeepEquals(expected, observed); | 103 assertDeepEquals(expected, observed); |
| 86 }, | 104 }, |
| 105 |
| 106 /** |
| 107 * Test if arg is a function. |
| 108 * @param {*} arg The argument to test. |
| 109 * @return True if arg is not function type. |
| 110 */ |
| 111 notFunction_: function(arg) { |
| 112 return typeof arg != 'function'; |
| 113 } |
| 87 }; | 114 }; |
| 88 | 115 |
| 89 /** | 116 /** |
| 90 * Controller for mocking methods. Tracks calls to mocked methods and verifies | 117 * Controller for mocking methods. Tracks calls to mocked methods and verifies |
| 91 * that call signatures match expectations. | 118 * that call signatures match expectations. |
| 92 * @constructor. | 119 * @constructor. |
| 93 */ | 120 */ |
| 94 function MockController() { | 121 function MockController() { |
| 95 /** | 122 /** |
| 96 * Original functions implementations, which are restored when |reset| is | 123 * Original functions implementations, which are restored when |reset| is |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 * Discard mocks reestoring default behavior. | 176 * Discard mocks reestoring default behavior. |
| 150 */ | 177 */ |
| 151 reset: function() { | 178 reset: function() { |
| 152 for (var i = 0; i < this.overrides_.length; i++) { | 179 for (var i = 0; i < this.overrides_.length; i++) { |
| 153 var override = this.overrides_[i]; | 180 var override = this.overrides_[i]; |
| 154 override.parent[override.functionName] = override.originalFunction; | 181 override.parent[override.functionName] = override.originalFunction; |
| 155 } | 182 } |
| 156 }, | 183 }, |
| 157 | 184 |
| 158 }; | 185 }; |
| OLD | NEW |