Skip to content

Commit

Permalink
Add matrix text generation to test infrastructure
Browse files Browse the repository at this point in the history
Relates to beautifier#216
  • Loading branch information
bitwiseman committed Oct 8, 2014
1 parent d437430 commit 4b6909b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 100 deletions.
14 changes: 11 additions & 3 deletions js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,32 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";');
bt('var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};');

// End With Newline
// End With Newline - (eof = "\n")
opts.end_with_newline = true;
test_fragment('', '\n');
test_fragment(' return .5', ' return .5\n');
test_fragment(' \n\nreturn .5\n\n\n\n', ' return .5\n');
test_fragment('\n');

// End With Newline - (eof = "")
opts.end_with_newline = false;
bt('');
test_fragment('');
test_fragment(' return .5');
test_fragment(' \n\nreturn .5\n\n\n\n', ' return .5');
test_fragment('\n', '');
bt('return .5');

// Common smoke tests
bt('');
test_fragment(' return .5');
test_fragment(' return .5;\n a();');
test_fragment(' return .5;\n a();');
test_fragment(' return .5;\n a();');
test_fragment(' < div');
bt('a = 1', 'a = 1');
bt('a=1', 'a = 1');
bt('(3) / 2');
bt('["a", "b"].join("")');

bt("a();\n\nb();", "a();\n\nb();");
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
Expand Down
34 changes: 21 additions & 13 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,32 @@ def unicode_char(value):
bt('var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = "hi";')
bt('var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};')

# End With Newline
self.options.end_with_newline = true;
# End With Newline - (eof = "\n")
self.options.end_with_newline = true
test_fragment('', '\n')
test_fragment(' return .5', ' return .5\n')
test_fragment(' \n\nreturn .5\n\n\n\n', ' return .5\n')
test_fragment('\n')

self.options.end_with_newline = False;
bt('');
test_fragment('\n', '');
bt('return .5');
test_fragment(' return .5');
test_fragment(' return .5;\n a();');
test_fragment(' < div');
bt('a = 1', 'a = 1');
bt('a=1', 'a = 1');
bt('(3) / 2');
bt('["a", "b"].join("")');
# End With Newline - (eof = "")
self.options.end_with_newline = false
test_fragment('')
test_fragment(' return .5')
test_fragment(' \n\nreturn .5\n\n\n\n', ' return .5')
test_fragment('\n', '')

# Common smoke tests
bt('')
test_fragment(' return .5')
test_fragment(' return .5;\n a();')
test_fragment(' return .5;\n a();')
test_fragment(' return .5;\n a();')
test_fragment(' < div')
bt('a = 1', 'a = 1')
bt('a=1', 'a = 1')
bt('(3) / 2')
bt('["a", "b"].join("")')

