Permalink
Browse files

Allows matchers to be used for function call verifications.

R=vbhasin,nnaze,chrishenry
DELTA=66 (62 added, 0 deleted, 4 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6164


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2450 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
1 parent e3213b3 commit 5c34e8724532fb5e0c03bbfe67d01b9ee8804b3a jemunoz@google.com committed Jan 17, 2013
Showing with 65 additions and 4 deletions.
  1. +10 −4 closure/goog/labs/mock/mock.js
  2. +55 −0 closure/goog/labs/mock/mock_test.html
View
14 closure/goog/labs/mock/mock.js
@@ -224,7 +224,7 @@ goog.labs.mock.MockManager_.prototype.addBinding =
goog.labs.mock.MockManager_.prototype.findBinding =
function(methodName, args) {
var stub = goog.array.find(this.methodBindings, function(binding) {
- return binding.matches(methodName, args);
+ return binding.matches(methodName, args, false /* isVerification */);
});
return stub && stub.getStub();
};
@@ -295,7 +295,7 @@ goog.labs.mock.MockManager_.prototype.verifyInvocation =
function(methodName, var_args) {
var args = goog.array.slice(arguments, 1);
var binding = goog.array.find(this.callRecords_, function(binding) {
- return binding.matches(methodName, args);
+ return binding.matches(methodName, args, true /* isVerification */);
});
if (!binding) {
@@ -579,13 +579,19 @@ goog.labs.mock.MethodBinding_.prototype.getStub = function() {
*
* @param {string} methodName The name of the method being stubbed.
* @param {!Array} args An array of arguments.
+ * @param {boolean} isVerification Whether this is a function verification call
+ * or not.
* @return {boolean} If it matches the stored arguments.
*/
-goog.labs.mock.MethodBinding_.prototype.matches = function(methodName, args) {
+goog.labs.mock.MethodBinding_.prototype.matches = function(
+ methodName, args, isVerification) {
+ var specs = isVerification ? args : this.args_;
+ var calls = isVerification ? this.args_ : args;
+
//TODO(user): More elaborate argument matching. Think about matching
// objects.
return this.methodName_ == methodName &&
- goog.array.equals(args, this.args_, function(arg, spec) {
+ goog.array.equals(calls, specs, function(arg, spec) {
// Duck-type to see if this is an object that implements the
// goog.labs.testing.Matcher interface.
if (goog.isFunction(spec.matches)) {
View
55 closure/goog/labs/mock/mock_test.html
@@ -13,6 +13,7 @@
goog.require('goog.labs.mock');
goog.require('goog.testing.jsunit');
+goog.require('goog.labs.testing.AnythingMatcher');
goog.require('goog.labs.testing.GreaterThanMatcher');
goog.require('goog.labs.testing.LessThanMatcher');
@@ -174,6 +175,7 @@
goog.labs.mock.when(mockFunc)(2).thenReturn(55);
assertEquals(55, mockFunc(2));
goog.labs.mock.verify(mockFunc)(2);
+ goog.labs.mock.verify(mockFunc)(lessThan(3));
var e = assertThrows(goog.bind(goog.labs.mock.verify(mockFunc), 3));
assertTrue(e instanceof goog.labs.mock.VerificationError);
@@ -201,6 +203,59 @@
assertUndefined(mockObj.method1(4));
}
+function testMatcherVerify() {
+ var obj = {
+ method: function(i) {
+ return 2 * i;
+ }
+ };
+
+ // Using spy objects.
+ var spy = goog.labs.mock.spy(obj);
+
+ spy.method(6);
+
+ goog.labs.mock.verify(spy).method(greaterThan(4));
+ var e = assertThrows(goog.bind(goog.labs.mock.verify(spy).method, lessThan(4)));
+ assertTrue(e instanceof goog.labs.mock.VerificationError);
+
+ // Using mocks
+ var mockObj = goog.labs.mock(obj);
+
+ mockObj.method(8);
+
+ goog.labs.mock.verify(mockObj).method(greaterThan(7))
+ var e = assertThrows(goog.bind(goog.labs.mock.verify(mockObj).method, lessThan(7)));
+ assertTrue(e instanceof goog.labs.mock.VerificationError);
+}
+
+function testMatcherVerifyColission() {
+ var obj = {
+ method: function(i) {
+ return 2 * i;
+ }
+ };
+ var mockObj = goog.labs.mock(obj);
+
+ goog.labs.mock.when(mockObj).method(5).thenReturn(100);
+ assertNotEquals(100, mockObj.method(greaterThan(2)));
+}
+
+function testMatcherVerifyColissionBetweenMatchers() {
+ var obj = {
+ method: function(i) {
+ return 2 * i;
+ }
+ };
+ var mockObj = goog.labs.mock(obj);
+
+ goog.labs.mock.when(mockObj).method(anything()).thenReturn(100);
+
+ var e = assertThrows(
+ goog.bind(goog.labs.mock.verify(mockObj).method, anything()));
+ assertTrue(e instanceof goog.labs.mock.VerificationError);
+}
+
function testVerifyForUnmockedMethods() {
var Task = function() {};
Task.prototype.run = function() {};

0 comments on commit 5c34e87

Please sign in to comment.