describe('Future', function () {

  var Sync = require('../index');
  var Fiber = require('fibers');
  var sinon = require("sinon"), sandbox;

  beforeEach(function () {
    sandbox = sinon.sandbox.create();
  });

  afterEach(function () {
    sandbox.restore();
  });

  describe('resolve', function () {

    it('should set result and error', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      future.resolve(null, true);
      future.result.should.be.true;
      future.resolved.should.be.true;
      expect(future.error).to.be.null;
    }));

    it('should run the fiber with the result when fiber was yielded', Sync.wrap(function (done) {
      var fiber = { run: function (res) { res.should.be.true;}};
      var Future = require('../lib/Future'), future = new Future;
      future.fiber = fiber;
      future.yielded = true;
      future.resolve(null, true);
      future.resolved.should.be.true;
    }));

    it('should run the fiber with the error when fiber was yielded', Sync.wrap(function (done) {
      var fiber = { run: function (res) { res.should.be.an.instanceOf(Error);}};
      var Future = require('../lib/Future'), future = new Future;
      future.fiber = fiber;
      future.yielded = true;
      future.resolve(new Error);
      future.resolved.should.be.true;
    }));

    it('should not run the fiber when the future is already resolved', Sync.wrap(function () {
      var nextTick = process.nextTick;
      process.nextTick = function (fn) { fn() };
      var fiber = { run: function (res) { chai.assert.fail(true, false, 'Fiber should not have been run') }};
      var Future = require('../lib/Future'), future = new Future;
      future.fiber = fiber;
      future.yielded = true;
      future.resolved = true;
      future.resolve(null, true);
      process.nextTick = nextTick;
    }));

    it('should set error as an Error', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      future.resolve('Something went wrong');
      future.error.should.be.an.instanceOf(Error);
    }));

    it('should not leak the fiber', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      future.resolve(null, true);
      expect(future.fiber).to.be.null;
    }));
  });

  describe('wait', function () {

    it('should call produce to get the results', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      var mock = sandbox.mock(future);
      mock.expects('produce').once().returns(true);
      future.wait().should.be.true;
      mock.verify();
    }));

    it('should call produce and throws the error', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      var mock = sandbox.mock(future);
      mock.expects('produce').once().throws(new Error('Something went wrong...'));
      expect(future.wait.bind(future)).to.throw('Something went wrong...');
      mock.verify();
    }));

    it('should not call produce when the future is already resolved', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      var mock = sandbox.mock(future);
      mock.expects('produce').never();
      future.resolved = true;
      future.result = true;
      future.wait().should.be.true;
      mock.verify();
    }));

    it('should not call produce and should throw an error', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      var mock = sandbox.mock(future);
      mock.expects('produce').never();
      future.resolved = true;
      future.error = new Error('Something went wrong...');
      expect(future.wait.bind(future)).to.throw('Something went wrong...');
      mock.verify();
    }));

    it('should not leak the fiber', Sync.wrap(function () {
      var Future = require('../lib/Future'), future = new Future;
      sandbox.stub(future, 'produce').returns(true);
      future.wait();
      expect(future.fiber).to.be.null;
    }));
  });

  describe('produce', function () {

    it('should yield the fiber and returns the result', Sync.wrap(function () {
      var mock = sandbox.mock(Fiber);
      mock.expects('yield').once().returns(true);
      var Future = require('../lib/Future'), future = new Future;
      future.produce().should.be.true;
      mock.verify();
    }));

    it('should yield the fiber and throws the error', Sync.wrap(function () {
      var mock = sandbox.mock(Fiber);
      mock.expects('yield').once().returns(new Error('Something went wrong...'));
      var Future = require('../lib/Future'), future = new Future;
      expect(future.produce.bind(future)).to.throw('Something went wrong...');
      mock.verify();
    }));

    it('should yield the fiber and marks the future as yielded', Sync.wrap(function () {
      var mock = sandbox.mock(Fiber);
      mock.expects('yield').once().returns(true);
      var Future = require('../lib/Future'), future = new Future;
      future.produce().should.be.true;
      future.yielded.should.be.true;
      mock.verify();
    }));
  });

});


@@ -33,20 +33,44 @@
 %0A%0A  var 
-Sync
+sandbox, future;%0A  var sinon
  = requi
@@ -76,18 +76,15 @@
 ire(
-'../index'
+%22sinon%22
 );%0A 
@@ -113,37 +113,36 @@
 fibers');%0A  var 
-sinon
+Sync
  = require(%22sino
@@ -140,25 +140,60 @@
 ire(
-%22sinon%22), sandbox
+'../index');%0A  var Future = require('../lib/Future')
 ;%0A%0A 
@@ -252,24 +252,49 @@
 x.create();%0A
+    future = new Future;%0A
   %7D);%0A%0A  aft
@@ -447,74 +447,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -767,74 +767,8 @@
 %7D%7D;%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -1084,74 +1084,8 @@
 %7D%7D;%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -1515,74 +1515,8 @@
 %7D%7D;%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -1744,74 +1744,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -1911,74 +1911,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -2107,74 +2107,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -2340,74 +2340,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -2663,74 +2663,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -2950,74 +2950,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -3284,74 +3284,8 @@
 ) %7B%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -3952,74 +3952,8 @@
 ));%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     
@@ -4239,74 +4239,8 @@
 e);%0A
-      var Future = require('../lib/Future'), future = new Future;%0A
     

Refactor Future tests