corrade-nucleus-nucleons – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | office | 1 | #!/usr/bin/env node |
2 | |||
3 | /* |
||
4 | The MIT License (MIT) |
||
5 | |||
6 | Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. |
||
7 | |||
8 | Permission is hereby granted, free of charge, to any person |
||
9 | obtaining a copy of this software and associated documentation files |
||
10 | (the "Software"), to deal in the Software without restriction, |
||
11 | including without limitation the rights to use, copy, modify, merge, |
||
12 | publish, distribute, sublicense, and/or sell copies of the Software, |
||
13 | and to permit persons to whom the Software is furnished to do so, |
||
14 | subject to the following conditions: |
||
15 | |||
16 | The above copyright notice and this permission notice shall be |
||
17 | included in all copies or substantial portions of the Software. |
||
18 | |||
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
||
23 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
||
24 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||
25 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
26 | SOFTWARE. |
||
27 | */ |
||
28 | |||
29 | var fs = require('fs'); |
||
30 | var mustache = require('mustache'); |
||
31 | var path = require('path'); |
||
32 | |||
33 | function generate_tests() { |
||
34 | // javascript |
||
35 | generate_test_files('javascript', 'bt', 'js/test/generated/beautify-javascript-tests.js', 'python/jsbeautifier/tests/generated/tests.py'); |
||
36 | |||
37 | // css |
||
38 | generate_test_files('css', 't', 'js/test/generated/beautify-css-tests.js', 'python/cssbeautifier/tests/generated/tests.py'); |
||
39 | |||
40 | // html |
||
41 | // no python html beautifier, so no tests |
||
42 | generate_test_files('html', 'bth', 'js/test/generated/beautify-html-tests.js'); |
||
43 | } |
||
44 | |||
45 | function generate_test_files(data_folder, test_method, node_output, python_output) { |
||
46 | var data_file_path, input_path, template_file_path; |
||
47 | var test_data, template; |
||
48 | |||
49 | input_path = path.resolve(__dirname, 'data', data_folder); |
||
50 | data_file_path = path.resolve(input_path, 'tests.js'); |
||
51 | test_data = require(data_file_path).test_data; |
||
52 | |||
53 | template_file_path = path.resolve(input_path, 'node.mustache'); |
||
54 | template = fs.readFileSync(template_file_path, { encoding: 'utf-8' }); |
||
55 | set_formatters(test_data, test_method, '// '); |
||
56 | set_generated_header(test_data, data_file_path, template_file_path); |
||
57 | fs.writeFileSync(path.resolve(__dirname, '..', node_output), |
||
58 | mustache.render(template, test_data), { encoding: 'utf-8' }); |
||
59 | |||
60 | if (python_output) { |
||
61 | template_file_path = path.resolve(input_path, 'python.mustache'); |
||
62 | template = fs.readFileSync(template_file_path, { encoding: 'utf-8' }); |
||
63 | set_formatters(test_data, test_method, '# '); |
||
64 | set_generated_header(test_data, data_file_path, template_file_path); |
||
65 | fs.writeFileSync(path.resolve(__dirname, '..', python_output), |
||
66 | mustache.render(template, test_data), { encoding: 'utf-8' }); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | function set_generated_header(data, data_file_path, template_file_path) { |
||
71 | var relative_script_path = path.relative(process.cwd(), __filename).split(path.sep).join('/'); |
||
72 | var relative_data_file_path = path.relative(process.cwd(), data_file_path).split(path.sep).join('/'); |
||
73 | var relative_template_file_path = path.relative(process.cwd(), template_file_path).split(path.sep).join('/'); |
||
74 | |||
75 | data.header_text = |
||
76 | ' AUTO-GENERATED. DO NOT MODIFY.\n' + |
||
77 | ' Script: ' + relative_script_path + '\n' + |
||
78 | ' Template: ' + relative_template_file_path + '\n' + |
||
79 | ' Data: ' + relative_data_file_path; |
||
80 | |||
81 | } |
||
82 | |||
83 | function isStringOrArray(val) { |
||
84 | return typeof val === 'string' || val instanceof Array; |
||
85 | } |
||
86 | |||
87 | function getTestString(val) { |
||
88 | val = val.split('\n'); |
||
89 | |||
90 | var result = "'" + val.join("\\n' +\n '").replace(/\t/g, '\\t') + "'"; |
||
91 | result = result.replace(/' \+\n ''$/, "'"); |
||
92 | return result; |
||
93 | } |
||
94 | |||
95 | function set_formatters(data, test_method, comment_mark) { |
||
96 | |||
97 | // utility mustache functions |
||
98 | data.matrix_context_string = function() { |
||
99 | var context = this; |
||
100 | return function(text, render) { |
||
101 | var outputs = []; |
||
102 | // text is ignored for this |
||
103 | for (var name in context) { |
||
104 | if (name === 'options') { |
||
105 | continue; |
||
106 | } |
||
107 | |||
108 | if (context.hasOwnProperty(name)) { |
||
109 | outputs.push(name + ' = "' + context[name].replace(/\n/g, '\\n').replace(/\t/g, '\\t') + '"'); |
||
110 | } |
||
111 | } |
||
112 | return render(outputs.join(', ')); |
||
113 | }; |
||
114 | }; |
||
115 | |||
116 | data.test_line = function() { |
||
117 | return function(text, render) { |
||
118 | var method_text = this.fragment ? 'test_fragment' : test_method; |
||
119 | var comment = ''; |
||
120 | var before_input = method_text + '('; |
||
121 | var input = null; |
||
122 | var before_output = ', '; |
||
123 | var output = null; |
||
124 | |||
125 | // text is ignored for this. |
||
126 | if (typeof this.comment === 'string') { |
||
127 | this.comment = this.comment.split('\n'); |
||
128 | } |
||
129 | |||
130 | if (this.comment instanceof Array) { |
||
131 | comment = '\n ' + comment_mark + this.comment.join('\n ' + comment_mark) + '\n '; |
||
132 | } |
||
133 | |||
134 | // input: the default field |
||
135 | // input_: allow underscore for formatting alignment with "output" |
||
136 | // unchanged: use "unchanged" instead of "input" if there is no output |
||
137 | input = this.input || this.input_ || this.unchanged; |
||
138 | if (input instanceof Array) { |
||
139 | input = input.join('\n'); |
||
140 | } |
||
141 | |||
142 | if (isStringOrArray(this.output)) { |
||
143 | output = this.output; |
||
144 | if (output instanceof Array) { |
||
145 | output = output.join('\n'); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // Do all most error checking |
||
150 | if (!(this.input !== null || this.input_ !== null || this.unchanged !== null)) { |
||
151 | throw "Missing test input field (input, input_, or unchanged)."; |
||
152 | } else if ((this.input !== null && (this.input_ !== null || this.unchanged !== null)) && |
||
153 | (this.input_ === null || this.unchanged === null)) { |
||
154 | throw "Only one test input field allowed (input, input_, or unchanged): " + input; |
||
155 | } else if (output && isStringOrArray(this.unchanged)) { |
||
156 | throw "Cannot specify 'output' with 'unchanged' test input: " + input; |
||
157 | } else if (!output && !isStringOrArray(this.unchanged)) { |
||
158 | throw "Neither 'output' nor 'unchanged' specified for test input: " + input; |
||
159 | } else if (input === output) { |
||
160 | // Raw input and output can be the same, just omit output. |
||
161 | throw "Test strings are identical. Omit 'output' and use 'unchanged': " + input; |
||
162 | } |
||
163 | |||
164 | if (output && output.indexOf('<%') !== -1) { |
||
165 | mustache.tags = ['<%', '%>']; |
||
166 | } |
||
167 | |||
168 | input = getTestString(render(input)); |
||
169 | |||
170 | if (output) { |
||
171 | output = getTestString(render(output)); |
||
172 | } else { |
||
173 | output = ''; |
||
174 | } |
||
175 | |||
176 | if (output && output.indexOf('<%') !== -1) { |
||
177 | mustache.tags = ['{{', '}}']; |
||
178 | } |
||
179 | |||
180 | if (this.input_ || input.indexOf('\n') !== -1 || output.indexOf('\n') !== -1) { |
||
181 | before_input = method_text + '(\n '; |
||
182 | before_output = ',\n ' + comment_mark + ' -- output --\n '; |
||
183 | } |
||
184 | if (output === '') { |
||
185 | before_output = ''; |
||
186 | } |
||
187 | |||
188 | // Rendered input and output can be the same, just omit output. |
||
189 | if (output === input) { |
||
190 | before_output = ''; |
||
191 | output = ''; |
||
192 | } |
||
193 | return comment + before_input + input + before_output + output + ')'; |
||
194 | }; |
||
195 | }; |
||
196 | |||
197 | data.set_mustache_tags = function() { |
||
198 | return function( /* text, render */ ) { |
||
199 | if (this.template) { |
||
200 | mustache.tags = this.template.split(' '); |
||
201 | } |
||
202 | return ''; |
||
203 | }; |
||
204 | }; |
||
205 | |||
206 | data.unset_mustache_tags = function() { |
||
207 | return function( /* text , render */ ) { |
||
208 | if (this.template) { |
||
209 | mustache.tags = ['{{', '}}']; |
||
210 | } |
||
211 | return ''; |
||
212 | }; |
||
213 | }; |
||
214 | } |
||
215 | |||
216 | generate_tests(); |