Skip to content

Commit bc20362

Browse files
author
Brian Ojeda
committed
Organize and add func() support
1 parent b1f9e89 commit bc20362

File tree

2 files changed

+122
-58
lines changed

2 files changed

+122
-58
lines changed

index.d.ts

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,110 @@
1-
export type TestDouble = Function;
21

3-
export type DoubledObjectWithKey<Key extends string> = {};
2+
//
3+
// types and interfaces
4+
// ----------------------------------------------------------------------------
45

56
export type DoubledObject<Subject> = Subject;
67

7-
export interface TestdoubleConfig {
8-
promiseConstructor?: any;
9-
ignoreWarnings?: boolean;
10-
suppressErrors?: boolean;
11-
}
12-
export function config(config: TestdoubleConfig): void;
13-
14-
declare function functionDouble(name?: string): TestDouble;
15-
export { functionDouble as function };
16-
17-
// When passed class or constructor function
18-
export function object<T>(constructor: { new (...args: any[]): T }): DoubledObject<T>;
19-
20-
// When passed array of props
21-
export function object<Key extends string>(props: Key[]): DoubledObjectWithKey<Key>;
22-
23-
// When passed class or constuctor function name as string value
24-
export function object<T>(object: string): DoubledObject<T>;
8+
export type DoubledObjectWithKey<Key extends string> = {};
259

26-
// When passed general object
27-
export function object<T>(object: T): DoubledObject<T>;
10+
export type TestDouble<Function> = Function;
2811

