Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-mockito

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-mockito - npm Package Compare versions

Comparing version 0.0.5 to 0.0.7

lib/matcher/AnyNumberMatcher.d.ts

145

lib/ts-mockito.d.ts

@@ -1,137 +0,8 @@

declare module "matcher/Matcher" {
export class Matcher {
match(value: any): boolean;
toString(): string;
}
}
declare module "matcher/AnyNumberMatcher" {
export function anyNumber(): number;
}
declare module "matcher/AnyStringMatcher" {
export function anyString(): string;
}
declare module "matcher/AnythingMatcher" {
export function anything(): any;
}
declare module "matcher/DeepEqualMatcher" {
export function deepEqual(expectedValue: any): any;
}
declare module "matcher/MatchersToStringConverter" {
import { Matcher } from "matcher/Matcher";
export class MatchersToStringConverter {
convert(matchers: Array<Matcher>): string;
}
}
declare module "matcher/NotNullMatcher" {
export function notNull(): any;
}
declare module "matcher/StrictEqualMatcher" {
import { Matcher } from "matcher/Matcher";
export function strictEqual(expectedValue: any): Matcher;
}
declare module "MethodAction" {
export class MethodAction {
methodName: string;
args: Array<any>;
constructor(methodName: string, args: Array<any>);
isApplicable(matchers: any): boolean;
}
}
declare module "MethodStub" {
import { Matcher } from "matcher/Matcher";
export class MethodStub {
private matchers;
private returns;
constructor(matchers: Array<Matcher>, returns: any);
isApplicable(args: any): boolean;
getValue(): any;
}
}
declare module "MethodStubCollection" {
import { MethodStub } from "MethodStub";
export class MethodStubCollection {
private hadMoreThanOneBehavior;
private items;
add(item: MethodStub): void;
getFirstMatchingAndRemove(args: any): MethodStub;
getFirstMatching(args: any): MethodStub;
getHadMoreThanOneBehavior(): boolean;
hasMatching(args: any): boolean;
private getFirstMatchingIndex(args);
}
}
declare module "Mock" {
import { Matcher } from "matcher/Matcher";
import { MethodAction } from "MethodAction";
export class Mock {
private clazz;
private methodStubCollections;
private methodActions;
private mock;
private instance;
constructor(clazz: any);
getMock(): any;
getAllMatchingActions(matchers: Array<Matcher>): Array<MethodAction>;
private createMethodStubs();
private createInstanceActionListeners();
private getMethodStub(key, args);
}
}
declare module "MethodToStub" {
import { MethodStubCollection } from "MethodStubCollection";
import { Matcher } from "matcher/Matcher";
import { Mock } from "Mock";
export class MethodToStub {
methodStubCollection: MethodStubCollection;
matchers: Array<Matcher>;
mock: Mock;
name: string;
constructor(methodStubCollection: MethodStubCollection, matchers: Array<Matcher>, mock: Mock, name: string);
}
}
declare module "MethodStubSetter" {
import { MethodToStub } from "MethodToStub";
export class MethodStubSetter<T> {
private methodToStub;
constructor(methodToStub: MethodToStub);
thenReturn(value: T): void;
throwsError(value: Error): void;
}
}
declare module "MethodStubVerificator" {
import { MethodToStub } from "MethodToStub";
export class MethodStubVerificator<T> {
private methodToVerify;
private matchersToStringConverter;
constructor(methodToVerify: MethodToStub);
called(): void;
never(): void;
once(): void;
twice(): void;
thrice(): void;
times(value: number): void;
atLeast(value: number): void;
atMost(value: number): void;
private getErrorBeginning(matchers);
}
}
declare module "ts-mockito" {
import { MethodStubVerificator } from "MethodStubVerificator";
import { MethodStubSetter } from "MethodStubSetter";
export function mock<T>(clazz: {
new (...args: any[]): T;
}): T;
export function verify<T>(method: T): MethodStubVerificator<T>;
export function when<T>(method: T): MethodStubSetter<T>;
export function instance<T>(mock: T): T;
}
declare module "mockito-node-tester" {
export class Generic<T> {
}
export class Temp {
sampleVar: string;
sampleMethod(param: number): string;
sampleMethod2(first: string, second: number): number;
}
export function main(): void;
}
import { MethodStubVerificator } from './MethodStubVerificator';
import { MethodStubSetter } from './MethodStubSetter';
export declare function mock<T>(clazz: {
new (...args: any[]): T;
}): T;
export declare function verify<T>(method: T): MethodStubVerificator<T>;
export declare function when<T>(method: T): MethodStubSetter<T>;
export declare function instance<T>(mock: T): T;

