all files / workspace/ index.js

100% Statements 40/40
100% Branches 52/52
100% Functions 10/10
100% Lines 40/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112           21×   13×     228× 228×       13×   22×   11×         11× 11×     22×   22×     13×   13× 13× 22×       13×                                                                                            
var stream = require('stream');
var assert = require('assert');
var Mocha = require('mocha');
 
var mocha = new Mocha();
 
process.once('beforeExit', function () {
    mocha.run();
});
 
module.exports = function (expected, message, data) {
    
    if (expected instanceof Function ||  typeof expected === 'function') {
        
        var unit = new stream.Duplex({
            objectMode: true,
            read: function() {
                this.push(data);
                this.pause();
            }
        });
        
        unit.on('pipe', function (source) {
            
            this._write = function (data, encoding, callback) {
                
                mocha.suite.addTest(new Mocha.Test(
                    message || expected.toString(),
                    expected.bind(null, data, callback)
                ));
                
                source.unpipe(this);
                this.unpipe(source);
            };
            
            this._readableState.pipesCount === 0 && this.pipe(source);
            
            this.emit('drain');
        });
        
        unit.pipe = (function (pipe) {
            
            return function () {
                for (var key in arguments)
                    pipe.call(this, arguments[key]);
            };
        })(unit.pipe);
        
        return unit;
    }
    
    if (expected instanceof String || typeof expected === 'string')
        
        return module.exports(
            assert.strictEqual.bind(null, expected),
            message || '\'' + expected + '\'',
            data || expected
        );
    
    if (expected instanceof RegExp)
        
        return module.exports(function (data) {
                assert(expected.test(data));
            },
            message || expected.toString(),
            data || expected
        );
    
    if (expected instanceof Object || typeof callback === 'object')
        
        return module.exports(
            assert.deepEqual.bind(null, expected),
            message || JSON.stringify(expected),
            data || expected
        );
    
    return module.exports(
        assert.bind(null, process.env.npm_package_config_test),
        'not expected'
    );
};
 
module.exports.not = function (expected, message, data) {
    
    if (expected instanceof String || typeof expected === 'string')
        
        return module.exports(
            assert.notStrictEqual.bind(null, expected),
            message || 'not \'' + expected + '\'',
            data || expected
        );
    
    if (expected instanceof RegExp)
        
        return module.exports(function (data) {
                assert(!expected.test(data));
            },
            message || 'not ' + expected.toString(),
            data || expected
        );
    
    if (expected instanceof Object || typeof callback === 'object')
        
        return module.exports(
            assert.notDeepStrictEqual.bind(null, expected),
            message || 'not ' + JSON.stringify(expected),
            data || expected
        );
    
    return module.exports(expected);
};