<commit_before>var fs = require("fs");
var cocb = require("./");
var path = require("path");
var test = require("tape");

var delay = cocb.toYieldable(function(time, err, data, callback){
    setTimeout(function(){
        callback(err, data);
    }, time);
});


test("run", function(t){
    cocb.run(function*(){
        return yield delay(1, null, 3);
    }, function(err, val){
        if(err) return t.end(err);

        t.equals(val, 3);

        cocb.run(function*(){
            return yield delay(1, "foo", 3);
        }, function(err, val){
            t.equals(err, "foo");
            t.end();
        });
    });
});

test("order", function(t){
    var events = [];

    var delayE = cocb.toYieldable(function(id, time, err, data, callback){
        events.push("start:" + id);
        setTimeout(function(){
            if(err) return callback(err);
            events.push("done:" + id);
            callback(null, data);
        }, time);
    });

    cocb.run(function*(){
        (yield delayE("a", 10, null, true )) || (yield delayE("b", 1, null, true));
        (yield delayE("c", 10, null, false)) || (yield delayE("d", 1, null, true));
        yield delayE(yield delayE("foo", 10, null, "bar"), 10, null, "");
        yield delayE("baz", 10, "qux", "quux");
        t.fail();
    }, function(err){
        t.deepEquals(err, "qux");
        t.deepEquals(events, [
            "start:a",
            "done:a",
            "start:c",
            "done:c",
            "start:d",
            "done:d",
            "start:foo",
            "done:foo",
            "start:bar",
            "done:bar",
            "start:baz",
        ]);
        t.end();
    });
});

test("throw", function(t){
    var throwup = function(){
        throw "foobar"
    };
    cocb.run(function*(){
        throwup();
        t.fail();
    }, function(err){
        t.deepEquals(err, "foobar");
        t.end();
    });
});

test("toYieldable", function(t){
    var readFileYieldable = cocb.toYieldable(fs.readFile);
    cocb.run(function*(){
        return yield readFileYieldable(path.resolve(__dirname, "package.json"));
    }, function(err, data){
        if(err) return t.end(err);
        t.ok(data.length > 0);
        t.end();
    });
});

test("promiseRun", function(t){
    cocb.promiseRun(function*(){
        return yield delay(1, null, 3);
    })
        .catch(t.end)
        .then(function(val){
            t.equals(val, 3);

            var caught = false;
            cocb.promiseRun(function*(){
                return yield delay(1, "foo", 3);
            })
                .catch(function(err){<commit_msg>testing throw in toYieldable<commit_after>var fs = require("fs");
var cocb = require("./");
var path = require("path");
var test = require("tape");

var delay = cocb.toYieldable(function(time, err, data, callback){
    setTimeout(function(){
        callback(err, data);
    }, time);
});


test("run", function(t){
    cocb.run(function*(){
        return yield delay(1, null, 3);
    }, function(err, val){
        if(err) return t.end(err);

        t.equals(val, 3);

        cocb.run(function*(){
            return yield delay(1, "foo", 3);
        }, function(err, val){
            t.equals(err, "foo");
            t.end();
        });
    });
});

test("order", function(t){
    var events = [];

    var delayE = cocb.toYieldable(function(id, time, err, data, callback){
        events.push("start:" + id);
        setTimeout(function(){
            if(err) return callback(err);
            events.push("done:" + id);
            callback(null, data);
        }, time);
    });

    cocb.run(function*(){
        (yield delayE("a", 10, null, true )) || (yield delayE("b", 1, null, true));
        (yield delayE("c", 10, null, false)) || (yield delayE("d", 1, null, true));
        yield delayE(yield delayE("foo", 10, null, "bar"), 10, null, "");
        yield delayE("baz", 10, "qux", "quux");
        t.fail();
    }, function(err){
        t.deepEquals(err, "qux");
        t.deepEquals(events, [
            "start:a",
            "done:a",
            "start:c",
            "done:c",
            "start:d",
            "done:d",
            "start:foo",
            "done:foo",
            "start:bar",
            "done:bar",
            "start:baz",
        ]);
        t.end();
    });
});

test("throw", function(t){

    t.plan(3);

    var throwup = function(){
        throw "foobar"
    };
    cocb.run(function*(){
        throwup();
        t.fail();
    }, function(err){
        t.deepEquals(err, "foobar");
    });


    var throwupYieldable = cocb.toYieldable(function(callback){
        throw "baz";
    });
    cocb.run(function*(){
        yield throwupYieldable();
        t.fail();
    }, function(err){
        t.deepEquals(err, "baz");
    });


    var throwupCBYieldable = cocb.toYieldable(function(callback){
        callback("qux");
    });
    cocb.run(function*(){
        yield throwupCBYieldable();
        t.fail();
    }, function(err){
        t.deepEquals(err, "qux");
    });
});

test("toYieldable", function(t){
    var readFileYieldable = cocb.toYieldable(fs.readFile);
    cocb.run(function*(){
        return yield readFileYieldable(path.resolve(__dirname, "package.json"));
    }, function(err, data){
        if(err) return t.end(err);
        t.ok(data.length > 0);
        t.end();
    });
});

test("promiseRun", function(t){
    cocb.promiseRun(function*(){
        return yield delay(1, null, 3);
    })
        .catch(t.end)
        .then(function(val){
            t.equals(val, 3);

            var caught = false;
            cocb.promiseRun(function*(){
                return yield delay(1, "foo", 3);
            })
                .catch(function(err){
{'fraction_non_alphanumeric': 0.18848895844201008, 'fraction_numerical': 0.008172491740566858, 'mean_word_length': 2.0005216484089723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4563827', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1856, 'n_words': 375}