game_test.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require 'chess-data'
  2. require 'minitest/autorun'
  3. describe ChessData::Game do
  4. it "correctly reads a test file" do
  5. File.open "test/data/fischer.pgn" do |file|
  6. game = ChessData::Game.from_pgn file
  7. _(game.event).must_equal "F/S Return Match"
  8. _(game.site).must_equal "Belgrade, Serbia JUG"
  9. _(game.date).must_equal "1992.11.04"
  10. _(game.round).must_equal "29"
  11. _(game.white).must_equal "Fischer, Robert J."
  12. _(game.black).must_equal "Spassky, Boris V."
  13. _(game.half_moves).must_equal 85
  14. _(game.start_position).must_equal ChessData::Board.start_position
  15. end
  16. end
  17. it "correctly uses a given fen as start position" do
  18. File.open "test/data/games4.pgn" do |file|
  19. game = ChessData::Game.from_pgn file
  20. _(game.start_position).must_equal \
  21. ChessData::Board.from_fen "r3k2r/1bqn1p1p/p1n1pb1Q/1p6/3N1P2/2N1B3/PPP1B1PP/3R1R1K b kq - 0 18"
  22. end
  23. end
  24. it "adds moves either of a move-class, or as a string" do
  25. game = ChessData::Game.new
  26. game << ChessData::Moves.new_move("e4")
  27. game << "e6"
  28. _(game.half_moves).must_equal 2
  29. _(game.moves[0].class).must_equal ChessData::Moves::SimplePawnMove
  30. _(game.moves[0].to_s).must_equal "e4"
  31. _(game.moves[1].class).must_equal ChessData::Moves::SimplePawnMove
  32. _(game.moves[1].to_s).must_equal "e6"
  33. assert_raises(ChessData::InvalidMoveError) { game << "rnd" }
  34. assert_raises(ChessData::InvalidMoveError) { game << ChessData::Board.start_position }
  35. end
  36. def check_move start, move, final
  37. board = ChessData::Moves.new_move(move).make_move ChessData::Board.from_fen start
  38. _(board).must_equal ChessData::Board.from_fen(final), "testing move #{move}"
  39. end
  40. it "manages moves" do
  41. [ # castling moves
  42. ["8/8/8/8/8/8/8/4K2R w K - 0 0", "O-O", "8/8/8/8/8/8/8/5RK1 b - - 1 0"],
  43. ["4k2r/8/8/8/8/8/8/8 b k - 0 0", "O-O", "5rk1/8/8/8/8/8/8/8 w - - 1 1"],
  44. ["8/8/8/8/8/8/8/R3K2R w KQ - 0 0", "O-O-O", "8/8/8/8/8/8/8/2KR3R b - - 1 0"],
  45. ["r3k2r/8/8/8/8/8/8/8 b kq - 0 0", "O-O-O", "2kr3r/8/8/8/8/8/8/8 w - - 1 1"],
  46. # simple pawn moves
  47. ["rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "e4",
  48. "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"],
  49. ["rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "a3",
  50. "rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR b KQkq - 0 1"],
  51. ["8/8/4P3/8/8/8/8/8 w - - 10 23", "e7", "8/4P3/8/8/8/8/8/8 b - - 0 23"],
  52. ["rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", "d5",
  53. "rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2"],
  54. ["rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", "g6",
  55. "rnbqkbnr/pppppp1p/6p1/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2"],
  56. ["8/8/8/8/8/2p5/8/8 b - - 10 23", "c2", "8/8/8/8/8/8/2p5/8 w - - 0 24"],
  57. # pawn promotions
  58. ["8/4P3/8/8/8/8/8/8 w - - 10 23", "e8=Q", "4Q3/8/8/8/8/8/8/8 b - - 0 23"],
  59. ["8/4P3/8/8/8/8/8/8 w - - 10 23", "e8=N", "4N3/8/8/8/8/8/8/8 b - - 0 23"],
  60. ["8/8/8/8/8/8/2p5/8 b - - 10 23", "c1=B", "8/8/8/8/8/8/8/2b5 w - - 0 24"],
  61. ["7k/4P3/8/8/8/8/8/8 w - - 10 23", "e8=Q+", "4Q2k/8/8/8/8/8/8/8 b - - 0 23"],
  62. # piece moves
  63. ["8/8/8/8/4Q3/8/8/8 w - - 10 23", "Qe8", "4Q3/8/8/8/8/8/8/8 b - - 11 23"],
  64. ["4q3/8/8/8/4Q3/8/8/8 w - - 10 23", "Qxe8", "4Q3/8/8/8/8/8/8/8 b - - 0 23"],
  65. ["8/8/8/8/4r3/8/8/8 b - - 10 23", "Rh4", "8/8/8/8/7r/8/8/8 w - - 11 24"],
  66. ["8/8/8/8/Pr3/8/8/8 b - - 10 23", "Rxa4", "8/8/8/8/r7/8/8/8 w - - 0 24"],
  67. ["8/8/5p2/8/4N3/8/8/8 w - - 10 23", "Nxf6", "8/8/5N2/8/8/8/8/8 b - - 0 23"],
  68. ["8/8/5p2/8/4k3/8/8/8 b - - 10 23", "Kd5", "8/8/5p2/3k4/8/8/8/8 w - - 11 24"],
  69. ["8/8/8/8/1N3N2/8/8/8 w - - 10 23", "Nbd5", "8/8/8/3N4/5N2/8/8/8 b - - 11 23"],
  70. ["8/8/8/8/1N3N2/8/8/8 w - - 10 23", "Nfd5", "8/8/8/3N4/1N6/8/8/8 b - - 11 23"],
  71. ["8/8/8/8/1R6/1p6/8/1R6 w - - 10 23", "R1xb3", "8/8/8/8/1R6/1R6/8/8 b - - 0 23"],
  72. ["8/8/8/8/1R6/1p6/8/1R6 w - - 10 23", "R4xb3", "8/8/8/8/8/1R6/8/1R6 b - - 0 23"],
  73. ["r1bqk2r/1pppbppp/p1n2n2/4p3/B3P3/5N2/PPPP1PPP/RNBQ1RK1 w - - 2 6", "Re1",
  74. "r1bqk2r/1pppbppp/p1n2n2/4p3/B3P3/5N2/PPPP1PPP/RNBQR1K1 b - - 3 6"],
  75. ["Q7/8/2p5/8/8/8/8/7Q w - - 10 23", "Qf3", "Q7/8/2p5/8/8/5Q2/8/8 b - - 11 23"],
  76. ["r2q1rk1/1b1nbppp/2pp1n2/1p2p3/3PP3/1BN2N1P/PP3PP1/R1BQR1K1 w - - 0 14",
  77. "Bg5",
  78. "r2q1rk1/1b1nbppp/2pp1n2/1p2p1B1/3PP3/1BN2N1P/PP3PP1/R2QR1K1 b - - 1 14"],
  79. ["r2q1rk1/1b1nbpp1/3p3p/2p1P3/1p2n2B/1B3N1P/PP3PP1/RN1QR1K1 w - - 0 18",
  80. "Bxe7",
  81. "r2q1rk1/1b1nBpp1/3p3p/2p1P3/1p2n3/1B3N1P/PP3PP1/RN1QR1K1 b - - 0 18"],
  82. ["8/8/8/8/1b6/2N5/8/4K1N1 w - - 0 10",
  83. "Ne2",
  84. "8/8/8/8/1b6/2N5/4N3/4K3 b - - 1 10"],
  85. # Pawn captures
  86. ["8/8/8/3p4/4P3/8/8/8 w - - 10 23", "exd5", "8/8/8/3P4/8/8/8/8 b - - 0 23"],
  87. ["8/8/8/3p4/4P3/8/8/8 b - - 10 23", "dxe4", "8/8/8/8/4p3/8/8/8 w - - 0 24"],
  88. ["8/8/8/8/3pP3/8/8/8 b - e3 10 23", "dxe3", "8/8/8/8/8/4p3/8/8 w - - 0 24"],
  89. ["8/8/8/3pP3/8/8/8/8 w - d6 10 23", "exd6", "8/8/3P4/8/8/8/8/8 b - - 0 23"],
  90. ["3n4/4P3/8/8/8/8/8/8 w - - 10 23", "exd8=B", "3B4/8/8/8/8/8/8/8 b - - 0 23"]
  91. ].each do |defn|
  92. check_move defn[0], defn[1], defn[2]
  93. end
  94. end
  95. end