/**
 * @fileOverview Promise Interface tests
 */
var sinon  = require('sinon');
var assert = require('chai').assert;
var Promise = require('bluebird');

var midd = require('../');

var noop = function(){};

suite('7. Promise Interface', function() {
  var thing;

  teardown(function() {});


  // The numbering (e.g. 1.1.1) has nothing to do with order
  // The purpose is to provide a unique string so specific tests are
  // run by using the mocha --grep "1.1.1" option.


  function applyTests(num, middMethod, thing) {
    setup(function() {
      var middOpts = {};

      if (middMethod !== 'use') {
        middOpts = {beforeAfter: true};
      }
      thing = Object.create(null);
      midd.make(thing, 'create', function() {
        return Promise.resolve();
      }, middOpts);

    });

    test('7.' + num + '.1 accepts a promise', function(done) {
      thing.create[middMethod](function() {
        return new Promise(function(resolve) {
          resolve();
        });
      });
      thing.create().then(done, done);
    });
    test('7.' + num + '.2 propagates error', function(done) {
      thing.create[middMethod](function() {
        return new Promise(function(resolve, reject) {
          reject();
        });
      });
      thing.create().then(noop, done.bind(null, null));

    });
    test('7.' + num + '.3 propagates error message', function(done) {
      thing.create[middMethod](function() {
        return new Promise(function(resolve, reject) {
          reject('Error');
        });
      });
      thing.create().then(null, function(err) {
        assert.equal(err, 'Error');
        done();
      }).then(null, done);
    });
    test('7.' + num + '.4 arguments propagate', function(done) {
      thing.create[middMethod](function(arg1) {
        return new Promise(function(resolve) {
          assert.equal(arg1, 1);
          resolve();
        });
      });
      thing.create[middMethod](function(arg1) {
        return new Promise(function(resolve) {
          assert.equal(arg1, 1);
          resolve();
        });
      });

      thing.create(1).then(done, done);
    });
  }

  suite('7.1 Middleware with use()', function() {
    applyTests(1, 'use', thing);
  });
  suite('7.2 Middleware with before()', function() {
    var thing = Object.create(null);
    midd.make(thing, 'create', function() {
      return Promise.resolve();
    }, {beforeAfter: true});
    applyTests(2, 'before', thing);
  });
  suite('7.3 Middleware with after()', function() {
    var thing = Object.create(null);
    midd.make(thing, 'create', function() {
      return Promise.resolve();
    }, {beforeAfter: true});
    applyTests(3, 'after', thing);
  });
  suite('7.8 Middleware with use() check', function() {
    var thing = Object.create(null);
    midd.make(thing, 'create', function() {
      return Promise.resolve();
    });

    test('7.8.3 propagates error message', function(done) {
      thing.create.use(function() {
        return new Promise(function(resolve, reject) {
          reject('Error');
        });
      });
      thing.create().then(null, function(err) {
        assert.equal(err, 'Error');
        done();
      }).then(null, done);
    });
  });
});

@@ -525,51 +525,49 @@
     
-setup(function() %7B%0A      var middOpts = %7B%7D;
+var middOpts = %7B%7D;%0A    setup(function() %7B
 %0A%0A  
@@ -2107,24 +2107,1309 @@
 ne);%0A    %7D);
+%0A%0A    test('7.' + num + '.5 async resolution', function(done) %7B%0A      var invoked = false;%0A      thing.create%5BmiddMethod%5D(function() %7B%0A        return new Promise(function(resolve) %7B%0A          setTimeout(function()%7B%0A            invoked = true;%0A            resolve();%0A          %7D);%0A        %7D);%0A      %7D);%0A      thing.create().then(function()%7B%0A        assert.ok(invoked);%0A      %7D).then(done, done);%0A    %7D);%0A    test('7.' + num + '.6 async rejection', function(done) %7B%0A      var invoked = false;%0A      thing.create%5BmiddMethod%5D(function() %7B%0A        return new Promise(function(resolve, reject) %7B%0A          setTimeout(function()%7B%0A            invoked = true;%0A            reject();%0A          %7D);%0A        %7D);%0A      %7D);%0A      thing.create().then(null, function() %7B%0A        assert.ok(invoked);%0A      %7D).then(done, done);%0A    %7D);%0A%0A    test('7.' + num + '.7 async resolution for final callback', function(done) %7B%0A      var invoked = false;%0A      thing = Object.create(null);%0A      midd.make(thing, 'create', function() %7B%0A        return new Promise(function(resolve) %7B%0A          setTimeout(function()%7B%0A            invoked = true;%0A            resolve();%0A          %7D);%0A        %7D);%0A      %7D, middOpts);%0A      thing.create().then(function() %7B%0A        assert.ok(invoked);%0A      %7D).then(done, done);%0A    %7D);
 %0A  %7D%0A%0A  suit
@@ -4468,24 +4468,24 @@
  done);%0A
-
     %7D);%0A
   %7D);%0A%7D)
@@ -4476,16 +4476,17 @@
     %7D);%0A
+%0A
   %7D);%0A%7D)

write async resolution / rejection tests