Comparing version 0.1.6 to 1.0.0
{ | ||
"name": "a_mock", | ||
"version": "0.1.6", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "title": "a_mock", |
@@ -5,2 +5,3 @@ function create(originalFunc) { | ||
var expectAnything = require('./partialMock/expectAnything'); | ||
var ignore = require('./partialMock/ignore'); | ||
var expectArray = require('./partialMock/expectArray'); | ||
@@ -38,3 +39,9 @@ var verify = require('./partialMock/verify'); | ||
mock.ignore = mock.expectAnything; | ||
mock.ignore = function() { | ||
negotiateEnd(); | ||
mockContext.compositeAreCorrectArguments = newEmptyAnd(); | ||
var args = [0,mockContext]; | ||
return ignore.apply(null,args); | ||
}; | ||
mock.reset = mockContext.reset; | ||
@@ -41,0 +48,0 @@ |
function expectAnything(index,mockContext) { | ||
var newHasArgument = require('./newHasArgument'); | ||
var expectCore = require('./expectCore'); | ||
var expect = require('./expect'); | ||
var expectAnything = require('./expectAnything'); | ||
var expectArray = require('./expectArray'); | ||
var _return = require('./return'); | ||
var newThrow = require('./newThrow'); | ||
var hasCorrectArgument = require('../and'); | ||
mockContext.whenCalledEmitter = require('../eventEmitter')(); | ||
var hasArgument = newHasArgument(index); | ||
return expectCore(hasArgument,index,mockContext); | ||
var c = {}; | ||
mockContext.compositeAreCorrectArguments.add(hasCorrectArgument); | ||
mockContext.expectAnything = true; | ||
mockContext.numberOfArgs = index+1; | ||
c.expect = function() { | ||
var args = [index+1,mockContext]; | ||
for (var i = 0; i < arguments.length; i++) { | ||
args.push(arguments[i]); | ||
}; | ||
return expect.apply(null,args); | ||
}; | ||
c.return = function(arg) { | ||
return _return(arg,index+1,mockContext); | ||
}; | ||
c.whenCalled = function(callback) { | ||
mockContext.whenCalledEmitter.add(callback); | ||
return c; | ||
}; | ||
c.repeat = function(times) { | ||
return _return(undefined,index+1,mockContext).repeat(times); | ||
}; | ||
c.throw = function(error) { | ||
var _throw = newThrow(error); | ||
return c.whenCalled(_throw); | ||
}; | ||
c.repeatAny = function() { | ||
return _return(undefined,index+1,mockContext).repeatAny(); | ||
}; | ||
return c; | ||
} | ||
module.exports = expectAnything; |
@@ -6,4 +6,6 @@ var assert = require('assert'); | ||
var expectCore = newRequireMock('./expectCore'); | ||
var newHasArgument = newRequireMock('./newHasArgument'); | ||
var _return = newRequireMock('./return'); | ||
var newWhenCalledEmitter = newRequireMock('../eventEmitter'); | ||
var newThrow = newRequireMock('./newThrow'); | ||
var hasCorrectArgument = newRequireMock('../and'); | ||
@@ -16,23 +18,146 @@ var sut = require('../expectAnything'); | ||
var mockContext = {}; | ||
var hasArgument = {}; | ||
var index = {}; | ||
var expected = {}; | ||
var compositeAreCorrectArguments = newMock(); | ||
var whenCalledEmitter = {} ; | ||
var index = 1; | ||
var didAdd; | ||
stubHasArgument(); | ||
stubExpectCore(); | ||
stubWhenCalledEmitter(); | ||
stubMockContext(); | ||
stubCompositeAreCorrectArguments(); | ||
function stubHasArgument() { | ||
newHasArgument.expect(index).return(hasArgument); | ||
function stubWhenCalledEmitter() { | ||
newWhenCalledEmitter.expect().return(whenCalledEmitter); | ||
} | ||
function stubExpectCore() { | ||
expectCore.expect(hasArgument).expect(index).expect(mockContext).return(expected); | ||
function stubMockContext() { | ||
mockContext.compositeAreCorrectArguments = compositeAreCorrectArguments; | ||
} | ||
var returned = sut(index,mockContext); | ||
function stubCompositeAreCorrectArguments() { | ||
var add = newMock(); | ||
add.expect(hasCorrectArgument).whenCalled(onAdd).return(null); | ||
compositeAreCorrectArguments.add = add; | ||
} | ||
test('it should return expected from expectCore',function() { | ||
assert.equal(expected,returned); | ||
function onAdd() { | ||
didAdd = true; | ||
} | ||
var sut2 = sut(index,mockContext); | ||
test('it should set mockContext.NumberOfArgs to index+1',function() { | ||
assert.equal(mockContext.numberOfArgs,index+1); | ||
}); | ||
test('it should set mockContext.expectAnything',function() { | ||
assert(mockContext.expectAnything); | ||
}); | ||
test('it should add areSameArgument to compositeAreCorrectArguments',function() { | ||
assert.ok(didAdd); | ||
}); | ||
test('it should set mockContext.whenCalledEmitter',function() { | ||
assert.equal(mockContext.whenCalledEmitter,whenCalledEmitter); | ||
}); | ||
(function() { | ||
console.log('return'); | ||
var expected = {}; | ||
var arg = {}; | ||
_return.expect(arg).expect(index+1).expect(mockContext).return(expected); | ||
var returned = sut2.return(arg); | ||
test('should return expected',function() { | ||
assert.equal(returned,expected) | ||
}); | ||
})(); | ||
(function() { | ||
console.log('whenCalled'); | ||
var callback = {}; | ||
var expected; | ||
var add = newMock(); | ||
whenCalledEmitter.add = add; | ||
var didAddCallback; | ||
add.expect(callback).whenCalled(onAddCallback).return(); | ||
function onAddCallback() { | ||
didAddCallback = true; | ||
} | ||
mockContext.whenCalledEmitter = whenCalledEmitter; | ||
var returned = sut2.whenCalled(callback); | ||
test('should add callback to whenCalledEmitter',function() { | ||
assert.ok(didAddCallback) | ||
}); | ||
test('should return self',function() { | ||
assert.equal(returned,sut2); | ||
}); | ||
})(); | ||
(function() { | ||
console.log('throw'); | ||
var error = {}; | ||
var _throw = {}; | ||
var expected = {}; | ||
newThrow.expect(error).return(_throw); | ||
sut2.whenCalled = newMock(); | ||
sut2.whenCalled.expect(_throw).return(expected); | ||
var returned = sut2.throw(error); | ||
test('should return expected',function() { | ||
assert.equal(expected,returned); | ||
}); | ||
})(); | ||
(function() { | ||
console.log('repeat'); | ||
var expected = {}; | ||
var times = 2; | ||
var returnContext = {}; | ||
var repeatMock = newMock(); | ||
_return.expectAnything().expect(index+1).expect(mockContext).whenCalled(onReturn).return(returnContext); | ||
function onReturn(returnValue) { | ||
if (returnValue !== undefined) | ||
throw new Error('unexpected argument'); | ||
} | ||
returnContext.repeat = repeatMock; | ||
repeatMock.expect(times).return(expected); | ||
var returned = sut2.repeat(times); | ||
test('should return expected',function() { | ||
assert.equal(returned,expected) | ||
}); | ||
})(); | ||
(function() { | ||
console.log('repeatAny'); | ||
var expected = {}; | ||
var returnContext = {}; | ||
var repeatMock = newMock(); | ||
_return.expectAnything().expect(index+1).expect(mockContext).whenCalled(onReturn).return(returnContext); | ||
function onReturn(returnValue) { | ||
if (returnValue !== undefined) | ||
throw new Error('unexpected argument'); | ||
} | ||
returnContext.repeatAny = repeatMock; | ||
repeatMock.expect().return(expected); | ||
var returned = sut2.repeatAny(); | ||
test('should return expected',function() { | ||
assert.equal(returned,expected) | ||
}); | ||
})(); | ||
})(); |
function expect(hasCorrectArgument,index,mockContext) { | ||
var expect = require('./expect'); | ||
var expectAnything = require('./expectAnything'); | ||
var ignore = require('./ignore'); | ||
var expectArray = require('./expectArray'); | ||
@@ -26,3 +27,5 @@ var _return = require('./return'); | ||
c.ignore = c.expectAnything; | ||
c.ignore = function() { | ||
return ignore(index+1,mockContext); | ||
} | ||
@@ -29,0 +32,0 @@ c.expectArray = function(array) { |
@@ -8,2 +8,3 @@ var assert = require('assert'); | ||
var expectAnything = newRequireMock('./expectAnything'); | ||
var ignore = newRequireMock('./ignore'); | ||
var expectArray = newRequireMock('./expectArray'); | ||
@@ -61,6 +62,2 @@ var _return = newRequireMock('./return'); | ||
test('should set ignore as alias for expectAnything', function() { | ||
assert.equal(sut2.ignore,sut2.expectAnything); | ||
}); | ||
(function() { | ||
@@ -92,2 +89,14 @@ console.log('expect'); | ||
(function() { | ||
console.log('ignore'); | ||
var expected = {}; | ||
var arg = {}; | ||
ignore.expect(index+1).expect(mockContext).return(expected); | ||
var returned = sut2.ignore(); | ||
test('should return expected',function() { | ||
assert.equal(returned,expected) | ||
}); | ||
})(); | ||
(function() { | ||
console.log('expectArray'); | ||
@@ -94,0 +103,0 @@ var expected = {}; |
@@ -6,3 +6,3 @@ var newThrow = require('./newThrow'); | ||
mockContext.whenCalledEmitter = require('../eventEmitter')(); | ||
var hasCorrectArgument = require('./newHasNoMoreArguments')(0); | ||
var hasCorrectArgument = require('./newHasNoMoreArguments')(0, mockContext); | ||
@@ -9,0 +9,0 @@ var c = {}; |
@@ -29,3 +29,3 @@ var assert = require('assert'); | ||
function stubHasNoMoreArguments() { | ||
newHasNoMoreArguments.expect(index).return(hasCorrectArgument); | ||
newHasNoMoreArguments.expect(index).expect(mockContext).return(hasCorrectArgument); | ||
} | ||
@@ -32,0 +32,0 @@ |
@@ -1,4 +0,10 @@ | ||
function _new(maxLength) { | ||
function hasNoMoreArguments(){ | ||
var empty = require('../and'); | ||
function _new(maxLength, context) { | ||
if (context.expectAnything) { | ||
context.expectAnything = false; | ||
return empty; | ||
} | ||
function hasNoMoreArguments(){ | ||
return (arguments.length == maxLength); | ||
@@ -5,0 +11,0 @@ } |
@@ -10,35 +10,56 @@ var assert = require('assert'); | ||
(function() { | ||
console.log('newHasNoMoreArguments'); | ||
var length = 1; | ||
console.log('newHasNoMoreArguments'); | ||
var length = 1; | ||
var mockContext = {}; | ||
var sut = newSut(length); | ||
(function() { | ||
console.log('two arguments too much.execute'); | ||
var returned = sut('somearg','c','d'); | ||
(function() { | ||
console.log('two arguments too much.execute'); | ||
mockContext.expectAnything = true; | ||
var sut = newSut(length, mockContext); | ||
var returned = sut('somearg', 'c', 'd'); | ||
test('it should return false',function() { | ||
assert.equal(false,returned); | ||
}); | ||
})(); | ||
test('it should return true', function() { | ||
assert.equal(true, returned); | ||
}); | ||
(function() { | ||
console.log('one argument too much.execute'); | ||
var returned = sut('somearg','c'); | ||
})(); | ||
test('it should return false',function() { | ||
assert.equal(false,returned); | ||
}); | ||
})(); | ||
(function() { | ||
console.log('two arguments too much, but expectAnything.execute'); | ||
mockContext.expectAnything = false; | ||
var sut = newSut(length, mockContext); | ||
(function() { | ||
console.log('correct number of arguments.execute'); | ||
var returned = sut('a'); | ||
var returned = sut('somearg', 'c', 'd'); | ||
test('it should return true',function() { | ||
assert.equal(true,returned); | ||
}); | ||
})(); | ||
test('it should return false', function() { | ||
assert.equal(false, returned); | ||
}); | ||
})(); | ||
(function() { | ||
console.log('one argument too much.execute'); | ||
mockContext.expectAnything = false; | ||
var sut = newSut(length, mockContext); | ||
var returned = sut('somearg', 'c'); | ||
test('it should return false', function() { | ||
assert.equal(false, returned); | ||
}); | ||
})(); | ||
(function() { | ||
console.log('correct number of arguments.execute'); | ||
mockContext.expectAnything = false; | ||
var sut = newSut(length, mockContext); | ||
var returned = sut('a'); | ||
test('it should return true', function() { | ||
assert.equal(true, returned); | ||
}); | ||
})(); | ||
})(); |
@@ -7,3 +7,3 @@ function _return(returnValue,index,mockContext) { | ||
mockContext.numberOfArgs = undefined; | ||
var hasNoMoreArguments = newHasNoMoreArguments(index); | ||
var hasNoMoreArguments = newHasNoMoreArguments(index, mockContext); | ||
var hasCorrectArgs = mockContext.compositeAreCorrectArguments.add(hasNoMoreArguments); | ||
@@ -10,0 +10,0 @@ setExecute(returnValue,hasCorrectArgs,mockContext,oneTime); |
@@ -27,3 +27,3 @@ var assert = require('assert'); | ||
function stubHasNoMoreArguments() { | ||
newHasNoMoreArguments.expect(index).return(hasNoMoreArguments); | ||
newHasNoMoreArguments.expect(index).expect(mockContext).return(hasNoMoreArguments); | ||
} | ||
@@ -30,0 +30,0 @@ |
@@ -7,2 +7,3 @@ var test = require('../test'); | ||
var expectAnything = newRequireMock('./partialMock/expectAnything'); | ||
var ignore = newRequireMock('./partialMock/ignore'); | ||
var expect = newRequireMock('./partialMock/expect'); | ||
@@ -39,6 +40,2 @@ var newMockContext = newRequireMock('./partialMock/newMockContext'); | ||
test('should set ignore as alias for expectAnything', function() { | ||
assert.equal(sut.ignore, sut.expectAnything); | ||
}); | ||
test('reset points at mockContext.reset', function() { | ||
@@ -163,2 +160,29 @@ assert.equal(sut.reset, mockContext.reset); | ||
(function() { | ||
console.log('when ignore'); | ||
var expected = {}; | ||
var didNegotiateEnd; | ||
mockContext.compositeAreCorrectArguments = null; | ||
newMutableAnd.expect().return(mutableAnd); | ||
ignore.expect(0).expect(mockContext).return(expected); | ||
negotiateEnd.expect(mockContext).whenCalled(onNegotiateEnd).return(); | ||
var returned = sut.ignore(); | ||
function onNegotiateEnd() { | ||
didNegotiateEnd = true; | ||
} | ||
test('should negotiateEnd', function() { | ||
assert.ok(didNegotiateEnd); | ||
}); | ||
test('should set mockContext.compositeAreCorrectArguments to mutableAnd',function() { | ||
assert.equal(mutableAnd,mockContext.compositeAreCorrectArguments); | ||
}); | ||
test('should return expected',function() { | ||
assert.equal(expected,returned) | ||
}); | ||
})(); | ||
(function() { | ||
console.log('when verify'); | ||
@@ -165,0 +189,0 @@ var expected = {}; |
@@ -234,3 +234,3 @@ var assert = require('assert'); | ||
test('it should return expected for arg, anything, ignore',function() { | ||
test('it should return expected for arg, anything twice',function() { | ||
var arg = 'a'; | ||
@@ -243,8 +243,27 @@ var arg2 = 'b'; | ||
sut.expect(arg).expectAnything().ignore().return(expected); | ||
sut.expect(arg).expectAnything().return(expected).repeat(2); | ||
var returned = sut(arg,arg2,arg3); | ||
var returned2 = sut(arg,arg2,arg3); | ||
var returned2 = sut(arg,arg2); | ||
var returned3 = sut('foo',arg2); | ||
assert.equal(returned,expected); | ||
assert.equal(returned2,expected); | ||
assert.equal(returned3,fallbackValue); | ||
assert.ok(sut.verify()); | ||
}); | ||
test('it should return expected for arg, ignore twice',function() { | ||
var arg = 'a'; | ||
var arg2 = 'b'; | ||
var expected = {}; | ||
var sut = newSut(fallback); | ||
sut.expect(arg).ignore().expect(arg2).return(expected).repeat(2); | ||
var returned = sut(arg,'foo',arg2); | ||
var returned2 = sut(arg,'bar',arg2, 'a'); | ||
var returned3 = sut(arg,{},arg2); | ||
assert.equal(returned,expected); | ||
assert.equal(returned2,fallbackValue); | ||
assert.equal(returned3,expected); | ||
assert.ok(sut.verify()); | ||
@@ -251,0 +270,0 @@ }); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
135111
126
4319
1