xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / uglify-js / test / unit / scripts.js

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