var Hash = require('traverse/hash');
var optimist = require('optimist');

exports.usageFail = function (assert) {
    var r = checkUsage(function () {
        return optimist('-x 10 -z 20'.split(' '))
            .usage('Usage: $0 -x NUM -y NUM')
            .demand(['x','y'])
            .argv;
    });
    assert.deepEqual(r, {
        result : { x : 10, z : 20, _ : [], $0 : './usage' },
        errors : [ 'Usage: ./usage -x NUM -y NUM', 'Missing arguments: y' ],
        logs : [],
        exit: true,
    });
};

exports.usagePass = function (assert) {
    var r = checkUsage(function () {
        return optimist('-x 10 -y 20'.split(' '))
            .usage('Usage: $0 -x NUM -y NUM')
            .demand(['x','y'])
            .argv;
    });
    assert.deepEqual(r, {
        result : { x : 10, y : 20, _ : [], $0 : './usage' },
        errors : [],
        logs : [],
        exit : false,
    });
};

exports.checkPass = function (assert) {
    var r = checkUsage(function () {
        return optimist('-x 10 -y 20'.split(' '))
            .usage('Usage: $0 -x NUM -y NUM')
            .check(function (argv) {
                return 'x' in argv && 'y' in argv
            })
            .argv;
    });
    assert.deepEqual(r, {
        result : { x : 10, y : 20, _ : [], $0 : './usage' },
        errors : [],
        logs : [],
        exit : false,
    });
};

exports.checkFail = function (assert) {
    var r = checkUsage(function () {
        return optimist('-x 10 -z 20'.split(' '))
            .usage('Usage: $0 -x NUM -y NUM')
            .check(function (argv) {
                if (!('x' in argv)) throw 'You forgot about -x';
                if (!('y' in argv)) throw 'You forgot about -y';
            })
            .argv;
    });
    assert.deepEqual(r, {
        result : { x : 10, z : 20, _ : [], $0 : './usage' },
        errors : [ 'Usage: ./usage -x NUM -y NUM', 'You forgot about -y' ],
        logs : [],
        exit: true,
    });
};

function checkUsage (f) {
    var _process = process;
    process = Hash.copy(process);
    var exit = false;
    process.exit = function () { exit = true };
    process.env = Hash.merge(process.env, { _ : 'node' });
    process.argv = [ './usage' ];
    
    var _console = console;
    console = Hash.copy(console);
    var errors = [];
    var logs = [];
    console.error = function (msg) { errors.push(msg) };
    console.log = function (msg) { logs.push(msg) };
    
    var result = f();
    
    process = _process;
    console = _console;
    
    return {
        errors : errors,
        logs : logs,
        exit : exit,
        result : result,
    };
};

@@ -1964,16 +1964,425 @@
 %7D);%0A%7D;%0A%0A
+exports.rebase = function (assert) %7B%0A    assert.equal(%0A        optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),%0A        './foo/bar/baz'%0A    );%0A    assert.equal(%0A        optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),%0A        '../../..'%0A    );%0A    assert.equal(%0A        optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),%0A        '../pow/zoom.txt'%0A    );%0A%7D;%0A%0A
 function

Rebase checks. That will be its own module eventually.