react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / ruglify / node_modules / uglify-js / test / run-tests.js
80760 views#! /usr/bin/env node12var U = require("../tools/node");3var path = require("path");4var fs = require("fs");5var assert = require("assert");6var sys = require("util");78var tests_dir = path.dirname(module.filename);910run_compress_tests();1112/* -----[ utils ]----- */1314function tmpl() {15return U.string_template.apply(this, arguments);16}1718function log() {19var txt = tmpl.apply(this, arguments);20sys.puts(txt);21}2223function log_directory(dir) {24log("*** Entering [{dir}]", { dir: dir });25}2627function log_start_file(file) {28log("--- {file}", { file: file });29}3031function log_test(name) {32log(" Running test [{name}]", { name: name });33}3435function find_test_files(dir) {36var files = fs.readdirSync(dir).filter(function(name){37return /\.js$/i.test(name);38});39if (process.argv.length > 2) {40var x = process.argv.slice(2);41files = files.filter(function(f){42return x.indexOf(f) >= 0;43});44}45return files;46}4748function test_directory(dir) {49return path.resolve(tests_dir, dir);50}5152function as_toplevel(input) {53if (input instanceof U.AST_BlockStatement) input = input.body;54else if (input instanceof U.AST_Statement) input = [ input ];55else throw new Error("Unsupported input syntax");56var toplevel = new U.AST_Toplevel({ body: input });57toplevel.figure_out_scope();58return toplevel;59}6061function run_compress_tests() {62var dir = test_directory("compress");63log_directory("compress");64var files = find_test_files(dir);65function test_file(file) {66log_start_file(file);67function test_case(test) {68log_test(test.name);69var options = U.defaults(test.options, {70warnings: false71});72var cmp = new U.Compressor(options, true);73var expect = make_code(as_toplevel(test.expect), false);74var input = as_toplevel(test.input);75var input_code = make_code(test.input);76var output = input.transform(cmp);77output.figure_out_scope();78output = make_code(output, false);79if (expect != output) {80log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {81input: input_code,82output: output,83expected: expect84});85}86}87var tests = parse_test(path.resolve(dir, file));88for (var i in tests) if (tests.hasOwnProperty(i)) {89test_case(tests[i]);90}91}92files.forEach(function(file){93test_file(file);94});95}9697function parse_test(file) {98var script = fs.readFileSync(file, "utf8");99var ast = U.parse(script, {100filename: file101});102var tests = {};103var tw = new U.TreeWalker(function(node, descend){104if (node instanceof U.AST_LabeledStatement105&& tw.parent() instanceof U.AST_Toplevel) {106var name = node.label.name;107tests[name] = get_one_test(name, node.body);108return true;109}110if (!(node instanceof U.AST_Toplevel)) croak(node);111});112ast.walk(tw);113return tests;114115function croak(node) {116throw new Error(tmpl("Can't understand test file {file} [{line},{col}]\n{code}", {117file: file,118line: node.start.line,119col: node.start.col,120code: make_code(node, false)121}));122}123124function get_one_test(name, block) {125var test = { name: name, options: {} };126var tw = new U.TreeWalker(function(node, descend){127if (node instanceof U.AST_Assign) {128if (!(node.left instanceof U.AST_SymbolRef)) {129croak(node);130}131var name = node.left.name;132test[name] = evaluate(node.right);133return true;134}135if (node instanceof U.AST_LabeledStatement) {136assert.ok(137node.label.name == "input" || node.label.name == "expect",138tmpl("Unsupported label {name} [{line},{col}]", {139name: node.label.name,140line: node.label.start.line,141col: node.label.start.col142})143);144var stat = node.body;145if (stat instanceof U.AST_BlockStatement) {146if (stat.body.length == 1) stat = stat.body[0];147else if (stat.body.length == 0) stat = new U.AST_EmptyStatement();148}149test[node.label.name] = stat;150return true;151}152});153block.walk(tw);154return test;155};156}157158function make_code(ast, beautify) {159if (arguments.length == 1) beautify = true;160var stream = U.OutputStream({ beautify: beautify });161ast.print(stream);162return stream.get();163}164165function evaluate(code) {166if (code instanceof U.AST_Node)167code = make_code(code);168return new Function("return(" + code + ")")();169}170171172