bt("a();\n\nb();", "a();\n\nb();");
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
Expand Down
131 changes: 94 additions & 37 deletions test/data.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
exports.test_data = {
default_options: [
{name: "indent_size", value: "4"},
{name: "indent_char", value: "' '"},
{name: "preserve_newlines", value: "true"},
{name: "jslint_happy", value: "false"},
{name: "keep_array_indentation", value: "false"},
{name: "brace_style", value: "'collapse'"}
{ name: "indent_size", value: "4" },
{ name: "indent_char", value: "' '" },
{ name: "preserve_newlines", value: "true" },
{ name: "jslint_happy", value: "false" },
{ name: "keep_array_indentation", value: "false" },
{ name: "brace_style", value: "'collapse'" }
],
tests: [{
groups: [{
name: "Unicode Support",
description: "",
values: [
tests: [
{ input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";" },
{ input: "var ' + unicode_char(228) + 'x = {\n ' + unicode_char(228) + 'rgerlich: true\n};" }
],
}, {
name: "End With Newline",
description: "",
matrix: [
{
source: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";"
options: [
{ name: "end_with_newline", value: "true" }
],
eof: '\\n'
}, {
source: "var ' + unicode_char(228) + 'x = {\\n ' + unicode_char(228) + 'rgerlich: true\\n};"
options: [
{ name: "end_with_newline", value: "false" }
],
eof: ''
}

],
tests: [
{ fragment: '', output: '{{eof}}' },
{ fragment: ' return .5', output: ' return .5{{eof}}' },
{ fragment: ' \n\nreturn .5\n\n\n\n', output: ' return .5{{eof}}' },
{ fragment: '\n', output: '{{eof}}' }
],
}, {
name: "End With Newline ",
name: "Common smoke tests",
description: "",
options: [
{name: "end_with_newline", value: "true"}
],
fragments: [
{ source: '', output: '\\n' },
{ source: ' return .5', output: ' return .5\\n' },
{ source: ' \\n\\nreturn .5\\n\\n\\n\\n', output: ' return .5\\n' },
{ source: '\\n', output: '\\n' }
options: [],
tests: [
{ input: '' },
{ fragment: ' return .5'},
{ fragment: ' return .5;\n a();' },
{ fragment: ' return .5;\n a();' },
{ fragment: ' return .5;\n a();' },
{ fragment: ' < div'},
{ input: 'a = 1', output: 'a = 1' },
{ input: 'a=1', output: 'a = 1' },
{ input: '(3) / 2' },
{ input: '["a", "b"].join("")' }
],
}],
// Example
examples: [{
name: "one",
group_name: "one",
description: "",
options: [],
values: [
Expand All @@ -43,25 +67,58 @@ exports.test_data = {
]
}],
// utility mustache functions
get_source: function() {
if (typeof this.source === "string") {
return "'" + this.source + "'";
} else if (this.source instanceof Array) {
return "'" + this.source.join("\\n' +\n '") + "'";
} else {
return "''";
matrix_context_string: function() {
var context = this;
return function(text, render) {
var outputs = [];
// text is ignored for this
for (var name in context) {
if (name === 'options') {
continue;
}

if (context.hasOwnProperty(name)) {
outputs.push(name + ' = "' + context[name] + '"');
}
}
return render(outputs.join(', '));
}
},
get_output: function() {
if (this.output === this.source) {
return "";
}
else if (typeof this.output === "string") {
return ", '" + this.output + "'";
} else if (this.output instanceof Array) {
return ",\n '" + this.output.join("\\n' +\n '") + "'";
} else {
return "";
test_line: function() {
return function(text, render) {
// text is ignored for this.
var method = "bt";
var input = "''";
if (typeof this.input === "string") {
input = "'" + this.input.replace(/\n/g,'\\n') + "'";
} else if (this.input instanceof Array) {
input = "'" + this.input.join("\\n' +\n '") + "'";

} else if (typeof this.fragment === "string") {
method = "test_fragment";
input = "'" + this.fragment.replace(/\n/g,'\\n') + "'";
} else if (this.fragment instanceof Array) {
method = "test_fragment";
input = "'" + this.fragment.join("\\n' +\n '") + "'";
}
input = render(input);

var output = "";
var before_output = "";
if (typeof this.output === "string") {
before_output = ', ';
output = "'" + this.output.replace(/\n/g,'\\n') + "'";
} else if (this.output instanceof Array) {
before_output = ',\n ';
output = "'" + this.output.join("\\n' +\n '") + "'";
}
output = render(output);

if (output === input) {
output = "";
before_output = "";
}
return method + "(" + input + before_output + output + ")";
}
}
}
42 changes: 20 additions & 22 deletions test/js-beautifier-node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -283,31 +283,29 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
{{#default_options}} opts.{{name}} = {{&value}};
{{/default_options}}
{{#tests}}
{{#groups}}
{{^matrix}}

// {{name}}
{{#options}}
// {{&name}}
{{#options}}
opts.{{name}} = {{&value}};
{{/options}}
{{#values}}
bt({{&get_source}}{{&get_output}});
{{/values}}
{{#fragments}}
test_fragment({{&get_source}}{{&get_output}});
{{/fragments}}
{{/tests}}
{{/options}}
{{#tests}}
{{#test_line}}.{{/test_line}};
{{/tests}}
{{/matrix}}
{{#matrix}}

// {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
{{#options}}
opts.{{name}} = {{&value}};
{{/options}}
{{#tests}}
{{#test_line}}.{{/test_line}};
{{/tests}}
{{/matrix}}
{{/groups}}

opts.end_with_newline = false;
bt('');
test_fragment('\n', '');
bt('return .5');
test_fragment(' return .5');
test_fragment(' return .5;\n a();');
test_fragment(' < div');
bt('a = 1', 'a = 1');
bt('a=1', 'a = 1');
bt('(3) / 2');
bt('["a", "b"].join("")');
bt("a();\n\nb();", "a();\n\nb();");
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
Expand Down
48 changes: 23 additions & 25 deletions test/js-beautifier-python.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,29 @@ class TestJSBeautifier(unittest.TestCase):

{{#default_options}} self.options.{{name}} = {{&value}}
{{/default_options}}
{{#tests}}

# {{name}}
{{#options}}
self.options.{{name}} = {{&value}};
{{/options}}
{{#values}}
bt({{&get_source}}{{&get_output}})
{{/values}}
{{#fragments}}
test_fragment({{&get_source}}{{&get_output}})
{{/fragments}}
{{/tests}}

self.options.end_with_newline = False;
bt('');
test_fragment('\n', '');
bt('return .5');
test_fragment(' return .5');
test_fragment(' return .5;\n a();');
test_fragment(' < div');
bt('a = 1', 'a = 1');
bt('a=1', 'a = 1');
bt('(3) / 2');
bt('["a", "b"].join("")');
{{#groups}}
{{^matrix}}

# {{&name}}
{{#options}}
self.options.{{name}} = {{&value}}
{{/options}}
{{#tests}}
{{#test_line}}.{{/test_line}}
{{/tests}}
{{/matrix}}
{{#matrix}}

# {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
{{#options}}
self.options.{{name}} = {{&value}}
{{/options}}
{{#tests}}
{{#test_line}}.{{/test_line}}
{{/tests}}
{{/matrix}}
{{/groups}}

bt("a();\n\nb();", "a();\n\nb();");
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
Expand Down

0 comments on commit 4b6909b

Please sign in to comment.