<commit_before>"use strict";
/*jshint mocha: true*/
/*eslint-env mocha*/
/*global assert*/

/*eslint-disable max-nested-callbacks*/

describe("events", function () {

    describe("gpf.events.EVENT_xxx", function () {
        [ // expected list of known events
            "ERROR",
            "READY",
            "DATA",
            "END_OF_DATA",
            "CONTINUE",
            "STOP",
            "STOPPED"
        ].forEach(function (eventName) {
            it("declares gpf.events.EVENT_" + eventName, function () {
                assert(undefined !== gpf.events["EVENT_" + eventName]);
            });
        });
    });

    describe("gpf.events.Event", function () {

        it("has a correct class structure", function () {
            var event = new gpf.events.Event("test");
            assert(event instanceof gpf.events.Event);
            assert(event.constructor === gpf.events.Event);
        });

        it("should expose type and a default scope", function () {
            var event = new gpf.events.Event("test");
            assert("test" === event.type);
            assert(event.scope === gpf.context());
        });

        it("should transmit parameters", function () {
            var now = new Date(),
                event = new gpf.events.Event("test", {
                    string: "Hello world!",
                    number: 123,
                    "boolean": true,
                    object: now
                });
            assert("test" === event.type);
            assert("Hello world!" === event.get("string"));
            assert(123 === event.get("number"));
            assert(true === event.get("boolean"));
            assert(now === event.get("object"));
            assert(event.scope === gpf.context());
        });

        it("should allow a custom event scope", function () {
            var scope = {},
                now = new Date(),
                event = new gpf.events.Event("test", {
                    string: "Hello world!",
                    number: 123,
                    "boolean": true,
                    object: now
                }, scope);
            assert("test" === event.type);
            assert("Hello world!" === event.get("string"));
            assert(123 === event.get("number"));
            assert(true === event.get("boolean"));
            assert(now === event.get("object"));
            assert(event.scope === scope);
        });

    });

    describe("gpf.events.fire", function () {

        it("triggers any Function", function (done) {
            gpf.events.fire("test", function (event) {
                assert("test" === event.type);
                assert(event.scope === gpf.context());
                done();
            });
        });

        it("triggers Function using event scope", function (done) {
            var scope = {};
            gpf.events.fire.apply(scope, ["test", function (event) {
                assert("test" === event.type);
                assert(event.scope === scope);
                assert(this === scope); //eslint-disable-line no-invalid-this
                done();
            }]);
        });

        it("triggers Function using initial event scope", function (done) {
            var scope1 = {},
                scope2 = {},
                eventObj = new gpf.events.Event("test", {}, scope1);
            assert(scope1 !== scope2);
            assert(eventObj.scope === scope1);
            gpf.events.fire.apply(scope2, [eventObj, function (event) {
                assert("test" === event.type);
                assert(event.scope === scope1);
                assert(this === scope1);  //eslint-disable-line no-invalid-this
                done();
            }]);
        });

        it("triggers Object method", function (done) {
            var scope = {
                fail: function (/*event*/) {
                    assert(false);
                },
                test: function (event) {
                    assert("test" === event.type);
                    assert(event.scope === gpf.context());
                    assert(this === scope);
                    done();
                }
            };
            gpf.events.fire("test", scope);
        });

        it("triggers Object method using object scope", function (done) {
            var scope1 = {},
                scope2 = {
                    fail: function (/*event*/) {
                        assert(false);
                    },
                    test: function (event) {
                        assert("test" === event.type);
                        assert(event.scope === scope1);
                        assert(this === scope2);
                        done();
                    }
                };
            gpf.events.fire.apply(scope1, ["test", scope2]);
        });

        it("triggers Object method using scope member", function (done) {
            var scope1 = {},
                scope2 = {},
                scope3 = {
                    fail: function (/*event*/) {
                        assert(false);
                    },
                    test: function (event) {
                        assert("test" === event.type);
                        assert(event.scope === scope1);
                        assert(this === scope2);
                        done();
                    },
                    scope: scope2
                };
            gpf.events.fire.apply(scope1, ["test", scope3]);
        });

        it("triggers Object default method", function (done) {
            var scope1 = {},
                scope2 = {},
                scope3 = {
                    fail: function (/*event*/) {
                        assert(false);
                    },
                    "*": function (event) {
                        assert("test" === event.type);
                        assert(event.scope === scope1);
                        assert(this === scope2);
                        done();
                    },
                    scope: scope2
                };
            gpf.events.fire.apply(scope1, ["test", scope3]);
        });

        it("triggers Object method instead of default", function (done) {
            var scope1 = {},
                scope2 = {},
                scope3 = {
                    fail: function (/*event*/) {
                        assert(false);
                    },
                    "*": function (/*event*/) {
                        assert(false);
                    },
                    test: function (event) {
                        assert("test" === event.type);
                        assert(event.scope === scope1);
                        assert(this === scope2);
                        done();
                    },
                    scope: scope2
                };
            gpf.events.fire.apply(scope1, ["test", scope3]);
        });

        it("defers sequential calls to limit stack usage");

    });

});<commit_msg>Added a Promise result to fire<commit_after>"use strict";
/*jshint mocha: true*/
/*eslint-env mocha*/
/*global assert*/

/*eslint-disable max-nested-callbacks*/

describe("events", function () {

    describe("gpf.events.EVENT_xxx", function () {
        [ // expected list of known events
            "ERROR",
            "READY",
            "DATA",
            "END_OF_DATA",
            "CONTINUE",
            "STOP",
            "STOPPED"
        ].forEach(function (eventName) {
            it("declares gpf.events.EVENT_" + eventName, function () {
                assert(undefined !== gpf.events["EVENT_" + eventName]);
            });
        });
    });

    describe("gpf.events.Event", function () {

        it("has a correct class structure", function () {
            var event = new gpf.events.Event("test");
            assert(event instanceof gpf.events.Event);
            assert(event.constructor === gpf.events.Event);
        });

        it("should expose type and a default scope", function () {
            var event = new gpf.events.Event("test");
            assert("test" === event.type);
            assert(event.scope === gpf.context());
        });

        it("should transmit parameters", function () {
            var now = new Date(),
                event = new gpf.events.Event("test", {
                    string: "Hello world!",
                    number: 123,
                    "boolean": true,
                    object: now
                });
            assert("test" === event.type);
            assert("Hello world!" === event.get("string"));
            assert(123 === event.get("number"));
            assert(true === event.get("boolean"));
            assert(now === event.get("object"));
            assert(event.scope === gpf.context());
        });

        it("should allow a custom event scope", function () {
            var scope = {},
                now = new Date(),
                event = new gpf.events.Event("test", {
                    string: "Hello world!",
                    number: 123,
                    "boolean": true,
                    object: now
                }, scope);
            assert("test" === event.type);
            assert("Hello world!" === event.get("string"));
            assert(123 === event.get("number"));
            assert(true === event.get("boolean"));
            assert(now === event.get("object"));
            assert(event.scope === scope);
        });

    });

    describe("gpf.events.fire", function () {

        var receivedEvent,
            receivedScope;

        function clean() {
            receivedEvent = null;
            receivedScope = null;
        }

        function generateTestCases (eventHandler) {

            it("triggers the handler", function (done) {
                clean();
                gpf.events.fire("test", eventHandler)
                    .then(function (event) {
                        assert(event === receivedEvent);
                        assert("test" === receivedEvent.type);
                        assert(gpf.context() === receivedScope);
                        assert(gpf.context() === receivedEvent.scope);
                        done();
                    });
            });

            it("triggers the handler using the event scope", function (done) {
                var scope = {};
                gpf.events.fire.apply(scope, ["test", eventHandler])
                    .then(function (event){
                        assert(event === receivedEvent);
                        assert("test" === receivedEvent.type);
                        assert(scope === receivedScope);
                        assert(scope === receivedEvent.scope);
                        done();
                    });
            });

            it("triggers the handler using only the event scope", function (done) {
                var scope1 = {},
                    scope2 = {},
                    eventObj = new gpf.events.Event("test", {}, scope1);
                assert(scope1 !== scope2);
                assert(eventObj.scope === scope1);
                gpf.events.fire.apply(scope2, [eventObj, eventHandler])
                    .then(function (event) {
                        assert(event === receivedEvent);
                        assert("test" === receivedEvent.type);
                        assert(scope1 === receivedScope);
                        assert(scope2 === receivedEvent.scope);
                        done();
                    });
            });

        }

        describe("on a function", function () {

            generateTestCases(function (event) {
                receivedEvent = event;
                receivedScope = this; //eslint-disable-line no-invalid-this
            });

        });

        describe("on an function dictionary", function () {

            generateTestCases({
                error: function (/*event*/) {
                    assert(false);
                },
                test: function (event) {
                    receivedEvent = event;
                    receivedScope = this; //eslint-disable-line no-invalid-this
                }
            });

        });

        describe("on an function dictionary's default method", function () {

             generateTestCases({
                error: function (/*event*/) {
                    assert(false);
                },
                test2: function (/*event*/) {
                    assert(false);
                },
                tes: function (/*event*/) {
                    assert(false);
                },
                "*": function (event) {
                    receivedEvent = event;
                    receivedScope = this; //eslint-disable-line no-invalid-this
                }
            });

        });

        it("defers recursive calls to limit the stack usage", function (done) {
            var inCall = false,
                depth = 0;
            function handler (/*event*/) {
                if (inCall) {
                    done();
                }
                assert(100 > depth);
                ++depth;
                inCall = true;
                gpf.events.fire("test", handler);
                inCall = false;
            }
            handler();
        });

    });

});
{'fraction_non_alphanumeric': 0.1382450331125828, 'fraction_numerical': 0.005794701986754967, 'mean_word_length': 1.1856907894736841, 'pattern_counts': {'":': 7, '<': 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': '1624365', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3015, 'n_words': 688}