scripts.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var fs = require('fs'),
  2. uglify = require('../../uglify-js'),
  3. jsp = uglify.parser,
  4. nodeunit = require('nodeunit'),
  5. path = require('path'),
  6. pro = uglify.uglify;
  7. var Script = process.binding('evals').Script;
  8. var scriptsPath = __dirname;
  9. function compress(code) {
  10. var ast = jsp.parse(code);
  11. ast = pro.ast_mangle(ast);
  12. ast = pro.ast_squeeze(ast, { no_warnings: true });
  13. ast = pro.ast_squeeze_more(ast);
  14. return pro.gen_code(ast);
  15. };
  16. var testDir = path.join(scriptsPath, "compress", "test");
  17. var expectedDir = path.join(scriptsPath, "compress", "expected");
  18. function getTester(script) {
  19. return function(test) {
  20. var testPath = path.join(testDir, script);
  21. var expectedPath = path.join(expectedDir, script);
  22. var content = fs.readFileSync(testPath, 'utf-8');
  23. var outputCompress = compress(content);
  24. // Check if the noncompressdata is larger or same size as the compressed data
  25. test.ok(content.length >= outputCompress.length);
  26. // Check that a recompress gives the same result
  27. var outputReCompress = compress(content);
  28. test.equal(outputCompress, outputReCompress);
  29. // Check if the compressed output is what is expected
  30. var expected = fs.readFileSync(expectedPath, 'utf-8');
  31. test.equal(outputCompress, expected.replace(/(\r?\n)+$/, ""));
  32. test.done();
  33. };
  34. };
  35. var tests = {};
  36. var scripts = fs.readdirSync(testDir);
  37. for (var i in scripts) {
  38. var script = scripts[i];
  39. if (/\.js$/.test(script)) {
  40. tests[script] = getTester(script);
  41. }
  42. }
  43. module.exports = nodeunit.testCase(tests);