Source

puzzle_nodes / 13 / test / board_test.rb

Full commit
require 'minitest/unit'
require 'minitest/autorun'
require './lib/board'

class BoardTest < MiniTest::Unit::TestCase
  def setup
    @input = <<BOARD
bR bN bB bQ bK bB bN bR
bP bP bP bP bP bP bP bP
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
wP wP wP wP wP wP wP wP
wR wN wB wQ wK wB wN wR
BOARD

  @board = Board.new @input
  end

  def test_board_reads_board_state_from_input
    assert_equal "bR", @board[0]
  end

  def test_algebraic_predicate
    assert @board.algebraic?(:a8), "Expected a8 to be algebraic, wasn't."
    assert @board.algebraic?(:g4), "Expected g4 to be algebraic, wasn't."
    refute @board.algebraic?(:d9), "Expected d9 not to be algebraic, was."
    refute @board.algebraic?(:n7), "Expected n7 not to be algebraic, was."
  end

  def test_board_gets_index_from_algebraic
    assert_equal 0, @board.index_from_algebraic(:a8)
    assert_equal 7, @board.index_from_algebraic(:h8)
    assert_equal 56, @board.index_from_algebraic(:a1)
    assert_equal 63, @board.index_from_algebraic(:h1)
  end

  def test_board_can_turn_indices_into_algebraic
    assert_equal "b7", @board.algebraic_from_index(9)
    assert_equal "g7", @board.algebraic_from_index(14)
    assert_equal "g2", @board.algebraic_from_index(54)
    assert_equal "b2", @board.algebraic_from_index(49)
  end

  def test_identifies_empty_squares
    assert @board.empty?(:d4), "Expected no piece at d4 but one was found."
    refute @board.empty?("a2"), "Expected a piece at a2 but none was found."
  end

  def test_identifies_empty_squares_from_indices
    assert @board.empty?(35), "Expected no piece at d4 but one was found."
    refute @board.empty?(49), "Expected a piece at a2 but none was found."
  end

  def test_board_responds_to_algebraic_methods
    assert_equal "bR", @board.a8
    assert_equal "wK", @board.e1
    assert_equal "wR", @board.h1
  end

  def test_board_determines_valid_moves
    assert @board.valid_move?(%w[a7 a6]), "Move from a7 to a6 should be legal"
    assert @board.valid_move?(%w[g1 f3]), "Move from g1 to f3 should be legal"
    refute @board.valid_move?(%w[a7 b6]), "Move from a7 to b6 shouldn't be legal"
    refute @board.valid_move?(%w[d1 g4]), "Move from d1 to g4 shouldn't be legal"
  end

  def test_board_identifies_capturable_pieces
    assert @board.capturable? 'w', 8
    assert @board.capturable? 'w', 4
    refute @board.capturable? 'b', 4
    refute @board.capturable? 'b', 34
  end
end