@fluffy-spoon/substitute
Advanced tools
Comparing version 1.0.55 to 1.0.56
@@ -39,2 +39,14 @@ "use strict"; | ||
}); | ||
ava_1.default('class method received', function (t) { | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "the1re"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
t.notThrows(function () { return substitute.received(4).c('hi', 'there'); }); | ||
t.notThrows(function () { return substitute.received(1).c('hi', 'the1re'); }); | ||
t.notThrows(function () { return substitute.received().c('hi', 'there'); }); | ||
var err = t.throws(function () { return substitute.received(7).c('hi', 'there'); }); | ||
t.deepEqual(err.message, "Expected 7 calls to the method c with arguments [hi, there], but received 4 of such calls.\nAll calls received to method c:\n-> 4 calls with arguments [hi, there]\n-> 1 call with arguments [hi, the1re]"); | ||
}); | ||
ava_1.default('are arguments equal', function (t) { | ||
@@ -112,12 +124,2 @@ t.true(Utilities_1.areArgumentsEqual(Index_1.Arg.any(), 'hi')); | ||
}); | ||
ava_1.default('class method received', function (t) { | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "the1re"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
t.throws(function () { return substitute.received(7).c('hi', 'there'); }); | ||
t.notThrows(function () { return substitute.received(4).c('hi', 'there'); }); | ||
t.notThrows(function () { return substitute.received().c('hi', 'there'); }); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
@@ -42,5 +42,6 @@ export declare abstract class ProxyPropertyContextBase { | ||
findActualPropertyCalls(propertyName: string): ProxyCallRecord[]; | ||
findActualMethodCalls(propertyName: string, args: any[]): ProxyCallRecord[]; | ||
findActualMethodCalls(propertyName: string, args?: any[]): ProxyCallRecord[]; | ||
getLastCall(): ProxyCallRecord; | ||
addActualPropertyCall(): void; | ||
fixExistingCallArguments(): void; | ||
} | ||
@@ -50,3 +51,4 @@ export declare class ProxyCallRecord { | ||
property: ProxyPropertyContext | ProxyMethodPropertyContext; | ||
argumentsSnapshot: any[]; | ||
constructor(property?: ProxyPropertyContext | ProxyMethodPropertyContext); | ||
} |
@@ -94,5 +94,7 @@ "use strict"; | ||
.filter(function (x) { | ||
if (args === void 0) | ||
return true; | ||
if (x.property.type !== 'function') | ||
return false; | ||
var args1 = x.property.method.arguments; | ||
var args1 = x.argumentsSnapshot; | ||
var args2 = args; | ||
@@ -130,3 +132,3 @@ if (!args1 || !args2) | ||
return x.property.type === thisProperty.type && | ||
Utilities_1.areArgumentsEqual(x.property.method.arguments, thisProperty.method.arguments); | ||
Utilities_1.areArgumentsEqual(x.argumentsSnapshot, thisProperty.method.arguments); | ||
})[0]; | ||
@@ -142,3 +144,13 @@ } | ||
existingCall.callCount++; | ||
this.fixExistingCallArguments(); | ||
}; | ||
ProxyObjectContext.prototype.fixExistingCallArguments = function () { | ||
var actualCalls = this.calls.actual; | ||
for (var _i = 0, _a = actualCalls.slice(); _i < _a.length; _i++) { | ||
var existingCall = _a[_i]; | ||
var existingCallProperty = existingCall.property; | ||
if (existingCallProperty.type === 'function' && existingCall.argumentsSnapshot === null) | ||
existingCall.argumentsSnapshot = existingCallProperty.method.arguments; | ||
} | ||
}; | ||
return ProxyObjectContext; | ||
@@ -151,2 +163,3 @@ }()); | ||
this.property = property || null; | ||
this.argumentsSnapshot = property && property.type === 'function' ? property.method.arguments : null; | ||
} | ||
@@ -153,0 +166,0 @@ return ProxyCallRecord; |
@@ -17,2 +17,3 @@ "use strict"; | ||
var existingCall = existingCalls[0]; | ||
var allCalls = objectContext.findActualMethodCalls(propertyContext.name); | ||
if (propertyContext.type === 'function') { | ||
@@ -23,3 +24,3 @@ var expected = objectContext.calls.expected; | ||
expected.propertyName = propertyContext.name; | ||
_this.assertCallMatchCount('method', expected, existingCalls); | ||
_this.assertCallMatchCount('method', expected, allCalls, existingCalls); | ||
return void 0; | ||
@@ -35,4 +36,7 @@ } | ||
} | ||
if (!existingCall) | ||
else { | ||
propertyContext.method.arguments = argumentsList; | ||
objectContext.addActualPropertyCall(); | ||
return void 0; | ||
} | ||
if (propertyContext.method.returnValues) | ||
@@ -45,2 +49,3 @@ return propertyContext.method.returnValues[existingCall.callCount - 1]; | ||
newMethodPropertyContext.method.returnValues = null; | ||
objectContext.fixExistingCallArguments(); | ||
return thisProxy; | ||
@@ -57,3 +62,3 @@ }, | ||
expected.propertyName = propertyContext.name; | ||
_this.assertCallMatchCount('property', expected, existingCalls); | ||
_this.assertCallMatchCount('property', expected, objectContext.findActualMethodCalls(propertyContext.name), existingCalls); | ||
return true; | ||
@@ -136,3 +141,3 @@ } | ||
expected.propertyName = existingCallProperty.name; | ||
_this.assertCallMatchCount('property', expected, [existingCall]); | ||
_this.assertCallMatchCount('property', expected, [existingCall], [existingCall]); | ||
return thisProxy; | ||
@@ -158,8 +163,13 @@ } | ||
}; | ||
Substitute.assertCallMatchCount = function (type, expected, existingCalls) { | ||
var existingCallCount = existingCalls.map(function (x) { return x.callCount; }).reduce(function (accumulator, value) { return accumulator + value; }); | ||
var isMatch = !((!expected.negated && ((expected.callCount === null && existingCallCount === 0) || | ||
(expected.callCount !== null && expected.callCount !== existingCallCount))) || | ||
(expected.negated && ((expected.callCount === null && existingCallCount !== 0) || | ||
(expected.callCount !== null && expected.callCount === existingCallCount)))); | ||
Substitute.assertCallMatchCount = function (type, expected, allCalls, matchingCalls) { | ||
var getCallCounts = function (calls) { | ||
var callCounts = calls.map(function (x) { return x.callCount; }); | ||
var totalCallCount = callCounts.length === 0 ? 0 : callCounts.reduce(function (accumulator, value) { return accumulator + value; }); | ||
return totalCallCount; | ||
}; | ||
var matchingCallsCount = getCallCounts(matchingCalls); | ||
var isMatch = !((!expected.negated && ((expected.callCount === null && matchingCallsCount === 0) || | ||
(expected.callCount !== null && expected.callCount !== matchingCallsCount))) || | ||
(expected.negated && ((expected.callCount === null && matchingCallsCount !== 0) || | ||
(expected.callCount !== null && expected.callCount === matchingCallsCount)))); | ||
if (!isMatch) { | ||
@@ -188,3 +198,3 @@ var errorMessage = ''; | ||
else if (type === 'method') { | ||
errorMessage += ' with arguments '; | ||
errorMessage += ' with '; | ||
errorMessage += Utilities_1.stringifyArguments(expected.arguments); | ||
@@ -194,15 +204,15 @@ } | ||
errorMessage += ', but received '; | ||
errorMessage += existingCallCount === 0 ? 'none' : existingCallCount; | ||
errorMessage += matchingCallsCount === 0 ? 'none' : matchingCallsCount; | ||
if (expected.arguments) { | ||
errorMessage += ' of such call'; | ||
errorMessage += existingCallCount !== 1 ? 's' : ''; | ||
errorMessage += matchingCallsCount !== 1 ? 's' : ''; | ||
} | ||
errorMessage += '.'; | ||
if (expected.arguments) { | ||
errorMessage += '\nCalls received to '; | ||
errorMessage += '\nAll calls received to '; | ||
errorMessage += type; | ||
errorMessage += ' '; | ||
errorMessage += expected.propertyName; | ||
errorMessage += ' in general: '; | ||
errorMessage += Utilities_1.stringifyCalls(existingCalls); | ||
errorMessage += ':'; | ||
errorMessage += Utilities_1.stringifyCalls(allCalls); | ||
} | ||
@@ -209,0 +219,0 @@ throw new Error(errorMessage); |
@@ -5,3 +5,3 @@ "use strict"; | ||
function stringifyArguments(args) { | ||
return args && args.length > 0 ? '[' + args + ']' : '(no arguments)'; | ||
return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments'; | ||
} | ||
@@ -13,3 +13,3 @@ exports.stringifyArguments = stringifyArguments; | ||
if (calls.length === 0) | ||
return '(no calls)'; | ||
return ' (no calls)'; | ||
var output = ''; | ||
@@ -21,3 +21,3 @@ for (var _i = 0, calls_1 = calls; _i < calls_1.length; _i++) { | ||
if (call.property.type === 'function') | ||
output += ' with arguments ' + stringifyArguments(call.property.method.arguments); | ||
output += ' with ' + stringifyArguments(call.argumentsSnapshot); | ||
} | ||
@@ -24,0 +24,0 @@ return output; |
{ | ||
"name": "@fluffy-spoon/substitute", | ||
"version": "1.0.55", | ||
"version": "1.0.56", | ||
"description": "An NSubstitute port to TypeScript called substitute.js.", | ||
@@ -16,3 +16,3 @@ "main": "dist/src/Index.js", | ||
"ava": "^0.25.0", | ||
"ts-node": "^7.0.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.0.0" | ||
@@ -19,0 +19,0 @@ }, |
@@ -32,2 +32,21 @@ import test from 'ava'; | ||
test('class method received', t => { | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "the1re"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
t.notThrows(() => substitute.received(4).c('hi', 'there')); | ||
t.notThrows(() => substitute.received(1).c('hi', 'the1re')); | ||
t.notThrows(() => substitute.received().c('hi', 'there')); | ||
const err: Error = t.throws(() => substitute.received(7).c('hi', 'there')); | ||
t.deepEqual(err.message, | ||
`Expected 7 calls to the method c with arguments [hi, there], but received 4 of such calls. | ||
All calls received to method c: | ||
-> 4 calls with arguments [hi, there] | ||
-> 1 call with arguments [hi, the1re]`); | ||
}); | ||
test('are arguments equal', t => { | ||
@@ -123,14 +142,2 @@ t.true(areArgumentsEqual(Arg.any(), 'hi')); | ||
t.notThrows(() => substitute.received(2).v = Arg.is(x => x && x.indexOf('ll') > -1)); | ||
}); | ||
test('class method received', t => { | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "the1re"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
void substitute.c("hi", "there"); | ||
t.throws(() => substitute.received(7).c('hi', 'there')); | ||
t.notThrows(() => substitute.received(4).c('hi', 'there')); | ||
t.notThrows(() => substitute.received().c('hi', 'there')); | ||
}); |
@@ -100,3 +100,3 @@ import { areArgumentsEqual } from "./Utilities"; | ||
findActualMethodCalls(propertyName: string, args: any[]) { | ||
findActualMethodCalls(propertyName: string, args?: any[]) { | ||
let result = this.calls | ||
@@ -106,5 +106,8 @@ .actual | ||
.filter(x => { | ||
if(args === void 0) | ||
return true; | ||
if(x.property.type !== 'function') return false; | ||
const args1 = x.property.method.arguments; | ||
const args1 = x.argumentsSnapshot; | ||
const args2 = args; | ||
@@ -151,3 +154,3 @@ | ||
x.property.type === thisProperty.type && | ||
areArgumentsEqual(x.property.method.arguments, thisProperty.method.arguments))[0]; | ||
areArgumentsEqual(x.argumentsSnapshot, thisProperty.method.arguments))[0]; | ||
} else { | ||
@@ -163,3 +166,14 @@ existingCall = existingCallCandidates[0]; | ||
existingCall.callCount++; | ||
this.fixExistingCallArguments(); | ||
} | ||
fixExistingCallArguments() { | ||
const actualCalls = this.calls.actual; | ||
for(let existingCall of [...actualCalls]) { | ||
const existingCallProperty = existingCall.property; | ||
if(existingCallProperty.type === 'function' && existingCall.argumentsSnapshot === null) | ||
existingCall.argumentsSnapshot = existingCallProperty.method.arguments; | ||
} | ||
} | ||
} | ||
@@ -169,3 +183,5 @@ | ||
callCount: number; | ||
property: ProxyPropertyContext | ProxyMethodPropertyContext; | ||
argumentsSnapshot: any[]; | ||
@@ -175,3 +191,4 @@ constructor(property?: ProxyPropertyContext | ProxyMethodPropertyContext) { | ||
this.property = property || null; | ||
this.argumentsSnapshot = property && property.type === 'function' ? property.method.arguments : null; | ||
} | ||
} |
@@ -18,2 +18,4 @@ import { ObjectSubstitute } from "./Transformations"; | ||
const allCalls = objectContext.findActualMethodCalls(propertyContext.name); | ||
if (propertyContext.type === 'function') { | ||
@@ -25,3 +27,5 @@ const expected = objectContext.calls.expected; | ||
this.assertCallMatchCount('method', expected, existingCalls); | ||
this.assertCallMatchCount('method', expected, | ||
allCalls, | ||
existingCalls); | ||
return void 0; | ||
@@ -38,6 +42,8 @@ } | ||
} | ||
} | ||
} else { | ||
propertyContext.method.arguments = argumentsList; | ||
objectContext.addActualPropertyCall(); | ||
if(!existingCall) | ||
return void 0; | ||
} | ||
@@ -54,2 +60,4 @@ if(propertyContext.method.returnValues) | ||
objectContext.fixExistingCallArguments(); | ||
return thisProxy; | ||
@@ -68,3 +76,5 @@ }, | ||
this.assertCallMatchCount('property', expected, existingCalls); | ||
this.assertCallMatchCount('property', expected, | ||
objectContext.findActualMethodCalls(propertyContext.name), | ||
existingCalls); | ||
return true; | ||
@@ -167,3 +177,3 @@ } | ||
this.assertCallMatchCount('property', expected, [existingCall]); | ||
this.assertCallMatchCount('property', expected, [existingCall], [existingCall]); | ||
return thisProxy; | ||
@@ -198,4 +208,16 @@ } | ||
private static assertCallMatchCount(type: 'property' | 'method', expected: ProxyExpectation, existingCalls: ProxyCallRecord[]): void { | ||
const existingCallCount = existingCalls.map(x => x.callCount).reduce((accumulator, value) => accumulator + value); | ||
private static assertCallMatchCount( | ||
type: 'property' | 'method', | ||
expected: ProxyExpectation, | ||
allCalls: ProxyCallRecord[], | ||
matchingCalls: ProxyCallRecord[]): void | ||
{ | ||
const getCallCounts = (calls: ProxyCallRecord[]) => { | ||
const callCounts = calls.map(x => x.callCount); | ||
const totalCallCount = callCounts.length === 0 ? 0 : callCounts.reduce((accumulator, value) => accumulator + value); | ||
return totalCallCount; | ||
} | ||
const matchingCallsCount = getCallCounts(matchingCalls); | ||
const isMatch = | ||
@@ -205,4 +227,4 @@ !( | ||
!expected.negated && ( | ||
(expected.callCount === null && existingCallCount === 0) || | ||
(expected.callCount !== null && expected.callCount !== existingCallCount) | ||
(expected.callCount === null && matchingCallsCount === 0) || | ||
(expected.callCount !== null && expected.callCount !== matchingCallsCount) | ||
) | ||
@@ -212,4 +234,4 @@ ) || | ||
expected.negated && ( | ||
(expected.callCount === null && existingCallCount !== 0) || | ||
(expected.callCount !== null && expected.callCount === existingCallCount) | ||
(expected.callCount === null && matchingCallsCount !== 0) || | ||
(expected.callCount !== null && expected.callCount === matchingCallsCount) | ||
) | ||
@@ -246,3 +268,3 @@ ) | ||
} else if(type === 'method') { | ||
errorMessage += ' with arguments '; | ||
errorMessage += ' with '; | ||
errorMessage += stringifyArguments(expected.arguments); | ||
@@ -253,7 +275,7 @@ } | ||
errorMessage += ', but received '; | ||
errorMessage += existingCallCount === 0 ? 'none' : existingCallCount; | ||
errorMessage += matchingCallsCount === 0 ? 'none' : matchingCallsCount; | ||
if(expected.arguments) { | ||
errorMessage += ' of such call'; | ||
errorMessage += existingCallCount !== 1 ? 's' : ''; | ||
errorMessage += matchingCallsCount !== 1 ? 's' : ''; | ||
} | ||
@@ -264,8 +286,8 @@ | ||
if(expected.arguments) { | ||
errorMessage += '\nCalls received to '; | ||
errorMessage += '\nAll calls received to '; | ||
errorMessage += type; | ||
errorMessage += ' '; | ||
errorMessage += expected.propertyName; | ||
errorMessage += ' in general: '; | ||
errorMessage += stringifyCalls(existingCalls); | ||
errorMessage += ':'; | ||
errorMessage += stringifyCalls(allCalls); | ||
} | ||
@@ -272,0 +294,0 @@ |
@@ -5,3 +5,3 @@ import { ProxyCallRecord } from "./Context"; | ||
export function stringifyArguments(args: any[]) { | ||
return args && args.length > 0 ? '[' + args + ']' : '(no arguments)'; | ||
return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments'; | ||
}; | ||
@@ -13,3 +13,3 @@ | ||
if(calls.length === 0) | ||
return '(no calls)'; | ||
return ' (no calls)'; | ||
@@ -22,3 +22,3 @@ let output = ''; | ||
if(call.property.type === 'function') | ||
output += ' with arguments ' + stringifyArguments(call.property.method.arguments); | ||
output += ' with ' + stringifyArguments(call.argumentsSnapshot); | ||
} | ||
@@ -25,0 +25,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
115196
1340