Skip to content

Commit

Permalink
Implement TSLint and fix all code issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bendykowski committed Sep 27, 2017
1 parent 3ff5257 commit 1ecbb4c
Show file tree
Hide file tree
Showing 53 changed files with 742 additions and 697 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"scripts": {
"compile": "rm -rf lib && ./node_modules/.bin/tsc -p ./src",
"test": "jest",
"test:watch": "jest --watch"
"test:watch": "jest --watch",
"tslint": "./node_modules/tslint/bin/tslint -c tslint.json 'src/**/*.ts'",
"test:tslint": "./node_modules/tslint/bin/tslint -c test/tslint.json 'test/**/*.ts'"
},
"author": "[email protected]",
"license": "ISC",
Expand All @@ -31,6 +33,7 @@
"jest": "^21.1.0",
"ts-helpers": "^1.1.1",
"ts-jest": "^21.0.1",
"tslint": "^5.7.0",
"typescript": "^2.4.0"
},
"dependencies": {
Expand Down
18 changes: 9 additions & 9 deletions src/MethodAction.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {Matcher} from "./matcher/type/Matcher";

export class MethodAction {
private static globalCallIndex:number = 0;
private callIndex:number;
private static globalCallIndex: number = 0;
private callIndex: number;

constructor(public methodName: string, public args: Array<any>){
constructor(public methodName: string, public args: Array<any>) {
this.callIndex = ++MethodAction.globalCallIndex;
}

public isApplicable(methodName:string, matchers:Matcher[]): boolean {
if (this.methodName != methodName) {
public isApplicable(methodName: string, matchers: Matcher[]): boolean {
if (this.methodName !== methodName) {
return false;
}
let allValid = true;
let index: number = 0;
for (let arg of this.args) {
for (const arg of this.args) {
if (matchers[index] && !matchers[index].match(arg)) {
allValid = false;
}
Expand All @@ -23,11 +23,11 @@ export class MethodAction {
return allValid;
}

public getCallIndex():number {
public getCallIndex(): number {
return this.callIndex;
}

public hasBeenCalledBefore(action:MethodAction):boolean {
public hasBeenCalledBefore(action: MethodAction): boolean {
return this.getCallIndex() < action.getCallIndex();
}
}
}
30 changes: 15 additions & 15 deletions src/MethodStubCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ export class MethodStubCollection {
}

public getFirstMatchingFromGroupAndRemoveIfNotLast(groupIndex: number, args: any[]): MethodStub {
let index = this.getFirstMatchingIndexFromGroup(groupIndex, args);
let result = this.getFirstMatchingFromGroup(groupIndex, args);
const index = this.getFirstMatchingIndexFromGroup(groupIndex, args);
const result = this.getFirstMatchingFromGroup(groupIndex, args);
if (index > -1 && this.getItemsCountInGroup(groupIndex) > 1) {
this.items.splice(index, 1);
}
return result;
}

private getFirstMatchingFromGroup(groupIndex: number, args: any[]): MethodStub {
for (let item of this.items) {
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) {
return item;
}
}
return null;
}

public hasMatchingInAnyGroup(args: any[]): boolean {
for (let item of this.items) {
for (const item of this.items) {
if (item.isApplicable(args)) {
return true;
}
}
return false;
}

private getFirstMatchingFromGroup(groupIndex: number, args: any[]): MethodStub {
for (const item of this.items) {
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) {
return item;
}
}
return null;
}

private getFirstMatchingIndexFromGroup(groupIndex: number, args: any[]): number {
let index = 0;
for (let item of this.items) {
for (const item of this.items) {
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) {
return index;
}
Expand All @@ -57,11 +57,11 @@ export class MethodStubCollection {

private getItemsCountInGroup(groupIndex: number): number {
let result = 0;
for (let item of this.items) {
for (const item of this.items) {
if (item.getGroupIndex() === groupIndex) {
result++;
}
}
return result;
}
}
}
8 changes: 4 additions & 4 deletions src/MethodStubSetter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MethodToStub} from "./MethodToStub";
import {CallFunctionMethodStub} from "./stub/CallFunctionMethodStub";
import {ReturnValueMethodStub} from "./stub/ReturnValueMethodStub";
import {ThrowErrorMethodStub} from "./stub/ThrowErrorMethodStub";
import {CallFunctionMethodStub} from "./stub/CallFunctionMethodStub";

export class MethodStubSetter<T> {
private static globalGroupIndex: number = 0;
Expand All @@ -12,14 +12,14 @@ export class MethodStubSetter<T> {
}

public thenReturn(...rest: T[]): this {
for (let value of rest) {
for (const value of rest) {
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub(this.groupIndex, this.methodToStub.matchers, value));
}
return this;
}

public thenThrow(...rest: Error[]): this {
for (let error of rest) {
for (const error of rest) {
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub(this.groupIndex, this.methodToStub.matchers, error));
}
return this;
Expand All @@ -29,4 +29,4 @@ export class MethodStubSetter<T> {
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub(this.groupIndex, this.methodToStub.matchers, func));
return this;
}
}
}
48 changes: 24 additions & 24 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,66 +29,66 @@ export class MethodStubVerificator<T> {
}

public times(value: number): void {
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value !== allMatchingActions.length) {
let 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).');
const 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).`);
}
}

public atLeast(value: number): void {
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value > allMatchingActions.length) {
let 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).');
const 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).`);
}
}

public atMost(value: number): void {
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value < allMatchingActions.length) {
let 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).');
const 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).`);
}
}

public calledBefore(method: any): void {
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
let mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
let secondMethodAsString = this.methodCallToStringConverter.convert(method);
let errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called before ' + secondMethodAsString;
const mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
const secondMethodAsString = this.methodCallToStringConverter.convert(method);
const 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.`);
}
}

public calledAfter(method: any): void {
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
let mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
let secondMethodAsString = this.methodCallToStringConverter.convert(method);
let errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called after ' + secondMethodAsString;
const mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
const secondMethodAsString = this.methodCallToStringConverter.convert(method);
const 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.`);
}
}
}
}
8 changes: 4 additions & 4 deletions src/MethodToStub.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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 class MethodToStub {
constructor(public methodStubCollection: MethodStubCollection,
public matchers: Array<Matcher>,
public mocker: Mocker,
public name: string) {
}
}
}
Loading

0 comments on commit 1ecbb4c

Please sign in to comment.