Ruby testコマンド じゃんけん

#じゃんけんのプログラムのテストを実行

##1.テストする、じゃんけんのプログラム

参照
https://qiita.com/ktpnobu/items/a8525f8cceddd486c219

~~~Ruby
#Rock-Paper-Scissors
#0-1-2

p 'rock-paper-scissors'
player=gets

a=0


if player=="rock"
	player_hand=0
	a=1
elsif player=="paper"
	player_hand=1
	a=1
elsif player=="scissors" 
	player_hand=2
	a=1
end


program_hand=rand(3)


if program_hand==0
	program="rock"
elsif program_hand==1
	program="paper"
elsif program_hand==2
	program="scissors"
end


if a==0
	p 'one more please'
if player_hand==program_hand
	'draw'
elsif (player_hand==0 and program_hand==2) or (player_hand==1 and program_hand==0) or (player_hand==2 and program_hand==1) 
	'you win'
else 
	'you lose'
end

p "player",player,"program",program
~~~


このプログラムの勝敗の部分について、test実行

##2.test/unit

test/unitの読み込み

~~~Ruby
require 'test/unit'
~~~

##3.引数から答えをだす関数を定義←これが正しいのかテストする
今回、じゃんけんの勝敗のテストなので、二つの手を引数とし、勝敗が正しく表示されるか

~~~Ruby
class Rock_paper_scissors
     def self.hantei(player_hand,program_hand)
            #ここにプログラムを書く
     end
end
~~~

player_handとprogram_handの２値が決まれば、正しい勝敗が出る(であろう)関数である
↑これが本当に正しいのかをtestするイメージ

関数の中身

~~~Ruby
if player_hand==program_hand
	'draw'
elsif (player_hand==0 and program_hand==2) or (player_hand==1 and program_hand==0) or (player_hand==2 and program_hand==1) 
	'you win'
else 
	'you lose'
end
~~~

↑player_handとprogram_handの２つから、結果を出すもの
上二つをまとめると、

~~~Ruby
class Rock_paper_scissors
     def self.hantei(player_hand,program_hand)


if player_hand==program_hand
	'draw'
elsif (player_hand==0 and program_hand==2) or (player_hand==1 and program_hand==0) or (player_hand==2 and program_hand==1) 
	'you win'
else 
	'you lose'
end

     end
end
~~~

##4.検証プログラム

２つの引数からでた値は本当に正しいのか検証するために、
すべての出力とその結果を書いたプログラムを作る

~~~Ruby
class TestRock_paper_scissors < Test::Unit::TestCase
  def test_Rock_paper_scissors
    assert_equal('draw', Rock_paper_scissors.hantei(0, 0))
    assert_equal('draw', Rock_paper_scissors.hantei(1, 1))
    assert_equal('draw', Rock_paper_scissors.hantei(2,2))
    assert_equal('you win', Rock_paper_scissors.hantei(1, 0))
    assert_equal('you win', Rock_paper_scissors.hantei(2, 1))
    assert_equal('you win', Rock_paper_scissors.hantei(0, 2))
    assert_equal('you lose', Rock_paper_scissors.hantei(2, 0))
    assert_equal('you lose', Rock_paper_scissors.hantei(0, 1))
    assert_equal('you lose', Rock_paper_scissors.hantei(1, 2))
  end
end
~~~

##5.まとめ

~~~Ruby
require 'test/unit'

class Rock_paper_scissors
def self.hantei(player_hand,program_hand)



if player_hand==program_hand
	'draw'
elsif (player_hand==0 and program_hand==2) or (player_hand==1 and program_hand==0) or (player_hand==2 and program_hand==1) 
	'you win'
else 
	'you lose'
end

end
end

#Rock-Paper-Scissors
#0-1-2

class TestRock_paper_scissors < Test::Unit::TestCase
  def test_Rock_paper_scissors
    assert_equal('draw', Rock_paper_scissors.hantei(0, 0))
    assert_equal('draw', Rock_paper_scissors.hantei(1, 1))
    assert_equal('draw', Rock_paper_scissors.hantei(2,2))
    assert_equal('you win', Rock_paper_scissors.hantei(1, 0))
    assert_equal('you win', Rock_paper_scissors.hantei(2, 1))
    assert_equal('you win', Rock_paper_scissors.hantei(0, 2))
    assert_equal('you lose', Rock_paper_scissors.hantei(2, 0))
    assert_equal('you lose', Rock_paper_scissors.hantei(0, 1))
    assert_equal('you lose', Rock_paper_scissors.hantei(1, 2))
  end
end

~~~


##6.参考文献

https://qiita.com/repeatedly/items/727b08599d87af7fa671#assert%E3%81%AE%E4%BD%9C%E3%82%8A%E6%96%B9

https://teratail.com/questions/197215

http://portaltan.hatenablog.com/entry/2015/10/28/111757

https://teratail.com/questions/164134

https://teratail.com/questions/51977

https://qiita.com/kazuki0714/items/b54d006af7672bb54eac