node-postgres/test/test-helper.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2010-10-24 11:07:00 +08:00
require.paths.unshift(__dirname + '/../lib/');
Client = require('client');
EventEmitter = require('events').EventEmitter;
sys = require('sys');
assert = require('assert');
BufferList = require(__dirname+'/buffer-list')
2010-10-24 11:07:00 +08:00
buffers = require(__dirname + '/test-buffers');
Connection = require('connection');
2010-10-25 14:30:14 +08:00
var args = require(__dirname + '/cli');
2010-10-24 11:07:00 +08:00
assert.same = function(actual, expected) {
for(var key in expected) {
assert.equal(actual[key], expected[key]);
}
};
assert.emits = function(item, eventName, callback) {
2010-10-24 11:07:00 +08:00
var called = false;
var id = setTimeout(function() {
test("Should have called " + eventName, function() {
assert.ok(called, "Expected '" + eventName + "' to be called.")
});
},20000);
2010-10-24 11:07:00 +08:00
item.once(eventName, function() {
called = true;
clearTimeout(id);
assert.ok(true);
if(callback) {
callback.apply(item, arguments);
}
});
2010-11-01 13:25:03 +08:00
};
assert.UTCDate = function(actual, year, month, day, hours, min, sec, milisecond) {
var actualYear = actual.getUTCFullYear();
assert.equal(actualYear, year, "expected year " + year + " but got " + actualYear);
var actualDate = actual.getUTCDate();
assert.equal(actualDate, day, "expected day " + day + " but got " + actualDate);
var actualHours = actual.getUTCHours();
assert.equal(actualHours, hours, "expected hours " + hours + " but got " + actualHours);
var actualMin = actual.getUTCMinutes();
assert.equal(actualMin, min, "expected min " + min + " but got " + actualMin);
var actualSec = actual.getUTCSeconds();
assert.equal(actualSec, sec, "expected sec " + sec + " but got " + actualSec);
2010-10-24 11:07:00 +08:00
2010-11-01 13:25:03 +08:00
var actualMili = actual.getUTCMilliseconds();
assert.equal(actualMili, milisecond, "expected milisecond " + milisecond + " but got " + actualMili);
2010-10-24 11:07:00 +08:00
};
assert.equalBuffers = function(actual, expected) {
if(actual.length != expected.length) {
2010-10-25 02:46:50 +08:00
console.log("");
console.log("actual " + sys.inspect(actual));
console.log("expect " + sys.inspect(expected));
console.log("");
2010-10-24 11:07:00 +08:00
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', 'emits', 'equalBuffers', 'same', 'ok'].forEach(function(name) {
2010-10-24 11:07:00 +08:00
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});
}
};
2010-10-25 14:30:14 +08:00
2010-10-24 11:07:00 +08:00
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;
});
});
2010-10-25 14:30:14 +08:00
module.exports = {
args: args
};