node-postgres/test/test-helper.js

96 lines
2.0 KiB
JavaScript
Raw Normal View History

sys = require('sys');
assert = require('assert');
Client = require(__dirname+"/../lib/").Client;
Parser = require(__dirname+"/../lib/").Parser;
2010-09-30 13:32:44 +08:00
assert.same = function(actual, expected) {
for(var key in expected) {
assert.equal(actual[key], expected[key]);
}
};
test = function(name, action) {
for(var i = 0; i < test.tabout; i++) {
name = ' ' + name;
}
test.tabout += 2;
console.log(name);
action();
2010-09-29 12:32:00 +08:00
test.tabout -= 2;
};
test.tabout = 0;
2010-09-29 12:30:06 +08:00
stringToHex = function(string) {
2010-09-29 14:01:52 +08:00
var b = Buffer(string,'utf8');
var result = [];
for(var i = 0; i < b.length; i++) {
result.push(b[i]);
}
return result;
2010-09-29 12:30:06 +08:00
};
hexToString = function(hexArray) {
return new Buffer(hexArray).toString('utf8');
}
2010-09-29 15:39:43 +08:00
BufferList = function() {
2010-09-29 15:28:23 +08:00
this.buffers = [];
};
BufferList.prototype.add = function(buffer, front) {
this.buffers[front ? "unshift" : "push"](buffer);
return this;
};
BufferList.prototype.addInt16 = function(val, front) {
return this.add(Buffer([(val >>> 8),(val >>> 0)]),front);
};
BufferList.prototype.getByteLength = function(initial) {
return this.buffers.reduce(function(previous, current){
return previous + current.length;
},initial || 0);
};
BufferList.prototype.addInt32 = function(val, first) {
return this.add(Buffer([
(val >>> 24),
(val >>> 16),
(val >>> 8),
(val >>> 0)
]),first);
};
BufferList.prototype.addCString = function(val) {
return this.add(Buffer(val + '\0','utf8'));
};
BufferList.prototype.join = function(appendLength, char) {
var length = this.getByteLength();
if(appendLength) {
this.addInt32(length+4, true);
return this.join(false, char);
}
if(char) {
this.buffers.unshift(Buffer(char,'utf8'));
length++;
}
var result = Buffer(length);
var index = 0;
this.buffers.forEach(function(buffer) {
buffer.copy(result, index, 0);
index += buffer.length;
});
return result;
};
2010-09-29 15:39:43 +08:00
BufferList.concat = function() {
var total = new BufferList();
for(var i = 0; i < arguments.length; i++) {
total.add(arguments[i]);
}
return total.join();
};