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