Path: blob/trunk/third_party/closure/goog/labs/mock/verificationmode.js
2868 views
// Copyright 2016 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides an interface that defines how users can extend the16* {@code goog.labs.mock} mocking framework with custom verification.17*18* In addition to the interface definition, it contains several static19* factories for creating common implementations of the interface.20*/21goog.provide('goog.labs.mock.verification');22goog.provide('goog.labs.mock.verification.VerificationMode');23242526/**27* A mode which defines how mock invocations should be verified.28* When an instance of {@code VerificationMode} is passed to29* {@code goog.labs.mock.verify}, then that instances's {@code #verify}30* method will be used to verify the invocation.31*32* If {@code #verify} returns false, then the test will fail and the33* description returned from {@code #describe} will be shown in the34* test failure message. Sample usage:35*36* goog.module('my.package.MyClassTest');37* goog.setTestOnly('my.package.MyClassTest');38*39* var testSuite = goog.require('goog.testing.testSuite');40* var verification = goog.require('goog.labs.mock.verification');41*42* var times = verification.times;43*44* testSuite({45* setUp: function() {46* // Code creating instances of MyClass and mockObj.47* },48*49* testMyMethod_shouldDoSomething: function() {50* myClassInstance.myMethod();51*52* goog.labs.mock.verify(mockObj, times(1));53* }54* });55*56* For an example implementation, see {@code TimesVerificationMode_}.57*58* @interface59*/60goog.labs.mock.verification.VerificationMode = function() {};616263/**64* Returns true if the recorded number of invocations,65* {@code actualNumberOfInvocations}, meets the expectations of this mode.66*67* TODO(user): Have this take in an object which contains the complete68* call record in order to allow more interesting verifications.69*70* @param {number} actualNumberOfInvocations71* @return {boolean}72*/73goog.labs.mock.verification.VerificationMode.prototype.verify =74goog.abstractMethod;757677/**78* Returns a description of what this VerificationMode expected.79*80* @return {string}81*/82goog.labs.mock.verification.VerificationMode.prototype.describe =83goog.abstractMethod;848586/**87* Returns a {@code VerificationMode} which verifies a method was called88* exactly {@code expectedNumberOfInvocations} times.89*90* @param {number} expectedNumberOfInvocations91* @return {!goog.labs.mock.verification.VerificationMode}92*/93goog.labs.mock.verification.times = function(expectedNumberOfInvocations) {94return new goog.labs.mock.verification.TimesVerificationMode_(95expectedNumberOfInvocations);96};979899/**100* Returns a {@code VerificationMode} which verifies a method was called at101* least {@code minimumNumberOfInvocations} times.102*103* @param {number} minimumNumberOfInvocations104* @return {!goog.labs.mock.verification.VerificationMode}105*/106goog.labs.mock.verification.atLeast = function(minimumNumberOfInvocations) {107return new goog.labs.mock.verification.AtLeastVerificationMode_(108minimumNumberOfInvocations);109};110111112/**113* Returns a {@code VerificationMode} which verifies a method was called at114* most {@code maxNumberOfInvocations} times.115*116* @param {number} maxNumberOfInvocations117* @return {!goog.labs.mock.verification.VerificationMode}118*/119goog.labs.mock.verification.atMost = function(maxNumberOfInvocations) {120return new goog.labs.mock.verification.AtMostVerificationMode_(121maxNumberOfInvocations);122};123124125/**126* Returns a {@code VerificationMode} which verifies a method was never127* called. An alias for {@code VerificatonMode.times(0)}.128*129* @return {!goog.labs.mock.verification.VerificationMode}130*/131goog.labs.mock.verification.never = function() {132return goog.labs.mock.verification.times(0);133};134135136/**137* A {@code VerificationMode} which verifies a method was called138* exactly {@code expectedNumberOfInvocations} times.139*140* @private @implements {goog.labs.mock.verification.VerificationMode}141*/142goog.labs.mock.verification.TimesVerificationMode_ = goog.defineClass(null, {143/**144* @param {number} expectedNumberOfInvocations145* @constructor146*/147constructor: function(expectedNumberOfInvocations) {148/** @private */149this.expectedNumberOfInvocations_ = expectedNumberOfInvocations;150},151152/** @override */153verify: function(actualNumberOfInvocations) {154return actualNumberOfInvocations == this.expectedNumberOfInvocations_;155},156157/** @override */158describe: function() { return this.expectedNumberOfInvocations_ + ' times'; }159});160161162/**163* A {@code VerificationMode} which verifies a method was called at164* least {@code minimumNumberOfInvocations} times.165*166* @private @implements {goog.labs.mock.verification.VerificationMode}167*/168goog.labs.mock.verification.AtLeastVerificationMode_ = goog.defineClass(null, {169/**170* @param {number} minimumNumberOfInvocations171* @constructor172*/173constructor: function(minimumNumberOfInvocations) {174/** @private */175this.minimumNumberOfInvocations_ = minimumNumberOfInvocations;176},177178/** @override */179verify: function(actualNumberOfInvocations) {180return actualNumberOfInvocations >= this.minimumNumberOfInvocations_;181},182183/** @override */184describe: function() {185return 'at least ' + this.minimumNumberOfInvocations_ + ' times';186}187});188189190/**191* A {@code VerificationMode} which verifies a method was called at192* most {@code maxNumberOfInvocations} times.193*194* @private @implements {goog.labs.mock.verification.VerificationMode}195*/196goog.labs.mock.verification.AtMostVerificationMode_ = goog.defineClass(null, {197/**198* @param {number} maxNumberOfInvocations199* @constructor200*/201constructor: function(maxNumberOfInvocations) {202/** @private */203this.maxNumberOfInvocations_ = maxNumberOfInvocations;204},205206/** @override */207verify: function(actualNumberOfInvocations) {208return actualNumberOfInvocations <= this.maxNumberOfInvocations_;209},210211/** @override */212describe: function() {213return 'at most ' + this.maxNumberOfInvocations_ + ' times';214}215});216217218