-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.ts
83 lines (65 loc) · 2.65 KB
/
sample.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as path from 'path';
import { FileAnalyser } from './analyser/FileAnalyser';
import { SourceFileDescriptor } from './descriptors';
import { GeneratorContext } from './generators/GeneratorContext';
import { NodeBuilder } from './generators/NodeBuilder';
import { TestGenerator } from './generators/TestGenerator';
import { SourceFileWriter } from './SourceFileWriter';
import { TemplateParser } from './templates/TemplateParser';
import { TemplateProvider } from './templates/TemplateProvider';
export class Sample {
constructor(no: number) {
}
run(file?: string): void {
// tslint:disable-next-line: no-parameter-reassignment
file = 'sample.ts';
const builder = new NodeBuilder();
const sourceFile = this.getDescriptor(file);
// const methods = sourceFile.classDescriptors
// .map(c => c.methods)
// .reduce((p, c) => p.concat(c));
// const nodes: Node[] = [];
// methods.forEach(m => {
// const items = TestGenerator.generateMethodCall('myNiceObject', m);
// if (items.length > 0) {
// nodes.push(...items);
// }
// });
const testStatementTemplate = `
"describe('\${moduleName}', () => {",
"\tit('', () => {",
"\${testStatement}",
"\t})",
"})"
`;
let targetPath = __dirname; // .replace('\src', '\test');
targetPath = targetPath.replace(/\\[^\\]*$/, '\\test');
// result = dialog.replace(/,\s(\w+)$/, " and $1");
targetPath = `${targetPath}\\${path.basename(file, '.ts')}.test.ts`;
const context = new GeneratorContext(sourceFile, targetPath);
const tg = new TestGenerator();
const generatorResult = tg.generateTests(context);
// const classTest = tg.generateClassTest(
// context.sourceDescriptor,
// context.targetDescriptor,
// sourceFile.classDescriptors[0].className);
// const sourceFilePath = `${__dirname.replace('\dist', '\src')}/${file}.test.ts`;
const templateParser = new TemplateParser(new TemplateProvider());
const x = templateParser.generateFile(generatorResult);
// Write to file
const fileWriter = new SourceFileWriter();
fileWriter.writeString(context.targetDescriptor, x);
// context.targetDescriptor.nodes = classTest.nodes;
// fileWriter.write(context.targetDescriptor);
// const result = NodePrinter.printNode(classTest.nodes);
console.log(x);
}
getDescriptor(file: string): SourceFileDescriptor {
const sourceFilePath = `${__dirname.replace('\dist', '\src')}/${file}`;
// Analyse source file
const fileAnalyser = new FileAnalyser();
return fileAnalyser.analyseFile(sourceFilePath, undefined);
}
}
const sample = new Sample(4);
sample.run();