minitestを使用してテストコードを記述していきます。
第一章で実装した基本ロジックを担保していきたいと思います。
place_pieceのテスト
まずは簡単なところからやっていきます。
以下のテストを実装しましょう
コピー # 概要: コマの配置メソッドを担保する
# 期待値: 入力した値に応じてコマが配置されること
def test_place_piece
end
課題コーナー
test_place_pieceメソッドを作成してテストを実行する
実装例
下記に実装例を記載します。
コピー # 概要: コマの配置メソッドを担保する
# 期待値: 入力した値に応じてコマが配置されること
def test_place_piece
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
place_piece(board, 1, 0, 0)
assert_equal 1, board[0][0]
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
place_piece(board, 2, 1, 1)
assert_equal 2, board[1][1]
end
get_oppnent_playerのテスト
1を渡したら2が、2を渡したら1が帰ってくることを担保するだけでOKです。
コピー # 概要: 相手プレイヤーの取得メソッドを担保する
# 期待値: 1を入力した場合は2が返却される
# 2を入力した場合は1が返却される
def test_get_opponent_player
end
課題コーナー
test_get_opponent_playerメソッドを作成してテストを実行する
実装例
コピー # 概要: 相手プレイヤーの取得メソッドを担保する
# 期待値: 1を入力した場合は2が返却される
# 2を入力した場合は1が返却される
def test_get_opponent_player
oppnent = get_opponent_player(1)
assert_equal 2, oppnent
oppnent = get_opponent_player(2)
assert_equal 1, oppnent
end
win?のテスト
勝利条件のテストは記述量が多くてちょっと面倒ですが、全て担保しておくに限ります。
横、縦、斜め全てのラインを担保させます。
コピー # 概要: プレイヤーの勝利条件を担保
# 期待値: 以下の条件時にtrueが返却される
# - プレイヤーのコマが横に三列揃っている
# - プレイヤーのコマが縦に三列揃っている
# - プレイヤーのコマが斜めに三列揃っている
# それ以外の場合はfalseが返却される
def test_win?
end
課題コーナー
test_win?メソッドを作成してテストを実行する
実装例
コピー # 概要: プレイヤーの勝利条件を担保
# 期待値: 以下の条件時にtrueが返却される
# - プレイヤーのコマが横に三列揃っている
# - プレイヤーのコマが縦に三列揃っている
# - プレイヤーのコマが斜めに三列揃っている
# それ以外の場合はfalseが返却される
def test_win?
player = 1
board = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
assert win?(player, board)
board = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
assert win?(player, board)
board = [[0, 0, 0], [0, 0, 0], [1, 1, 1]]
assert win?(player, board)
board = [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
assert win?(player, board)
board = [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
assert win?(player, board)
board = [[0, 0, 1], [0, 0, 1], [0, 0, 1]]
assert win?(player, board)
board = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
assert win?(player, board)
board = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
assert win?(player, board)
board = [[0, 0, 0], [0, 1, 1], [0, 0, 0]]
assert_equal false, win?(player, board)
board = [[0, 0, 0], [2, 2, 2], [0, 0, 0]]
assert_equal false, win?(player, board)
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
assert_equal false, win?(player, board)
end
can_plase_piece?のテスト
ゲーム板に空きマスが存在するかどうかのメソッドをテストします。
ゲーム板を埋めるのがポイントです。
コピー # 概要: コマを置く場所が存在するかどうかの判定部分を担保する
# 期待値: 以下の条件時にtrueが返却される
# - 置き場所がある
# 以下の条件時にfalseが返却される
# - 置き場所がない
def test_can_plase_piece?
end
課題コーナー
test_can_plase_piece?メソッドを作成してテストを実行する
実装例
コピー # 概要: コマを置く場所が存在するかどうかの判定部分を担保する
# 期待値: 以下の条件時にtrueが返却される
# - 置き場所がある
# 以下の条件時にfalseが返却される
# - 置き場所がない
def test_can_plase_piece?
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
assert can_plase_piece(board)
board = [[1, 1, 2], [2, 0, 1], [1, 2, 1]]
assert can_plase_piece(board)
board = [[1, 1, 2], [2, 2, 1], [1, 2, 1]]
assert_equal false, can_plase_piece(board)
end
continue?のテスト
ゲーム継続条件をテストします。
ゲームが終了する条件を考えたらテストしやすいです。
コピー # 概要: ゲームの継続条件を担保する
# 期待値: ゲームが進行可能な状態な時にtrueが返却される
# 以下の条件時にfalseが返却される
# - プレイヤーが勝利した
# - 引き分けになった
def test_continue?
end
課題コーナー
test_continue?メソッドを作成してテストを実行する
実装例
コピー # 概要: ゲームの継続条件を担保する
# 期待値: ゲームが進行可能な状態な時にtrueが返却される
# 以下の条件時にfalseが返却される
# - プレイヤーが勝利した
# - 引き分けになった
def test_continue?
player = 1
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
assert continue?(player, board)
board = [[1, 2, 0], [0, 0, 0], [0, 0, 0]]
assert continue?(player, board)
board = [[1, 1, 1], [2, 2, 2], [0, 0, 0]]
assert_equal false, continue?(player, board)
board = [[1, 0, 2], [1, 1, 0], [0, 0, 2]]
assert continue?(player, board)
board = [[1, 1, 2], [2, 2, 1], [1, 2, 1]]
assert_equal false, continue?(player, board)
end
gets_piece_locationのテスト
最後にgets_piece_locationをテストしてみましょう。
このメソッドに関しては今までどおりいきません。
スタブを使わないと実装するのが難しいと思います。
一度トライしてみてください。
コピー # 概要: コマを置く場所を取得した時に整数に変換されることを担保する
# 期待値: 整数の1がrowとcolにそれぞれ返却されること
def test_gets_piece_location
end
課題コーナー
stubを使ってtest_gets_positionを実装してください
実装例
※ printにスタブつけてるのは、テスト時に標準出力が出るのが邪魔だからです。
コピー # 概要: コマを置く場所を取得した時に整数に変換されることを担保する
# 期待値: 整数の1がrowとcolにそれぞれ返却されること
def test_gets_position
stub(:print, nil) do
stub(:gets, "1") do
row, col = gets_position()
assert_equal 1, row
assert_equal 1, col
end
end
end
第二章で実装するテストは、これでおしまいです。
次にテストが登場するのは第四章のリファクタリングです。
第四章以降からはコードの実装とテストの実装を同時に行っていきます。
最後に、この章で実装したコードを以下のリポジトリに乗せておきます。