<commit_before>'use strict';

var _       = require('underscore');
var Class   = require('../');
var expect  = require('./lib/expect');

describe("events", function(){

describe("Class ext: events", function(){
    var EventClass = Class({
            Implements: 'events',
            a: 1,
            b: 2
        });


    describe(".on(), feated with .fire()", function(){
        it("could register a sync event handler", function(){
            var flag = true;
                
            var obj = new EventClass().on('myEvent', function(){
                flag = !flag
            });
        
            expect(flag).toBe(true);
            
            obj.fire('myEvent');
            expect(flag).toBe(false);
            
            obj.fire('myEvent');
            expect(flag).toBe(true);
        });
        
        
        // it("could deal with function overloading, and register a list of event handlers", function(){
        //     var flag1 = true,
        //         flag2 = true;
                
        //     var obj = new EventClass().on({
        //             event1: function(){
        //                 flag1 = !flag1;
        //             },
                    
        //             event2: function(){
        //                 flag2 = !flag2;
        //             }
        //         });
            
        //     obj.fire('event1');
        //     expect(flag1).toBe(false);
            
        //     obj.fire('event2');
        //     expect(flag2).toBe(false);
        // });
        
        it("the `this` object should be the current instance", function(){
            var obj = new EventClass().on('increase', function(){
                this.a ++;
            });
            
            expect(obj.a).toBe(1);
            
            obj.fire('increase');
            expect(obj.a).toBe(2);
        });
        
        it("could register more than one handlers to a same event type", function(){
            var obj = new EventClass();
            
            obj.on('increase', function(){
                this.a ++;
            });
            
            obj.on('increase', function(){
                this.a ++;
            });
        
            expect(obj.a).toBe(1);
            
            obj.fire('increase');
            expect(obj.a).toBe(3);
        });
        
        it("the execution of handlers should be maintained by registering sequence", function(){
            var obj = new EventClass();
            
            obj.on('increase', function(){
                this.a ++;
            });
            
            obj.on('increase', function(){
                this.c = this.a * this.b;
            });
            
            expect(obj.c).toBe(undefined);
            
            obj.fire('increase');
            expect(obj.c === 2).toBe(false);
            expect(obj.c).toBe(4);
        });
    });

    describe(".off(type, fn), feated with .fire()", function(){
    
        it("should return the instance", function(){
            var handler = function(){},
                obj = new EventClass().on('increase', handler);
        
            expect(obj.off('increase', handler)).toBe(obj);
            expect(obj.off('increase')).toBe(obj);
            expect(obj.off()).toBe(obj);
            
            // error invocation
            expect(obj.off(null)).toBe(obj);
            expect(obj.off(null, null)).toBe(obj);
            expect(obj.off(null, handler)).toBe(obj);
        });
        
        it("could remove a specific handler of a certain type: .off(type, fn)", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                obj = new EventClass().on('increase', handler);
            
            obj.fire('increase');
            expect(flag).toBe(2);
            
            obj.off('increase', handler);
            obj.fire('increase');
            expect(flag).toBe(2);
        });
        
        it("could remove all handlers of a certain type: .off(type)", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                handler2 = function(){
                    flag = flag + 2;  
                },
                obj = new EventClass().on('increase', handler).on('increase', handler2);
            
            obj.off('increase');
            obj.fire('increase');
            expect(flag).toBe(1);
        });
        
        it("could remove all registered events: .off()", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                handler2 = function(){
                    flag = flag + 2;  
                },
                obj = new EventClass().on('increase1', handler).on('increase2', handler2);
            
            obj.off();
            obj.fire('increase1');
            obj.fire('increase2');
            expect(flag).toBe(1);
        });
        
        it("would do nothing if bad arguments", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                handler2 = function(){
                    flag = flag + 2;  
                },
                obj = new EventClass().on('increase1', handler).on('increase1', handler).on('increase2', handler2);
            
            obj.off(undefined);
            obj.off(undefined, handler);
            obj.off(undefined, undefined);
            obj.off('increase1', undefined);
            
            obj.fire('increase1');
            obj.fire('increase2');
            expect(flag).toBe(5);
        });
    });
    
    
    describe(".fire()", function(){
        <commit_msg>#4: update test cases for events<commit_after>'use strict';

var _       = require('underscore');
var Class   = require('../');
var expect  = require('./lib/expect');

describe("events", function(){

describe("Class ext: events", function(){
    var EventClass = Class({
            Implements: 'events',
            a: 1,
            b: 2
        });


    describe(".on(), feated with .emit()", function(){
        it("could register a sync event handler", function(){
            var flag = true;
                
            var obj = new EventClass().on('myEvent', function(){
                flag = !flag
            });
        
            expect(flag).toBe(true);
            
            obj.emit('myEvent');
            expect(flag).toBe(false);
            
            obj.emit('myEvent');
            expect(flag).toBe(true);
        });
        
        
        // it("could deal with function overloading, and register a list of event handlers", function(){
        //     var flag1 = true,
        //         flag2 = true;
                
        //     var obj = new EventClass().on({
        //             event1: function(){
        //                 flag1 = !flag1;
        //             },
                    
        //             event2: function(){
        //                 flag2 = !flag2;
        //             }
        //         });
            
        //     obj.emit('event1');
        //     expect(flag1).toBe(false);
            
        //     obj.emit('event2');
        //     expect(flag2).toBe(false);
        // });
        
        it("the `this` object should be the current instance", function(){
            var obj = new EventClass().on('increase', function(){
                this.a ++;
            });
            
            expect(obj.a).toBe(1);
            
            obj.emit('increase');
            expect(obj.a).toBe(2);
        });
        
        it("could register more than one handlers to a same event type", function(){
            var obj = new EventClass();
            
            obj.on('increase', function(){
                this.a ++;
            });
            
            obj.on('increase', function(){
                this.a ++;
            });
        
            expect(obj.a).toBe(1);
            
            obj.emit('increase');
            expect(obj.a).toBe(3);
        });
        
        it("the execution of handlers should be maintained by registering sequence", function(){
            var obj = new EventClass();
            
            obj.on('increase', function(){
                this.a ++;
            });
            
            obj.on('increase', function(){
                this.c = this.a * this.b;
            });
            
            expect(obj.c).toBe(undefined);
            
            obj.emit('increase');
            expect(obj.c === 2).toBe(false);
            expect(obj.c).toBe(4);
        });
    });

    describe(".removeListener(type, fn), feated with .emit()", function(){
    
        it("should return the instance", function(){
            var handler = function(){},
                obj = new EventClass().on('increase', handler);
        
            expect(obj.removeListener('increase', handler)).toBe(obj);
            expect(obj.removeAllListeners('increase')).toBe(obj);
            expect(obj.removeAllListeners()).toBe(obj);
        });
        
        it("could remove a specific handler of a certain type: .removeListener(type, fn)", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                obj = new EventClass().on('increase', handler);
            
            obj.emit('increase');
            expect(flag).toBe(2);
            
            obj.removeListener('increase', handler);
            obj.emit('increase');
            expect(flag).toBe(2);
        });
        
        it("could remove all handlers of a certain type: .removeListener(type)", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                handler2 = function(){
                    flag = flag + 2;  
                },
                obj = new EventClass().on('increase', handler).on('increase', handler2);
            
            obj.removeAllListeners('increase');
            obj.emit('increase');
            expect(flag).toBe(1);
        });
        
        it("could remove all registered events: .removeListener()", function(){
            var flag = 1,
                handler = function(){
                    flag ++;  
                },
                handler2 = function(){
                    flag = flag + 2;  
                },
                obj = new EventClass().on('increase1', handler).on('increase2', handler2);
            
            obj.removeAllListeners();
            obj.emit('increase1');
            obj.emit('increase2');
            expect(flag).toBe(1);
        });
    });
    
    
    describe(".emit()", function(){
        
{'fraction_non_alphanumeric': 0.14245014245014245, 'fraction_numerical': 0.007903685323040161, 'mean_word_length': 1.1429696730996455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5124898', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2656, 'n_words': 576}