> For the complete documentation index, see [llms.txt](https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/tesutokdo/tesutokdono.md).

# テストコードの記述

minitestを使用してテストコードを記述していきます。

第一章で実装した基本ロジックを担保していきたいと思います。

## place\_pieceのテスト

まずは簡単なところからやっていきます。

以下のテストを実装しましょう

```ruby
  # 概要: コマの配置メソッドを担保する
  # 期待値: 入力した値に応じてコマが配置されること
  def test_place_piece
  end
```

{% hint style="info" %}
**課題コーナー**

&#x20;test\_place\_pieceメソッドを作成してテストを実行する
{% endhint %}

### 実装例

下記に実装例を記載します。

```ruby
  # 概要: コマの配置メソッドを担保する
  # 期待値: 入力した値に応じてコマが配置されること
  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です。

```ruby
  # 概要: 相手プレイヤーの取得メソッドを担保する
  # 期待値: 1を入力した場合は2が返却される
  #        2を入力した場合は1が返却される
  def test_get_opponent_player
  end
```

{% hint style="info" %}
**課題コーナー**

&#x20;test\_get\_opponent\_playerメソッドを作成してテストを実行する
{% endhint %}

### 実装例

```ruby
  # 概要: 相手プレイヤーの取得メソッドを担保する
  # 期待値: 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?のテスト

勝利条件のテストは記述量が多くてちょっと面倒ですが、全て担保しておくに限ります。

横、縦、斜め全てのラインを担保させます。

```ruby
  # 概要: プレイヤーの勝利条件を担保
  # 期待値: 以下の条件時にtrueが返却される
  #         - プレイヤーのコマが横に三列揃っている
  #         - プレイヤーのコマが縦に三列揃っている
  #         - プレイヤーのコマが斜めに三列揃っている
  #        それ以外の場合はfalseが返却される
  def test_win?
  end
```

{% hint style="info" %}
**課題コーナー**

&#x20;test\_win?メソッドを作成してテストを実行する
{% endhint %}

### 実装例

```ruby
  # 概要: プレイヤーの勝利条件を担保
  # 期待値: 以下の条件時に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?のテスト

ゲーム板に空きマスが存在するかどうかのメソッドをテストします。

ゲーム板を埋めるのがポイントです。

```ruby
  # 概要:  コマを置く場所が存在するかどうかの判定部分を担保する
  # 期待値: 以下の条件時にtrueが返却される
  #         - 置き場所がある
  #         以下の条件時にfalseが返却される
  #         - 置き場所がない
  def test_can_plase_piece?
  end 
```

{% hint style="info" %}
**課題コーナー**

&#x20;test\_can\_plase\_piece?メソッドを作成してテストを実行する
{% endhint %}

### 実装例

```ruby
  # 概要:  コマを置く場所が存在するかどうかの判定部分を担保する
  # 期待値: 以下の条件時に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?のテスト

ゲーム継続条件をテストします。

ゲームが終了する条件を考えたらテストしやすいです。

```ruby
  # 概要:  ゲームの継続条件を担保する
  # 期待値: ゲームが進行可能な状態な時にtrueが返却される
  #         以下の条件時にfalseが返却される
  #         - プレイヤーが勝利した
  #         - 引き分けになった
  def test_continue?
  end
```

{% hint style="info" %}
**課題コーナー**

&#x20;test\_continue?メソッドを作成してテストを実行する
{% endhint %}

### 実装例

```ruby
  # 概要:  ゲームの継続条件を担保する
  # 期待値: ゲームが進行可能な状態な時に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をテストしてみましょう。

このメソッドに関しては今までどおりいきません。

スタブを使わないと実装するのが難しいと思います。

{% embed url="<https://www.rubydoc.info/gems/minitest/Object:stub>" %}

一度トライしてみてください。

```ruby
  # 概要:  コマを置く場所を取得した時に整数に変換されることを担保する
  # 期待値: 整数の1がrowとcolにそれぞれ返却されること
  def test_gets_piece_location
  end
```

{% hint style="info" %}
**課題コーナー**

stubを使ってtest\_gets\_positionを実装してください
{% endhint %}

### 実装例

※ printにスタブつけてるのは、テスト時に標準出力が出るのが邪魔だからです。

```ruby
  # 概要:  コマを置く場所を取得した時に整数に変換されることを担保する
  # 期待値: 整数の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
```

第二章で実装するテストは、これでおしまいです。

次にテストが登場するのは第四章のリファクタリングです。

第四章以降からはコードの実装とテストの実装を同時に行っていきます。

最後に、この章で実装したコードを以下のリポジトリに乗せておきます。

{% embed url="<https://github.com/Kashiwara0205/ruby-tic-tac-toe/tree/master/ch2>" %}

##
