Skip to content

Commit 1e793c4

Browse files
committed
argument stuff.
1 parent cf47cc2 commit 1e793c4

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

src/Arguments.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
export class Arg {
2-
private constructor(
3-
private description: string,
4-
private matchingFunction: (arg: any) => boolean
5-
) {
6-
}
7-
8-
matches(arg: any) {
9-
return this.matchingFunction(arg);
10-
}
11-
12-
toString() {
13-
return this.description;
14-
}
15-
16-
inspect() {
17-
return this.description;
18-
}
19-
202
static any()
213
static any<T extends 'string'|'number'|'boolean'|'symbol'|'undefined'|'object'|'function'|'array'>(type: T)
22-
static any(type?: string): any {
4+
static any(type?: string): Argument<any> {
235
const description = !type ? '{any arg}' : '{arg matching ' + type + '}';
24-
return new Arg(description, x => {
6+
return new Argument<any>(description, x => {
257
if(typeof type === 'string')
268
return true;
279

@@ -35,8 +17,8 @@ export class Arg {
3517
});
3618
}
3719

38-
static is<T>(predicate: (input: T) => boolean): Arg & T {
39-
return new Arg('{arg matching predicate ' + this.toStringify(predicate) + '}', predicate) as any;
20+
static is<T>(predicate: (input: T) => boolean): Argument<T> {
21+
return new Argument<T>('{arg matching predicate ' + this.toStringify(predicate) + '}', predicate) as any;
4022
}
4123

4224
private static toStringify(obj: any) {
@@ -48,4 +30,24 @@ export class Arg {
4830

4931
return obj;
5032
}
33+
}
34+
35+
export class Argument<T> {
36+
constructor(
37+
private description: string,
38+
private matchingFunction: (arg: T) => boolean
39+
) {
40+
}
41+
42+
matches(arg: T) {
43+
return this.matchingFunction(arg);
44+
}
45+
46+
toString() {
47+
return this.description;
48+
}
49+
50+
inspect() {
51+
return this.description;
52+
}
5153
}

src/Transformations.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
export type FunctionSubstitute<F extends any[], T> = (...args: F) => (T & {
2-
returns: (...args: T[]) => void;
3-
})
1+
import { Argument } from "./Arguments";
42

5-
export type PropertySubstitute<T> = T & {
6-
returns: (...args: T[]) => void;
7-
}
3+
export type FunctionSubstitute<F extends any[], T> = (...args: F) => (T & MockMethodMixin<T>);
4+
export type PropertySubstitute<T> = T & MockMethodMixin<T>;
85

96
export type ObjectSubstitute<T extends Object> = ObjectSubstituteTransformation<T> & {
107
received(amount?: number): T;
@@ -14,4 +11,10 @@ type ObjectSubstituteTransformation<T extends Object> = {
1411
[P in keyof T]:
1512
T[P] extends (...args: infer F) => infer R ? FunctionSubstitute<F, R> :
1613
PropertySubstitute<T[P]>;
14+
}
15+
16+
type MockArgumentInput<T> = T | Argument<T>;
17+
18+
type MockMethodMixin<TReturnType> = {
19+
returns: (...args: TReturnType[]) => void;
1720
}

0 commit comments

Comments
 (0)