generate-tests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env node
  2. var fs = require('fs');
  3. var mustache = require('mustache');
  4. function generate_tests() {
  5. var test_data, template;
  6. // javascript
  7. test_data = require(__dirname + '/data/javascript.js').test_data;
  8. set_formatters(test_data, 'bt', '// ')
  9. template = fs.readFileSync(__dirname + '/template/node-javascript.mustache', {encoding: 'utf-8'});
  10. fs.writeFileSync(__dirname + '/../js/test/beautify-javascript-tests.js', mustache.render(template, test_data), {encoding: 'utf-8'});
  11. set_formatters(test_data, 'bt', '# ')
  12. template = fs.readFileSync(__dirname + '/template/python-javascript.mustache', {encoding: 'utf-8'});
  13. fs.writeFileSync(__dirname + '/../python/jsbeautifier/tests/testjsbeautifier.py', mustache.render(template, test_data), {encoding: 'utf-8'});
  14. // css
  15. test_data = require(__dirname + '/data/css.js').test_data;
  16. set_formatters(test_data, 't', '// ')
  17. template = fs.readFileSync(__dirname + '/template/node-css.mustache', {encoding: 'utf-8'});
  18. fs.writeFileSync(__dirname + '/../js/test/beautify-css-tests.js', mustache.render(template, test_data), {encoding: 'utf-8'});
  19. set_formatters(test_data, 't', '# ')
  20. template = fs.readFileSync(__dirname + '/template/python-css.mustache', {encoding: 'utf-8'});
  21. fs.writeFileSync(__dirname + '/../python/cssbeautifier/tests/test.py', mustache.render(template, test_data), {encoding: 'utf-8'});
  22. // html
  23. test_data = require(__dirname + '/data/html.js').test_data;
  24. set_formatters(test_data, 'bth', '// ')
  25. template = fs.readFileSync(__dirname + '/template/node-html.mustache', {encoding: 'utf-8'});
  26. fs.writeFileSync(__dirname + '/../js/test/beautify-html-tests.js', mustache.render(template, test_data), {encoding: 'utf-8'});
  27. // no python html beautifier, so no tests
  28. }
  29. function isStringOrArray(val) {
  30. return typeof val === 'string' || val instanceof Array;
  31. }
  32. function getTestString(val) {
  33. if (typeof val === 'string') {
  34. return "'" + val.replace(/\n/g,'\\n').replace(/\t/g,'\\t') + "'";
  35. } else if (val instanceof Array) {
  36. return "'" + val.join("\\n' +\n '").replace(/\t/g,'\\t') + "'";
  37. } else {
  38. return null;
  39. }
  40. }
  41. function set_formatters (data, test_method, comment_mark) {
  42. // utility mustache functions
  43. data.matrix_context_string = function() {
  44. var context = this;
  45. return function(text, render) {
  46. var outputs = [];
  47. // text is ignored for this
  48. for (var name in context) {
  49. if (name === 'options') {
  50. continue;
  51. }
  52. if (context.hasOwnProperty(name)) {
  53. outputs.push(name + ' = "' + context[name] + '"');
  54. }
  55. }
  56. return render(outputs.join(', '));
  57. }
  58. };
  59. data.test_line = function() {
  60. return function(text, render) {
  61. var method_text = test_method;
  62. if (this.fragment) {
  63. method_text = 'test_fragment';
  64. }
  65. // text is ignored for this.
  66. var comment = '';
  67. if (typeof this.comment === 'string') {
  68. comment = '\n ' + comment_mark + this.comment + '\n ';
  69. } else if (this.comment instanceof Array) {
  70. comment = '\n ' + comment_mark + this.comment.join('\n ' + comment_mark) + '\n ';
  71. }
  72. var input = null;
  73. var before_input = method_text + '(';
  74. var before_output = ', ';
  75. function set_input(field, opt_newline) {
  76. if (input !== null && isStringOrArray(field)) {
  77. throw "Only one test input field allowed (input, input_, or unchanged): " + input;
  78. }
  79. if (typeof field === 'string' && !opt_newline) {
  80. input = getTestString(field);
  81. } else if (field instanceof Array || (typeof field === 'string' && opt_newline)) {
  82. before_input = method_text + '(\n ';
  83. before_output = ',\n ';
  84. input = getTestString(field);
  85. }
  86. }
  87. set_input(this.input);
  88. // allow underscore for formatting alignment with "output"
  89. set_input(this.input_, true);
  90. // use "unchanged" instead of "input" if there is no output
  91. set_input(this.unchanged);
  92. if(isStringOrArray(this.unchanged) && isStringOrArray(this.output)) {
  93. throw "Cannot specify 'output' with 'unchanged' test input: " + input;
  94. }
  95. if (input === null) {
  96. throw "Missing test input field (input, input_, or unchanged).";
  97. }
  98. var output = null;
  99. if (typeof this.output === 'string') {
  100. output = getTestString(this.output);
  101. } else if (this.output instanceof Array) {
  102. before_input = method_text + '(\n ';
  103. before_output = ',\n ';
  104. output = getTestString(this.output);
  105. } else {
  106. before_output = '';
  107. }
  108. if (input === output) {
  109. throw "Test strings are identical. Omit 'output' and use 'unchanged': " + input;
  110. }
  111. if(output && output.indexOf('<%') !== -1) {
  112. mustache.tags = ['<%', '%>'];
  113. }
  114. input = render(input);
  115. output = render(output);
  116. if(output && output.indexOf('<%') !== -1) {
  117. mustache.tags = ['{{', '}}'];
  118. }
  119. if (output === input) {
  120. before_output = '';
  121. output = '';
  122. }
  123. return comment + before_input + input + before_output + output + ')';
  124. }
  125. };
  126. data.set_mustache_tags = function() {
  127. return function(text, render) {
  128. if(this.template) {
  129. mustache.tags = this.template.split(' ');
  130. }
  131. return '';
  132. }
  133. };
  134. data.unset_mustache_tags = function() {
  135. return function(text, render) {
  136. if(this.template) {
  137. mustache.tags = ['{{', '}}'];
  138. }
  139. return '';
  140. }
  141. };
  142. }
  143. generate_tests();