I tested how to use Ruby's test / unit and rock-paper-scissors code.

First of all

The assignment said, "Test the rock-paper-scissors program using test / unit." I heard the test code for the first time, so I will write it as a memo for myself. The operating environment is as follows.

mac Ruby 2.6.5 test/unit 3.3.6

What you want to achieve

Test the rock-paper-scissors program using "test / unit".

What is test / unit?

In the first place, the test code is a program to check whether the logic of the written program is working as expected.

test / unit is a test framework for Ruby, and it seems that it was originally installed as standard. Please refer to here for the installation of tesu / unit itself.

Basic usage of test / unit

As a premise, please note that it differs slightly depending on the ver. The basic syntax is as follows.

require 'test/unit'

 class [class name] <Test :: Unit :: TestCase

end

First, require``'test / unit' to use this framework. Next, create a class that inherits Test :: Unit :: TestCase. In Ruby, by inheriting Test :: Unit :: TestCase, the class becomes a test executable file.

require 'test/unit'

 class [class name] <Test :: Unit :: TestCase

 def test_ [method name]
    foo = "hoge"
    assert_equal("hoge", foo)
  end

end

The test process is as above. Create a method and write the program you want to test there. The rule is that method names start with test_. In this method, write the check method in the following format. (There are other check methods)

 assert_equal (assumed values, variables, methods, etc.)

ʻAssert_equal` makes sure that the value of the variable and the return value of the method match what you expected.

This is the end of the quick tutorial, but there are more methods available by default, so check out the reference.

Solution

Then, the solution of the achievement condition is described below. First of all, I will write the rock-paper-scissors program first. (It's not a very smart program, but please forgive me) If you input g, c or p as standard input, the result of rock-paper-scissors is output.

player = gets.chomp

if player == "g"
 player = "goo"
elsif player == "c"
 player = "choki"
elsif player == "p"
 player = "par"
end

com = rand(3)
if com == 0
 com = "goo"
elsif com  == 1
 com = "choki"
else
 com = "par"
end

if player == com
 res = "draw"
 elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
 res = "You win"
else
 res = "You lose"
end

 puts "You are # {player}, I am # {com}, # {res}."

In this case, we will test whether the correct result is displayed when the values of the two variables player and com are decided, so we will test the following part.

if player == com
 res = "draw"
 elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
 res = "You win"
else
 res = "You lose"
end

 puts "You are # {player}, I am # {com}, # {res}."

This time, we will summarize the above process of determining victory or defeat in a class.

class Janken
  def self.judge(player,com)
    if player == com
 res = "draw"
     elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
 res = "You win"
    else
 res = "You lose"
    end
 return "You are # {player}, I am # {com}, # {res}."
  end
end

The processing of winning and losing of rock-paper-scissors is summarized in the class. Then, write the test process.

require 'test/unit'

class Test_Janken < Test::Unit::TestCase
  def test_janken
 assert_equal ('You are goo, I am goo, draw.', Janken.judge ("goo", "goo"))
 assert_equal ('You are choki, I am choki, draw.', Janken.judge ("choki", "choki"))
 assert_equal ('You are par, I am par, draw.', Janken.judge ("par", "par"))
 assert_equal ('You are goo, I am choki, you win.', Janken.judge ("goo", "choki"))
 assert_equal ('You are choki, I am par, you win.', Janken.judge ("choki", "par"))
 assert_equal ('You are par, I am goo, you win.', Janken.judge ("par", "goo"))
 assert_equal ('You are goo, I am par, you lose.', Janken.judge ("goo", "par"))
 assert_equal ('You are choki, I am goo, you lose.', Janken.judge ("choki", "goo"))
 assert_equal ('You are par, I am choki, you lose.', Janken.judge ("par", "choki"))
  end
end

It's like verifying all 9 patterns of rock-paper-scissors wins and losses with ʻassert_equal`. This is combined with the processing of rock-paper-scissors. The following code is the final one.

require 'test/unit'

class Janken
  def self.judge(player,com)
    if player == com
 res = "draw"
     elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
 res = "You win"
    else
 res = "You lose"
    end

 return "You are # {player}, I am # {com}, # {res}."
    
  end
end

class Test_Janken < Test::Unit::TestCase
  def test_janken
 assert_equal ('You are goo, I am goo, draw.', Janken.judge ("goo", "goo"))
 assert_equal ('You are choki, I am choki, draw.', Janken.judge ("choki", "choki"))
 assert_equal ('You are par, I am par, draw.', Janken.judge ("par", "par"))
 assert_equal ('You are goo, I am choki, you win.', Janken.judge ("goo", "choki"))
 assert_equal ('You are choki, I am par, you win.', Janken.judge ("choki", "par"))
 assert_equal ('You are par, I am goo, you win.', Janken.judge ("par", "goo"))
 assert_equal ('You are goo, I am par, you lose.', Janken.judge ("goo", "par"))
 assert_equal ('You are choki, I am goo, you lose.', Janken.judge ("choki", "goo"))
 assert_equal ('You are par, I am choki, you lose.', Janken.judge ("par", "choki"))
  end
end

Result is,,

Loaded suite test
Started
.
Finished in 0.000811 seconds.
--------------------------------------------------------------------------------
1 tests, 9 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
--------------------------------------------------------------------------------
1233.05 tests/s, 11097.41 assertions/s

I was able to test without any problems.

reference

test / unit library --doc.ruby-lang.org

How to use ruby's default Unit test-Brain juice portal

Write a test with Test :: Unit -Qiita

Recommended Posts

I tested how to use Ruby's test / unit and rock-paper-scissors code.
How to use StringBurrer and Arrays.toString.
How to unit test Spring AOP
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
[RSpec] How to write test code
How to use OrientJS and OrientDB together
Java Artery-Easy to use unit test library
I want to write a unit test!
How to set up and use kapt
How to use substring and substr methods
How to use @Builder and @NoArgsConstructor together
I was curious about how to use Optional orElse () and orElseGet () properly.
How to set different source / target versions for production code and test code
[Java] How to use FileReader class and BufferedReader class
[Ruby] How to use gsub method and sub method
How to use "sign_in" in integration test (RSpec)
How to use Segmented Control and points to note
Model unit test code to check uniqueness constraints
How to use scope and pass processing (Jakarta)
How to write test code with Basic authentication
[Java] How to use Calendar class and Date class
[Java] I studied polymorphism, so I will summarize how to use it and its merits.
How to use Map
How to use rbenv
How to write a unit test for Spring Boot 2
How to unit test with JVM with source using RxAndroid
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use Maven that I can't hear anymore
To implement, write the test and then code the process
How to use map
How to use collection_select
Use Allure to generate language- and framework-independent test reports!
[Rails] I don't know how to use the model ...
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes
How to use JUnit 5
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to check CircleCI code and automatically deploy to Heroku
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use and apply Java's JFrame / Canvas class
How to use Map
I would like to know how to improve redundant code!
How to dynamically write iterative test cases using test / unit (Test :: Unit)
How to test including images when using ActiveStorage and Faker
How to specify character code and line feed code in JAXB
How to use OpenCV 4 on Android and view camera live view
I want to randomly generate information when writing test code
I tried to collect and solve Ruby's "class" related problems.
How to set character code and line feed code in Eclipse