Skip to content

Commit

Permalink
More changes to tests so they really work under context module loader.
Browse files Browse the repository at this point in the history
Plus, getting rid of test/common.js defining things in global.
  • Loading branch information
Herbert Vojčík authored and ry committed Aug 17, 2010
1 parent 1872719 commit cf2b206
Show file tree
Hide file tree
Showing 25 changed files with 62 additions and 47 deletions.
15 changes: 14 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ exports.assert = require('assert');

var sys = require("sys");
for (var i in sys) exports[i] = sys[i];
for (var i in exports) global[i] = exports[i];
//for (var i in exports) global[i] = exports[i];

function protoCtrChain (o) {
var result = [];
for (; o; o = o.__proto__) { result.push(o.constructor); }
return result.join();
}

exports.indirectInstanceOf = function (obj, cls) {
if (obj instanceof cls) { return true; }
var clsChain = protoCtrChain(cls.prototype);
var objChain = protoCtrChain(obj);
return objChain.slice(-clsChain.length) === clsChain;
};
4 changes: 2 additions & 2 deletions test/disabled/test-http-big-proxy-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var chargen = http.createServer(function (req, res) {
assert.ok(len > 0);
res.writeHead(200, {"transfer-encoding":"chunked"});
for (var i=0; i<len; i++) {
if (i % 1000 == 0) print(',');
if (i % 1000 == 0) common.print(',');
res.write(chunk);
}
res.end();
Expand Down Expand Up @@ -42,7 +42,7 @@ var proxy = http.createServer(function (req, res) {
var count = 0;

proxy_res.addListener('data', function(d) {
if (count++ % 1000 == 0) print('.');
if (count++ % 1000 == 0) common.print('.');
res.write(d);
sent += d.length;
assert.ok(sent <= (len*chunk.length));
Expand Down
4 changes: 2 additions & 2 deletions test/disabled/test-http-stress.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ server.addListener('listening', function () {
http.cat('http://localhost:'+common.PORT+'/', 'utf8', function (err, content) {
requests_complete++;
if (err) {
print("-");
common.print("-");
} else {
assert.equal(body, content)
print(".");
common.print(".");
requests_ok++;
}
if (requests_complete == request_count) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/b/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var package = require("./package");

assert.equal("world", package.hello);

debug("load fixtures/b/c.js");
common.debug("load fixtures/b/c.js");

var string = "C";

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/b/d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
debug("load fixtures/b/d.js");
common.debug("load fixtures/b/d.js");

var string = "D";

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/b/package/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.hello = "world";
debug("load package/index.js");
common.debug("load package/index.js");
2 changes: 1 addition & 1 deletion test/fixtures/echo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
common = require("../common");
assert = common.assert

print("hello world\r\n");
common.print("hello world\r\n");

var stdin = process.openStdin();

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/net-fd-passing-receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ receiver = net.createServer(function(socket) {

/* To signal the test runne we're up and listening */
receiver.addListener("listening", function() {
print("ready");
common.print("ready");
});

receiver.listen(path);
6 changes: 3 additions & 3 deletions test/pummel/test-net-many-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for (var i = 0; i < bytes; i++) {
var server = net.createServer(function (c) {
c.addListener("connect", function () {
total_connections++;
print("#");
common.print("#");
c.write(body);
c.end();
});
Expand All @@ -31,7 +31,7 @@ function runClient (callback) {
client.setEncoding("utf8");

client.addListener("connect", function () {
print("c");
common.print("c");
client.recved = "";
client.connections += 1;
});
Expand All @@ -50,7 +50,7 @@ function runClient (callback) {
});

client.addListener("close", function (had_error) {
print(".");
common.print(".");
assert.equal(false, had_error);
assert.equal(bytes, client.recved.length);

Expand Down
2 changes: 1 addition & 1 deletion test/pummel/test-net-pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ server.on('listening', function(){
client = net.createConnection(common.PORT);
client.setEncoding("ascii");
client.addListener("data", function (d) {
print(d);
common.print(d);
recv += d;
});

Expand Down
4 changes: 2 additions & 2 deletions test/pummel/test-net-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var echo_server = net.createServer(function (socket) {
socket.addListener("timeout", function () {
console.log("server timeout");
timeouttime = new Date;
p(timeouttime);
common.p(timeouttime);
socket.destroy();
});

Expand Down Expand Up @@ -53,7 +53,7 @@ client.addListener("data", function (chunk) {
if (exchanges == 5) {
console.log("wait for timeout - should come in " + timeout + " ms");
starttime = new Date;
p(starttime);
common.p(starttime);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function makeBlock (f) {
};
}

assert.ok(a.AssertionError instanceof Error,
assert.ok(common.indirectInstanceOf(a.AssertionError.prototype, Error),
"a.AssertionError instanceof Error");

assert.throws(makeBlock(a.ok, false),
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var copied = b.copy(c, 0, 0, 512);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
print('.');
common.print('.');
assert.equal(i % 256, c[i]);
}
console.log("");
Expand Down
6 changes: 3 additions & 3 deletions test/simple/test-c-ares.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ var dns = require("dns");
// Try resolution without callback

dns.getHostByName('localhost', function (error, result) {
p(result);
common.p(result);
assert.deepEqual(['127.0.0.1'], result);
});

dns.getHostByName('127.0.0.1', function (error, result) {
p(result);
common.p(result);
assert.deepEqual(['127.0.0.1'], result);
});

Expand All @@ -33,7 +33,7 @@ dns.lookup('::1', function (error, result, addressType) {

dns.lookup('ipv6.google.com', function (error, result, addressType) {
if (error) throw error;
p(arguments);
common.p(arguments);
//assert.equal('string', typeof result);
assert.equal(6, addressType);
});
2 changes: 1 addition & 1 deletion test/simple/test-child-process-buffering.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function pwd (callback) {


pwd(function (result) {
p(result);
common.p(result);
assert.equal(true, result.length > 1);
assert.equal("\n", result[result.length-1]);
});
Expand Down
3 changes: 2 additions & 1 deletion test/simple/test-child-process-exec-env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('../common');
common = require('../common');
assert = common.assert;
var exec = require('child_process').exec,
sys = require('sys');
success_count = 0;
Expand Down
4 changes: 2 additions & 2 deletions test/simple/test-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exec("ls /", function (err, stdout, stderr) {
assert.equal(false, err.killed);
} else {
success_count++;
p(stdout);
common.p(stdout);
}
});

Expand All @@ -30,7 +30,7 @@ exec("ls /DOES_NOT_EXIST", function (err, stdout, stderr) {
console.log("stderr: " + JSON.stringify(stderr));
} else {
success_count++;
p(stdout);
common.p(stdout);
assert.equal(true, stdout != "");
}
});
Expand Down
3 changes: 2 additions & 1 deletion test/simple/test-fs-realpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function asynctest(testBlock, args, callback, assertBlock) {
}

function bashRealpath(path, callback) {
exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function (err, o) {
common.exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function
(err, o) {
callback(err, o.trim());
});
}
Expand Down
10 changes: 5 additions & 5 deletions test/simple/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fs.stat(".", function (err, stats) {
if (err) {
got_error = true;
} else {
p(stats);
common.p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
Expand All @@ -18,7 +18,7 @@ fs.lstat(".", function (err, stats) {
if (err) {
got_error = true;
} else {
p(stats);
common.p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
Expand All @@ -33,7 +33,7 @@ fs.open(".", "r", undefined, function(err, fd) {
if (err) {
got_error = true;
} else {
p(stats);
common.p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
fs.close(fd);
Expand All @@ -50,7 +50,7 @@ fs.open(".", "r", undefined, function(err, fd) {
got_error = true;
}
if (stats) {
p(stats);
common.p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
Expand All @@ -62,7 +62,7 @@ fs.stat(__filename, function (err, s) {
if (err) {
got_error = true;
} else {
p(s);
common.p(s);
success_count++;

console.log("isDirectory: " + JSON.stringify( s.isDirectory() ) );
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var server = http.createServer(function (req, res) {
assert.equal("GET", req.method);
assert.equal("/hello", url.parse(req.url).pathname);

p(req.headers);
common.p(req.headers);
assert.equal(true, "accept" in req.headers);
assert.equal("*/*", req.headers["accept"]);

Expand Down
18 changes: 9 additions & 9 deletions test/simple/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ var d4 = require("../fixtures/b/d");

assert.equal(false, false, "testing the test program.");

assert.equal(true, a.A instanceof Function);
assert.equal(true, common.indirectInstanceOf(a.A, Function));
assert.equal("A", a.A());

assert.equal(true, a.C instanceof Function);
assert.equal(true, common.indirectInstanceOf(a.C, Function));
assert.equal("C", a.C());

assert.equal(true, a.D instanceof Function);
assert.equal(true, common.indirectInstanceOf(a.D, Function));
assert.equal("D", a.D());

assert.equal(true, d.D instanceof Function);
assert.equal(true, common.indirectInstanceOf(d.D, Function));
assert.equal("D", d.D());

assert.equal(true, d2.D instanceof Function);
assert.equal(true, common.indirectInstanceOf(d2.D, Function));
assert.equal("D", d2.D());

assert.equal(true, d3.D instanceof Function);
assert.equal(true, common.indirectInstanceOf(d3.D, Function));
assert.equal("D", d3.D());

assert.equal(true, d4.D instanceof Function);
assert.equal(true, common.indirectInstanceOf(d4.D, Function));
assert.equal("D", d4.D());

assert.ok((new a.SomeClass) instanceof c.SomeClass);
Expand All @@ -52,7 +52,7 @@ assert.equal(root.sayHello(), root.hello);
common.debug("test name clashes");
// this one exists and should import the local module
var my_path = require("./path");
assert.equal(true, my_path.path_func instanceof Function);
assert.ok(common.indirectInstanceOf(my_path.path_func, Function));
// this one does not exist and should throw
assert.throws(function() { require("./utils")});

Expand Down Expand Up @@ -98,7 +98,7 @@ require.registerExtension('.test', function(content) {
});

assert.equal(require('../fixtures/registerExt2').custom, 'passed');
debug("load modules by absolute id, then change require.paths, and load another module with the same absolute id.");
common.debug("load modules by absolute id, then change require.paths, and load another module with the same absolute id.");
// this will throw if it fails.
var foo = require("../fixtures/require-path/p1/foo");
process.assert(foo.bar.expect === foo.bar.actual);
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-net-binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ echoServer.addListener("listening", function() {
});

c.addListener("close", function () {
p(recv);
common.p(recv);
echoServer.close();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/simple/test-readdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var files = ['are'

console.log('readdirSync ' + readdirDir);
var f = fs.readdirSync(readdirDir);
p(f);
common.p(f);
assert.deepEqual(files, f.sort());


Expand All @@ -29,7 +29,7 @@ fs.readdir(readdirDir, function (err, f) {
console.log("error");
got_error = true;
} else {
p(f);
common.p(f);
assert.deepEqual(files, f.sort());
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-stdout-to-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function test (size, useBuffer, cb) {
fs.unlinkSync(tmpFile);
} catch (e) {}

print(size + ' chars to ' + tmpFile + '...');
common.print(size + ' chars to ' + tmpFile + '...');

childProccess.exec(cmd, function(err) {
if (err) throw err;
Expand Down
4 changes: 2 additions & 2 deletions test/simple/test-string-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ charLengths = [0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5];
// 0 i j buffer.length
// Scan through every possible 3 segment combination
// and make sure that the string is always parsed.
print('scanning ');
common.print('scanning ');
for (var j = 2; j < buffer.length; j++) {
for (var i = 1; i < j; i++) {
var decoder = new StringDecoder('utf8');
Expand All @@ -57,7 +57,7 @@ for (var j = 2; j < buffer.length; j++) {
sum += decoder.write(buffer.slice(i, j));
sum += decoder.write(buffer.slice(j, buffer.length));
assert.equal(expected, sum);
print(".");
common.print(".");
}
}
console.log(" crayon!");
Expand Down

0 comments on commit cf2b206

Please sign in to comment.