moves_test.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Test the chess moves
  2. require 'chess-data'
  3. require 'minitest/autorun'
  4. describe ChessData::Moves do
  5. it "correctly packages move into type" do
  6. [["O-O", ChessData::Moves::KingsideCastles],
  7. ["O-O-O", ChessData::Moves::QueensideCastles],
  8. ["e4", ChessData::Moves::SimplePawnMove],
  9. ["e8=Q", ChessData::Moves::PromotionPawnMove],
  10. ["Qe8", ChessData::Moves::PieceMove],
  11. ["Bxf7+", ChessData::Moves::PieceMove],
  12. ["exd5", ChessData::Moves::PawnCapture],
  13. ["exd8=B", ChessData::Moves::PromotionPawnCapture]
  14. ].each do |defn|
  15. _(ChessData::Moves.new_move(defn[0])).must_be_kind_of defn[1]
  16. end
  17. end
  18. it "notices invalid move definitions" do
  19. assert_raises(ChessData::InvalidMoveError) { ChessData::Moves.new_move("rnd") }
  20. end
  21. it "check if pieces can reach squares correctly" do
  22. [ # rook moves
  23. ["R", "e4", "e8", true, "8/8/8/8/4R3/8/8/8"],
  24. ["R", "e4", "d8", false, "8/8/8/8/4R3/8/8/8"],
  25. ["r", "e4", "c4", true, "8/8/8/8/4r3/8/8/8"],
  26. # bishop moves
  27. ["B", "d4", "h8", true, "8/8/8/8/3R4/8/8/8"],
  28. ["b", "g8", "a2", true, "6b1/8/8/8/8/8/8/8"],
  29. ["B", "d4", "d8", false, "8/8/8/8/3B4/8/8/8"],
  30. ["b", "b4", "e1", false, "8/8/8/8/1b6/2N5/8/4K3"],
  31. # queen moves
  32. ["Q", "e4", "e8", true, "8/8/8/8/4Q3/8/8/8"],
  33. ["Q", "e4", "d8", false, "8/8/8/8/4Q3/8/8/8"],
  34. ["Q", "e4", "h1", true, "8/8/8/8/4Q3/8/8/8"],
  35. ["Q", "e4", "a1", false, "8/8/8/8/4Q3/8/8/8"],
  36. # king moves
  37. ["K", "e1", "f1", true, "8/8/8/8/8/8/8/4K3"],
  38. ["K", "e1", "f2", true, "8/8/8/8/8/8/8/4K3"],
  39. ["K", "e1", "f3", false, "8/8/8/8/8/8/8/4K3"],
  40. ["K", "e1", "g1", false, "8/8/8/8/8/8/8/4K3"],
  41. ["K", "e4", "d3", true, "8/8/8/8/4R3/8/8/8"],
  42. # knight moves
  43. ["N", "g1", "f3", true, "8/8/8/8/8/8/8/6N1"],
  44. ["N", "g1", "g3", false, "8/8/8/8/8/8/8/6N1"],
  45. ["n", "e5", "d7", true, "8/8/8/4n3/8/8/8/8"]
  46. ].each do |defn|
  47. if defn[3]
  48. assert ChessData::Moves.send(:can_reach, ChessData::Board.from_fen(defn[4]), defn[0], defn[1], defn[2]),
  49. "test can reach #{defn.join(",")}"
  50. else
  51. assert !ChessData::Moves.send(:can_reach, ChessData::Board.from_fen(defn[4]), defn[0], defn[1], defn[2]),
  52. "test cannot reach #{defn.join(",")}"
  53. end
  54. end
  55. end
  56. end