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() { |
(...skipping 10 matching lines...) Expand all Loading... |
21 fn.recordCall(args); | 21 fn.recordCall(args); |
22 if (callbacks.length == 1) { | 22 if (callbacks.length == 1) { |
23 callbacks[0].apply(undefined, fn.callbackData); | 23 callbacks[0].apply(undefined, fn.callbackData); |
24 return; | 24 return; |
25 } | 25 } |
26 return fn.returnValue; | 26 return fn.returnValue; |
27 }; | 27 }; |
28 | 28 |
29 /** | 29 /** |
30 * List of signatures for fucntion calls. | 30 * List of signatures for fucntion calls. |
31 * @type {!Array.<!Array>} | 31 * @type {!Array<!Array>} |
32 * @private | 32 * @private |
33 */ | 33 */ |
34 fn.calls_ = []; | 34 fn.calls_ = []; |
35 | 35 |
36 /** | 36 /** |
37 * List of expected call signatures. | 37 * List of expected call signatures. |
38 * @type {!Array.<!Array>} | 38 * @type {!Array<!Array>} |
39 * @private | 39 * @private |
40 */ | 40 */ |
41 fn.expectations_ = []; | 41 fn.expectations_ = []; |
42 | 42 |
43 /** | 43 /** |
44 * Value returned from call to function. | 44 * Value returned from call to function. |
45 * @type {*} | 45 * @type {*} |
46 */ | 46 */ |
47 fn.returnValue = undefined; | 47 fn.returnValue = undefined; |
48 | 48 |
49 /** | 49 /** |
50 * List of arguments for callback function. | 50 * List of arguments for callback function. |
51 * @type {!Array.<!Array>} | 51 * @type {!Array<!Array>} |
52 */ | 52 */ |
53 fn.callbackData = []; | 53 fn.callbackData = []; |
54 | 54 |
55 fn.__proto__ = MockMethod.prototype; | 55 fn.__proto__ = MockMethod.prototype; |
56 return fn; | 56 return fn; |
57 } | 57 } |
58 | 58 |
59 MockMethod.prototype = { | 59 MockMethod.prototype = { |
60 /** | 60 /** |
61 * Adds an expected call signature. | 61 * Adds an expected call signature. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 115 |
116 /** | 116 /** |
117 * Controller for mocking methods. Tracks calls to mocked methods and verifies | 117 * Controller for mocking methods. Tracks calls to mocked methods and verifies |
118 * that call signatures match expectations. | 118 * that call signatures match expectations. |
119 * @constructor. | 119 * @constructor. |
120 */ | 120 */ |
121 function MockController() { | 121 function MockController() { |
122 /** | 122 /** |
123 * Original functions implementations, which are restored when |reset| is | 123 * Original functions implementations, which are restored when |reset| is |
124 * called. | 124 * called. |
125 * @type {!Array.<!Object>} | 125 * @type {!Array<!Object>} |
126 * @private | 126 * @private |
127 */ | 127 */ |
128 this.overrides_ = []; | 128 this.overrides_ = []; |
129 | 129 |
130 /** | 130 /** |
131 * List of registered mocks. | 131 * List of registered mocks. |
132 * @type {!Array.<!MockMethod>} | 132 * @type {!Array<!MockMethod>} |
133 * @private | 133 * @private |
134 */ | 134 */ |
135 this.mocks_ = []; | 135 this.mocks_ = []; |
136 } | 136 } |
137 | 137 |
138 MockController.prototype = { | 138 MockController.prototype = { |
139 /** | 139 /** |
140 * Creates a mock function. | 140 * Creates a mock function. |
141 * @param {Object=} opt_parent Optional parent object for the function. | 141 * @param {Object=} opt_parent Optional parent object for the function. |
142 * @param {string=} opt_functionName Optional name of the function being | 142 * @param {string=} opt_functionName Optional name of the function being |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 * Discard mocks reestoring default behavior. | 176 * Discard mocks reestoring default behavior. |
177 */ | 177 */ |
178 reset: function() { | 178 reset: function() { |
179 for (var i = 0; i < this.overrides_.length; i++) { | 179 for (var i = 0; i < this.overrides_.length; i++) { |
180 var override = this.overrides_[i]; | 180 var override = this.overrides_[i]; |
181 override.parent[override.functionName] = override.originalFunction; | 181 override.parent[override.functionName] = override.originalFunction; |
182 } | 182 } |
183 }, | 183 }, |
184 | 184 |
185 }; | 185 }; |
OLD | NEW |