<commit_before>'use strict';

var _ = require('lodash');
var expect = require('chai').expect;
var Logger = require('../index').Logger;
var Observable = require('rxjs').Observable;

function multiLevelTest(logger, which, methods, counts, levels) {
    var callCount = 0;
    logger[which].apply(logger, levels)
        .subscribe(function onNext() {
            callCount++;
        });
    expect(callCount).to.equal(counts[0]);
    _.forEach(methods, function(method, index) {
        logger[method]();
        expect(callCount).to.equal(counts[index + 1]);
    });
    expect(callCount).to.equal(counts.pop());
}

describe('Logger', function() {

    /* jshint -W030 */

    it('returns function', function() {
        expect(Logger).to.be.a('function');
    });

    it('creates separate instances', function() {
        expect(new Logger()).not.to.equal(new Logger());
    });

    it('has static Levels member', function() {
        expect(Logger.Levels).not.to.be.undefined;
        expect(Logger.Levels.ALL).to.equal('ALL');
    });

    describe('> instance', function() {

        beforeEach(function createInstance() {
            this.logger = new Logger();
        });

        it('does not provide static Level member', function() {
            expect(this.logger.Levels).to.be.undefined;
        });

        it('has expected log methods', function() {
            var logger = this.logger;
            expect(logger.all).to.be.undefined;
            expect(logger.none).to.be.undefined;
            _.forEach(_.without(Logger.Levels, 'ALL', 'NONE'), function(level) {
                expect(logger[level.toLowerCase()]).to.be.a('function');
            });
        });

        it('has expected observable methods', function() {
            expect(this.logger.from).to.be.a('function');
            expect(this.logger.filter).to.be.a('function');
        });

        describe('.filter', function() {

            it('accepts single level', function() {
                expect(this.logger.filter('ALL')).to.be.defined;
            });

            it('accepts multiple levels', function() {
                expect(this.logger.filter('INFO', 'ERROR')).to.be.defined;
            });

            it('accepts array of levels', function() {
                expect(this.logger.filter(['INFO', 'WARN'])).to.be.defined;
            });

            it('returns Observable', function() {
                expect(this.logger.filter('ALL').subscribe).to.be.a('function');
            });

            it('ignores invalid levels', function() {
                expect(this.logger.filter('DNE')).to.be.defined;
            });

            it('ignores duplicate levels', function() {
                multiLevelTest(this.logger, 'filter', ['warn'], [0, 1], ['WARN', 'WARN']);
            });

            it('ALL returns all entries', function() {
                multiLevelTest(this.logger, 'filter', ['warn', 'info', 'debug', 'error'], [0, 1, 2, 3, 4], ['ALL']);
            });

            it('NONE returns no entries', function() {
                multiLevelTest(this.logger, 'filter', ['warn', 'info', 'debug', 'error'], [0, 0, 0, 0, 0], ['NONE']);
            });

            it('NONE and ALL defaults to NONE', function() {
                multiLevelTest(this.logger, 'filter', ['warn', 'info', 'debug', 'error'], [0, 0, 0, 0, 0], ['NONE', 'ALL']);
            });

            it('only shows messages at specified level', function() {
                multiLevelTest(this.logger, 'filter', ['warn', 'error', 'debug'], [0, 0, 1, 1], ['ERROR']);
            });

            it('passes level and message to subscribers', function() {
                this.logger.filter('INFO').subscribe(function onNext(data) {
                    expect(data.level).to.equal('INFO');
                    expect(data.msg).to.be.a('string');
                    expect(data.msg).to.equal('test message');
                });
                this.logger.info('test message');
            });

            it('formats message strings', function() {
                this.logger.filter('INFO')
                    .subscribe(function onNext(data) {
                        expect(data.msg).to.be.a('string');
                        expect(data.msg).to.equal('Hello, world: 123');
                    });
                this.logger.info('Hello, %s: %d', 'world', 123);
            });

            it('formats message objects', function() {
                this.logger.filter('INFO')
                    .subscribe(function onNext(data) {
                        expect(data.msg).to.be.a('string');
                        expect(data.msg).to.equal('{ a: \'b\', c: [Function: d] }');
                    });
                this.logger.info({a: 'b', c: function d() {}});
            });

            it('previous log entries will not appear', function() {
                this.logger.warn('warning 1');
                this.logger.warn('warning 2');
                multiLevelTest(this.logger, 'filter', ['error', 'warn'], [0, 0, 1], ['WARN']);
            });

        });

        describe('.from', function() {

            it('returns Observable', function() {
                expect(this.logger.from().subscribe).to.be.a('function');
            });

            it('ALL shows all messages', function() {
                multiLevelTest(this.logger, 'from', ['trace', 'debug', 'warn', 'info', 'error'], [0, 1, 2, 3, 4, 5], ['ALL']);
            });

            it('NONE shows no messages', function() {
                multiLevelTest(this.logger, 'from', ['trace', 'debug', 'warn', 'info', 'error'], [0, 0, 0, 0, 0, 0], ['NONE']);
            });

            it('WARN shows WARN and ERROR but not INFO', function() {
                multiLevelTest(this.logger, 'from', ['trace', 'debug', 'warn', 'info', 'error'], [0, 0, 0, 1, 1, 2], ['WARN']);
            });

            it('previous log entries will not appear', function() {
                this.logger.warn('warning 1');
                this.logger.warn('warning 2');
                multiLevelTest(this.logger, 'from', ['debug', 'warn'], [0, 0, 1], ['WARN']);
            });

        });

    });

});<commit_msg>Removed old spec<commit_after>'use strict';

