forked from mongodb-js/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-test-file.js
executable file
·171 lines (151 loc) · 4.21 KB
/
make-test-file.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
/* eslint-disable no-sync */
/* eslint-disable no-console */
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');
const testpath = path.join(__dirname, 'test', 'yaml');
const { singleQuoteStringify, doubleQuoteStringify } = require('./helper/format');
if (process.argv.length !== 3) {
console.log('Usage: <outputLanguage> | pbcopy (or > testfile)');
process.exit();
}
const output = process.argv[2].toLowerCase();
if (!['python', 'csharp', 'java', 'javascript'].includes(output)) {
console.log(`${output} not a supported language`);
process.exit();
}
const readYAML = (filename) => {
let parseResult;
try {
parseResult = yaml.load(fs.readFileSync(filename));
} catch (err) {
err.message = `${filename}: ${err.message}`;
throw err;
}
return parseResult;
};
const getAllImports = () => {
const importtests = readYAML(path.join(testpath, 'imports.yaml'));
return importtests.tests.every[0].output[output];
};
const imports = getAllImports();
const javaFileTemplate = (code) => {
return `${imports}
public class Test {
public void all() throws Exception {
try {
${code}
} catch (Exception e) {
System.out.println("failure");
throw e;
}
}
public static void main(String[] args) throws Exception {
Test test = new Test();
test.all();
}
}`;
};
const javaDocTemplate = (name, code) => {
return `
\t\tDocument ${name} = new Document()${code};`;
};
const javaLineTemplate = (description, result) => {
return `\n\t\t\t.append(${doubleQuoteStringify(description)}, ${result})`;
};
const pythonFileTemplate = (code) => {
return `${imports}
x = {
${code}
}
print(x)
`;
};
const pythonDocTemplate = (name, code) => {
return `\n ${singleQuoteStringify(name)}: {${code}\n },\n`;
};
const pythonLineTemplate = (description, result) => {
return `\n ${singleQuoteStringify(description)}: ${result},`;
};
const csharpFileTemplate = (code) => {
return `${imports}
namespace csharp_test
{
class Program
{
static void Main(string[] args)
{
${code}
Console.WriteLine("executed chsarp testfile");
}
}
}`;
};
const csharpDocTemplate = (name, code) => {
return `
\t\tvar ${name} = new BsonDocument{${code}};`;
};
const csharpLineTemplate = (description, result) => {
return `\n\t\t\t{ ${doubleQuoteStringify(description)}, ${result} },`;
};
const jsFileTemplate = (code) => {
return `${imports}
x = {
${code}
}
console.log(x)
`;
};
const jsDocTemplate = (name, code) => {
return `\n ${singleQuoteStringify(name)}: {${code}\n },\n`;
};
const jsLineTemplate = (description, result) => {
return `\n ${singleQuoteStringify(description)}: ${result},`;
};
const templates = {
python: {
file: pythonFileTemplate,
doc: pythonDocTemplate,
line: pythonLineTemplate
},
java: {
file: javaFileTemplate,
doc: javaDocTemplate,
line: javaLineTemplate
},
csharp: {
file: csharpFileTemplate,
doc: csharpDocTemplate,
line: csharpLineTemplate
},
javascript: {
file: jsFileTemplate,
doc: jsDocTemplate,
line: jsLineTemplate
}
};
const makeFile = (side) => {
return templates[output].file(fs.readdirSync(testpath).reduce(
(str0, file) => {
if (!file.startsWith('error') && !file.startsWith('edge') && !file.startsWith('imports') && !file.startsWith('partial') && !file.startsWith('builders')) {
const tests = readYAML(path.join(testpath, file)).tests;
return str0 + Object.keys(tests).reduce(
(str, key) => {
let name = file.slice(0, -5) + key;
name = name.replace(/\/|-|\s/g, '');
return str + templates[output].doc(name, tests[key].reduce(
(str2, test) => {
if (output in test[side] && test[side][output] !== '') {
const desc = 'description' in test ? test.description : 'x';
const line = templates[output].line(desc, test[side][output]);
return str2 + line;
}
return str2;
}, ''));
}, '');
}
return str0;
}, ''));
};
console.log(makeFile('output'));