<commit_before>/// <reference path="../src/future.ts" />
/// <reference path="../d.ts/mocha.d.ts" />
/// <reference path="../d.ts/chai.d.ts" />

module Katana.Spec {

    chai.should();

    describe('Future', () => {
        it('should callback onComplete with Success if success', (ok) => {
            new Katana.Future<number>((success, failure) => {
                success(1);
            }).onComplete(r => {
                r.should.instanceof(Katana.Success);
                ok();
            });
        });

        it('should callback onComplete with Success if success on other context', (ok) => {
            new Katana.Future<number>((success, failure) => {
                setTimeout(() => success(1), 100);
            }).onComplete(r => {
                r.should.instanceof(Katana.Success);
                ok();
            });
        });

        it('should callback onComplete with Failure if failure', (ok) => {
            new Katana.Future<number>((success, failure) => {
                failure(new Error());
            }).onComplete(r => {
                r.should.instanceof(Katana.Failure);
                ok();
            });
        });

        it('should callback onComplete with Failure if failure on other context', (ok) => {
            new Katana.Future<number>((success, failure) => {
                setTimeout(() => failure(new Error()), 100);
            }).onComplete(r => {
                r.should.instanceof(Katana.Failure);
                ok();
            });
        });

        describe('When succeed', () => {
            var future: Katana.Future<string>;
            before(() => {
                future = new Katana.Future<string>((success, failure) => {
                    success('value');
                });
            });

            describe('#onSuccess', () => {
                it('should callback with value', (ok) => {
                    var value: string;
                    future.onSuccess(v => {
                        v.should.equal('value');
                        ok();
                    });
                });
            });

            describe('#onFailure', () => {
                it('should not callback', () => {
                    var called = false;
                    future.onFailure(e => called = true);
                    called.should.be.false;
                });
            });

            describe('#filter', () => {
                it('promises success and pass to next if matcher function returns true', (ok) => {
                    future.filter(v => true).onSuccess(v => {
                        v.should.equal('value');
                        ok();    
                    });
                });

                it('promises failure if matcher function retuns false', (ok) => {
                    future.filter(v => false).onFailure(e => {
                        e.message.should.equal('No such element.');
                        ok();
                    });
                });
            });

            describe('#reject', () => {
                it('promises failure if matcher function returns true', (ok) => {
                    future.reject(v => true).onFailure(e => {
                        e.message.should.equal('No such element.');
                        ok();
                    });
                });

                it('promises success and pass to next if matcher function retuns false', (ok) => {
                    future.reject(v => false).onSuccess(v => {
                        v.should.equal('value');
                        ok();    
                    });
                });
            });
        });

        describe('When failed', () => {
            var future: Katana.Future<string>;
            before(() => {
                future = new Katana.Future<string>((success, failure) => {
                    failure(new Error('Some error.'));
                });
            });

            describe('#onSuccess', () => {
                it('should not callback', () => {
                    var called = false;
                    future.onSuccess(v => called = true);
                    called.should.be.false;
                });
            });

            describe('#onFailure', () => {
                it('should callback with Error', (ok) => {
                    var error: Error;
                    future.onFailure(e => { 
                        e.message.should.equal('Some error.');
                        ok();
                    });
                });
            });

            describe('#map', () => {
                it('never do anything', (ok) => {
                    var a = future.map<number>((v, success, failure) => {
                        success(100);
                    }).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok();    
                    });
                });
            });

            describe('#flatMap', () => {
                it('never do anything', (ok) => {
                    future.flatMap(v => new Katana.Future((success, failure) => success(100))).onFailure(e => {
                        e.message.should.equal('Some error.');   
                        ok();
                    });
                });
            });

            describe('#filter', () => {
                it('never do anything', (ok) => {
                    future.filter(v => true).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok()
                    });
                });
            });

            describe('#reject', () => {
                it('never do anything', (ok) => {
                    future.reject(v => false).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok()
                    });
                });
            });
        });
    });

    describe('Promise', () => {<commit_msg>Add Future#map test<commit_after>/// <reference path="../src/future.ts" />
/// <reference path="../d.ts/mocha.d.ts" />
/// <reference path="../d.ts/chai.d.ts" />

module Katana.Spec {

    chai.should();

    describe('Future', () => {
        it('should callback onComplete with Success if success', (ok) => {
            new Katana.Future<number>((success, failure) => {
                success(1);
            }).onComplete(r => {
                r.should.instanceof(Katana.Success);
                ok();
            });
        });

        it('should callback onComplete with Success if success on other context', (ok) => {
            new Katana.Future<number>((success, failure) => {
                setTimeout(() => success(1), 100);
            }).onComplete(r => {
                r.should.instanceof(Katana.Success);
                ok();
            });
        });

        it('should callback onComplete with Failure if failure', (ok) => {
            new Katana.Future<number>((success, failure) => {
                failure(new Error());
            }).onComplete(r => {
                r.should.instanceof(Katana.Failure);
                ok();
            });
        });

        it('should callback onComplete with Failure if failure on other context', (ok) => {
            new Katana.Future<number>((success, failure) => {
                setTimeout(() => failure(new Error()), 100);
            }).onComplete(r => {
                r.should.instanceof(Katana.Failure);
                ok();
            });
        });

        describe('When succeed', () => {
            var future: Katana.Future<string>;
            before(() => {
                future = new Katana.Future<string>((success, failure) => {
                    success('value');
                });
            });

            describe('#onSuccess', () => {
                it('should callback with value', (ok) => {
                    var value: string;
                    future.onSuccess(v => {
                        v.should.equal('value');
                        ok();
                    });
                });
            });

            describe('#onFailure', () => {
                it('should not callback', () => {
                    var called = false;
                    future.onFailure(e => called = true);
                    called.should.be.false;
                });
            });

            describe('#map', () => {
                it('returns mixed Future that create by func', (ok) => {
                    future.map<number>((v, success, failure) => {
                        success(100);
                    }).onSuccess((v: number) => {
                        v.should.equal(100);
                        ok();    
                    });
                });
            });
/*
            describe('#flatMap', () => {
                it('never do anything', (ok) => {
                    var a = future.flatMap(v => new Katana.Future<number>((success, failure) => success(100))).onFailure(e => {
                        e.message.should.equal('Some error.');   
                        ok();
                    });
                });
            });
*/

            describe('#filter', () => {
                it('promises success and pass to next if matcher function returns true', (ok) => {
                    future.filter(v => true).onSuccess(v => {
                        v.should.equal('value');
                        ok();    
                    });
                });

                it('promises failure if matcher function retuns false', (ok) => {
                    future.filter(v => false).onFailure(e => {
                        e.message.should.equal('No such element.');
                        ok();
                    });
                });
            });

            describe('#reject', () => {
                it('promises failure if matcher function returns true', (ok) => {
                    future.reject(v => true).onFailure(e => {
                        e.message.should.equal('No such element.');
                        ok();
                    });
                });

                it('promises success and pass to next if matcher function retuns false', (ok) => {
                    future.reject(v => false).onSuccess(v => {
                        v.should.equal('value');
                        ok();    
                    });
                });
            });
        });

        describe('When failed', () => {
            var future: Katana.Future<string>;
            before(() => {
                future = new Katana.Future<string>((success, failure) => {
                    failure(new Error('Some error.'));
                });
            });

            describe('#onSuccess', () => {
                it('should not callback', () => {
                    var called = false;
                    future.onSuccess(v => called = true);
                    called.should.be.false;
                });
            });

            describe('#onFailure', () => {
                it('should callback with Error', (ok) => {
                    var error: Error;
                    future.onFailure(e => { 
                        e.message.should.equal('Some error.');
                        ok();
                    });
                });
            });

            describe('#map', () => {
                it('never do anything', (ok) => {
                    future.map<number>((v, success, failure) => {
                        success(100);
                    }).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok();    
                    });
                });
            });

            describe('#flatMap', () => {
                it('never do anything', (ok) => {
                    future.flatMap(v => new Katana.Future<number>((success, failure) => success(100))).onFailure(e => {
                        e.message.should.equal('Some error.');   
                        ok();
                    });
                });
            });

            describe('#filter', () => {
                it('never do anything', (ok) => {
                    future.filter(v => true).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok()
                    });
                });
            });

            describe('#reject', () => {
                it('never do anything', (ok) => {
                    future.reject(v => false).onFailure(e => {
                        e.message.should.equal('Some error.');
                        ok()
                    });
                });
            });
        });
    });

    describe('Promise', () => {
{'fraction_non_alphanumeric': 0.15435821129297517, 'fraction_numerical': 0.002897642728483045, 'mean_word_length': 1.0256979695431472, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 169, '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': '3572137', 'n_tokens_mistral': 3136, 'n_tokens_neox': 2870, 'n_words': 562}