ts-mockito
Advanced tools
Comparing version 2.2.2 to 2.2.3
import { Matcher } from "./type/Matcher"; | ||
export declare class ArgsToMatchersValidator { | ||
validate(matchers: Array<Matcher>, args: any[]): boolean; | ||
validate(matchers: Matcher[], args: any[]): boolean; | ||
} |
@@ -10,12 +10,3 @@ "use strict"; | ||
} | ||
var allValid = true; | ||
var index = 0; | ||
for (var _i = 0, args_1 = args; _i < args_1.length; _i++) { | ||
var arg = args_1[_i]; | ||
if (!matchers[index].match(arg)) { | ||
allValid = false; | ||
} | ||
index++; | ||
} | ||
return allValid; | ||
return matchers.every(function (matcher, index) { return matcher.match(args[index]); }); | ||
}; | ||
@@ -22,0 +13,0 @@ return ArgsToMatchersValidator; |
import { Matcher } from "./matcher/type/Matcher"; | ||
export declare class MethodAction { | ||
methodName: string; | ||
args: Array<any>; | ||
args: any[]; | ||
private static globalCallIndex; | ||
private callIndex; | ||
constructor(methodName: string, args: Array<any>); | ||
constructor(methodName: string, args: any[]); | ||
isApplicable(methodName: string, matchers: Matcher[]): boolean; | ||
@@ -9,0 +9,0 @@ getCallIndex(): number; |
@@ -10,17 +10,9 @@ "use strict"; | ||
MethodAction.prototype.isApplicable = function (methodName, matchers) { | ||
var _this = this; | ||
var methodNameMatch = this.methodName === methodName; | ||
var argumentsCountMatch = this.args.length == matchers.length; | ||
var argumentsCountMatch = this.args.length === matchers.length; | ||
if (!methodNameMatch || !argumentsCountMatch) { | ||
return false; | ||
} | ||
var allValid = true; | ||
var index = 0; | ||
for (var _i = 0, _a = this.args; _i < _a.length; _i++) { | ||
var arg = _a[_i]; | ||
if (matchers[index] && !matchers[index].match(arg)) { | ||
allValid = false; | ||
} | ||
index++; | ||
} | ||
return allValid; | ||
return matchers.every(function (matcher, index) { return matcher.match(_this.args[index]); }); | ||
}; | ||
@@ -27,0 +19,0 @@ MethodAction.prototype.getCallIndex = function () { |
@@ -8,2 +8,3 @@ import { MethodStub } from "./stub/MethodStub"; | ||
hasMatchingInAnyGroup(args: any[]): boolean; | ||
private removeIfNotLast(groupIndex, args); | ||
private getFirstMatchingFromGroup(groupIndex, args); | ||
@@ -10,0 +11,0 @@ private getFirstMatchingIndexFromGroup(groupIndex, args); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var MethodStubCollection = (function () { | ||
@@ -11,56 +12,27 @@ function MethodStubCollection() { | ||
MethodStubCollection.prototype.getLastMatchingGroupIndex = function (args) { | ||
for (var i = this.items.length - 1; i >= 0; i--) { | ||
var item = this.items[i]; | ||
if (item.isApplicable(args)) { | ||
return item.getGroupIndex(); | ||
} | ||
} | ||
return -1; | ||
var matchingGroup = _.clone(this.items).reverse().find(function (item) { return item.isApplicable(args); }); | ||
return matchingGroup ? matchingGroup.getGroupIndex() : -1; | ||
}; | ||
MethodStubCollection.prototype.getFirstMatchingFromGroupAndRemoveIfNotLast = function (groupIndex, args) { | ||
var index = this.getFirstMatchingIndexFromGroup(groupIndex, args); | ||
var result = this.getFirstMatchingFromGroup(groupIndex, args); | ||
if (index > -1 && this.getItemsCountInGroup(groupIndex) > 1) { | ||
this.items.splice(index, 1); | ||
} | ||
this.removeIfNotLast(groupIndex, args); | ||
return result; | ||
}; | ||
MethodStubCollection.prototype.hasMatchingInAnyGroup = function (args) { | ||
for (var _i = 0, _a = this.items; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
if (item.isApplicable(args)) { | ||
return true; | ||
} | ||
return this.items.some(function (item) { return item.isApplicable(args); }); | ||
}; | ||
MethodStubCollection.prototype.removeIfNotLast = function (groupIndex, args) { | ||
var index = this.getFirstMatchingIndexFromGroup(groupIndex, args); | ||
if (index > -1 && this.getItemsCountInGroup(groupIndex) > 1) { | ||
this.items.splice(index, 1); | ||
} | ||
return false; | ||
}; | ||
MethodStubCollection.prototype.getFirstMatchingFromGroup = function (groupIndex, args) { | ||
for (var _i = 0, _a = this.items; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { | ||
return item; | ||
} | ||
} | ||
return null; | ||
return this.items.find(function (item) { return item.getGroupIndex() === groupIndex && item.isApplicable(args); }); | ||
}; | ||
MethodStubCollection.prototype.getFirstMatchingIndexFromGroup = function (groupIndex, args) { | ||
var index = 0; | ||
for (var _i = 0, _a = this.items; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { | ||
return index; | ||
} | ||
index++; | ||
} | ||
return -1; | ||
return this.items.findIndex(function (item) { return item.getGroupIndex() === groupIndex && item.isApplicable(args); }); | ||
}; | ||
MethodStubCollection.prototype.getItemsCountInGroup = function (groupIndex) { | ||
var result = 0; | ||
for (var _i = 0, _a = this.items; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
if (item.getGroupIndex() === groupIndex) { | ||
result++; | ||
} | ||
} | ||
return result; | ||
return this.items.filter(function (item) { return item.getGroupIndex() === groupIndex; }).length; | ||
}; | ||
@@ -67,0 +39,0 @@ return MethodStubCollection; |
@@ -6,6 +6,6 @@ import { Matcher } from "./matcher/type/Matcher"; | ||
methodStubCollection: MethodStubCollection; | ||
matchers: Array<Matcher>; | ||
matchers: Matcher[]; | ||
mocker: Mocker; | ||
name: string; | ||
constructor(methodStubCollection: MethodStubCollection, matchers: Array<Matcher>, mocker: Mocker, name: string); | ||
constructor(methodStubCollection: MethodStubCollection, matchers: Matcher[], mocker: Mocker, name: string); | ||
} |
import { Matcher } from "./matcher/type/Matcher"; | ||
import { MethodAction } from "./MethodAction"; | ||
import { MethodStub } from "./stub/MethodStub"; | ||
import { ObjectInspector } from "./utils/ObjectInspector"; | ||
export declare class Mocker { | ||
private clazz; | ||
protected instance: any; | ||
protected objectInspector: ObjectInspector; | ||
private methodStubCollections; | ||
private methodActions; | ||
private mock; | ||
private redundantMethodNameInCodeFinder; | ||
private subKeysInCodeFinder; | ||
private mockableFunctionsFinder; | ||
private objectPropertyCodeRetriever; | ||
constructor(clazz: any, instance?: any); | ||
getMock(): any; | ||
createCatchAllHandlerForRemainingPropertiesWithoutGetters(): ProxyHandler<any>; | ||
createCatchAllHandlerForRemainingPropertiesWithoutGetters(): any; | ||
reset(): void; | ||
@@ -20,19 +22,13 @@ resetCalls(): void; | ||
getActionsByName(name: string): MethodAction[]; | ||
protected createMethodStubsFromOwnProperties(prototype?: any): void; | ||
protected createInstanceActionListenersFromOwnPropertyDescriptors(prototype?: any): void; | ||
protected processProperties(object: any): void; | ||
protected createInstancePropertyDescriptorListener(key: string, descriptor: PropertyDescriptor, prototype: any): void; | ||
protected createInstanceActionListenersFromOwnPropertyNames(prototype?: any): void; | ||
protected createInstanceActionListener(key: string, prototype: any): void; | ||
protected createActionListener(key: string): () => any; | ||
protected getEmptyMethodStub(key: string, args: any[]): MethodStub; | ||
private createMethodStubsFromPrototypeKeys(); | ||
private createMethodStubsFromClassCode(); | ||
private createMethodStubsFromFunctionsCode(); | ||
private processClassCode(clazz); | ||
private processFunctionsCode(object); | ||
private createPropertyStub(key); | ||
private createMethodStub(key); | ||
private createMethodToStub(key); | ||
private createInstanceActionListenersFromPrototypeKeys(); | ||
private createInstanceActionListenersFromClassCode(); | ||
private createInstanceActionListenersFromFunctionsCode(); | ||
private getMethodStub(key, args); | ||
} |
144
lib/Mock.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./matcher/type/Matcher"); | ||
@@ -9,4 +10,5 @@ var MethodAction_1 = require("./MethodAction"); | ||
var ts_mockito_1 = require("./ts-mockito"); | ||
var PrototypeKeyCodeGetter_1 = require("./utils/PrototypeKeyCodeGetter"); | ||
var RedundantMethodNameInCodeFinder_1 = require("./utils/RedundantMethodNameInCodeFinder"); | ||
var MockableFunctionsFinder_1 = require("./utils/MockableFunctionsFinder"); | ||
var ObjectInspector_1 = require("./utils/ObjectInspector"); | ||
var ObjectPropertyCodeRetriever_1 = require("./utils/ObjectPropertyCodeRetriever"); | ||
var Mocker = (function () { | ||
@@ -17,18 +19,15 @@ function Mocker(clazz, instance) { | ||
this.instance = instance; | ||
this.objectInspector = new ObjectInspector_1.ObjectInspector(); | ||
this.methodStubCollections = {}; | ||
this.methodActions = []; | ||
this.mock = {}; | ||
this.redundantMethodNameInCodeFinder = new RedundantMethodNameInCodeFinder_1.RedundantMethodNameInCodeFinder(); | ||
this.subKeysInCodeFinder = new PrototypeKeyCodeGetter_1.PrototypeKeyCodeGetter(); | ||
this.mockableFunctionsFinder = new MockableFunctionsFinder_1.MockableFunctionsFinder(); | ||
this.objectPropertyCodeRetriever = new ObjectPropertyCodeRetriever_1.ObjectPropertyCodeRetriever(); | ||
this.mock.__tsmockitoInstance = this.instance; | ||
this.mock.__tsmockitoMocker = this; | ||
this.createMethodStubsFromOwnProperties(); | ||
this.createMethodStubsFromPrototypeKeys(); | ||
this.createMethodStubsFromClassCode(); | ||
this.createMethodStubsFromFunctionsCode(); | ||
this.createInstanceActionListenersFromOwnPropertyDescriptors(); | ||
this.createInstanceActionListenersFromOwnPropertyNames(); | ||
this.createInstanceActionListenersFromPrototypeKeys(); | ||
this.createInstanceActionListenersFromClassCode(); | ||
this.createInstanceActionListenersFromFunctionsCode(); | ||
if (_.isObject(this.clazz) && _.isObject(this.instance)) { | ||
this.processProperties(this.clazz.prototype); | ||
this.processClassCode(this.clazz); | ||
this.processFunctionsCode(this.clazz.prototype); | ||
} | ||
} | ||
@@ -51,3 +50,3 @@ Mocker.prototype.getMock = function () { | ||
return target[name]; | ||
} | ||
}, | ||
}; | ||
@@ -77,42 +76,18 @@ }; | ||
}; | ||
Mocker.prototype.createMethodStubsFromOwnProperties = function (prototype) { | ||
Mocker.prototype.processProperties = function (object) { | ||
var _this = this; | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
try { | ||
while (prototype !== Object.prototype) { | ||
Object.getOwnPropertyNames(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name); | ||
if (!descriptor) { | ||
return; | ||
} | ||
if (descriptor.get) { | ||
_this.createPropertyStub(name); | ||
} | ||
else { | ||
_this.createMethodStub(name); | ||
} | ||
}); | ||
prototype = Object.getPrototypeOf(prototype); | ||
} | ||
} | ||
catch (error) { | ||
} | ||
this.objectInspector.getObjectPrototypes(object).forEach(function (obj) { | ||
_this.objectInspector.getObjectOwnPropertyNames(obj).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, name); | ||
if (descriptor.get) { | ||
_this.createPropertyStub(name); | ||
_this.createInstancePropertyDescriptorListener(name, descriptor, obj); | ||
} | ||
else { | ||
_this.createMethodStub(name); | ||
} | ||
_this.createInstanceActionListener(name, obj); | ||
}); | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromOwnPropertyDescriptors = function (prototype) { | ||
var _this = this; | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
try { | ||
while (prototype !== Object.prototype) { | ||
Object.getOwnPropertyNames(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name); | ||
if (descriptor && descriptor.get) { | ||
_this.createInstancePropertyDescriptorListener(name, descriptor, prototype); | ||
} | ||
}); | ||
prototype = Object.getPrototypeOf(prototype); | ||
} | ||
} | ||
catch (error) { | ||
} | ||
}; | ||
Mocker.prototype.createInstancePropertyDescriptorListener = function (key, descriptor, prototype) { | ||
@@ -126,16 +101,2 @@ if (this.instance.hasOwnProperty(key)) { | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromOwnPropertyNames = function (prototype) { | ||
var _this = this; | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
try { | ||
while (prototype !== Object.prototype) { | ||
Object.getOwnPropertyNames(prototype).forEach(function (name) { | ||
_this.createInstanceActionListener(name, prototype); | ||
}); | ||
prototype = Object.getPrototypeOf(prototype); | ||
} | ||
} | ||
catch (error) { | ||
} | ||
}; | ||
Mocker.prototype.createInstanceActionListener = function (key, prototype) { | ||
@@ -164,21 +125,20 @@ if (this.instance.hasOwnProperty(key)) { | ||
}; | ||
Mocker.prototype.createMethodStubsFromPrototypeKeys = function () { | ||
Mocker.prototype.processClassCode = function (clazz) { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
_this.createMethodStub(key); | ||
var classCode = typeof clazz.toString !== "undefined" ? clazz.toString() : ""; | ||
var functionNames = this.mockableFunctionsFinder.find(classCode); | ||
functionNames.forEach(function (functionName) { | ||
_this.createMethodStub(functionName); | ||
_this.createInstanceActionListener(functionName, _this.clazz.prototype); | ||
}); | ||
}; | ||
Mocker.prototype.createMethodStubsFromClassCode = function () { | ||
Mocker.prototype.processFunctionsCode = function (object) { | ||
var _this = this; | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.toString()); | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createMethodStub(subKey); | ||
}); | ||
}; | ||
Mocker.prototype.createMethodStubsFromFunctionsCode = function () { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
var subKeys = _this.redundantMethodNameInCodeFinder.find(_this.subKeysInCodeFinder.get(_this.clazz.prototype, key)); | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createMethodStub(subKey); | ||
this.objectInspector.getObjectPrototypes(object).forEach(function (obj) { | ||
_this.objectInspector.getObjectOwnPropertyNames(obj).forEach(function (propertyName) { | ||
var functionNames = _this.mockableFunctionsFinder.find(_this.objectPropertyCodeRetriever.get(obj, propertyName)); | ||
functionNames.forEach(function (functionName) { | ||
_this.createMethodStub(functionName); | ||
_this.createInstanceActionListener(functionName, _this.clazz.prototype); | ||
}); | ||
}); | ||
@@ -224,24 +184,2 @@ }); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromPrototypeKeys = function () { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
_this.createInstanceActionListener(key, _this.clazz.prototype); | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromClassCode = function () { | ||
var _this = this; | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.toString()); | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createInstanceActionListener(subKey, _this.clazz.prototype); | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromFunctionsCode = function () { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
var subKeys = _this.redundantMethodNameInCodeFinder.find(_this.subKeysInCodeFinder.get(_this.clazz.prototype, key)); | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createInstanceActionListener(subKey, _this.clazz.prototype); | ||
}); | ||
}); | ||
}; | ||
Mocker.prototype.getMethodStub = function (key, args) { | ||
@@ -248,0 +186,0 @@ var methodStub = this.methodStubCollections[key]; |
@@ -21,5 +21,5 @@ "use strict"; | ||
var _this = _super.call(this, instance.constructor, instance) || this; | ||
_this.createMethodStubsFromOwnProperties(instance); | ||
_this.createInstanceActionListenersFromOwnPropertyDescriptors(instance); | ||
_this.createInstanceActionListenersFromOwnPropertyNames(instance); | ||
if (_.isObject(instance)) { | ||
_this.processProperties(instance); | ||
} | ||
return _this; | ||
@@ -26,0 +26,0 @@ } |
import { MethodToStub } from "../MethodToStub"; | ||
export declare class MethodCallToStringConverter { | ||
private matchersToStringConverter; | ||
convert(method: MethodToStub): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var MatchersToStringConverter_1 = require("../matcher/MatchersToStringConverter"); | ||
var MethodCallToStringConverter = (function () { | ||
function MethodCallToStringConverter() { | ||
this.matchersToStringConverter = new MatchersToStringConverter_1.MatchersToStringConverter(); | ||
} | ||
MethodCallToStringConverter.prototype.convert = function (method) { | ||
var matchersAsString = this.matchersToStringConverter.convert(method.matchers); | ||
return method.name + "(" + matchersAsString + ")\" "; | ||
var stringifiedMatchers = method.matchers.map(function (matcher) { return matcher.toString(); }).join(", "); | ||
return method.name + "(" + stringifiedMatchers + ")\" "; | ||
}; | ||
@@ -12,0 +10,0 @@ return MethodCallToStringConverter; |
{ | ||
"name": "ts-mockito", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "Mocking library for TypeScript", | ||
@@ -29,13 +29,13 @@ "main": "lib/ts-mockito.js", | ||
"devDependencies": { | ||
"@types/jest": "^20.0.8", | ||
"@types/lodash": "^4.14.34", | ||
"@types/node": "^6.0.38", | ||
"jest": "^21.1.0", | ||
"ts-helpers": "^1.1.1", | ||
"@types/jest": "^21.1.1", | ||
"@types/lodash": "^4.14.76", | ||
"@types/node": "^8.0.31", | ||
"jest": "^21.2.1", | ||
"ts-helpers": "^1.1.2", | ||
"ts-jest": "^21.0.1", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.4.0" | ||
"typescript": "^2.5.3" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
"lodash": "^4.17.4" | ||
}, | ||
@@ -52,4 +52,12 @@ "jest": { | ||
"json" | ||
], | ||
"mapCoverage": true, | ||
"collectCoverage": true, | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
], | ||
"coverageReporters": [ | ||
"lcov" | ||
] | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# ts-mockito [![build badge](https://travis-ci.org/NagRock/ts-mockito.svg?branch=master)](https://travis-ci.org/NagRock/ts-mockito) | ||
# ts-mockito [![build badge](https://travis-ci.org/NagRock/ts-mockito.svg?branch=master)](https://travis-ci.org/NagRock/ts-mockito) [![codecov](https://codecov.io/gh/NagRock/ts-mockito/branch/master/graph/badge.svg)](https://codecov.io/gh/NagRock/ts-mockito) | ||
@@ -3,0 +3,0 @@ Mocking library for TypeScript inspired by http://mockito.org/ |
@@ -19,5 +19,6 @@ { | ||
"node", | ||
"lodash" | ||
"lodash", | ||
"jest" | ||
] | ||
} | ||
} | ||
} |
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
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107830
1533
Updatedlodash@^4.17.4