verificationmode.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2016 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides an interface that defines how users can extend the
  16. * {@code goog.labs.mock} mocking framework with custom verification.
  17. *
  18. * In addition to the interface definition, it contains several static
  19. * factories for creating common implementations of the interface.
  20. */
  21. goog.provide('goog.labs.mock.verification');
  22. goog.provide('goog.labs.mock.verification.VerificationMode');
  23. /**
  24. * A mode which defines how mock invocations should be verified.
  25. * When an instance of {@code VerificationMode} is passed to
  26. * {@code goog.labs.mock.verify}, then that instances's {@code #verify}
  27. * method will be used to verify the invocation.
  28. *
  29. * If {@code #verify} returns false, then the test will fail and the
  30. * description returned from {@code #describe} will be shown in the
  31. * test failure message. Sample usage:
  32. *
  33. * goog.module('my.package.MyClassTest');
  34. * goog.setTestOnly('my.package.MyClassTest');
  35. *
  36. * var testSuite = goog.require('goog.testing.testSuite');
  37. * var verification = goog.require('goog.labs.mock.verification');
  38. *
  39. * var times = verification.times;
  40. *
  41. * testSuite({
  42. * setUp: function() {
  43. * // Code creating instances of MyClass and mockObj.
  44. * },
  45. *
  46. * testMyMethod_shouldDoSomething: function() {
  47. * myClassInstance.myMethod();
  48. *
  49. * goog.labs.mock.verify(mockObj, times(1));
  50. * }
  51. * });
  52. *
  53. * For an example implementation, see {@code TimesVerificationMode_}.
  54. *
  55. * @interface
  56. */
  57. goog.labs.mock.verification.VerificationMode = function() {};
  58. /**
  59. * Returns true if the recorded number of invocations,
  60. * {@code actualNumberOfInvocations}, meets the expectations of this mode.
  61. *
  62. * TODO(user): Have this take in an object which contains the complete
  63. * call record in order to allow more interesting verifications.
  64. *
  65. * @param {number} actualNumberOfInvocations
  66. * @return {boolean}
  67. */
  68. goog.labs.mock.verification.VerificationMode.prototype.verify =
  69. goog.abstractMethod;
  70. /**
  71. * Returns a description of what this VerificationMode expected.
  72. *
  73. * @return {string}
  74. */
  75. goog.labs.mock.verification.VerificationMode.prototype.describe =
  76. goog.abstractMethod;
  77. /**
  78. * Returns a {@code VerificationMode} which verifies a method was called
  79. * exactly {@code expectedNumberOfInvocations} times.
  80. *
  81. * @param {number} expectedNumberOfInvocations
  82. * @return {!goog.labs.mock.verification.VerificationMode}
  83. */
  84. goog.labs.mock.verification.times = function(expectedNumberOfInvocations) {
  85. return new goog.labs.mock.verification.TimesVerificationMode_(
  86. expectedNumberOfInvocations);
  87. };
  88. /**
  89. * Returns a {@code VerificationMode} which verifies a method was called at
  90. * least {@code minimumNumberOfInvocations} times.
  91. *
  92. * @param {number} minimumNumberOfInvocations
  93. * @return {!goog.labs.mock.verification.VerificationMode}
  94. */
  95. goog.labs.mock.verification.atLeast = function(minimumNumberOfInvocations) {
  96. return new goog.labs.mock.verification.AtLeastVerificationMode_(
  97. minimumNumberOfInvocations);
  98. };
  99. /**
  100. * Returns a {@code VerificationMode} which verifies a method was called at
  101. * most {@code maxNumberOfInvocations} times.
  102. *
  103. * @param {number} maxNumberOfInvocations
  104. * @return {!goog.labs.mock.verification.VerificationMode}
  105. */
  106. goog.labs.mock.verification.atMost = function(maxNumberOfInvocations) {
  107. return new goog.labs.mock.verification.AtMostVerificationMode_(
  108. maxNumberOfInvocations);
  109. };
  110. /**
  111. * Returns a {@code VerificationMode} which verifies a method was never
  112. * called. An alias for {@code VerificatonMode.times(0)}.
  113. *
  114. * @return {!goog.labs.mock.verification.VerificationMode}
  115. */
  116. goog.labs.mock.verification.never = function() {
  117. return goog.labs.mock.verification.times(0);
  118. };
  119. /**
  120. * A {@code VerificationMode} which verifies a method was called
  121. * exactly {@code expectedNumberOfInvocations} times.
  122. *
  123. * @private @implements {goog.labs.mock.verification.VerificationMode}
  124. */
  125. goog.labs.mock.verification.TimesVerificationMode_ = goog.defineClass(null, {
  126. /**
  127. * @param {number} expectedNumberOfInvocations
  128. * @constructor
  129. */
  130. constructor: function(expectedNumberOfInvocations) {
  131. /** @private */
  132. this.expectedNumberOfInvocations_ = expectedNumberOfInvocations;
  133. },
  134. /** @override */
  135. verify: function(actualNumberOfInvocations) {
  136. return actualNumberOfInvocations == this.expectedNumberOfInvocations_;
  137. },
  138. /** @override */
  139. describe: function() { return this.expectedNumberOfInvocations_ + ' times'; }
  140. });
  141. /**
  142. * A {@code VerificationMode} which verifies a method was called at
  143. * least {@code minimumNumberOfInvocations} times.
  144. *
  145. * @private @implements {goog.labs.mock.verification.VerificationMode}
  146. */
  147. goog.labs.mock.verification.AtLeastVerificationMode_ = goog.defineClass(null, {
  148. /**
  149. * @param {number} minimumNumberOfInvocations
  150. * @constructor
  151. */
  152. constructor: function(minimumNumberOfInvocations) {
  153. /** @private */
  154. this.minimumNumberOfInvocations_ = minimumNumberOfInvocations;
  155. },
  156. /** @override */
  157. verify: function(actualNumberOfInvocations) {
  158. return actualNumberOfInvocations >= this.minimumNumberOfInvocations_;
  159. },
  160. /** @override */
  161. describe: function() {
  162. return 'at least ' + this.minimumNumberOfInvocations_ + ' times';
  163. }
  164. });
  165. /**
  166. * A {@code VerificationMode} which verifies a method was called at
  167. * most {@code maxNumberOfInvocations} times.
  168. *
  169. * @private @implements {goog.labs.mock.verification.VerificationMode}
  170. */
  171. goog.labs.mock.verification.AtMostVerificationMode_ = goog.defineClass(null, {
  172. /**
  173. * @param {number} maxNumberOfInvocations
  174. * @constructor
  175. */
  176. constructor: function(maxNumberOfInvocations) {
  177. /** @private */
  178. this.maxNumberOfInvocations_ = maxNumberOfInvocations;
  179. },
  180. /** @override */
  181. verify: function(actualNumberOfInvocations) {
  182. return actualNumberOfInvocations <= this.maxNumberOfInvocations_;
  183. },
  184. /** @override */
  185. describe: function() {
  186. return 'at most ' + this.maxNumberOfInvocations_ + ' times';
  187. }
  188. });