@@ -1,488 +0,21 @@

var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define("matcher/Matcher", ["require", "exports"], function (require, exports) {
"use strict";
var Matcher = (function () {
function Matcher() {
}
Matcher.prototype.match = function (value) {
return false;
};
Matcher.prototype.toString = function () {
return '';
};
return Matcher;
}());
exports.Matcher = Matcher;
});
define("matcher/AnyNumberMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_1) {
"use strict";
function anyNumber() {
return new AnyNumberMatcher();
}
exports.anyNumber = anyNumber;
var AnyNumberMatcher = (function (_super) {
__extends(AnyNumberMatcher, _super);
function AnyNumberMatcher() {
_super.call(this);
}
AnyNumberMatcher.prototype.match = function (value) {
return _.isNumber(value);
};
AnyNumberMatcher.prototype.toString = function () {
return 'anyNumber()';
};
return AnyNumberMatcher;
}(Matcher_1.Matcher));
});
define("matcher/AnyStringMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_2) {
"use strict";
function anyString() {
return new AnyStringMatcher();
}
exports.anyString = anyString;
var AnyStringMatcher = (function (_super) {
__extends(AnyStringMatcher, _super);
function AnyStringMatcher() {
_super.call(this);
}
AnyStringMatcher.prototype.match = function (value) {
return _.isString(value);
};
AnyStringMatcher.prototype.toString = function () {
return 'anyString()';
};
return AnyStringMatcher;
}(Matcher_2.Matcher));
});
define("matcher/AnythingMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_3) {
"use strict";
function anything() {
return new AnythingMatcher();
}
exports.anything = anything;
var AnythingMatcher = (function (_super) {
__extends(AnythingMatcher, _super);
function AnythingMatcher() {
_super.call(this);
}
AnythingMatcher.prototype.match = function (value) {
return true;
};
AnythingMatcher.prototype.toString = function () {
return 'anything()';
};
return AnythingMatcher;
}(Matcher_3.Matcher));
});
define("matcher/DeepEqualMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_4) {
"use strict";
function deepEqual(expectedValue) {
return new DeepEqualMatcher(expectedValue);
}
exports.deepEqual = deepEqual;
var DeepEqualMatcher = (function (_super) {
__extends(DeepEqualMatcher, _super);
function DeepEqualMatcher(expectedValue) {
_super.call(this);
this.expectedValue = expectedValue;
}
DeepEqualMatcher.prototype.match = function (value) {
return _.isEqual(this.expectedValue, value);
};
DeepEqualMatcher.prototype.toString = function () {
return 'deepEqual(' + this.expectedValue + ')';
};
return DeepEqualMatcher;
}(Matcher_4.Matcher));
});
define("matcher/MatchersToStringConverter", ["require", "exports"], function (require, exports) {
"use strict";
var MatchersToStringConverter = (function () {
function MatchersToStringConverter() {
}
MatchersToStringConverter.prototype.convert = function (matchers) {
var result = '';
for (var _i = 0, matchers_1 = matchers; _i < matchers_1.length; _i++) {
var matcher = matchers_1[_i];
result += matcher.toString();
}
return result;
};
return MatchersToStringConverter;
}());
exports.MatchersToStringConverter = MatchersToStringConverter;
});
define("matcher/NotNullMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_5) {
"use strict";
function notNull() {
return new NotNullMatcher();
}
exports.notNull = notNull;
var NotNullMatcher = (function (_super) {
__extends(NotNullMatcher, _super);
function NotNullMatcher() {
_super.apply(this, arguments);
}
NotNullMatcher.prototype.match = function (value) {
return !_.isNull(value);
};
NotNullMatcher.prototype.toString = function () {
return 'notNull()';
};
return NotNullMatcher;
}(Matcher_5.Matcher));
});
define("matcher/StrictEqualMatcher", ["require", "exports", "matcher/Matcher"], function (require, exports, Matcher_6) {
"use strict";
function strictEqual(expectedValue) {
return new StrictEqualMatcher(expectedValue);
}
exports.strictEqual = strictEqual;
var StrictEqualMatcher = (function (_super) {
__extends(StrictEqualMatcher, _super);
function StrictEqualMatcher(expectedValue) {
_super.call(this);
this.expectedValue = expectedValue;
}
StrictEqualMatcher.prototype.match = function (value) {
return this.expectedValue === value;
};
StrictEqualMatcher.prototype.toString = function () {
return 'strictEqual(' + this.expectedValue + ')';
};
return StrictEqualMatcher;
}(Matcher_6.Matcher));
});
define("MethodAction", ["require", "exports"], function (require, exports) {
"use strict";
var MethodAction = (function () {
function MethodAction(methodName, args) {
this.methodName = methodName;
this.args = args;
}
MethodAction.prototype.isApplicable = function (matchers) {
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;
}
}
return allValid;
};
return MethodAction;
}());
exports.MethodAction = MethodAction;
});
define("MethodStub", ["require", "exports"], function (require, exports) {
"use strict";
var MethodStub = (function () {
function MethodStub(matchers, returns) {
this.matchers = matchers;
this.returns = returns;
}
MethodStub.prototype.isApplicable = function (args) {
var allValid = true;
var index = 0;
for (var _i = 0, args_1 = args; _i < args_1.length; _i++) {
var arg = args_1[_i];
if (!this.matchers[index].match(arg)) {
allValid = false;
}
index++;
}
return allValid;
};
MethodStub.prototype.getValue = function () {
if (this.returns && this.returns.error != null) {
throw this.returns.error;
}
return this.returns;
};
return MethodStub;
}());
exports.MethodStub = MethodStub;
});
define("MethodStubCollection", ["require", "exports"], function (require, exports) {
"use strict";
var MethodStubCollection = (function () {
function MethodStubCollection() {
this.hadMoreThanOneBehavior = false;
this.items = [];
}
MethodStubCollection.prototype.add = function (item) {
this.items.push(item);
if (this.items.length > 1) {
this.hadMoreThanOneBehavior = true;
}
};
MethodStubCollection.prototype.getFirstMatchingAndRemove = function (args) {
var index = this.getFirstMatchingIndex(args);
var result = this.getFirstMatching(args);
if (index > -1) {
this.items.splice(index, 1);
}
return result;
};
MethodStubCollection.prototype.getFirstMatching = function (args) {
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(args)) {
return item;
}
}
return null;
};
MethodStubCollection.prototype.getHadMoreThanOneBehavior = function () {
return this.hadMoreThanOneBehavior;
};
MethodStubCollection.prototype.hasMatching = function (args) {
return this.getFirstMatchingIndex(args) > -1;
};
MethodStubCollection.prototype.getFirstMatchingIndex = function (args) {
var index = 0;
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(args)) {
return index;
}
index++;
}
return -1;
};
return MethodStubCollection;
}());
exports.MethodStubCollection = MethodStubCollection;
});
define("Mock", ["require", "exports", "MethodStubCollection", "MethodToStub", "MethodStub", "matcher/Matcher", "matcher/StrictEqualMatcher", "MethodAction"], function (require, exports, MethodStubCollection_1, MethodToStub_1, MethodStub_1, Matcher_7, StrictEqualMatcher_1, MethodAction_1) {
"use strict";
var Mock = (function () {
function Mock(clazz) {
this.clazz = clazz;
this.methodStubCollections = {};
this.methodActions = [];
this.mock = {};
this.instance = {};
this.mock.__tsmockitoInstance = this.instance;
this.createMethodStubs();
this.createInstanceActionListeners();
}
Mock.prototype.getMock = function () {
return this.mock;
};
Mock.prototype.getAllMatchingActions = function (matchers) {
var result = [];
for (var _i = 0, _a = this.methodActions; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(matchers)) {
result.push(item);
}
}
return result;
};
Mock.prototype.createMethodStubs = function () {
var _this = this;
var _loop_1 = function(key) {
this_1.mock[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
if (!_this.methodStubCollections[key]) {
_this.methodStubCollections[key] = new MethodStubCollection_1.MethodStubCollection();
}
var matchers = [];
for (var _a = 0, args_2 = args; _a < args_2.length; _a++) {
var arg = args_2[_a];
if (!(arg instanceof Matcher_7.Matcher)) {
matchers.push(StrictEqualMatcher_1.strictEqual(arg));
}
else {
matchers.push(arg);
}
}
return new MethodToStub_1.MethodToStub(_this.methodStubCollections[key], matchers, _this, key);
};
};
var this_1 = this;
for (var key in this.clazz.prototype) {
_loop_1(key);
}
};
Mock.prototype.createInstanceActionListeners = function () {
var _this = this;
var _loop_2 = function(key) {
this_2.instance[key] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
var action = new MethodAction_1.MethodAction(key, args);
_this.methodActions.push(action);
return _this.getMethodStub(key, args);
};
};
var this_2 = this;
for (var key in this.clazz.prototype) {
_loop_2(key);
}
};
Mock.prototype.getMethodStub = function (key, args) {
var methodStub = this.methodStubCollections[key];
if (!methodStub) {
return new MethodStub_1.MethodStub([], null).getValue();
}
else if (methodStub.getHadMoreThanOneBehavior() && methodStub.hasMatching(args)) {
return methodStub.getFirstMatchingAndRemove(args).getValue();
}
else if (methodStub.hasMatching(args)) {
return methodStub.getFirstMatching(args).getValue();
}
else {
return new MethodStub_1.MethodStub([], null).getValue();
}
};
return Mock;
}());
exports.Mock = Mock;
});
define("MethodToStub", ["require", "exports"], function (require, exports) {
"use strict";
var MethodToStub = (function () {
function MethodToStub(methodStubCollection, matchers, mock, name) {
this.methodStubCollection = methodStubCollection;
this.matchers = matchers;
this.mock = mock;
this.name = name;
}
return MethodToStub;
}());
exports.MethodToStub = MethodToStub;
});
define("MethodStubSetter", ["require", "exports", "MethodStub"], function (require, exports, MethodStub_2) {
"use strict";
var MethodStubSetter = (function () {
function MethodStubSetter(methodToStub) {
this.methodToStub = methodToStub;
}
MethodStubSetter.prototype.thenReturn = function (value) {
this.methodToStub.methodStubCollection.add(new MethodStub_2.MethodStub(this.methodToStub.matchers, value));
};
MethodStubSetter.prototype.throwsError = function (value) {
this.methodToStub.methodStubCollection.add(new MethodStub_2.MethodStub(this.methodToStub.matchers, { error: value }));
};
return MethodStubSetter;
}());
exports.MethodStubSetter = MethodStubSetter;
});
define("MethodStubVerificator", ["require", "exports", "matcher/MatchersToStringConverter"], function (require, exports, MatchersToStringConverter_1) {
"use strict";
var MethodStubVerificator = (function () {
function MethodStubVerificator(methodToVerify) {
this.methodToVerify = methodToVerify;
this.matchersToStringConverter = new MatchersToStringConverter_1.MatchersToStringConverter();
}
MethodStubVerificator.prototype.called = function () {
this.atLeast(1);
};
MethodStubVerificator.prototype.never = function () {
this.times(0);
};
MethodStubVerificator.prototype.once = function () {
this.times(1);
};
MethodStubVerificator.prototype.twice = function () {
this.times(2);
};
MethodStubVerificator.prototype.thrice = function () {
this.times(3);
};
MethodStubVerificator.prototype.times = function (value) {
var allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.matchers);
if (value !== allMatchingActions.length) {
var msg = this.getErrorBeginning(this.methodToVerify.matchers);
throw new Error(msg + 'to be called ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
}
};
MethodStubVerificator.prototype.atLeast = function (value) {
var allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.matchers);
if (value > allMatchingActions.length) {
var msg = this.getErrorBeginning(this.methodToVerify.matchers);
throw new Error(msg + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
}
};
MethodStubVerificator.prototype.atMost = function (value) {
var allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.matchers);
if (value < allMatchingActions.length) {
var msg = this.getErrorBeginning(this.methodToVerify.matchers);
throw new Error(msg + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
}
};
MethodStubVerificator.prototype.getErrorBeginning = function (matchers) {
var matchersAsString = this.matchersToStringConverter.convert(matchers);
return 'Expected "' + this.methodToVerify.name + '(' + matchersAsString + ')" ';
};
return MethodStubVerificator;
}());
exports.MethodStubVerificator = MethodStubVerificator;
});
define("ts-mockito", ["require", "exports", "Mock", "MethodStubVerificator", "MethodStubSetter"], function (require, exports, Mock_1, MethodStubVerificator_1, MethodStubSetter_1) {
"use strict";
function mock(clazz) {
return new Mock_1.Mock(clazz).getMock();
}
exports.mock = mock;
function verify(method) {
return new MethodStubVerificator_1.MethodStubVerificator(method);
}
exports.verify = verify;
function when(method) {
return new MethodStubSetter_1.MethodStubSetter(method);
}
exports.when = when;
function instance(mock) {
return mock.__tsmockitoInstance;
}
exports.instance = instance;
});
define("mockito-node-tester", ["require", "exports", "matcher/AnyNumberMatcher", "ts-mockito"], function (require, exports, AnyNumberMatcher_1, ts_mockito_1) {
"use strict";
var Generic = (function () {
function Generic() {
}
return Generic;
}());
exports.Generic = Generic;
var Temp = (function () {
function Temp() {
this.sampleVar = 'test';
}
Temp.prototype.sampleMethod = function (param) {
this.sampleVar = param.toString();
return this.sampleVar;
};
Temp.prototype.sampleMethod2 = function (first, second) {
return 10;
};
return Temp;
}());
exports.Temp = Temp;
function main() {
var myMock = ts_mockito_1.mock(Temp);
ts_mockito_1.when(myMock.sampleMethod(1)).thenReturn('jeden');
ts_mockito_1.when(myMock.sampleMethod(2)).thenReturn('dwa');
ts_mockito_1.when(myMock.sampleMethod(3)).thenReturn('trzy');
ts_mockito_1.when(myMock.sampleMethod2('sampleString', 3)).thenReturn(1);
console.log('>>', ts_mockito_1.instance(myMock).sampleMethod(1));
console.log('>>', ts_mockito_1.instance(myMock).sampleMethod(2));
console.log('>>', ts_mockito_1.instance(myMock).sampleMethod(2));
ts_mockito_1.verify(myMock.sampleMethod(AnyNumberMatcher_1.anyNumber())).thrice();
}
exports.main = main;
});
"use strict";
var Mock_1 = require('./Mock');
var MethodStubVerificator_1 = require('./MethodStubVerificator');
var MethodStubSetter_1 = require('./MethodStubSetter');
function mock(clazz) {
return new Mock_1.Mock(clazz).getMock();
}
exports.mock = mock;
function verify(method) {
return new MethodStubVerificator_1.MethodStubVerificator(method);
}
exports.verify = verify;
function when(method) {
return new MethodStubSetter_1.MethodStubSetter(method);
}
exports.when = when;
function instance(mock) {
return mock.__tsmockitoInstance;
}
exports.instance = instance;
//# sourceMappingURL=ts-mockito.js.map
{
"name": "ts-mockito",
"version": "0.0.5",
"version": "0.0.7",
"description": "",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
"scripts": {

@@ -7,0 +8,0 @@ "test": "echo \"Error: no test specified\" && exit 1"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc