<commit_before>'use strict';

const sinon = require('sinon');

const EventParser   = require('./../../lib/events/EventParser');
const ParserContext = require('./../../lib/events/ParseContext');

require('./../support');

describe('EventParser', () => {

    let parser, beginSpy;

    let beginObj = {
        "context":   "1337",
        "timestamp": 12345,
        "begin":     {"parent": null, "scope": {}}
    };

    let customObj = {
        "context":   "1337",
        "timestamp": 12346,
        "something": "happened"
    };

    beforeEach(() => {

        parser   = new EventParser();
        beginSpy = sinon.spy();

        parser.on('begin', beginSpy);
    });

    describe('context parsing', () => {

        it('should parse contexts when the "begin" event is fired', () => {

            parser.receive(beginObj);

            beginSpy.should.have.been.calledOnce;
            beginSpy.should.have.been.calledWithMatch(sinon.match.instanceOf(ParserContext));
        });

        it('should expose contexts via the getContext method', () => {

            parser.receive(beginObj);
            parser.getContext("1337").should.be.an.instanceOf(ParserContext);
        });
    });

    describe('.use', () => {

        it('should allow to attach reducers', () => {

            let reducer = sinon.spy();
            parser.use(data => reducer(data.get('something')));

            parser.receive(beginObj);
            parser.receive(customObj);

            reducer.should.have.been.calledOnce;
            reducer.should.have.been.calledWith('happened');
        });

        it('should not call the second reducer when the first one returned null', () => {

            let reducer = sinon.spy();
            parser.use(data => null);
            parser.use(reducer);

            parser.receive(beginObj);
            parser.receive(customObj);

            reducer.should.not.have.been.called;
        });
    });

    describe('.useCombine', () => {

        it('should allow to attach handlers for specific properties inside the event object', () => {

            let reducer = sinon.spy();
            parser.useCombine({
                something: reducer
            });

            parser.receive(beginObj);
            parser.receive(customObj);

            reducer.should.have.been.calledWith('happened');
        });
    });

    describe('strict mode (.setStrict())', () => {

        it('should throw an error if an event object goes unreduced', () => {

            parser.setStrict();

            parser.receive(beginObj);
            (() => parser.receive(customObj)).should.throw();
        });

        it('should not throw an error if an attached handler reduces the event object', () => {

            parser.setStrict();
            parser.use(data => null);

            parser.receive(beginObj);
            (() => parser.receive(customObj)).should.not.throw();<commit_msg>Update tests<commit_after>'use strict';

const sinon = require('sinon');

const EventParser   = require('./../../lib/events/EventParser');
const ParserContext = require('./../../lib/events/ParseContext');

require('./../support');

describe('EventParser', () => {

    let parser, beginSpy;

    let beginObj = {
        "context":   "1337",
        "timestamp": 12345,
        "begin":     {"parent": null, "scope": {}}
    };

    let customObj = {
        "context":   "1337",
        "timestamp": 12346,
        "something": "happened"
    };

    beforeEach(() => {

        parser   = new EventParser();
        beginSpy = sinon.spy();

        parser.on('begin', beginSpy);
    });

    describe('context parsing', () => {

        it('should parse contexts when the "begin" event is fired', () => {

            parser.receive(beginObj);

            beginSpy.should.have.been.calledOnce;
            beginSpy.should.have.been.calledWithMatch(sinon.match.instanceOf(ParserContext));
        });

        it('should expose contexts via the getContext method', () => {

            parser.receive(beginObj);
            parser.getContext("1337").should.be.an.instanceOf(ParserContext);
        });
    });

    describe('.use', () => {

        it('should allow to attach reducers', () => {

            let reducer = sinon.spy();
            parser.use(data => reducer(data.get('something')));

            parser.receive(customObj);

            reducer.should.have.been.calledOnce;
            reducer.should.have.been.calledWith('happened');
        });

        it('should not call the second reducer when the first one returned null', () => {

            let reducer = sinon.spy();
            parser.use(data => null);
            parser.use(reducer);

            parser.receive(beginObj);
            parser.receive(customObj);

            reducer.should.not.have.been.called;
        });
    });

    describe('.useCombine', () => {

        it('should allow to attach handlers for specific properties inside the event object', () => {

            let reducer = sinon.spy();
            parser.useCombine({
                something: reducer
            });

            parser.receive(beginObj);
            parser.receive(customObj);

            reducer.should.have.been.calledWith('happened');
        });
    });

    describe('strict mode (.setStrict())', () => {

        it('should throw an error if an event object goes unreduced', () => {

            parser.setStrict();

            (() => parser.receive(customObj)).should.throw();
        });

        it('should not throw an error if an attached handler reduces the event object', () => {

            parser.setStrict();
            parser.use(data => null);

            parser.receive(beginObj);
            (() => parser.receive(customObj)).should.not.throw();
{'fraction_non_alphanumeric': 0.15693746752121948, 'fraction_numerical': 0.007621687164385935, 'mean_word_length': 2.004162330905307, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 39, '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': '6242835', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1527, 'n_words': 312}