-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from NagRock/issue/12
2.x version with new mocking and capturing API
- Loading branch information
Showing
20 changed files
with
498 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,67 @@ | ||
import {MethodStub} from './stub/MethodStub'; | ||
import {MethodStub} from "./stub/MethodStub"; | ||
|
||
export class MethodStubCollection { | ||
private hadMoreThanOneBehavior: boolean = false; | ||
private items: Array<MethodStub> = []; | ||
private items: MethodStub[] = []; | ||
|
||
public add(item: MethodStub) { | ||
this.items.push(item); | ||
} | ||
|
||
if(this.items.length > 1) { | ||
this.hadMoreThanOneBehavior = true; | ||
public getLastMatchingGroupIndex(args): number { | ||
for (let i = this.items.length - 1; i >= 0; i--) { | ||
const item = this.items[i]; | ||
if (item.isApplicable(args)) { | ||
return item.getGroupIndex(); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public getFirstMatchingAndRemove(args): MethodStub { | ||
let index = this.getFirstMatchingIndex(args); | ||
let result = this.getFirstMatching(args); | ||
if (index > -1) { | ||
public getFirstMatchingFromGroupAndRemoveIfNotLast(groupIndex: number, args: any[]): MethodStub { | ||
let index = this.getFirstMatchingIndexFromGroup(groupIndex, args); | ||
let result = this.getFirstMatchingFromGroup(groupIndex, args); | ||
if (index > -1 && this.getItemsCountInGroup(groupIndex) > 1) { | ||
this.items.splice(index, 1); | ||
} | ||
return result; | ||
} | ||
|
||
public getFirstMatching(args): MethodStub { | ||
private getFirstMatchingFromGroup(groupIndex: number, args: any[]): MethodStub { | ||
for (let item of this.items) { | ||
if (item.isApplicable(args)) { | ||
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { | ||
return item; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public getHadMoreThanOneBehavior(): boolean { | ||
return this.hadMoreThanOneBehavior; | ||
} | ||
|
||
public hasMatching(args): boolean { | ||
return this.getFirstMatchingIndex(args) > -1; | ||
public hasMatchingInAnyGroup(args: any[]): boolean { | ||
for (let item of this.items) { | ||
if (item.isApplicable(args)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private getFirstMatchingIndex(args): number { | ||
private getFirstMatchingIndexFromGroup(groupIndex: number, args: any[]): number { | ||
let index = 0; | ||
for (let item of this.items) { | ||
if (item.isApplicable(args)) { | ||
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) { | ||
return index; | ||
} | ||
index++; | ||
} | ||
return -1; | ||
} | ||
|
||
private getItemsCountInGroup(groupIndex: number): number { | ||
let result = 0; | ||
for (let item of this.items) { | ||
if (item.getGroupIndex() === groupIndex) { | ||
result++; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
import {MethodToStub} from './MethodToStub'; | ||
import {ReturnValueMethodStub} from './stub/ReturnValueMethodStub'; | ||
import {ThrowErrorMethodStub} from './stub/ThrowErrorMethodStub'; | ||
import {CallFunctionMethodStub} from './stub/CallFunctionMethodStub'; | ||
import {Captor} from './Captor'; | ||
import {CaptorMethodStub} from './stub/CaptorMethodStub'; | ||
import {MethodToStub} from "./MethodToStub"; | ||
import {ReturnValueMethodStub} from "./stub/ReturnValueMethodStub"; | ||
import {ThrowErrorMethodStub} from "./stub/ThrowErrorMethodStub"; | ||
import {CallFunctionMethodStub} from "./stub/CallFunctionMethodStub"; | ||
|
||
export class MethodStubSetter<T> { | ||
constructor(private methodToStub: MethodToStub) { | ||
|
||
} | ||
private static globalGroupIndex: number = 0; | ||
private groupIndex: number; | ||
|
||
public thenReturn(value: T): void { | ||
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub(this.methodToStub.matchers, value)); | ||
constructor(private methodToStub: MethodToStub) { | ||
this.groupIndex = ++MethodStubSetter.globalGroupIndex; | ||
} | ||
|
||
public throwsError(error: Error): void { | ||
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub(this.methodToStub.matchers, error)); | ||
public thenReturn(...rest: T[]): this { | ||
for (let value of rest) { | ||
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub(this.groupIndex, this.methodToStub.matchers, value)); | ||
} | ||
return this; | ||
} | ||
|
||
public thenCall(func: (...args:any[]) => any): void { | ||
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub(this.methodToStub.matchers, func)); | ||
public thenThrow(...rest: Error[]): this { | ||
for (let error of rest) { | ||
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub(this.groupIndex, this.methodToStub.matchers, error)); | ||
} | ||
return this; | ||
} | ||
|
||
public thenCapture(...args:Captor<any>[]):void { | ||
this.methodToStub.methodStubCollection.add(new CaptorMethodStub(this.methodToStub.matchers, args)); | ||
public thenCall(func: (...args: any[]) => any): this { | ||
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub(this.groupIndex, this.methodToStub.matchers, func)); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.