<commit_before>/*global describe,before,it,expect,after */

var promiseMe = require("../index.js");

function convertFunctionToString(fn) {
    var lines = fn.toString().split("\n");
    lines.pop();
    lines.shift();
    lines = lines.map(function(line) {
        return line.trim();
    });
    return lines.join("\n");
}

// This utility function allows us to write tests as Javascript functions
// instead of strings. Note: the functions are never executed so they don't
// need to make semantic sense.
var compare = function(before, after) {
    before = convertFunctionToString(before);
    after = convertFunctionToString(after);

    expect(promiseMe.convert(
        before,
        {format: {indent: {style: '', base: 0 }}}
    )).toEqual(after);
};

describe("promise-me basics", function() {
    // defined to avoid jshint errors
    var a, b, c, d, e, f, g, h, i, j;

    it("changes a callback to then", function() {
        compare(function() {
            a(function (err, value) {
                console.log(value);
            });
        }, function() {
            a().then(function (value) {
                console.log(value);
            });
        });
    });

    it("doesn't change a callback to then when it doesn't have two parameters", function() {
        compare(function() {
            a(function (err) {
                console.log(err);
            });
        }, function() {
            a(function (err) {
                console.log(err);
            });
        });

        compare(function() {
            a(function (err, value, more) {
                console.log(value, more);
            });
        }, function() {
            a(function (err, value, more) {
                console.log(value, more);
            });
        });
    });

    describe("error handler", function() {
        it("creates a rejection handler from if statement", function() {
            compare(function() {
                a(function (err, value) {
                    if(err) {
                        return;
                    }
                    console.log(value);
                });
            }, function() {
                a().then(function (value) {
                    console.log(value);
                }, function (err) {
                    return;
                });
            });
        });

        it("handles consequant that isn't a block statement", function() {
            compare(function() {
                a(function (err, value) {
                    if(err) return;
                    console.log(value);
                });
            }, function() {
                a().then(function (value) {
                    console.log(value);
                }, function (err) {
                    return;
                });
            });
        });
    });

    describe("nested", function() {
        describe("callbacks", function() {
            it("are transformed into a chained thens", function() {
                compare(function () {
                    a(function (errA, valueA) {
                        b(valueA, function (errB, valueB) {
                            console.log(valueB);
                        });
                    });
                }, function() {
                    a().then(function (valueA) {
                        return b(valueA);
                    }).then(function (valueB) {
                        console.log(valueB);
                    });
                });<commit_msg>Add test for if/else in Node callback<commit_after>/*global describe,before,it,expect,after */

var promiseMe = require("../index.js");

function convertFunctionToString(fn) {
    var lines = fn.toString().split("\n");
    lines.pop();
    lines.shift();
    lines = lines.map(function(line) {
        return line.trim();
    });
    return lines.join("\n");
}

// This utility function allows us to write tests as Javascript functions
// instead of strings. Note: the functions are never executed so they don't
// need to make semantic sense.
var compare = function(before, after) {
    before = convertFunctionToString(before);
    after = convertFunctionToString(after);

    expect(promiseMe.convert(
        before,
        {format: {indent: {style: '', base: 0 }}}
    )).toEqual(after);
};

describe("promise-me basics", function() {
    // defined to avoid jshint errors
    var a, b, c, d, e, f, g, h, i, j;

    it("changes a callback to then", function() {
        compare(function() {
            a(function (err, value) {
                console.log(value);
            });
        }, function() {
            a().then(function (value) {
                console.log(value);
            });
        });
    });

    it("doesn't change a callback to then when it doesn't have two parameters", function() {
        compare(function() {
            a(function (err) {
                console.log(err);
            });
        }, function() {
            a(function (err) {
                console.log(err);
            });
        });

        compare(function() {
            a(function (err, value, more) {
                console.log(value, more);
            });
        }, function() {
            a(function (err, value, more) {
                console.log(value, more);
            });
        });
    });

    describe("error handler", function() {
        it("creates a rejection handler from if statement", function() {
            compare(function() {
                a(function (err, value) {
                    if(err) {
                        return;
                    }
                    console.log(value);
                });
            }, function() {
                a().then(function (value) {
                    console.log(value);
                }, function (err) {
                    return;
                });
            });
        });

        it("handles consequant that isn't a block statement", function() {
            compare(function() {
                a(function (err, value) {
                    if(err) return;
                    console.log(value);
                });
            }, function() {
                a().then(function (value) {
                    console.log(value);
                }, function (err) {
                    return;
                });
            });
        });

        it("handles if else/alternate", function() {
            compare(function() {
                a(function (err, value) {
                    if(err) {
                        console.error(err);
                    } else {
                        console.log(value);
                    }
                });
            }, function() {
                a().then(function (value) {
                    console.log(value);
                }, function (err) {
                    console.error(err);
                });
            });
        });
    });

    describe("nested", function() {
        describe("callbacks", function() {
            it("are transformed into a chained thens", function() {
                compare(function () {
                    a(function (errA, valueA) {
                        b(valueA, function (errB, valueB) {
                            console.log(valueB);
                        });
                    });
                }, function() {
                    a().then(function (valueA) {
                        return b(valueA);
                    }).then(function (valueB) {
                        console.log(valueB);
                    });
                });
{'fraction_non_alphanumeric': 0.13373015873015873, 'fraction_numerical': 0.00026455026455026457, 'mean_word_length': 1.2383066903493192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1956069', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1809, 'n_words': 437}