29-
export interface Stubber {
30-
thenReturn(...args: any[]): TestDouble;
31-
thenDo(f: Function): TestDouble;
32-
thenThrow(e: Error): TestDouble;
33-
thenResolve(v: any): TestDouble;
34-
thenReject(e: Error): TestDouble;
35-
thenCallback(...args: any[]): TestDouble;
12+
interface Call {
13+
context: {};
14+
args: any[];
3615
}
3716

38-
export function callback(...args: any[]): void;
17+
export interface Captor {
18+
capture(): any;
19+
value?: any;
20+
values?: any[];
21+
}
3922

40-
export function when(...args: any[]): Stubber;
23+
export interface Explanation {
24+
callCount: number;
25+
calls: Call[];
26+
description: string;
27+
}
4128

4229
export interface Matchers {
4330
anything(): any;
4431
isA(type: Function): any;
45-
contains(a: string|any[]|{}): any;
32+
contains(a: string | any[] | {}): any;
4633
argThat(matcher: Function): any;
4734
not(v: any): any;
4835
captor(): Captor
4936
}
5037

51-
export interface Captor {
52-
capture(): any;
53-
value?: any;
54-
values?: any[];
55-
}
56-
5738
export const matchers: Matchers;
5839

59-
export function replace(path: string, f?: any): any;
60-
export function replace(path: {}, property: string, f?: any): any;
40+
export interface Stubber {
41+
thenReturn<T>(...args: any[]): TestDouble<T>;
42+
thenDo<T>(f: Function): TestDouble<T>;
43+
thenThrow<T>(e: Error): TestDouble<T>;
44+
thenResolve<T>(v: any): TestDouble<T>;
45+
thenReject<T>(e: Error): TestDouble<T>;
46+
thenCallback<T>(...args: any[]): TestDouble<T>;
47+
}
6148

62-
export function reset(): void;
49+
export interface TestdoubleConfig {
50+
promiseConstructor?: any;
51+
ignoreWarnings?: boolean;
52+
suppressErrors?: boolean;
53+
}
6354

6455
export interface VerificationConfig {
6556
ignoreExtraArgs?: boolean;
6657
times?: number;
6758
}
6859

69-
export function verify(a: any, check?: VerificationConfig): void;
60+
//
61+
// general
62+
// ----------------------------------------------------------------------------
7063

71-
interface Call {
72-
context: {};
73-
args: any[];
74-
}
64+
export function config(config: TestdoubleConfig): void;
7565

76-
export interface Explanation {
77-
callCount: number;
78-
calls: Call[];
79-
description: string;
80-
}
66+
export function reset(): void;
67+
68+
export function explain<T>(f: TestDouble<T>): Explanation;
69+
70+
71+
//
72+
// fake: constructors
73+
// ----------------------------------------------------------------------------
74+
75+
//
76+
// fake: functions
77+
// ----------------------------------------------------------------------------
78+
79+
declare function functionDouble(name?: string): TestDouble<Function>;
8180

82-
export function explain(f: TestDouble): Explanation;
81+
declare function functionDouble<T>(name?: T): TestDouble<T>;
82+
83+
export { functionDouble as function };
84+
export { functionDouble as func };
85+
86+
//
87+
// fake: objects
88+
// ----------------------------------------------------------------------------
89+
90+
export function object<T>(constructor: { new (...args: any[]): T }): DoubledObject<T>;
91+
92+
export function object<Key extends string>(props: Key[]): DoubledObjectWithKey<Key>;
93+
94+
export function object<T>(object: string): DoubledObject<T>;
95+
96+
export function object<T>(object: T): DoubledObject<T>;
97+
98+
//
99+
// stubbing
100+
// ----------------------------------------------------------------------------
101+
102+
export function callback(...args: any[]): void;
103+
104+
export function replace(path: string, f?: any): any;
105+
106+
export function replace(path: {}, property: string, f?: any): any;
107+
108+
export function when(...args: any[]): Stubber;
109+
110+
export function verify(a: any, check?: VerificationConfig): void;

test/src/typescript/test.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,72 @@
11
import * as td from "../../../";
22

3+
// dummies
4+
5+
class Dog {
6+
constructor() {};
7+
bark(): string { return "bark! bark!" }
8+
}
9+
10+
function sum (first: number, second: number): number {
11+
return first + second
12+
}
13+
14+
// td.functions()
15+
316
const f = td.function();
417
td.when(f(10)).thenReturn(10);
518
td.when(f(1)).thenThrow(new Error("ok"));
619
td.when(f(td.matchers.isA(String))).thenDo(function(s: string) { return s; });
720
td.when(f(td.matchers.not(true))).thenResolve("value");
821
td.when(f(td.matchers.not(false))).thenReject(new Error("rejected"));
922

10-
class Dog {
11-
bark() {}
12-
}
23+
const fakeSum = td.function(sum);
24+
td.when(fakeSum(1, 2)).thenReturn(3);
25+
26+
const fakerSum = td.function("sum");
27+
td.when(fakerSum(1, 2)).thenReturn(3);
28+
29+
const fakestSum = td.function("sum");
30+
td.when(fakestSum(1, 2)).thenReturn(3);
31+
32+
// td.func()
33+
34+
const ff = td.func();
35+
td.when(ff(10)).thenReturn(10);
36+
td.when(ff(1)).thenThrow(new Error("ok"));
37+
td.when(ff(td.matchers.isA(String))).thenDo(function(s: string) { return s; });
38+
td.when(ff(td.matchers.not(true))).thenResolve("value");
39+
td.when(ff(td.matchers.not(false))).thenReject(new Error("rejected"));
40+
41+
// td.constructor()
1342

1443
const dog = td.constructor(Dog);
1544
td.when(dog.bark()).thenReturn("bark!");
1645

46+
// td.object()
47+
1748
const bird = td.object({ fly: function(){} });
1849
td.when(bird.fly()).thenReturn("fly!");
1950

20-
class Bear { constructor() {}; sleep() {}; };
21-
const bear = td.object<Bear>("Bear");
22-
td.when(bear.sleep()).thenReturn("zzz");
51+
const bear = td.object<Dog>("Bear");
52+
td.when(bear.bark()).thenReturn("bark!");
53+
54+
// td.replace()
2355

2456
td.replace({}, "prop");
2557
td.replace({}, "prop", 42);
2658
td.replace("../../..");
2759
td.replace("../../../", 42);
2860

61+
// td.verify()
62+
2963
td.verify(f());
3064
td.verify(f(), { times: 1 });
3165
td.verify(f(), { ignoreExtraArgs: false });
3266
td.verify(f(), { ignoreExtraArgs: true, times: 2 });
3367

68+
// td.explain()
69+
3470
const explanation = td.explain(f);
3571
console.log(
3672
explanation.description,

0 commit comments

Comments
 (0)