node-postgres/test/test-helper.js
2010-10-25 01:30:14 -05:00

113 lines
2.7 KiB
JavaScript

sys = require('sys');
assert = require('assert');
require.paths.unshift(__dirname + '/../lib/');
Client = require('client');
EventEmitter = require('events').EventEmitter;
BufferList = require('buffer-list')
buffers = require(__dirname + '/test-buffers');
Connection = require('connection');
var args = require(__dirname + '/cli');
assert.same = function(actual, expected) {
for(var key in expected) {
assert.equal(actual[key], expected[key]);
}
};
assert.raises = function(item, eventName, callback) {
var called = false;
var id = setTimeout(function() {
test("Should have called " + eventName, function() {
assert.ok(called, "Expected '" + eventName + "' to be called.")
});
},1000);
item.once(eventName, function() {
called = true;
clearTimeout(id);
assert.ok(true);
if(callback) {
callback.apply(item, arguments);
}
});
};
assert.equalBuffers = function(actual, expected) {
if(actual.length != expected.length) {
console.log("");
console.log("actual " + sys.inspect(actual));
console.log("expect " + sys.inspect(expected));
console.log("");
assert.equal(actual.length, expected.length);
}
for(var i = 0; i < actual.length; i++) {
if(actual[i] != expected[i]) {
console.log(actual);
console.log(expected);
}
assert.equal(actual[i],expected[i]);
}
};
assert.empty = function(actual) {
assert.length(actual, 0);
};
assert.length = function(actual, expectedLength) {
assert.equal(actual.length, expectedLength);
};
['equal', 'length', 'empty', 'strictEqual', 'raises', 'equalBuffers', 'same', 'ok'].forEach(function(name) {
var old = assert[name];
assert[name] = function() {
test.assertCount++
old.apply(this, arguments);
};
});
test = function(name, action) {
try{
test.testCount ++;
var result = action();
if(result === false) {
test.ignored.push(name);
process.stdout.write('?');
}else{
process.stdout.write('.');
}
}catch(e) {
process.stdout.write('E');
test.errors.push({name: name, e: e});
}
};
test.assertCount = test.assertCount || 0;
test.testCount = test.testCount || 0;
test.ignored = test.ignored || [];
test.errors = test.errors || [];
test.start = test.start || new Date();
process.on('exit', function() {
console.log('');
var duration = ((new Date() - test.start)/1000);
console.log(test.testCount + ' tests ' + test.assertCount + ' assertions in ' + duration + ' seconds');
test.ignored.forEach(function(name) {
console.log("Ignored: " + name);
});
test.errors.forEach(function(error) {
console.log("Error: " + error.name);
});
test.errors.forEach(function(error) {
throw error.e;
});
});
module.exports = {
args: args
};