var should = require("should");
var rules = require("../chessjs/rules");
var _ = require("underscore");

var hasMove = function(piece, parsed) {
    var rightSquare = function(option) {
        return option.x === parsed.x && option.y === parsed.y;
    }

    var allMoves = parsed.mover.validMoves();
    var moves = allMoves.moves.filter(rightSquare);
    var caps = allMoves.captures.filter(rightSquare);
    should.equal(moves.length + caps.length, 1,
                 piece.type + " at (" + piece.x + ", " + piece.y + ") " +
                 "cannot move to (" + parsed.x + ", " + parsed.y + ")");
}

var checkMoves = function(moves, done) {
    rules.withRules("base", function(Board, BaseRules) {
        var board = new Board();
        board.setup();
        _.each(moves, function(text, ix) {
            var parsed = board.parseMove(text);

            hasMove(parsed.mover, parsed);

            parsed.mover.moveTo(parsed.x, parsed.y, parsed.promo);
        });
        done();
    });
}

describe('Basic rules', function() {
    it('should follow the basic interface', function(done) {
        rules.withRules("base", function(Board, BaseRules) {
            should.exist(BaseRules.validMoves);
            should.exist(BaseRules.startingPieces);
            done();
        });
    });
    it('should allow pawn moves forward', function(done) {
        checkMoves(['a1-a2', 'a6-a5', 'b1-b3'], done);
    });
    it('should allow pawn captures', function(done) {
        checkMoves(['a1-a3', 'b6-b4', 'a3-b4'], done);
    });
    it('should allow en passant captures', function(done) {
        checkMoves(['a1-a3', 'h6-h4', 'a3-a4', 'b6-b4', 'a4-b5'], done);
    });
});

@@ -102,23 +102,25 @@
 );%0A%0Avar 
-has
+can
 Move
+To
  = funct
@@ -407,25 +407,20 @@
 e);%0A
+%0A
     
-should.equal(
+return 
 move
@@ -445,11 +445,102 @@
 ngth
-, 1
+ === 1;%0A%7D%0A%0Avar hasMove = function(piece, move) %7B%0A    should.equal(canMoveTo(piece, move), true
 ,%0A  
@@ -653,32 +653,258 @@
 %22 + 
-parsed.x + %22, %22 + parsed
+move.x + %22, %22 + move.y + %22)%22);%0A%7D%0A%0Avar noMoveTo = function(piece, move) %7B%0A    should.equal(canMoveTo(piece, move), false,%0A                 piece.type + %22 at (%22 + piece.x + %22, %22 + piece.y + %22) %22 +%0A                 %22can move to (%22 + move.x + %22, %22 + move
 .y +
@@ -946,20 +946,20 @@
 (moves, 
-done
+cont
 ) %7B%0A    
@@ -1278,24 +1278,249 @@
         %7D);%0A
+        cont(board);%0A    %7D);%0A%7D%0A%0Avar checkInvalid = function(moves, invalid, done) %7B%0A    checkMoves(moves, function(board) %7B%0A        var notAllowed = board.parseMove(invalid);%0A%0A        noMoveTo(notAllowed.mover, notAllowed);%0A%0A
         done
@@ -1936,28 +1936,45 @@
 , 'b1-b3'%5D, 
-done
+function() %7B done() %7D
 );%0A    %7D);%0A 
@@ -2070,28 +2070,45 @@
 , 'a3-b4'%5D, 
-done
+function() %7B done() %7D
 );%0A    %7D);%0A 
@@ -2231,16 +2231,207 @@
 a4-b5'%5D,
+%0A                   function() %7B done() %7D);%0A    %7D);%0A    it('should not allow invalid en passant captures', function(done) %7B%0A        checkInvalid(%5B'a1-a3', 'b6-b4', 'h1-h2', 'b4-b3'%5D, 'a3-b4',
  done);%0A

Check invalid en passant moves.