var tape = require('tape');
var ginga = require('../');

function Clock(){
  this._tick = 'tick';
  this._tock = 'tock';
}

function base(ctx, next){
  ctx.logs = ['clock'];
  next();
}
function tick(ctx, next){
  ctx.logs.push(this._tick);
  next();
}
function tock(ctx, next){
  ctx.logs.push(this._tock);
  next();
}
function end(ctx, done){
  ctx.logs.push('done');
  done(null, ctx.logs);
}
ginga(Clock.prototype)
  .define('tick', base, tick, end)
  .define('tock', base, tick, tock, end);


var plugin = ginga()
  .use(
    'tick',
    function(ctx, next){
      ctx.logs.push('more');
      next();
    },
    function(ctx, next){
      ctx.logs.push('and more tick');
      next();
    }
  )
  .use('tock',function(ctx, next){
    next('booooom');
  })
  .define('doom', function(ctx, next){
    next('dooooom');
  });


tape('ginga plugin', function (t) {
  t.plan(10);

  var clock = new Clock();

  clock.tick(function(err,res){
    t.notOk(err, 'no error');
    t.deepEqual(res,['clock','tick','done']);
  });
  clock.tock(function(err,res){
    t.notOk(err, 'no error');
    t.deepEqual(res,['clock','tick','tock','done']);
  });

  clock.use(plugin);

  clock.tick(function(err,res){
    t.notOk(err, 'no error');
    t.deepEqual(res,['clock','tick','more','and more tick','done']);
  });
  clock.tock(function(err,res){
    t.notOk(res, 'no result');
    t.equal(err,'booooom', 'return error');
  });
  clock.doom(function(err, res){
    t.notOk(res, 'no result');
    t.equal(err,'dooooom', 'return error');
  });
});

@@ -754,73 +754,8 @@
 ');%0A
-  %7D)%0A  .define('doom', function(ctx, next)%7B%0A    next('dooooom');%0A
   %7D)
@@ -1349,122 +1349,8 @@
 %7D);%0A
-  clock.doom(function(err, res)%7B%0A    t.notOk(res, 'no result');%0A    t.equal(err,'dooooom', 'return error');%0A  %7D);%0A
 %7D);%0A

fix test case