var assert = require("assert"),
    hiredis = require("../src/build/default/hiredis");

exports.testCreateReader = function() {
    var reader = new hiredis.Reader();
    assert.isNotNull(reader);
}

exports.testStatusReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("+OK\r\n");
    assert.equal("OK", reader.get());
}

exports.testIntegerReply = function() {
    var reader = new hiredis.Reader();
    reader.feed(":1\r\n");
    assert.equal(1, reader.get());
}

exports.testErrorReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("-ERR foo\r\n");
    assert.equal("ERR foo", reader.get());
}

exports.testNullBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("$-1\r\n");
    assert.equal(null, reader.get());
}

exports.testEmptyBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("$0\r\n\r\n");
    assert.equal("", reader.get());
}

exports.testBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("$3\r\nfoo\r\n");
    assert.equal("foo", reader.get());
}

exports.testNullMultiBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("*-1\r\n");
    assert.equal(null, reader.get());
}

exports.testEmptyMultiBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("*0\r\n");
    assert.eql([], reader.get());
}

exports.testMultiBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n");
    assert.eql(["foo", "bar"], reader.get());
}

exports.testNestedMultiBulkReply = function() {
    var reader = new hiredis.Reader();
    reader.feed("*2\r\n*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$3\r\nqux\r\n");
    assert.eql([["foo", "bar"], "qux"], reader.get());
}

exports.testMultiBulkReplyWithNonStringValues = function() {
    var reader = new hiredis.Reader();
    reader.feed("*3\r\n:1\r\n+OK\r\n$-1\r\n");
    assert.eql([1, "OK", null], reader.get());
}

exports.testFeedWithBuffer = function() {
    var reader = new hiredis.Reader();
    reader.feed(new Buffer("$3\r\nfoo\r\n"));
    assert.eql("foo", reader.get());
}


@@ -25,16 +25,60 @@
 sert%22),%0A
+    spawn = require('child_process').spawn,%0A
     hire
@@ -2184,28 +2184,683 @@
 ql(%22foo%22, reader.get());%0A%7D%0A%0A
+exports.testLeaks = function(beforeExit) %7B%0A    /* The %22leaks%22 utility is only available on OSX. */%0A    if (process.platform != %22darwin%22) return;%0A%0A    var done = 0;%0A    var leaks = spawn(%22leaks%22, %5Bprocess.pid%5D);%0A    leaks.stdout.on(%22data%22, function(data) %7B%0A        var str = data.toString();%0A        var matches;%0A        if ((matches = /(%5Cd+) leaks?/i.exec(str)) != null) %7B%0A            if (parseInt(matches%5B1%5D) %3E 0) %7B%0A                console.log(str);%0A            %7D%0A        %7D%0A        done = 1;%0A    %7D);%0A%0A    beforeExit(function() %7B%0A        setTimeout(function() %7B%0A            assert.ok(done, %22Leaks test should have completed%22);%0A        %7D, 100);%0A    %7D);%0A%7D%0A%0A

Add test to check for memory leaks