/**
 * test-gulp.jslint
 * Some basic tests to ensure that
 * package is working as intended.
 *
 * Copyright (C) 2014 Karim Alibhai.
 **/

(function () {
    "use strict";

    var fs = require('fs'),
        path = require('path'),
        test = require('tape'),
        Vinyl = require('vinyl'),
        jslint = require('../'),
        lint = function (why, file, dir) {
            var goodCode = true;

            // directives
            dir = dir || {};

            // create test
            test(why, function (t) {
                t.plan(1);

                // create stream with custom reporter
                var str = jslint(dir);
                str.pipe(jslint.reporter(function (evt) {
                    if (goodCode && evt.success) {
                        t.ok(true, 'lint passed (' + file + ')');
                    } else {
                        t.ok(!goodCode, 'lint failed (' + file + ')');
                    }
                }));

                // read in sample file
                fs.readFile(path.resolve(__dirname, './' + file), function (err, data) {
                    if (err) {
                        t.fail(err);
                    } else {
                        // prepare callback
                        str.on('error', function (err) {
                            err = String(err);

                            if (err.indexOf('failed') === -1) {
                                t.fail(err);
                            }
                        });

                        // push file into stream
                        str.write(new Vinyl({
                            base: __dirname,
                            cwd: path.resolve(__dirname, '../'),
                            path: path.join(__dirname, file),
                            contents: data
                        }));
                    }
                });
            });

            // allow test to expect failure
            return {
                fail: function () {
                    goodCode = false;
                }
            };
        };

    lint('with good code', 'test-good.js');
    lint('with bad code', 'test-nomen.js').fail();
    lint('with directives', 'test-nomen.js', {
        nomen: true
    });
    lint('with good code (with shebang)', 'test-shebang.js', {
        node: true
    });
    lint('with good code (missing globals)', 'test-jquery.js').fail();
    lint('with good code (given globals)', 'test-jquery.js', {
        global: ['$']
    });
    lint('with good code (given predef)', 'test-jquery.js', {
        predef: ['$']
    });

    test('stream support', function (t) {
        t.plan(1);

        var str = jslint();

        str.on('error', function () {
            t.ok(true, 'errored out on stream');
        });

        str.write({
            isStream: function () {
                return true;
            }
        });
    });

    test('null input', function (t) {
        t.plan(1);

        var str = jslint();

        str.on('data', function () {
            t.ok(true, 'ignored null');
        });

        str.on('error', function () {
            t.ok(false, 'errored out on null');
        });

        str.write({
            isStream: function () {
                return false;
            },
            isNull: function () {
                return true;
            }
        });
    });

    test('custom reporter via string', function (t) {
        t.plan(4);

        var str = jslint();
        str.pipe(jslint.reporter(require('./test-reporter')));

        str.on('data', function () {
            t.ok(!!global.GULP_JSLINT_REPORTER, 'reporter fired');
            t.ok(global.GULP_JSLINT_REPORTER.hasOwnProperty('filename'), 'source file is in event data');
            t.ok(global.GULP_JSLINT_REPORTER.hasOwnProperty('success'), 'lint status is in event data');
            t.ok(global.GULP_JSLINT_REPORTER.hasOwnProperty('errors'), 'errors list is in event data');
        });

        fs.readFile(path.resolve(__dirname, './test-good.js'), function (err, data) {
            if (err) {
                t.fail(err);
            } else {
                str.write(new Vinyl({
                    base: __dirname,
                    cwd: path.resolve(__dirname, '../'),
                    path: path.join(__dirname, 'test-good.js'),
                    contents: data
                }));
            }
        });
    });

    test('custom reporter via function', function (t) {
        t.plan(4);

        var str = jslint();
        str.pipe(jslint.reporter(function (evt) {
            t.ok(!!evt, 'reporter fired');
            t.ok(evt.hasOwnProperty('filename'), 'source file is in event data');
            t.ok(evt.hasOwnProperty('success'), 'lint status is in event data');
            t.ok(evt.hasOwnProperty('errors'), 'errors list is in event data');
        }));

        fs.readFile(path.resolve(__dirname, './test-good.js'), function (err, data) {
            if (err) {
                t.fail(err);
            } else {
                str.write(new Vinyl({
                    base: __dirname,
                    cwd: path.resolve(__dirname, '../'),
                    path: path.join(__dirname, 'test-good.js'),
                    contents: data
                }));
            }
        });
    });

    test('custom bad reporter (object)', function (t) {
        t.plan(1);
        t.throws(function () {
            var str = jslint();
            str.pipe(jslint.reporter({
                what: 'this object should be ignored'
            }));
        }, 'path must be a string');
    });

    test('custom bad reporter (missing module)', function (t) {
        t.plan(1);
        t.throws(function () {
            var str = jslint();
            str.pipe(jslint.reporter('some-random-ass-reporter'));
        }, 'Cannot find module \'some-random-ass-reporter\'');
    });
}());

@@ -2807,32 +2807,111 @@
     str.write(%7B%0A
+            isNull: function () %7B%0A                return false;%0A            %7D,%0A
             isSt

fix stream input test