var expect = require('chai').expect;
var Logger = require('../dist/Logger').Logger;

describe('Logger', function() {

    it('has static log method', function() {
        expect(Logger).itself.to.respondTo('log');
    });

    it('throws if name not provided', function() {
        expect(function() {
            new Logger();
        }).to.throw('A name must be specified.');
    });

    it('has name property', function() {
        expect(new Logger('test name').name).to.equal('test name');
    });

    it('name property is readonly', function() {
        expect(function() {
            new Logger('test').name = 'readonly';
        }).to.throw();
    });

    ['trace', 'debug', 'info', 'warn', 'error'].forEach(function(level) {

        describe('.' + level, function() {

            it('calls static log method', function() {
                var orig = Logger.log;
                Logger.log = function spy(lvl, message, logger) {
                    expect(lvl).to.equal(level.toUpperCase());
                    expect(
                        message.message ||
                        message.split('\n').shift() ||
                        message
                    ).to.equal('test message');
                    expect(logger).to.equal('logger name');
                };
                new Logger('logger name')[level]('test message');
                Logger.log = orig;
            });

            it('returns Logger instance for chaining', function() {
                expect(new Logger('test')[level]()).to.be.an.instanceof(Logger);
            });

            if (level === 'error') {

                it('turns message into Error instance', function() {
                    var orig = Logger.log;
                    Logger.log = function spy(lvl, message) {
                        expect(message).to.be.an.instanceof(Error);
                    };
                    new Logger('test').error('test message');
                    Logger.log = orig;
                });

            } else if (level === 'trace') {

                it('includes stack trace with message', function() {
                    var orig = Logger.log;
                    Logger.log = function spy(lvl, message) {
                        expect(message.split('\n').some(function(line) {
                            return line.indexOf('Logger-spec') !== -1;
                        })).to.equal(true);
                    };
                    new Logger('test').trace('test message');
                    Logger.log = orig;
                });

            }

        });

    });

});
{'fraction_non_alphanumeric': 0.17518745739604635, 'fraction_numerical': 0.0070438536696205405, 'mean_word_length': 1.7347002174588382, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4398209', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2272, 'n_words': 504}