board_test.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require 'chess-data'
  2. require 'minitest/autorun'
  3. describe ChessData::Board do
  4. it "must set up an empty board on new" do
  5. board = ChessData::Board.new
  6. 8.times do |i|
  7. 8.times do |j|
  8. square = (65+i).chr + (49+j).chr
  9. _(board[square]).must_be_nil
  10. end
  11. end
  12. end
  13. it "must raise an ArgumentError on invalid squares" do
  14. board = ChessData::Board.new
  15. _(proc {board["9"]}).must_raise ArgumentError
  16. _(proc {board["E9"]}).must_raise ArgumentError
  17. end
  18. it "must raise ArgumentError on invalid FEN" do
  19. _(proc {ChessData::Board.from_fen("1 2 3")}).must_raise ArgumentError
  20. end
  21. it "must set up valid FEN position correctly" do
  22. board = ChessData::Board.from_fen "3k4/7p/8/8/8/8/2P5/R7 b Kq - 5 46"
  23. _(board[:A1]).must_equal "R"
  24. _(board[:C2]).must_equal "P"
  25. _(board[:H7]).must_equal "p"
  26. _(board[:D8]).must_equal "k"
  27. _(board.to_move).must_equal "b"
  28. _(board.white_king_side_castling).must_equal true
  29. _(board.white_queen_side_castling).must_equal false
  30. _(board.black_king_side_castling).must_equal false
  31. _(board.black_queen_side_castling).must_equal true
  32. _(board.enpassant_target).must_equal "-"
  33. _(board.halfmove_clock).must_equal 5
  34. _(board.fullmove_number).must_equal 46
  35. end
  36. it "must clone a board correctly" do
  37. board = ChessData::Board.from_fen "3k4/7p/8/8/8/8/2P5/R7 b Kq - 5 46"
  38. clone = board.clone
  39. _(clone[:A1]).must_equal "R"
  40. _(clone[:C2]).must_equal "P"
  41. _(clone[:H7]).must_equal "p"
  42. _(clone[:D8]).must_equal "k"
  43. _(clone.to_move).must_equal "b"
  44. _(clone.white_king_side_castling).must_equal true
  45. _(clone.white_queen_side_castling).must_equal false
  46. _(clone.black_king_side_castling).must_equal false
  47. _(clone.black_queen_side_castling).must_equal true
  48. _(clone.enpassant_target).must_equal "-"
  49. _(clone.halfmove_clock).must_equal 5
  50. _(clone.fullmove_number).must_equal 46
  51. end
  52. it "must create valid start position" do
  53. board = ChessData::Board.start_position
  54. _(board[:A1]).must_equal "R"
  55. _(board[:E8]).must_equal "k"
  56. _(board[:E2]).must_equal "P"
  57. _(board[:A8]).must_equal "r"
  58. _(board[:E4]).must_be_nil
  59. end
  60. it "must see a clone as equal" do
  61. board = ChessData::Board.start_position
  62. _(board.clone).must_equal board
  63. end
  64. it "must see two boards with small changes as different: Q" do
  65. board = ChessData::Board.start_position
  66. board.white_queen_side_castling = false
  67. _(board).wont_equal ChessData::Board.start_position
  68. end
  69. it "must see two boards with small changes as different: half-move" do
  70. board = ChessData::Board.start_position
  71. board.halfmove_clock = 5
  72. _(board).wont_equal ChessData::Board.start_position
  73. end
  74. it "must see two boards with small changes as different: Pe4" do
  75. board = ChessData::Board.start_position
  76. board[:e4] = "P"
  77. _(board).wont_equal ChessData::Board.start_position
  78. end
  79. it "must retrieve locations of pawns in start position" do
  80. board = ChessData::Board.start_position
  81. _(board.locations_of("P").length).must_equal 8
  82. _(board.locations_of("P")).must_include "A2"
  83. _(board.locations_of("P")).must_include "B2"
  84. _(board.locations_of("P")).must_include "C2"
  85. _(board.locations_of("P")).must_include "D2"
  86. _(board.locations_of("P")).must_include "E2"
  87. _(board.locations_of("P")).must_include "F2"
  88. _(board.locations_of("P")).must_include "G2"
  89. _(board.locations_of("P")).must_include "H2"
  90. end
  91. it "must retrieve locations of knights in start position" do
  92. board = ChessData::Board.start_position
  93. _(board.locations_of("n").length).must_equal 2
  94. _(board.locations_of("n")).must_include "B8"
  95. _(board.locations_of("n")).must_include "G8"
  96. end
  97. it "must retrieve locations of knights in start position filtered by rank" do
  98. board = ChessData::Board.start_position
  99. _(board.locations_of("n", "B").length).must_equal 1
  100. _(board.locations_of("n")).must_include "B8"
  101. end
  102. it "must count pieces on the board" do
  103. board = ChessData::Board.start_position
  104. _(board.count("K")).must_equal 1
  105. _(board.count("n")).must_equal 2
  106. _(board.count("P")).must_equal 8
  107. _(board.count("O")).must_equal 0
  108. end
  109. end