test/test-board.rb in shogi-ruby-0.0.2 vs test/test-board.rb in shogi-ruby-0.0.3
- old
+ new
@@ -1,6 +1,7 @@
require "shogi/board"
+require "stringio"
class BoardTest < Test::Unit::TestCase
def setup
@board = Shogi::Board.new
end
@@ -10,11 +11,11 @@
assert_equal(9, rows.size)
assert_true(rows.all? {|row| row.size == 9 })
end
def test_initialize_csa
- csa = <<-EOT
+ position = <<-EOT
P1 * * * * +HI * * -KE *
P2 * * * * * +KA-OU * -KY
P3 * * * * * * -FU-FU-FU
P4 * * * * +KY * * -GI *
P5 * * * * * * * * *
@@ -23,12 +24,28 @@
P8 * * * * * * * * *
P9 * * * * * * * * *
P+00HI00GI00KE
P-
EOT
- @board = Shogi::Board.new(csa)
- assert_equal(csa, @board.to_csa)
+ @board = Shogi::Board.new(:csa, position)
+ assert_equal(position, @board.to_csa)
+ assert_nothing_raised do
+ @board.move("+0031HI")
+ end
+ assert_equal(<<-EOT, @board.to_csa)
+P1 * * * * +HI * +HI-KE *
+P2 * * * * * +KA-OU * -KY
+P3 * * * * * * -FU-FU-FU
+P4 * * * * +KY * * -GI *
+P5 * * * * * * * * *
+P6 * * * * * * * * *
+P7 * * * * * * * * *
+P8 * * * * * * * * *
+P9 * * * * * * * * *
+P+00GI00KE
+P-
+ EOT
end
def test_to_csa
before_state = @board.instance_variable_get(:@position).dup
assert_equal(<<-EOT, @board.to_csa)
@@ -45,10 +62,36 @@
P-
EOT
assert_equal(before_state, @board.instance_variable_get(:@position))
end
+ def test_show
+ before_state = @board.instance_variable_get(:@position).dup
+ s = ""
+ io = StringIO.new(s)
+ $stdout = io
+ @board.default_format = :csa
+
+ @board.show
+
+ $stdout = STDOUT
+ assert_equal(<<-EOT, s)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI * * * * * -KA *
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 * * * * * * * * *
+P5 * * * * * * * * *
+P6 * * * * * * * * *
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA * * * * * +HI *
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+P+
+P-
+ EOT
+ assert_equal(before_state, @board.instance_variable_get(:@position))
+ end
+
def test_set_from_csa
csa = <<-EOT
P1 * * * * +HI * * -KE *
P2 * * * * * +KA-OU * -KY
P3 * * * * * * -FU-FU-FU
@@ -61,45 +104,54 @@
P+00HI00GI00KE
P-
EOT
@board.set_from_csa(csa)
assert_equal(csa, @board.to_csa)
+ assert_nothing_raised do
+ @board.move("+0031HI", :csa)
+ end
end
def test_to_usi
before_state = @board.instance_variable_get(:@position).dup
assert_equal(<<-EOT, @board.to_usi)
lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL
EOT
assert_equal(before_state, @board.instance_variable_get(:@position))
end
- def test_move_from_csa
+ def test_move_csa
+ @board.default_format = :csa
+
assert_raise Shogi::Board::FormatError do
- @board.move_from_csa("+27FU")
+ @board.move("+27FU")
end
assert_raise Shogi::Board::UndefinedPieceError do
- @board.move_from_csa("+2726AA")
+ @board.move("+2726AA")
end
assert_raise Shogi::Board::MoveError do
- assert_false(@board.move_from_csa("+2726HI"))
+ assert_false(@board.move("+2726HI"))
end
assert_raise Shogi::Board::MoveError do
- assert_false(@board.move_from_csa("+2827HI"))
+ assert_false(@board.move("+2827HI"))
end
assert_raise Shogi::Board::MoveError do
- assert_false(@board.move_from_csa("+2625FU"))
+ assert_false(@board.move("+2625FU"))
end
assert_raise Shogi::Board::MovementError do
- assert_false(@board.move_from_csa("+2725FU"))
+ assert_false(@board.move("+2725FU"))
end
assert_raise Shogi::Board::MoveError do
- assert_false(@board.move_from_csa("-4131KI"))
+ assert_false(@board.move("-4131KI"))
end
- assert_true(@board.move_from_csa("+7776FU"))
- assert_true(@board.move_from_csa("-4132KI"))
- assert_true(@board.move_from_csa("+2868HI"))
+
+ assert_nothing_raised do
+ @board.move("+7776FU")
+ @board.move("-4132KI")
+ @board.move("+2868HI")
+ end
+
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU * -GI-KE-KY
P2 * -HI * * * * -KI-KA *
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 * * * * * * * * *
@@ -111,16 +163,37 @@
P+
P-
EOT
end
- def test_move_from_csa_at_captured
- @board.move_from_csa("+7776FU")
- @board.move_from_csa("-3334FU")
- assert_true(@board.move_from_csa("+8822KA"))
- assert_true(@board.move_from_csa("-3122GI"))
+ def test_move_csa_chain
+ assert_nothing_raised do
+ @board.move("+7776FU", :csa).move("-4132KI", :csa)
+ end
assert_equal(<<-EOT, @board.to_csa)
+P1-KY-KE-GI-KI-OU * -GI-KE-KY
+P2 * -HI * * * * -KI-KA *
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 * * * * * * * * *
+P5 * * * * * * * * *
+P6 * * +FU * * * * * *
+P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P8 * +KA * * * * * +HI *
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+P+
+P-
+ EOT
+ end
+
+ def test_move_csa_at_captured
+ assert_nothing_raised do
+ @board.move("+7776FU", :csa)
+ @board.move("-3334FU", :csa)
+ @board.move("+8822KA", :csa)
+ @board.move("-3122GI", :csa)
+ end
+ assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI * -KE-KY
P2 * -HI * * * * * -GI *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 * * * * * * -FU * *
P5 * * * * * * * * *
@@ -129,11 +202,13 @@
P8 * * * * * * * +HI *
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
P+00KA
P-00KA
EOT
- assert_true(@board.move_from_csa("+0055KA"))
+ assert_nothing_raised do
+ @board.move("+0055KA", :csa)
+ end
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI * -KE-KY
P2 * -HI * * * * * -GI *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 * * * * * * -FU * *
@@ -145,17 +220,19 @@
P+
P-00KA
EOT
end
- def test_move_from_csa_promote
- @board.move_from_csa("+7776FU")
- @board.move_from_csa("-3334FU")
+ def test_move_csa_promote
+ @board.move("+7776FU", :csa)
+ @board.move("-3334FU", :csa)
assert_raise Shogi::Board::MovementError do
- assert_false(@board.move_from_csa("+2726TO"))
+ assert_false(@board.move("+2726TO", :csa))
end
- assert_true(@board.move_from_csa("+8822UM"))
+ assert_nothing_raised do
+ @board.move("+8822UM", :csa)
+ end
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI * * * * * +UM *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 * * * * * * -FU * *
@@ -165,11 +242,13 @@
P8 * * * * * * * +HI *
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
P+00KA
P-
EOT
- assert_true(@board.move_from_csa("-3122GI"))
+ assert_nothing_raised do
+ @board.move("-3122GI", :csa)
+ end
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI * -KE-KY
P2 * -HI * * * * * -GI *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 * * * * * * -FU * *
@@ -179,14 +258,16 @@
P8 * * * * * * * +HI *
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
P+00KA
P-00KA
EOT
- assert_true(@board.move_from_csa("+0033KA"))
- assert_true(@board.move_from_csa("-0078KA"))
- assert_true(@board.move_from_csa("+3366UM"))
- assert_true(@board.move_from_csa("-7867UM"))
+ assert_nothing_raised do
+ @board.move("+0033KA", :csa)
+ @board.move("-0078KA", :csa)
+ @board.move("+3366UM", :csa)
+ @board.move("-7867UM", :csa)
+ end
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI * -KE-KY
P2 * -HI * * * * * -GI *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 * * * * * * -FU * *
@@ -198,20 +279,20 @@
P+
P-00FU
EOT
end
- def test_move_from_csa_lines
+ def test_move_csa_lines
csa_lines = <<-EOT
+7776FU
-3334FU
+8822KA
-3122GI
+0055KA
EOT
- @board.move_from_csa_lines(csa_lines)
+ @board.move(csa_lines, :csa)
assert_equal(<<-EOT, @board.to_csa)
P1-KY-KE-GI-KI-OU-KI * -KE-KY
P2 * -HI * * * * * -GI *
P3-FU-FU-FU-FU-FU-FU * -FU-FU
@@ -228,8 +309,10 @@
def test_validate_movement_false
assert_true(@board.validate_movement)
@board.validate_movement = false
assert_false(@board.validate_movement)
- assert_true(@board.move_from_csa("+2755FU"))
+ assert_nothing_raised do
+ @board.move("+2755FU", :csa)
+ end
end
end