Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / uglify-js / test / unit / scripts.js
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
8 var Script = process.binding('evals').Script;
9
10 var scriptsPath = __dirname;
11
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);
18 };
19
20 var testDir = path.join(scriptsPath, "compress", "test");
21 var expectedDir = path.join(scriptsPath, "compress", "expected");
22
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);
29
30                 // Check if the noncompressdata is larger or same size as the compressed data
31                 test.ok(content.length >= outputCompress.length);
32
33                 // Check that a recompress gives the same result
34                 var outputReCompress = compress(content);
35                 test.equal(outputCompress, outputReCompress);
36
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)+$/, ""));
40
41                 test.done();
42         };
43 };
44
45 var tests = {};
46
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);
52         }
53 }
54
55 module.exports = nodeunit.testCase(tests);