scripts.js 1.6 KB

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