Skip to content

Commit

Permalink
Rewrote test line generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Feb 19, 2017
1 parent 891f45d commit 481f4d3
Show file tree
Hide file tree
Showing 8 changed files with 6,361 additions and 1,000 deletions.
791 changes: 716 additions & 75 deletions js/test/generated/beautify-css-tests.js

Large diffs are not rendered by default.

1,308 changes: 1,126 additions & 182 deletions js/test/generated/beautify-html-tests.js

Large diffs are not rendered by default.

2,158 changes: 1,863 additions & 295 deletions js/test/generated/beautify-javascript-tests.js

Large diffs are not rendered by default.

791 changes: 716 additions & 75 deletions python/cssbeautifier/tests/generated/tests.py

Large diffs are not rendered by default.

2,158 changes: 1,863 additions & 295 deletions python/jsbeautifier/tests/generated/tests.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/data/css/tests.js
Expand Up @@ -275,7 +275,7 @@ exports.test_data = {
{ input: '.tabs ( ) { }', output: '.tabs () {}' },
{ input: '.tabs( ) { }', output: '.tabs() {}' },
{ input: '.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', output: '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}' },
{ input: '.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}' }
{ unchanged: '.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}' }
],
}, {
name: "Comments",
Expand Down
44 changes: 22 additions & 22 deletions test/data/javascript/tests.js
Expand Up @@ -40,9 +40,9 @@ exports.test_data = {
name: "Unicode Support",
description: "",
tests: [{
input: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";"
unchanged: "var ' + unicode_char(3232) + '_' + unicode_char(3232) + ' = \"hi\";"
}, {
input: [
unchanged: [
"var ' + unicode_char(228) + 'x = {",
" ' + unicode_char(228) + 'rgerlich: true",
"};"
Expand All @@ -54,8 +54,8 @@ exports.test_data = {
tests: [
{ unchanged: '`This is a ${template} string.`' },
{ unchanged: '`This\n is\n a\n ${template}\n string.`' },
{ unchanged: 'a = `This is a continuation\\\nstring.`' },
{ unchanged: 'a = "This is a continuation\\\nstring."' },
{ unchanged: 'a = `This is a continuation\\\\\nstring.`' },
{ unchanged: 'a = "This is a continuation\\\\\nstring."' },
{ unchanged: '`SELECT\n nextval(\\\'${this.options.schema ? `${this.options.schema}.` : \\\'\\\'}"${this.tableName}_${this.autoIncrementField}_seq"\\\'::regclass\n ) nextval;`' },
{
comment: 'Tests for #1030',
Expand Down Expand Up @@ -339,7 +339,7 @@ exports.test_data = {
{ input: 'var a=1, b=c[d], e=6;', output: 'var a = 1{{c1}}b = c[d]{{c1}}e = 6;' },
{ input: "for(var a=1,b=2,c=3;d<3;d++)\ne", output: "for (var a = 1, b = 2, c = 3; d < 3; d++)\n e" },
{ input: "for(var a=1,b=2,\nc=3;d<3;d++)\ne", output: "for (var a = 1, b = 2{{c2}}c = 3; d < 3; d++)\n e" },
{ input: 'function foo() {\n return [\n "one"{{c2}}"two"\n ];\n}' },
{ unchanged: 'function foo() {\n return [\n "one"{{c2}}"two"\n ];\n}' },
{ input: 'a=[[1,2],[4,5],[7,8]]', output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}[7, 8]\n]" },
{ input: 'a=[[1,2],[4,5],[7,8],]', output: "a = [\n [1, 2]{{c1}}[4, 5]{{c1}}[7, 8]{{c0}}]" },
{
Expand Down Expand Up @@ -730,12 +730,12 @@ exports.test_data = {
name: "Async / await tests",
description: "ES7 async / await tests",
tests: [
{ input: "async function foo() {}" },
{ input: "let w = async function foo() {}" },
{ input: "async function foo() {}\nvar x = await foo();" },
{ unchanged: "async function foo() {}" },
{ unchanged: "let w = async function foo() {}" },
{ unchanged: "async function foo() {}\nvar x = await foo();" },
{
comment: "async function as an input to another function",
input: "wrapper(async function foo() {})"
unchanged: "wrapper(async function foo() {})"
},
{
comment: "await on inline anonymous function. should have a space after await",
Expand All @@ -744,7 +744,7 @@ exports.test_data = {
},
{
comment: "ensure that this doesn't break anyone with the async library",
input: "async.map(function(t) {})"
unchanged: "async.map(function(t) {})"
}
]
}, {
Expand Down Expand Up @@ -2571,7 +2571,7 @@ exports.test_data = {
{ input: 'a = 1', output: 'a = 1' },
{ input: 'a=1', output: 'a = 1' },
{ unchanged: '(3) / 2' },
{ input: '["a", "b"].join("")' },
{ unchanged: '["a", "b"].join("")' },
{ unchanged: 'a();\n\nb();' },
{ input: 'var a = 1 var b = 2', output: 'var a = 1\nvar b = 2' },
{ input: 'var a=1, b=c[d], e=6;', output: 'var a = 1,\n b = c[d],\n e = 6;' },
Expand All @@ -2591,7 +2591,7 @@ exports.test_data = {
{ input: 'var a = 1 if (2) 3;', output: 'var a = 1\nif (2) 3;' },
{ unchanged: 'a = a + 1' },
{ unchanged: 'a = a == 1' },
{ input: '/12345[^678]*9+/.match(a)' },
{ unchanged: '/12345[^678]*9+/.match(a)' },
{ unchanged: 'a /= 5' },
{ unchanged: 'a = 0.5 * 3' },
{ unchanged: 'a *= 10.55' },
Expand Down Expand Up @@ -2713,7 +2713,7 @@ exports.test_data = {
{ input: 'x={a:1,b:w=="foo"?x:y,c:z}', output: 'x = {\n a: 1,\n b: w == "foo" ? x : y,\n c: z\n}' },
{ input: 'x=a?b?c?d:e:f:g;', output: 'x = a ? b ? c ? d : e : f : g;' },
{ input: 'x=a?b?c?d:{e1:1,e2:2}:f:g;', output: 'x = a ? b ? c ? d : {\n e1: 1,\n e2: 2\n} : f : g;' },
{ input: 'function void(void) {}' },
{ unchanged: 'function void(void) {}' },
{ input: 'if(!a)foo();', output: 'if (!a) foo();' },
{ input: 'a=~a', output: 'a = ~a' },
{ input: 'a;/*comment*/b;', output: "a; /*comment*/\nb;" },
Expand All @@ -2733,31 +2733,31 @@ exports.test_data = {
{ input: 'for(;;++i)a', output: 'for (;; ++i) a' },
{ input: 'return(1)', output: 'return (1)' },
{ input: 'try{a();}catch(b){c();}finally{d();}', output: "try {\n a();\n} catch (b) {\n c();\n} finally {\n d();\n}" },
{ input: '(xx)()', comment: ' magic function call' },
{ input: 'a[1]()', comment: 'another magic function call' },
{ unchanged: '(xx)()', comment: ' magic function call' },
{ unchanged: 'a[1]()', comment: 'another magic function call' },
{ input: 'if(a){b();}else if(c) foo();', output: "if (a) {\n b();\n} else if (c) foo();" },
{ input: 'switch(x) {case 0: case 1: a(); break; default: break}', output: "switch (x) {\n case 0:\n case 1:\n a();\n break;\n default:\n break\n}" },
{ input: 'switch(x){case -1:break;case !y:break;}', output: 'switch (x) {\n case -1:\n break;\n case !y:\n break;\n}' },
{ input: 'a !== b' },
{ unchanged: 'a !== b' },
{ input: 'if (a) b(); else c();', output: "if (a) b();\nelse c();" },
{ input: "// comment\n(function something() {})", comment: 'typical greasemonkey start' },
{ input: "{\n\n x();\n\n}", comment: 'duplicating newlines' },
{ unchanged: "// comment\n(function something() {})", comment: 'typical greasemonkey start' },
{ unchanged: "{\n\n x();\n\n}", comment: 'duplicating newlines' },
{ unchanged: 'if (a in b) foo();' },
{ unchanged: 'if (a of b) foo();' },
{ unchanged: 'if (a of [1, 2, 3]) foo();' },
{
input: 'if(X)if(Y)a();else b();else c();',
output: "if (X)\n if (Y) a();\n else b();\nelse c();"
},
{ input: 'if (foo) bar();\nelse break' },
{ unchanged: 'if (foo) bar();\nelse break' },
{ unchanged: 'var a, b;' },
{ unchanged: 'var a = new function();' },
{ fragment: true, unchanged: 'new function' },
{ unchanged: 'var a, b' },
{ input: '{a:1, b:2}', output: "{\n a: 1,\n b: 2\n}" },
{ input: 'a={1:[-1],2:[+1]}', output: 'a = {\n 1: [-1],\n 2: [+1]\n}' },
{ input: "var l = {\\'a\\':\\'1\\', \\'b\\':\\'2\\'}", output: "var l = {\n \\'a\\': \\'1\\',\n \\'b\\': \\'2\\'\n}" },
{ input: 'if (template.user[n] in bk) foo();' },
{ unchanged: 'if (template.user[n] in bk) foo();' },
{ unchanged: 'return 45' },
{ unchanged: 'return this.prevObject ||\n\n this.constructor(null);' },
{ unchanged: 'If[1]' },
Expand All @@ -2783,7 +2783,7 @@ exports.test_data = {

{
comment: 'was: extra space appended',
input: "if (a) {\n do();\n}"
unchanged: "if (a) {\n do();\n}"
},

{
Expand Down Expand Up @@ -2879,7 +2879,7 @@ exports.test_data = {
{ fragment: true, unchanged: "#" },
{ fragment: true, unchanged: "#!" },

{ input: "function namespace::something()" },
{ unchanged: "function namespace::something()" },

{ fragment: true, unchanged: "<!--\nsomething();\n-->" },
{ fragment: true, input: "<!--\nif(i<0){bla();}\n-->", output: "<!--\nif (i < 0) {\n bla();\n}\n-->" },
Expand Down
109 changes: 54 additions & 55 deletions test/generate-tests.js
Expand Up @@ -85,13 +85,11 @@ function isStringOrArray(val) {
}

function getTestString(val) {
if (typeof val === 'string') {
return "'" + val.replace(/\n/g, '\\n').replace(/\t/g, '\\t') + "'";
} else if (val instanceof Array) {
return "'" + val.join("\\n' +\n '").replace(/\t/g, '\\t') + "'";
} else {
return null;
}
val = val.split('\n');

var result = "'" + val.join("\\n' +\n '").replace(/\t/g, '\\t') + "'";
result = result.replace(/' \+\n ''$/, "'");
return result;
}

function set_formatters(data, test_method, comment_mark) {
Expand All @@ -108,7 +106,7 @@ function set_formatters(data, test_method, comment_mark) {
}

if (context.hasOwnProperty(name)) {
outputs.push(name + ' = "' + context[name] + '"');
outputs.push(name + ' = "' + context[name].replace(/\n/g, '\\n').replace(/\t/g, '\\t') + '"');
}
}
return render(outputs.join(', '));
Expand All @@ -117,76 +115,77 @@ function set_formatters(data, test_method, comment_mark) {

data.test_line = function() {
return function(text, render) {
var method_text = test_method;
if (this.fragment) {
method_text = 'test_fragment';
}
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.
var comment = '';
if (typeof this.comment === 'string') {
comment = '\n ' + comment_mark + this.comment + '\n ';
} else if (this.comment instanceof Array) {
comment = '\n ' + comment_mark + this.comment.join('\n ' + comment_mark) + '\n ';
this.comment = this.comment.split('\n');
}

var input = null;
var before_input = method_text + '(';
var before_output = ', ';
if (this.comment instanceof Array) {
comment = '\n ' + comment_mark + this.comment.join('\n ' + comment_mark) + '\n ';
}

function set_input(field, opt_newline) {
if (input !== null && isStringOrArray(field)) {
throw "Only one test input field allowed (input, input_, or unchanged): " + input;
}
// 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 (typeof field === 'string' && !opt_newline) {
input = getTestString(field);
} else if (field instanceof Array || (typeof field === 'string' && opt_newline)) {
before_input = method_text + '(\n ';
before_output = ',\n ';
input = getTestString(field);
if (isStringOrArray(this.output)) {
output = this.output;
if (output instanceof Array) {
output = output.join('\n');
}
}

set_input(this.input);

// allow underscore for formatting alignment with "output"
set_input(this.input_, true);

// use "unchanged" instead of "input" if there is no output
set_input(this.unchanged);
if (isStringOrArray(this.unchanged) && isStringOrArray(this.output)) {
// 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;
}

if (input === null) {
throw "Missing test input field (input, input_, or unchanged).";
if (output && output.indexOf('<%') !== -1) {
mustache.tags = ['<%', '%>'];
}

var output = null;
if (typeof this.output === 'string') {
output = getTestString(this.output);
} else if (this.output instanceof Array) {
before_input = method_text + '(\n ';
before_output = ',\n ';
output = getTestString(this.output);
input = getTestString(render(input));

if (output) {
output = getTestString(render(output));
} else {
before_output = '';
output = '';
}

if (input === output) {
throw "Test strings are identical. Omit 'output' and use 'unchanged': " + input;
if (output && output.indexOf('<%') !== -1) {
mustache.tags = ['{{', '}}'];
}

if (output && output.indexOf('<%') !== -1) {
mustache.tags = ['<%', '%>'];
if (this.input_ || input.indexOf('\n') !== -1 || output.indexOf('\n') !== -1) {
before_input = method_text + '(\n ';
before_output = ',\n ' + comment_mark + ' -- output --\n ';
}
input = render(input);
output = render(output);
if (output && output.indexOf('<%') !== -1) {
mustache.tags = ['{{', '}}'];
if (output === '') {
before_output = '';
}

// Rendered input and output can be the same, just omit output.
if (output === input) {
before_output = '';
output = '';
Expand Down

0 comments on commit 481f4d3

Please sign in to comment.