ts-mockito
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -1,4 +0,4 @@ | ||
import { Matcher } from './type/Matcher'; | ||
import { Matcher } from "./type/Matcher"; | ||
export declare class ArgsToMatchersValidator { | ||
validate(matchers: Array<Matcher>, args: any[]): boolean; | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
ArgsToMatchersValidator.prototype.validate = function (matchers, args) { | ||
if (matchers.length != args.length) { | ||
if (matchers.length !== args.length) { | ||
return false; | ||
@@ -10,0 +10,0 @@ } |
@@ -1,4 +0,4 @@ | ||
import { Matcher } from './type/Matcher'; | ||
import { Matcher } from "./type/Matcher"; | ||
export declare class MatchersToStringConverter { | ||
convert(matchers: Array<Matcher>): string; | ||
} |
@@ -13,4 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./Matcher"); | ||
var _ = require("lodash"); | ||
var AnyFunctionMatcher = (function (_super) { | ||
@@ -17,0 +17,0 @@ __extends(AnyFunctionMatcher, _super); |
@@ -13,4 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./Matcher"); | ||
var _ = require("lodash"); | ||
var AnyNumberMatcher = (function (_super) { | ||
@@ -17,0 +17,0 @@ __extends(AnyNumberMatcher, _super); |
@@ -1,2 +0,2 @@ | ||
import { Matcher } from './Matcher'; | ||
import { Matcher } from "./Matcher"; | ||
export declare class AnyOfClassMatcher<T> extends Matcher { | ||
@@ -3,0 +3,0 @@ private expectedClass; |
@@ -20,3 +20,3 @@ "use strict"; | ||
if (expectedClass === null) { | ||
throw new Error('The expected class cannot be null.'); | ||
throw new Error("The expected class cannot be null."); | ||
} | ||
@@ -29,3 +29,3 @@ return _this; | ||
AnyOfClassMatcher.prototype.toString = function () { | ||
return "anyOfClass(" + this.expectedClass['name'] + ")"; | ||
return "anyOfClass(" + this.expectedClass["name"] + ")"; | ||
}; | ||
@@ -32,0 +32,0 @@ return AnyOfClassMatcher; |
@@ -13,4 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./Matcher"); | ||
var _ = require("lodash"); | ||
var AnyStringMatcher = (function (_super) { | ||
@@ -17,0 +17,0 @@ __extends(AnyStringMatcher, _super); |
@@ -13,4 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./Matcher"); | ||
var _ = require("lodash"); | ||
var DeepEqualMatcher = (function (_super) { | ||
@@ -17,0 +17,0 @@ __extends(DeepEqualMatcher, _super); |
@@ -13,4 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("lodash"); | ||
var Matcher_1 = require("./Matcher"); | ||
var _ = require("lodash"); | ||
var NotNullMatcher = (function (_super) { | ||
@@ -17,0 +17,0 @@ __extends(NotNullMatcher, _super); |
@@ -10,3 +10,3 @@ "use strict"; | ||
MethodAction.prototype.isApplicable = function (methodName, matchers) { | ||
if (this.methodName != methodName) { | ||
if (this.methodName !== methodName) { | ||
return false; | ||
@@ -13,0 +13,0 @@ } |
@@ -7,6 +7,6 @@ import { MethodStub } from "./stub/MethodStub"; | ||
getFirstMatchingFromGroupAndRemoveIfNotLast(groupIndex: number, args: any[]): MethodStub; | ||
hasMatchingInAnyGroup(args: any[]): boolean; | ||
private getFirstMatchingFromGroup(groupIndex, args); | ||
hasMatchingInAnyGroup(args: any[]): boolean; | ||
private getFirstMatchingIndexFromGroup(groupIndex, args); | ||
private getItemsCountInGroup(groupIndex); | ||
} |
@@ -27,19 +27,19 @@ "use strict"; | ||
}; | ||
MethodStubCollection.prototype.getFirstMatchingFromGroup = function (groupIndex, args) { | ||
MethodStubCollection.prototype.hasMatchingInAnyGroup = function (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; | ||
if (item.isApplicable(args)) { | ||
return true; | ||
} | ||
} | ||
return null; | ||
return false; | ||
}; | ||
MethodStubCollection.prototype.hasMatchingInAnyGroup = function (args) { | ||
MethodStubCollection.prototype.getFirstMatchingFromGroup = function (groupIndex, args) { | ||
for (var _i = 0, _a = this.items; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
if (item.isApplicable(args)) { | ||
return true; | ||
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { | ||
return item; | ||
} | ||
} | ||
return false; | ||
return null; | ||
}; | ||
@@ -46,0 +46,0 @@ MethodStubCollection.prototype.getFirstMatchingIndexFromGroup = function (groupIndex, args) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var CallFunctionMethodStub_1 = require("./stub/CallFunctionMethodStub"); | ||
var ReturnValueMethodStub_1 = require("./stub/ReturnValueMethodStub"); | ||
var ThrowErrorMethodStub_1 = require("./stub/ThrowErrorMethodStub"); | ||
var CallFunctionMethodStub_1 = require("./stub/CallFunctionMethodStub"); | ||
var MethodStubSetter = (function () { | ||
@@ -7,0 +7,0 @@ function MethodStubSetter(methodToStub) { |
@@ -28,3 +28,3 @@ "use strict"; | ||
var methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); | ||
throw new Error('Expected "' + methodToVerifyAsString + 'to be called ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).'); | ||
throw new Error("Expected \"" + methodToVerifyAsString + "to be called " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."); | ||
} | ||
@@ -36,3 +36,3 @@ }; | ||
var methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); | ||
throw new Error('Expected "' + methodToVerifyAsString + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).'); | ||
throw new Error("Expected \"" + methodToVerifyAsString + "to be called at least " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."); | ||
} | ||
@@ -44,3 +44,3 @@ }; | ||
var methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); | ||
throw new Error('Expected "' + methodToVerifyAsString + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).'); | ||
throw new Error("Expected \"" + methodToVerifyAsString + "to be called at least " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."); | ||
} | ||
@@ -53,16 +53,16 @@ }; | ||
var secondMethodAsString = this.methodCallToStringConverter.convert(method); | ||
var errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called before ' + secondMethodAsString; | ||
var errorBeginning = "Expected \"" + mainMethodToVerifyAsString + " to be called before " + secondMethodAsString; | ||
if (firstMethodAction && secondMethodAction) { | ||
if (!firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { | ||
throw new Error(errorBeginning + 'but has been called after.'); | ||
throw new Error(errorBeginning + "but has been called after."); | ||
} | ||
} | ||
else if (firstMethodAction && !secondMethodAction) { | ||
throw new Error(errorBeginning + 'but ' + secondMethodAsString + 'has never been called.'); | ||
throw new Error(errorBeginning + "but " + secondMethodAsString + "has never been called."); | ||
} | ||
else if (!firstMethodAction && secondMethodAction) { | ||
throw new Error(errorBeginning + 'but ' + mainMethodToVerifyAsString + 'has never been called.'); | ||
throw new Error(errorBeginning + "but " + mainMethodToVerifyAsString + "has never been called."); | ||
} | ||
else { | ||
throw new Error(errorBeginning + 'but none of them has been called.'); | ||
throw new Error(errorBeginning + "but none of them has been called."); | ||
} | ||
@@ -75,16 +75,16 @@ }; | ||
var secondMethodAsString = this.methodCallToStringConverter.convert(method); | ||
var errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called after ' + secondMethodAsString; | ||
var errorBeginning = "Expected \"" + mainMethodToVerifyAsString + "to be called after " + secondMethodAsString; | ||
if (firstMethodAction && secondMethodAction) { | ||
if (firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { | ||
throw new Error(errorBeginning + 'but has been called before.'); | ||
throw new Error(errorBeginning + "but has been called before."); | ||
} | ||
} | ||
else if (firstMethodAction && !secondMethodAction) { | ||
throw new Error(errorBeginning + 'but ' + secondMethodAsString + 'has never been called.'); | ||
throw new Error(errorBeginning + "but " + secondMethodAsString + "has never been called."); | ||
} | ||
else if (!firstMethodAction && secondMethodAction) { | ||
throw new Error(errorBeginning + 'but ' + mainMethodToVerifyAsString + 'has never been called.'); | ||
throw new Error(errorBeginning + "but " + mainMethodToVerifyAsString + "has never been called."); | ||
} | ||
else { | ||
throw new Error(errorBeginning + 'but none of them has been called.'); | ||
throw new Error(errorBeginning + "but none of them has been called."); | ||
} | ||
@@ -91,0 +91,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
import { MethodStubCollection } from './MethodStubCollection'; | ||
import { Matcher } from './matcher/type/Matcher'; | ||
import { Mocker } from './Mock'; | ||
import { Matcher } from "./matcher/type/Matcher"; | ||
import { MethodStubCollection } from "./MethodStubCollection"; | ||
import { Mocker } from "./Mock"; | ||
export declare class MethodToStub { | ||
@@ -5,0 +5,0 @@ methodStubCollection: MethodStubCollection; |
@@ -14,2 +14,3 @@ import { Matcher } from "./matcher/type/Matcher"; | ||
getMock(): any; | ||
createCatchAllHandlerForRemainingPropertiesWithoutGetters(): ProxyHandler<any>; | ||
reset(): void; | ||
@@ -19,3 +20,10 @@ resetCalls(): void; | ||
getFirstMatchingAction(methodName: string, matchers: Array<Matcher>): MethodAction; | ||
protected createMethodStubsFromOwnProperties(prototype?: any, recurse?: boolean): void; | ||
getActionsByName(name: string): MethodAction[]; | ||
protected createMethodStubsFromOwnProperties(prototype?: any): void; | ||
protected createInstanceActionListenersFromOwnPropertyDescriptors(prototype?: 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(); | ||
@@ -27,13 +35,6 @@ private createMethodStubsFromClassCode(); | ||
private createMethodToStub(key); | ||
protected createInstanceActionListenersFromOwnPropertyDescriptors(prototype?: any, recurse?: boolean): void; | ||
protected createInstancePropertyDescriptorListener(key: string, descriptor: PropertyDescriptor, prototype: any): void; | ||
protected createInstanceActionListenersFromOwnPropertyNames(prototype?: any, recurse?: boolean): void; | ||
private createInstanceActionListenersFromPrototypeKeys(); | ||
private createInstanceActionListenersFromClassCode(); | ||
private createInstanceActionListenersFromFunctionsCode(); | ||
protected createInstanceActionListener(key: string, prototype: any): void; | ||
protected createActionListener(key: string): () => any; | ||
private getMethodStub(key, args); | ||
protected getEmptyMethodStub(key: string, args: any[]): MethodStub; | ||
getActionsByName(name: string): MethodAction[]; | ||
} |
255
lib/Mock.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Matcher_1 = require("./matcher/type/Matcher"); | ||
var MethodAction_1 = require("./MethodAction"); | ||
var MethodStubCollection_1 = require("./MethodStubCollection"); | ||
var MethodToStub_1 = require("./MethodToStub"); | ||
var Matcher_1 = require("./matcher/type/Matcher"); | ||
var MethodAction_1 = require("./MethodAction"); | ||
var ReturnValueMethodStub_1 = require("./stub/ReturnValueMethodStub"); | ||
var RedundantMethodNameInCodeFinder_1 = require("./utils/RedundantMethodNameInCodeFinder"); | ||
var ts_mockito_1 = require("./ts-mockito"); | ||
var PrototypeKeyCodeGetter_1 = require("./utils/PrototypeKeyCodeGetter"); | ||
var RedundantMethodNameInCodeFinder_1 = require("./utils/RedundantMethodNameInCodeFinder"); | ||
var Mocker = (function () { | ||
@@ -34,4 +34,20 @@ function Mocker(clazz, instance) { | ||
Mocker.prototype.getMock = function () { | ||
return this.mock; | ||
if (typeof Proxy === "undefined") { | ||
return this.mock; | ||
} | ||
return new Proxy(this.mock, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters()); | ||
}; | ||
Mocker.prototype.createCatchAllHandlerForRemainingPropertiesWithoutGetters = function () { | ||
var _this = this; | ||
return { | ||
get: function (target, name) { | ||
var hasMethodStub = name in target; | ||
if (!hasMethodStub) { | ||
_this.createPropertyStub(name.toString()); | ||
_this.createInstancePropertyDescriptorListener(name.toString(), {}, _this.clazz.prototype); | ||
} | ||
return target[name]; | ||
} | ||
}; | ||
}; | ||
Mocker.prototype.reset = function () { | ||
@@ -46,8 +62,7 @@ this.methodStubCollections = {}; | ||
var result = []; | ||
for (var _i = 0, _a = this.methodActions; _i < _a.length; _i++) { | ||
var item = _a[_i]; | ||
this.methodActions.forEach(function (item) { | ||
if (item.isApplicable(methodName, matchers)) { | ||
result.push(item); | ||
} | ||
} | ||
}); | ||
return result; | ||
@@ -58,24 +73,63 @@ }; | ||
}; | ||
Mocker.prototype.createMethodStubsFromOwnProperties = function (prototype, recurse) { | ||
Mocker.prototype.getActionsByName = function (name) { | ||
return this.methodActions.filter(function (action) { return action.methodName === name; }); | ||
}; | ||
Mocker.prototype.createMethodStubsFromOwnProperties = function (prototype) { | ||
var _this = this; | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
if (recurse === void 0) { recurse = true; } | ||
if (prototype === Object.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) { | ||
} | ||
}; | ||
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) { | ||
if (this.instance.hasOwnProperty(key)) { | ||
return; | ||
} | ||
Object.defineProperty(this.instance, key, { | ||
get: this.createActionListener(key), | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromOwnPropertyNames = function (prototype) { | ||
var _this = this; | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
try { | ||
for (var _i = 0, _a = Object.getOwnPropertyNames(prototype); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, key); | ||
if (!descriptor) { | ||
continue; | ||
} | ||
if (descriptor.get) { | ||
this.createPropertyStub(key); | ||
} | ||
else { | ||
this.createMethodStub(key); | ||
} | ||
while (prototype !== Object.prototype) { | ||
Object.getOwnPropertyNames(prototype).forEach(function (name) { | ||
_this.createInstanceActionListener(name, prototype); | ||
}); | ||
prototype = Object.getPrototypeOf(prototype); | ||
} | ||
prototype = prototype.__proto__; | ||
this.createMethodStubsFromOwnProperties(prototype); | ||
} | ||
@@ -85,20 +139,46 @@ catch (error) { | ||
}; | ||
Mocker.prototype.createMethodStubsFromPrototypeKeys = function () { | ||
for (var key in this.clazz.prototype) { | ||
this.createMethodStub(key); | ||
Mocker.prototype.createInstanceActionListener = function (key, prototype) { | ||
if (this.instance.hasOwnProperty(key)) { | ||
return; | ||
} | ||
this.instance[key] = this.createActionListener(key); | ||
}; | ||
Mocker.prototype.createActionListener = function (key) { | ||
var _this = this; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var action = new MethodAction_1.MethodAction(key, args); | ||
_this.methodActions.push(action); | ||
var methodStub = _this.getMethodStub(key, args); | ||
methodStub.execute(args); | ||
return methodStub.getValue(); | ||
}; | ||
}; | ||
Mocker.prototype.getEmptyMethodStub = function (key, args) { | ||
return new ReturnValueMethodStub_1.ReturnValueMethodStub(-1, [], null); | ||
}; | ||
Mocker.prototype.createMethodStubsFromPrototypeKeys = function () { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
_this.createMethodStub(key); | ||
}); | ||
}; | ||
Mocker.prototype.createMethodStubsFromClassCode = function () { | ||
var _this = this; | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.toString()); | ||
for (var subKey in subKeys) { | ||
this.createMethodStub(subKey); | ||
} | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createMethodStub(subKey); | ||
}); | ||
}; | ||
Mocker.prototype.createMethodStubsFromFunctionsCode = function () { | ||
for (var key in this.clazz.prototype) { | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.subKeysInCodeFinder.get(this.clazz.prototype, key)); | ||
for (var subKey in subKeys) { | ||
this.createMethodStub(subKey); | ||
} | ||
} | ||
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); | ||
}); | ||
}); | ||
}; | ||
@@ -110,3 +190,3 @@ Mocker.prototype.createPropertyStub = function (key) { | ||
Object.defineProperty(this.mock, key, { | ||
get: this.createMethodToStub(key) | ||
get: this.createMethodToStub(key), | ||
}); | ||
@@ -143,90 +223,23 @@ }; | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromOwnPropertyDescriptors = function (prototype, recurse) { | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
if (recurse === void 0) { recurse = true; } | ||
try { | ||
var names = Object.getOwnPropertyNames(prototype); | ||
for (var i = 0; i < names.length; i++) { | ||
var key = names[i]; | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, key); | ||
if (descriptor && descriptor.get) { | ||
this.createInstancePropertyDescriptorListener(key, descriptor, prototype); | ||
} | ||
} | ||
if (!recurse) { | ||
return; | ||
} | ||
prototype = prototype.__proto__; | ||
if (prototype && prototype !== Object.prototype) { | ||
this.createInstanceActionListenersFromOwnPropertyDescriptors(prototype); | ||
} | ||
} | ||
catch (error) { | ||
} | ||
}; | ||
Mocker.prototype.createInstancePropertyDescriptorListener = function (key, descriptor, prototype) { | ||
if (this.instance.hasOwnProperty(key)) { | ||
return; | ||
} | ||
Object.defineProperty(this.instance, key, { | ||
get: this.createActionListener(key) | ||
Mocker.prototype.createInstanceActionListenersFromPrototypeKeys = function () { | ||
var _this = this; | ||
Object.keys(this.clazz.prototype).forEach(function (key) { | ||
_this.createInstanceActionListener(key, _this.clazz.prototype); | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromOwnPropertyNames = function (prototype, recurse) { | ||
if (prototype === void 0) { prototype = this.clazz.prototype; } | ||
if (recurse === void 0) { recurse = true; } | ||
try { | ||
var names = Object.getOwnPropertyNames(prototype); | ||
for (var i = 0; i < names.length; i++) { | ||
this.createInstanceActionListener(names[i], prototype); | ||
} | ||
if (!recurse) { | ||
return; | ||
} | ||
prototype = prototype.__proto__; | ||
if (prototype && prototype !== Object.prototype) { | ||
this.createInstanceActionListenersFromOwnPropertyNames(prototype); | ||
} | ||
} | ||
catch (error) { | ||
} | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromPrototypeKeys = function () { | ||
for (var key in this.clazz.prototype) { | ||
this.createInstanceActionListener(key, this.clazz.prototype); | ||
} | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromClassCode = function () { | ||
var _this = this; | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.toString()); | ||
for (var subKey in subKeys) { | ||
this.createInstanceActionListener(subKey, this.clazz.prototype); | ||
} | ||
Object.keys(subKeys).forEach(function (subKey) { | ||
_this.createInstanceActionListener(subKey, _this.clazz.prototype); | ||
}); | ||
}; | ||
Mocker.prototype.createInstanceActionListenersFromFunctionsCode = function () { | ||
for (var key in this.clazz.prototype) { | ||
var subKeys = this.redundantMethodNameInCodeFinder.find(this.subKeysInCodeFinder.get(this.clazz.prototype, key)); | ||
for (var subKey in subKeys) { | ||
this.createInstanceActionListener(subKey, this.clazz.prototype); | ||
} | ||
} | ||
}; | ||
Mocker.prototype.createInstanceActionListener = function (key, prototype) { | ||
if (this.instance.hasOwnProperty(key)) { | ||
return; | ||
} | ||
this.instance[key] = this.createActionListener(key); | ||
}; | ||
Mocker.prototype.createActionListener = function (key) { | ||
var _this = this; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var action = new MethodAction_1.MethodAction(key, args); | ||
_this.methodActions.push(action); | ||
var methodStub = _this.getMethodStub(key, args); | ||
methodStub.execute(args); | ||
return methodStub.getValue(); | ||
}; | ||
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); | ||
}); | ||
}); | ||
}; | ||
@@ -243,8 +256,2 @@ Mocker.prototype.getMethodStub = function (key, args) { | ||
}; | ||
Mocker.prototype.getEmptyMethodStub = function (key, args) { | ||
return new ReturnValueMethodStub_1.ReturnValueMethodStub(-1, [], null); | ||
}; | ||
Mocker.prototype.getActionsByName = function (name) { | ||
return this.methodActions.filter(function (action) { return action.methodName === name; }); | ||
}; | ||
return Mocker; | ||
@@ -251,0 +258,0 @@ }()); |
@@ -15,4 +15,4 @@ "use strict"; | ||
var Mock_1 = require("./Mock"); | ||
var RealMethod_1 = require("./spy/RealMethod"); | ||
var CallThroughMethodStub_1 = require("./stub/CallThroughMethodStub"); | ||
var RealMethod_1 = require("./spy/RealMethod"); | ||
var Spy = (function (_super) { | ||
@@ -22,5 +22,5 @@ __extends(Spy, _super); | ||
var _this = _super.call(this, instance.constructor, instance) || this; | ||
_this.createMethodStubsFromOwnProperties(instance, false); | ||
_this.createInstanceActionListenersFromOwnPropertyDescriptors(instance, false); | ||
_this.createInstanceActionListenersFromOwnPropertyNames(instance, false); | ||
_this.createMethodStubsFromOwnProperties(instance); | ||
_this.createInstanceActionListenersFromOwnPropertyDescriptors(instance); | ||
_this.createInstanceActionListenersFromOwnPropertyNames(instance); | ||
return _this; | ||
@@ -58,3 +58,3 @@ } | ||
get: this.createActionListener(key), | ||
configurable: true | ||
configurable: true, | ||
}); | ||
@@ -61,0 +61,0 @@ }; |
import { Matcher } from "../matcher/type/Matcher"; | ||
import { AbstractMethodStub } from "./AbstractMethodStub"; | ||
import { MethodStub } from "./MethodStub"; | ||
import { AbstractMethodStub } from "./AbstractMethodStub"; | ||
export declare class CallFunctionMethodStub extends AbstractMethodStub implements MethodStub { | ||
@@ -5,0 +5,0 @@ protected groupIndex: number; |
@@ -1,4 +0,4 @@ | ||
import { Matcher } from '../matcher/type/Matcher'; | ||
import { MethodStub } from './MethodStub'; | ||
import { Matcher } from "../matcher/type/Matcher"; | ||
import { AbstractMethodStub } from "./AbstractMethodStub"; | ||
import { MethodStub } from "./MethodStub"; | ||
export declare class ReturnValueMethodStub extends AbstractMethodStub implements MethodStub { | ||
@@ -5,0 +5,0 @@ protected groupIndex: number; |
@@ -1,4 +0,4 @@ | ||
import { Matcher } from '../matcher/type/Matcher'; | ||
import { MethodStub } from './MethodStub'; | ||
import { Matcher } from "../matcher/type/Matcher"; | ||
import { AbstractMethodStub } from "./AbstractMethodStub"; | ||
import { MethodStub } from "./MethodStub"; | ||
export declare class ThrowErrorMethodStub extends AbstractMethodStub implements MethodStub { | ||
@@ -5,0 +5,0 @@ protected groupIndex: number; |
@@ -5,3 +5,3 @@ import { ArgCaptor1, ArgCaptor10, ArgCaptor2, ArgCaptor3, ArgCaptor4, ArgCaptor5, ArgCaptor6, ArgCaptor7, ArgCaptor8, ArgCaptor9 } from "./capture/ArgCaptor"; | ||
import { MethodStubVerificator } from "./MethodStubVerificator"; | ||
export declare function spy<T>(instance: T): T; | ||
export declare function spy<T>(instanceToSpy: T): T; | ||
export declare function mock<T>(clazz: { | ||
@@ -14,3 +14,3 @@ new (...args: any[]): T; | ||
export declare function when<T>(method: T): MethodStubSetter<T>; | ||
export declare function instance<T>(mock: T): T; | ||
export declare function instance<T>(mockedValue: T): T; | ||
export declare function capture<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8, j: T9) => any): ArgCaptor10<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>; | ||
@@ -26,4 +26,4 @@ export declare function capture<T0, T1, T2, T3, T4, T5, T6, T7, T8>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8) => any): ArgCaptor9<T0, T1, T2, T3, T4, T5, T6, T7, T8>; | ||
export declare function capture<T0>(method: (a: T0) => any): ArgCaptor1<T0>; | ||
export declare function reset<T>(mock: T): void; | ||
export declare function resetCalls<T>(mock: T): void; | ||
export declare function reset<T>(mockedValue: T): void; | ||
export declare function resetCalls<T>(mockedValue: T): void; | ||
export declare function anyOfClass<T>(expectedClass: { | ||
@@ -30,0 +30,0 @@ new (...args: any[]): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ArgCaptor_1 = require("./capture/ArgCaptor"); | ||
var AnyFunctionMatcher_1 = require("./matcher/type/AnyFunctionMatcher"); | ||
var AnyNumberMatcher_1 = require("./matcher/type/AnyNumberMatcher"); | ||
var AnyOfClassMatcher_1 = require("./matcher/type/AnyOfClassMatcher"); | ||
var ArgCaptor_1 = require("./capture/ArgCaptor"); | ||
var AnyNumberMatcher_1 = require("./matcher/type/AnyNumberMatcher"); | ||
var AnyStringMatcher_1 = require("./matcher/type/AnyStringMatcher"); | ||
@@ -18,4 +18,4 @@ var AnythingMatcher_1 = require("./matcher/type/AnythingMatcher"); | ||
var Spy_1 = require("./Spy"); | ||
function spy(instance) { | ||
return new Spy_1.Spy(instance).getMock(); | ||
function spy(instanceToSpy) { | ||
return new Spy_1.Spy(instanceToSpy).getMock(); | ||
} | ||
@@ -35,4 +35,9 @@ exports.spy = spy; | ||
exports.when = when; | ||
function instance(mock) { | ||
return mock.__tsmockitoInstance; | ||
function instance(mockedValue) { | ||
var tsmockitoInstance = mockedValue.__tsmockitoInstance; | ||
if (typeof Proxy === "undefined") { | ||
return tsmockitoInstance; | ||
} | ||
var tsmockitoMocker = mockedValue.__tsmockitoMocker; | ||
return new Proxy(tsmockitoInstance, tsmockitoMocker.createCatchAllHandlerForRemainingPropertiesWithoutGetters()); | ||
} | ||
@@ -51,8 +56,8 @@ exports.instance = instance; | ||
exports.capture = capture; | ||
function reset(mock) { | ||
mock.__tsmockitoMocker.reset(); | ||
function reset(mockedValue) { | ||
mockedValue.__tsmockitoMocker.reset(); | ||
} | ||
exports.reset = reset; | ||
function resetCalls(mock) { | ||
mock.__tsmockitoMocker.resetCalls(); | ||
function resetCalls(mockedValue) { | ||
mockedValue.__tsmockitoMocker.resetCalls(); | ||
} | ||
@@ -59,0 +64,0 @@ exports.resetCalls = resetCalls; |
@@ -10,3 +10,3 @@ "use strict"; | ||
var matchersAsString = this.matchersToStringConverter.convert(method.matchers); | ||
return method.name + '(' + matchersAsString + ')" '; | ||
return method.name + "(" + matchersAsString + ")\" "; | ||
}; | ||
@@ -13,0 +13,0 @@ return MethodCallToStringConverter; |
export declare class RedundantMethodNameInCodeFinder { | ||
private notMockableFunctionNames; | ||
find(code: string): any; | ||
find(code: string): {}; | ||
} |
@@ -8,3 +8,3 @@ "use strict"; | ||
RedundantMethodNameInCodeFinder.prototype.find = function (code) { | ||
var result = []; | ||
var result = {}; | ||
var subCodes = code.match(/\.(.{0,40})\(/g); | ||
@@ -11,0 +11,0 @@ if (subCodes && subCodes.length > 0) { |
{ | ||
"name": "ts-mockito", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Mocking library for TypeScript", | ||
@@ -10,3 +10,4 @@ "main": "lib/ts-mockito.js", | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
"test:watch": "jest --watch", | ||
"lint": "./node_modules/tslint/bin/tslint -c tslint.json 'src/**/*.ts' 'test/**/*.ts'" | ||
}, | ||
@@ -35,2 +36,3 @@ "author": "kuster.maciej@gmail.com", | ||
"ts-jest": "^21.0.1", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.4.0" | ||
@@ -37,0 +39,0 @@ }, |
@@ -86,2 +86,8 @@ # ts-mockito [![build badge](https://travis-ci.org/NagRock/ts-mockito.svg?branch=master)](https://travis-ci.org/NagRock/ts-mockito) | ||
### Stubbing property values that have no getters | ||
Syntax is the same as with getter values. | ||
Please note, that stubbing properties that don't have getters only works if [Proxy](http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects) object is available (ES6). | ||
### Call count verification | ||
@@ -342,2 +348,5 @@ | ||
* Michał Stocki (https://github.com/michalstocki) | ||
* Łukasz Bendykowski (https://github.com/viman) | ||
* Andrey Ermakov (https://github.com/dreef3) | ||
* Markus Ende (https://github.com/Markus-Ende) | ||
* Thomas Hilzendegen (https://github.com/thomashilzendegen) |
@@ -14,2 +14,3 @@ { | ||
"es5", | ||
"es6", | ||
"dom" | ||
@@ -16,0 +17,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
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
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
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
114516
1649
351
8