-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoFixture.ts
53 lines (42 loc) · 1.54 KB
/
AutoFixture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import IAutoFixture from './IAutoFixture';
import IObjectBuilder from './IObjectBuilder';
import ISpecimen from './ISpecimen';
import getRandomInt from './getRandomInt';
import ObjectBuilder from './ObjectBuilder';
import specimensFactory from './specimens';
export { default as IAutoFixture } from './IAutoFixture';
export { default as IObjectBuilder } from './IObjectBuilder';
export { default as ISpecimen } from './ISpecimen';
export default class AutoFixture implements IAutoFixture {
private specimens: Array<ISpecimen<any>>;
constructor() {
this.specimens = specimensFactory(this.builderFactory.bind(this));
}
public create<T>(typeInfo: any): T {
return this.createInternal<T>(typeInfo, arguments);
}
public createMany<T>(typeInfo: any, manyCount?: number): T[] {
const count = manyCount ? manyCount : getRandomInt(3, 10);
const accum = Array(Math.max(0, count));
for (let i = 0; i < count; i++) {
accum[i] = this.createInternal(typeInfo, arguments);
}
return accum;
}
public build<T>() {
return this.builderFactory<T>();
}
private builderFactory<T>(): IObjectBuilder<T> {
return new ObjectBuilder(this);
}
private createInternal<T>(typeInfo: any, providedArgs: IArguments): T {
const validSpeciments = this.specimens.filter(specimen =>
specimen.handles(typeInfo)
);
if (validSpeciments.length === 0) {
throw new Error('Unsupported Specimen: ' + typeInfo);
}
const [, ...tailArgs] = providedArgs;
return validSpeciments[0].create(typeInfo, tailArgs);
}
}