-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate-tests.js
More file actions
372 lines (324 loc) · 14.4 KB
/
Copy pathgenerate-tests.js
File metadata and controls
372 lines (324 loc) · 14.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env node
/*
The MIT License (MIT)
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
var fs = require('fs');
var mustache = require('mustache');
var path = require('path');
mustache.escape = function(text) {
return text;
};
function generate_tests() {
// javascript
generate_test_files('javascript', 'bt', 'js/test/generated/beautify-javascript-tests.js', 'python/jsbeautifier/tests/generated/tests.py', 'src/test/java/io/beautifier/javascript/GeneratedTests.java');
// css
generate_test_files('css', 't', 'js/test/generated/beautify-css-tests.js', 'python/cssbeautifier/tests/generated/tests.py', 'src/test/java/io/beautifier/css/GeneratedTests.java');
// html
// no python html beautifier, so no tests
generate_test_files('html', 'bth', 'js/test/generated/beautify-html-tests.js', '', 'src/test/java/io/beautifier/html/GeneratedTests.java');
}
function generate_test_files(data_folder, test_method, node_output, python_output, java_output) {
var data_file_path, input_path, template_file_path;
var test_data, template;
input_path = path.resolve(__dirname, 'data', data_folder);
data_file_path = path.resolve(input_path, 'tests.js');
test_data = require(data_file_path).test_data;
template_file_path = path.resolve(input_path, 'node.mustache');
if (fs.existsSync(template_file_path)) {
template = fs.readFileSync(template_file_path, { encoding: 'utf-8' });
set_formatters(test_data, test_method, '// ', data_folder);
set_generated_header(test_data, data_file_path, template_file_path);
fs.mkdirSync(path.resolve(__dirname, '..', node_output, '..'), { recursive: true });
fs.writeFileSync(path.resolve(__dirname, '..', node_output),
mustache.render(template, test_data), { encoding: 'utf-8' });
}
if (python_output) {
template_file_path = path.resolve(input_path, 'python.mustache');
if (fs.existsSync(template_file_path)) {
template = fs.readFileSync(template_file_path, { encoding: 'utf-8' });
set_formatters(test_data, test_method, '# ', data_folder);
set_generated_header(test_data, data_file_path, template_file_path);
fs.mkdirSync(path.resolve(__dirname, '..', python_output, '..'), { recursive: true });
fs.writeFileSync(path.resolve(__dirname, '..', python_output),
mustache.render(template, test_data), { encoding: 'utf-8' });
}
}
if (java_output) {
template_file_path = path.resolve(input_path, 'java.mustache');
if (fs.existsSync(template_file_path)) {
template = fs.readFileSync(template_file_path, { encoding: 'utf-8' });
set_formatters(test_data, test_method, '// ', data_folder, '"');
set_generated_header(test_data, data_file_path, template_file_path);
fs.mkdirSync(path.resolve(__dirname, '..', java_output, '..'), { recursive: true });
fs.writeFileSync(path.resolve(__dirname, '..', java_output),
mustache.render(template, test_data), { encoding: 'utf-8' });
}
}
}
function set_generated_header(data, data_file_path, template_file_path) {
var relative_script_path = path.relative(process.cwd(), __filename).split(path.sep).join('/');
var relative_data_file_path = path.relative(process.cwd(), data_file_path).split(path.sep).join('/');
var relative_template_file_path = path.relative(process.cwd(), template_file_path).split(path.sep).join('/');
data.header_text =
' AUTO-GENERATED. DO NOT MODIFY.\n' +
' Script: ' + relative_script_path + '\n' +
' Template: ' + relative_template_file_path + '\n' +
' Data: ' + relative_data_file_path;
}
function isStringOrArray(val) {
return typeof val === 'string' || val instanceof Array;
}
function getTestString(val, quote) {
val = val.split('\n');
var result = "'" + val.join("\\n' +\n '").replace(/\t/g, '\\t') + "'";
result = result.replace(/' \+\n ''$/, "'");
if (quote === '"') {
result = convertSingleToDoubleQuotes(result);
}
return result;
}
function set_formatters(data, test_method, comment_mark, mode, quote) {
// utility mustache functions
data.matrix_context_string = function() {
var context = this;
return function(text, render) {
var outputs = [];
// text is ignored for this
if (context.options) {
var item;
for (var x = 0; x < context.options.length; x++) {
item = context.options[x];
outputs.push(item.name + ' = "' + item.value.replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[']/g, "\"") + '"');
}
}
return render(outputs.join(', '));
};
};
data.test_line = function() {
return function(text, render) {
var method_text = this.fragment ? 'test_fragment' : test_method;
var comment = '';
var before_input = method_text + '(';
var input = null;
var before_output = ', ';
var output = null;
// text is ignored for this.
if (typeof this.comment === 'string') {
this.comment = this.comment.split('\n');
}
if (this.comment instanceof Array) {
comment = '\n ' + comment_mark + this.comment.join('\n ' + comment_mark) + '\n ';
}
// input: the default field
// input_: allow underscore for formatting alignment with "output"
// unchanged: use "unchanged" instead of "input" if there is no output
input = this.input || this.input_ || this.unchanged;
if (input instanceof Array) {
input = input.join('\n');
}
if (isStringOrArray(this.output)) {
output = this.output;
if (output instanceof Array) {
output = output.join('\n');
}
}
// Do all most error checking
if (!(this.input !== null || this.input_ !== null || this.unchanged !== null)) {
throw "Missing test input field (input, input_, or unchanged).";
} else if ((this.input !== null && (this.input_ !== null || this.unchanged !== null)) &&
(this.input_ === null || this.unchanged === null)) {
throw "Only one test input field allowed (input, input_, or unchanged): " + input;
} else if (output && isStringOrArray(this.unchanged)) {
throw "Cannot specify 'output' with 'unchanged' test input: " + input;
} else if (!output && !isStringOrArray(this.unchanged)) {
throw "Neither 'output' nor 'unchanged' specified for test input: " + input;
} else if (input === output) {
// Raw input and output can be the same, just omit output.
throw "Test strings are identical. Omit 'output' and use 'unchanged': " + input;
}
input = getTestString(render(input), quote);
if (output) {
output = getTestString(render(output), quote);
} else {
output = '';
}
if (this.input_ || input.indexOf('\n') !== -1 || output.indexOf('\n') !== -1) {
before_input = method_text + '(\n ';
before_output = ',\n ' + comment_mark + ' -- output --\n ';
}
if (output === '') {
before_output = '';
}
// Rendered input and output can be the same, just omit output.
if (output === input) {
before_output = '';
output = '';
}
return comment + before_input + input + before_output + output + ')';
};
};
data.set_mustache_tags = function() {
return function( /* text, render */ ) {
if (this.template) {
mustache.tags = this.template.split(' ');
}
return '';
};
};
data.unset_mustache_tags = function() {
return function( /* text , render */ ) {
if (this.template) {
mustache.tags = ['{{', '}}'];
}
return '';
};
};
var generated_java_identifiers = new Set();
data.java_identifier = function() {
return function(text, render) {
const saveMustacheTags = mustache.tags;
mustache.tags = ['{{', '}}'];
var identifier = render(text).replace(/[^a-zA-Z0-9_]+/g, '_').replace(/^[0-9]/, '_$0');
mustache.tags = saveMustacheTags;
if (!identifier) {
identifier = "untitled";
}
if (!generated_java_identifiers.has(identifier)) {
generated_java_identifiers.add(identifier);
return identifier;
}
for (var counter = 1; true; counter++) {
if (!generated_java_identifiers.has(identifier + counter)) {
generated_java_identifiers.add(identifier + counter);
return identifier + counter;
}
}
};
};
data.java_escape_string = function() {
return function(text, render) {
const saveMustacheTags = mustache.tags;
mustache.tags = ['{{', '}}'];
var result = render(text);
mustache.tags = saveMustacheTags;
result = result.replace(/"/g, "\\\"");
return result;
};
};
data.java_opt = function() {
return function(text, render) {
const saveMustacheTags = mustache.tags;
mustache.tags = ['{{', '}}'];
var result = render(text);
mustache.tags = saveMustacheTags;
const match = result.match(/opts.([a-zA-Z0-9_]+) = (.*)/);
const name = match[1];
const value = match[2];
if (name === 'brace_style') {
const statements = [];
const brace_style_split = value.replace(/["']/g, "").split(/\s*,\s*/);
for (var bs = 0; bs < brace_style_split.length; bs++) {
if (brace_style_split[bs] === 'preserve-inline') {
statements.push('opts.brace_preserve_inline = true');
} else if (brace_style_split[bs] === 'end-expand') {
statements.push(`opts.brace_style = BraceStyle.endExpand`);
} else if (brace_style_split[bs] === 'collapse-preserve-inline') {
statements.push('opts.brace_preserve_inline = true');
statements.push('opts.brace_style = BraceStyle.collapse');
} else {
statements.push(`opts.brace_style = BraceStyle.${brace_style_split[bs]}`)
}
}
return statements.join('; ');
} else if (name === 'operator_position') {
if (value === "'before-newline'") {
return 'opts.operator_position = OperatorPosition.beforeNewline';
} else if (value === "'after-newline'") {
return 'opts.operator_position = OperatorPosition.afterNewline';
} else if (value === "'preserve-newline'") {
return 'opts.operator_position = OperatorPosition.preserveNewline';
} else {
throw new Error(`Unsupported operator_position: ${value}`);
}
} else if (name === 'wrap_attributes') {
if (value === "'auto'") {
return 'opts.wrap_attributes = WrapAttributes.auto';
} else if (value === "'force'") {
return 'opts.wrap_attributes = WrapAttributes.force';
} else if (value === "'force-aligned'") {
return 'opts.wrap_attributes = WrapAttributes.forceAligned';
} else if (value === "'force-expand-multiline'") {
return 'opts.wrap_attributes = WrapAttributes.forceExpandMultiline';
} else if (value === "'aligned-multiple'") {
return 'opts.wrap_attributes = WrapAttributes.alignedMultiple';
} else if (value === "'preserve'") {
return 'opts.wrap_attributes = WrapAttributes.preserve';
} else if (value === "'preserve-aligned'") {
return 'opts.wrap_attributes = WrapAttributes.preserveAligned';
} else {
throw new Error(`Unsupported wrap_attributes: ${value}`);
}
} else if (name == 'indent_scripts') {
if (value === "'normal'") {
return 'opts.indent_scripts = IndentScripts.normal';
} else if (value === "'keep'") {
return 'opts.indent_scripts = IndentScripts.keep';
} else if (value === "'separate'") {
return 'opts.indent_scripts = IndentScripts.separate';
} else {
throw new Error(`Unsupported indent_scripts: ${value}`);
}
} else if (name === 'templating') {
return `opts.${name} = EnumSet.of(${value.replace(/[\[\]]/g, '').split(/, ?/).map(t => `TemplateLanguage.valueOf(${convertSingleToDoubleQuotes(t)})`)})`
} else if (name === 'extra_liners' || name === 'unformatted' || name == 'content_unformatted') {
if (value === 'null') {
return `opts.${name} = null`;
} else if (value.match(/^'[^']*'$/)) {
return `opts.${name} = new java.util.HashSet<>(java.util.Arrays.asList(${'"' + value.replace(/^'/, '').replace(/'$/, '').split(/[^a-zA-Z0-9_\/\-]+/).join('", "') + '"'}))`
} else {
return `opts.${name} = new java.util.HashSet<>(java.util.Arrays.asList(${convertSingleToDoubleQuotes(value.replace(/[\[\]]/g, ''))}))`
}
} else if (name === 'max_preserve_newlines') {
return `opts.${name} = ${!value || value === 'null' ? 32786 : value}`
} else if (name === 'js') {
return `opts.js().apply(new JSONObject("${value}"))`
} else if (name === 'css') {
return `opts.css().apply(new JSONObject("${value}"))`
} else if (name === 'html') {
return `opts.html().apply(new JSONObject("${value}"))`
} else {
return `opts.${name} = ${convertSingleToDoubleQuotes(value)}`;
}
}
}
}
function convertSingleToDoubleQuotes(str) {
if (str.match(/^".*"$/)) {
return str;
}
var result = str;
result = result.replace(/"/g, "TEMP_REPLACED_DOUBLE_QUOTE");
result = result.replace(/(?<!(^|[^\\])\\)'/g, '"');
result = result.replace(/TEMP_REPLACED_DOUBLE_QUOTE/g, "\\\"");
return result;
}
generate_tests();