0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby testコマンド じゃんけん

Last updated at Posted at 2019-12-24

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

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

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

#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の読み込み

require 'test/unit'

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

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

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

関数の中身

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の2つから、結果を出すもの
上二つをまとめると、

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.検証プログラム

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

##5.まとめ

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.参考文献

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?