// Generated by CoffeeScript 1.3.3
var assert, echo, equal, line, timeout, vows;

assert = require('assert');

vows = require('vows');

line = require('line');

equal = assert.equal;

if (!vows.add) {
  vows.add = function(name, batch) {
    return vows.describe(name).addBatch(batch)["export"](module);
  };
}

timeout = function(delay, fn) {
  setTimeout(fn, delay);
};

echo = function(val, fn) {
  return process.nextTick(function() {
    return fn(null, val);
  });
};

vows.add('line', {
  'nested lines': {
    topic: function() {
      var l, results,
        _this = this;
      results = [];
      l = new line.Line(null, function() {
        var l2;
        timeout(10, l.wait(function() {
          return results.push(0);
        }));
        l2 = new line.Line(l, function() {
          return timeout(30, l2.wait(function() {
            return results.push(2);
          }));
        }, function() {
          return timeout(10, l2.wait(function() {
            return results.push(3);
          }));
        });
        return timeout(20, l.wait(function() {
          return results.push(1);
        }));
      }, function() {
        return timeout(10, l.wait(function() {
          return results.push(4);
        }));
      }, function() {
        return _this.success(results);
      });
    },
    'should have all the results in the right order': function(results) {
      equal(results[0], 0);
      equal(results[1], 1);
      equal(results[2], 2);
      equal(results[3], 3);
      return equal(results[4], 4);
    }
  },
  'arguments to wait()': {
    topic: function() {
      var success;
      success = this.success;
      new line.Line(null, function() {
        echo(1, this.wait('a'));
        echo(2, this.wait());
        echo(3, this.wait(true));
        echo(4, this.wait('b'));
        echo(5, this.wait('c'));
        return echo(6, this.wait());
      }, function(result) {
        return this.results['test'] = result;
      }, function() {
        return success(this.results);
      });
    },
    'should be added correctly': function(results) {
      equal(results['a'], 1);
      equal(results[2], 2);
      equal(results[3], 3);
      equal(results['b'], 4);
      equal(results['c'], 5);
      equal(results[4], 6);
      return equal(results['test'], 3);
    }
  },
  'stopping a line': {
    topic: function() {
      var results, success;
      success = this.success;
      results = [];
      new line.Line(null, function() {
        return timeout(10, this.wait(function() {
          return results.push(1);
        }));
      }, function() {
        return timeout(10, this.wait(function() {
          return results.push(2);
        }));
      }, function() {
        return this.stop();
      }, function() {
        return timeout(10, this.wait(function() {
          return results.push(3);
        }));
      }, function() {
        return success(results);
      });
    },
    'should prevent the rest of the blocks from running': function(results) {
      equal(results.length, 2);
      equal(results[0], 1);
      return equal(results[1], 2);
    }
  },
  'true/false callbacks': {
    topic: function() {
      var falseFn, results, success, trueFn;
      results = [];
      success = this.success;
      trueFn = function(cb) {
        return process.nextTick(function() {
          return cb(true);
        });
      };
      falseFn = function(cb) {
        return process.nextTick(function() {
          return cb(false);
        });
      };
      new line.Line(null, function() {
        return trueFn(this.wait(function(result) {
          return results.push(result);
        }));
      }, function() {
        return falseFn(this.wait(function(result) {
          return results.push(result);
        }));
      }, function() {
        return success(results);
      });
    },
    'should not trigger errors': function(results) {
      equal(results[0], true);
      return equal(results[1], false);
    }
  },
  'more nested lines': {
    topic: function() {
      var results, success;
      success = this.success;
      results = [];
      new line.Line(null, function() {
        results.push(0);
        new line.Line(this, function() {
          return echo(1, this.wait());
        }, function(one) {
          return echo(2, this.wait());
        }, function(two) {
          return echo(3, this.wait());
        }, function(three) {
          results.push(2);
          return three;
        });
        results.push(1);
      }, function(three) {
        return results.push(three);
      }, function() {
        return results.push(4);
      }, function() {
        return success(results);
      });
    },
    'should make their parents wait ': function(result) {
      equal(result[0], 0);
      equal(result[1], 1);
      equal(result[2], 2);
      return equal(result[4], 4);
    },
    'should be able to pass on results': function(result) {
      return equal(result[3], 3);
    }
  },
  'old api': {
    topic: function() {
      var results, success;
      success = this.success;
      results = [];
      line.add(function() {
        results.push(0);
        line.add(function() {
          return echo(1, this.wait());
        });
        line.add(function(one) {
          return echo(2, this.wait());
        });
        line.add(function(two) {
          return echo(3, this.wait());
        });
        line.run(function(three) {
          results.push(2);
          return three;
        });
        results.push(1);
      });
      line.add(function(three) {
        return results.push(three);
      });
      line.add(function() {
        return results.push(4);
      });
      line.run(function() {
        return success(results);
      });
    },
    'should get the same results ': function(result) {
      equal(result[0], 0);
      equal(result[1], 1);
      equal(result[2], 2);
      equal(result[3], 3);
      return equal(result[4], 4);
    }
  }
});
