Tests the index.js require just to make sure

This commit is contained in:
dsimard 2012-12-27 10:53:36 -05:00
parent 29863ad62c
commit a21be26f4e

View File

@ -1,170 +1,173 @@
#! /usr/bin/env node #! /usr/bin/env node
var U = require("../tools/node"); // Test the two requires
var path = require("path"); ["../tools/node", "../"].forEach(function(requireFile) {
var fs = require("fs"); var U = require(requireFile);
var assert = require("assert"); var path = require("path");
var sys = require("util"); var fs = require("fs");
var assert = require("assert");
var sys = require("util");
var tests_dir = path.dirname(module.filename); var tests_dir = path.dirname(module.filename);
run_compress_tests(); run_compress_tests();
/* -----[ utils ]----- */ /* -----[ utils ]----- */
function tmpl() { function tmpl() {
return U.string_template.apply(this, arguments); return U.string_template.apply(this, arguments);
} }
function log() { function log() {
var txt = tmpl.apply(this, arguments); var txt = tmpl.apply(this, arguments);
sys.puts(txt); sys.puts(txt);
} }
function log_directory(dir) { function log_directory(dir) {
log("*** Entering [{dir}]", { dir: dir }); log("*** Entering [{dir}]", { dir: dir });
} }
function log_start_file(file) { function log_start_file(file) {
log("--- {file}", { file: file }); log("--- {file}", { file: file });
} }
function log_test(name) { function log_test(name) {
log(" Running test [{name}]", { name: name }); log(" Running test [{name}]", { name: name });
} }
function find_test_files(dir) { function find_test_files(dir) {
var files = fs.readdirSync(dir).filter(function(name){ var files = fs.readdirSync(dir).filter(function(name){
return /\.js$/i.test(name); return /\.js$/i.test(name);
}); });
if (process.argv.length > 2) { if (process.argv.length > 2) {
var x = process.argv.slice(2); var x = process.argv.slice(2);
files = files.filter(function(f){ files = files.filter(function(f){
return x.indexOf(f) >= 0; return x.indexOf(f) >= 0;
}); });
} }
return files; return files;
} }
function test_directory(dir) { function test_directory(dir) {
return path.resolve(tests_dir, dir); return path.resolve(tests_dir, dir);
} }
function as_toplevel(input) { function as_toplevel(input) {
if (input instanceof U.AST_BlockStatement) input = input.body; if (input instanceof U.AST_BlockStatement) input = input.body;
else if (input instanceof U.AST_Statement) input = [ input ]; else if (input instanceof U.AST_Statement) input = [ input ];
else throw new Error("Unsupported input syntax"); else throw new Error("Unsupported input syntax");
var toplevel = new U.AST_Toplevel({ body: input }); var toplevel = new U.AST_Toplevel({ body: input });
toplevel.figure_out_scope(); toplevel.figure_out_scope();
return toplevel; return toplevel;
} }
function run_compress_tests() { function run_compress_tests() {
var dir = test_directory("compress"); var dir = test_directory("compress");
log_directory("compress"); log_directory("compress");
var files = find_test_files(dir); var files = find_test_files(dir);
function test_file(file) { function test_file(file) {
log_start_file(file); log_start_file(file);
function test_case(test) { function test_case(test) {
log_test(test.name); log_test(test.name);
var options = U.defaults(test.options, { var options = U.defaults(test.options, {
warnings: false warnings: false
}); });
var cmp = new U.Compressor(options, true); var cmp = new U.Compressor(options, true);
var expect = make_code(as_toplevel(test.expect), false); var expect = make_code(as_toplevel(test.expect), false);
var input = as_toplevel(test.input); var input = as_toplevel(test.input);
var input_code = make_code(test.input); var input_code = make_code(test.input);
var output = input.transform(cmp); var output = input.transform(cmp);
output.figure_out_scope(); output.figure_out_scope();
output = make_code(output, false); output = make_code(output, false);
if (expect != output) { if (expect != output) {
log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", { log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {
input: input_code, input: input_code,
output: output, output: output,
expected: expect expected: expect
}); });
} }
} }
var tests = parse_test(path.resolve(dir, file)); var tests = parse_test(path.resolve(dir, file));
for (var i in tests) if (tests.hasOwnProperty(i)) { for (var i in tests) if (tests.hasOwnProperty(i)) {
test_case(tests[i]); test_case(tests[i]);
} }
} }
files.forEach(function(file){ files.forEach(function(file){
test_file(file); test_file(file);
}); });
} }
function parse_test(file) { function parse_test(file) {
var script = fs.readFileSync(file, "utf8"); var script = fs.readFileSync(file, "utf8");
var ast = U.parse(script, { var ast = U.parse(script, {
filename: file filename: file
}); });
var tests = {}; var tests = {};
var tw = new U.TreeWalker(function(node, descend){ var tw = new U.TreeWalker(function(node, descend){
if (node instanceof U.AST_LabeledStatement if (node instanceof U.AST_LabeledStatement
&& tw.parent() instanceof U.AST_Toplevel) { && tw.parent() instanceof U.AST_Toplevel) {
var name = node.label.name; var name = node.label.name;
tests[name] = get_one_test(name, node.body); tests[name] = get_one_test(name, node.body);
return true; return true;
} }
if (!(node instanceof U.AST_Toplevel)) croak(node); if (!(node instanceof U.AST_Toplevel)) croak(node);
}); });
ast.walk(tw); ast.walk(tw);
return tests; return tests;
function croak(node) { function croak(node) {
throw new Error(tmpl("Can't understand test file {file} [{line},{col}]\n{code}", { throw new Error(tmpl("Can't understand test file {file} [{line},{col}]\n{code}", {
file: file, file: file,
line: node.start.line, line: node.start.line,
col: node.start.col, col: node.start.col,
code: make_code(node, false) code: make_code(node, false)
})); }));
} }
function get_one_test(name, block) { function get_one_test(name, block) {
var test = { name: name, options: {} }; var test = { name: name, options: {} };
var tw = new U.TreeWalker(function(node, descend){ var tw = new U.TreeWalker(function(node, descend){
if (node instanceof U.AST_Assign) { if (node instanceof U.AST_Assign) {
if (!(node.left instanceof U.AST_SymbolRef)) { if (!(node.left instanceof U.AST_SymbolRef)) {
croak(node); croak(node);
} }
var name = node.left.name; var name = node.left.name;
test[name] = evaluate(node.right); test[name] = evaluate(node.right);
return true; return true;
} }
if (node instanceof U.AST_LabeledStatement) { if (node instanceof U.AST_LabeledStatement) {
assert.ok( assert.ok(
node.label.name == "input" || node.label.name == "expect", node.label.name == "input" || node.label.name == "expect",
tmpl("Unsupported label {name} [{line},{col}]", { tmpl("Unsupported label {name} [{line},{col}]", {
name: node.label.name, name: node.label.name,
line: node.label.start.line, line: node.label.start.line,
col: node.label.start.col col: node.label.start.col
}) })
); );
var stat = node.body; var stat = node.body;
if (stat instanceof U.AST_BlockStatement) { if (stat instanceof U.AST_BlockStatement) {
if (stat.body.length == 1) stat = stat.body[0]; if (stat.body.length == 1) stat = stat.body[0];
else if (stat.body.length == 0) stat = new U.AST_EmptyStatement(); else if (stat.body.length == 0) stat = new U.AST_EmptyStatement();
} }
test[node.label.name] = stat; test[node.label.name] = stat;
return true; return true;
} }
}); });
block.walk(tw); block.walk(tw);
return test; return test;
}; };
} }
function make_code(ast, beautify) { function make_code(ast, beautify) {
if (arguments.length == 1) beautify = true; if (arguments.length == 1) beautify = true;
var stream = U.OutputStream({ beautify: beautify }); var stream = U.OutputStream({ beautify: beautify });
ast.print(stream); ast.print(stream);
return stream.get(); return stream.get();
} }
function evaluate(code) { function evaluate(code) {
if (code instanceof U.AST_Node) if (code instanceof U.AST_Node)
code = make_code(code); code = make_code(code);
return new Function("return(" + code + ")")(); return new Function("return(" + code + ")")();
} }
});