<commit_before>var co = require('co');
var compose = require('./');

describe('Koa Compose', function(){
  it('should work', function(done){
    var arr = [];
    var stack = [];

    stack.push(function *(next){
      arr.push(1);
      yield next();
      arr.push(6);
    })

    stack.push(function *(next){
      arr.push(2);
      yield next();
      arr.push(5);
    })

    stack.push(function *(next){
      arr.push(3);
      yield next();
      arr.push(4);
    })

    co(compose(stack))(function(err){
      if (err) throw err;

      arr.should.eql([1, 2, 3, 4, 5, 6]);
      done();
    })
  })

  it('should work with 0 middleware', function(done){
    co(compose([]))(done);
  })

  it('should work within a generator', function(done){
    var arr = [];

    co(function *(){
      arr.push(0);

      var stack = [];

      stack.push(function* (next){
        arr.push(1);
        yield next();
        arr.push(4);
      });

      stack.push(function *(next){
        arr.push(2);
        yield next();
        arr.push(3);
      });

      yield compose(stack)

      arr.push(5);
    })(function(err){
      if (err) throw err;

      arr.should.eql([0, 1, 2, 3, 4, 5]);
      done();
    })
  })

  it('should work when yielding at the end of the stack', function(done) {
    var stack = [];

    stack.push(function *(next){
      yield next();
    });

    co(compose(stack))(done);
  })

  it('should keep the context', function(done){
    var ctx = {};

    var stack = [];

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    co.call(ctx, compose(stack))(done);
  })

  it('should throw when `next()` out of the stack', function(done){
    var stack = [];

    stack.push(function *(next){
      yield next();
      yield next();
    })

    co(compose(stack))(function(err){
      err.should.be.ok;
      done();
    })
  })
})<commit_msg>some more tests<commit_after>var co = require('co');
var compose = require('./');

function wait(ms) {
  return function (done) {
    setTimeout(done, ms || 0);
  }
}

describe('Koa Compose', function(){
  it('should work', function(done){
    var arr = [];
    var stack = [];

    stack.push(function *(next){
      arr.push(1);
      wait(1);
      yield next();
      wait(1);
      arr.push(6);
    })

    stack.push(function *(next){
      arr.push(2);
      wait(1);
      yield next();
      wait(1);
      arr.push(5);
    })

    stack.push(function *(next){
      arr.push(3);
      wait(1);
      yield next();
      wait(1);
      arr.push(4);
    })

    co(compose(stack))(function(err){
      if (err) throw err;

      arr.should.eql([1, 2, 3, 4, 5, 6]);
      done();
    })
  })

  it('should work with 0 middleware', function(done){
    co(compose([]))(done);
  })

  it('should work within a generator', function(done){
    var arr = [];

    co(function *(){
      arr.push(0);

      var stack = [];

      stack.push(function* (next){
        arr.push(1);
        yield next();
        arr.push(4);
      });

      stack.push(function *(next){
        arr.push(2);
        yield next();
        arr.push(3);
      });

      yield compose(stack)

      arr.push(5);
    })(function(err){
      if (err) throw err;

      arr.should.eql([0, 1, 2, 3, 4, 5]);
      done();
    })
  })

  it('should work when yielding at the end of the stack', function(done) {
    var stack = [];

    stack.push(function *(next){
      yield next();
    });

    co(compose(stack))(done);
  })

  it('should keep the context', function(done){
    var ctx = {};

    var stack = [];

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    stack.push(function *(next){
      this.should.equal(ctx);
    })

    co.call(ctx, compose(stack))(done);
  })

  it('should throw when `next()` out of the stack', function(done){
    var stack = [];

    stack.push(function *(next){
      yield next();
      yield next();
    })

    co(compose(stack))(function(err){
      err.should.be.ok;
      done();
    })
  })

  it('should catch downstream errors', function(done){
    var arr = [];
    var stack = [];

    stack.push(function *(next){
      arr.push(1);
      try {
        yield next();
      } catch (err) {
        arr.push(2);
      }
      arr.push(3);
    })

    stack.push(function *(next){
      arr.push(4);
      throw new Error();
      arr.push(5);
    })

    co(compose(stack))(function(err){
      if (err) throw err;

      arr.should.eql([1, 4, 2, 3]);
      done();
    })
  })
})
{'fraction_non_alphanumeric': 0.19028511087645195, 'fraction_numerical': 0.013938753959873283, 'mean_word_length': 2.085342019543974, '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': '2151998', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1573, 'n_words': 352}