Write a test by implementing the story of Mr. Nabeats in the world with ruby

Why i decided to do it

--I wanted to write a unit test with rspec --A simple program was good ――Because the story of Mr. Nabeats in the world is simple, lean and interesting ――Because I came up with it ――Because I wanted to do it

specification

――First, "I'm going to do something interesting, that is, something funny" --Count from 1 to 40 ――When the number has 3, it becomes stupid ――When it is a multiple of 3, it becomes stupid --Become a dog when it is a multiple of 5 (become a dog when it is a multiple of 3 and 5) ――Finally, it ’s called “Omoro”

First, implementation

omoro.rb


TSUJO_KAO_MOJI = "(・`ω ・ ´)" #Normal
AHO_KAO_MOJI = "ʅ( ՞ਊ՞)ʃ≡" #I'm going to be stupid
DOG_KAO_MOJI = "∪ ・ ω ・ ∪" #Inu's intention

puts 'I'm going to do something interesting, that is, something funny'

(1..40).each do |figure|
  sleep 0.5
  
  if figure % 5 == 0
    puts figure.to_s + DOG_KAO_MOJI
    next
  end

  if figure % 3 == 0
    puts figure.to_s + AHO_KAO_MOJI
    next
  end

  if /3/ =~ figure.to_s
    puts figure.to_s + AHO_KAO_MOJI
    next
  end
  
  puts figure.to_s + TSUJO_KAO_MOJI
end

puts 'Omorrow'

ruby omoro.Run on rb



```I'm going to do something interesting, that is, something funny
1(・`ω ・ ´)
2(・`ω ・ ´)
3ʅ( ՞ਊ՞)ʃ≡
4(・`ω ・ ´)
5∪ ・ ω ・ ∪
6ʅ( ՞ਊ՞)ʃ≡
7(・`ω ・ ´)
8(・`ω ・ ´)
9ʅ( ՞ਊ՞)ʃ≡
10∪ ・ ω ・ ∪
11(・`ω ・ ´)
12ʅ( ՞ਊ՞)ʃ≡
13ʅ( ՞ਊ՞)ʃ≡
14(・`ω ・ ´)
15∪ ・ ω ・ ∪
16(・`ω ・ ´)
17(・`ω ・ ´)
18ʅ( ՞ਊ՞)ʃ≡
19(・`ω ・ ´)
20∪ ・ ω ・ ∪
21ʅ( ՞ਊ՞)ʃ≡
22(・`ω ・ ´)
23ʅ( ՞ਊ՞)ʃ≡
24ʅ( ՞ਊ՞)ʃ≡
25∪ ・ ω ・ ∪
26(・`ω ・ ´)
27ʅ( ՞ਊ՞)ʃ≡
28(・`ω ・ ´)
29(・`ω ・ ´)
30∪ ・ ω ・ ∪
31ʅ( ՞ਊ՞)ʃ≡
32ʅ( ՞ਊ՞)ʃ≡
33ʅ( ՞ਊ՞)ʃ≡
34ʅ( ՞ਊ՞)ʃ≡
35∪ ・ ω ・ ∪
36ʅ( ՞ਊ՞)ʃ≡
37ʅ( ՞ਊ՞)ʃ≡
38ʅ( ՞ਊ՞)ʃ≡
39ʅ( ՞ਊ՞)ʃ≡
40∪ ・ ω ・ ∪
Omorrow

did it. However, with this, there is no method, all the processing is the same, and unit tests cannot be written. Separated methods by responsibility.

omoro_class.rb


class Omoro
  TSUKAMI = "I'm going to do something interesting, that is, something funny"
  OCHI = "Omorrow"
  TSUJO_KAO_MOJI = "(・`ω ・ ´)"
  AHO_KAO_MOJI = "ʅ( ՞ਊ՞)ʃ≡"
  DOG_KAO_MOJI = "∪ ・ ω ・ ∪"
  
  #Neta
  def main(max_kazu = 40)
    speak(TSUKAMI)
    
    (1..max_kazu).each do |figure|
      sleep 1
      kao_moji = verification(figure)
      serif = make_serif(figure, kao_moji)
      speak(serif)
    end
    
    speak(OCHI)
  end
  
  #Make lines
  def make_serif(figure, kao_moji)
    figure.to_s + kao_moji
  end
  
  #to decide
  def verification(figure)
    if figure % 5 == 0
      return DOG_KAO_MOJI
    end
    
    if figure % 3 == 0 || /3/ =~ figure.to_s
      return AHO_KAO_MOJI
    end
    TSUJO_KAO_MOJI
  end
  
  #speak
  def speak(serif)
    puts serif
  end
end
  
omoro = Omoro.new
omoro.main(40)

Omorrow class is ready. As an aside, you can dynamically change the maximum number to count by passing an arbitrary number to the main method. If you enter 400, you will be a stupid or dog from 300 to 399. It's hard if you use human power, but it's easy if you write it programmatically.

Write a test. studying. I don't use let because I don't understand it well. If you have a gentle Masakari, please write an example in the comments.

omoro_spec.rb


require 'rspec'
require_relative '../omoro_class'

RSpec.describe Omoro do
    TSUJO_KAO_MOJI = "(・`ω ・ ´)"
    AHO_KAO_MOJI = "ʅ( ՞ਊ՞)ʃ≡"
    DOG_KAO_MOJI = "∪ ・ ω ・ ∪"

  it 'Numbers(1)+ Emoticon((・`ω ・ ´))To generate the lines of' do
    expect(Omoro.new.make_serif(1, TSUJO_KAO_MOJI)).to eq '1(・`ω ・ ´)'
  end

  it 'Numbers(3)+ Emoticon(3ʅ( ՞ਊ՞)ʃ≡)To generate the lines of' do
    expect(Omoro.new.make_serif(3, AHO_KAO_MOJI)).to eq '3ʅ( ՞ਊ՞)ʃ≡'
  end

  it 'Inu' do
    expect(Omoro.new.make_serif(5, DOG_KAO_MOJI)).to eq '5∪ ・ ω ・ ∪'
  end

  it '3 is attached' do
    expect(Omoro.new.make_serif(33, 'ʅ( ՞ਊ՞)ʃ≡')).to eq '33ʅ( ՞ਊ՞)ʃ≡'
  end

  it 'verification' do
    omoro = Omoro.new
    (1..40).each do |figure|
      if figure % 5 == 0
        expect(omoro.verification(figure)).to eq DOG_KAO_MOJI
      elsif figure % 3 == 0 || /3/ =~ figure.to_s
        expect(omoro.verification(figure)).to eq AHO_KAO_MOJI
      else
        expect(omoro.verification(figure)).to eq TSUJO_KAO_MOJI
      end
    end
  end

  it 'main' do
    omoro = Omoro.new
    expect { omoro.main(5) }.to output(
      "I'm going to do something interesting, that is, something funny\n1(・`ω ・ ´)\n2(・`ω ・ ´)\n3ʅ( ՞ਊ՞)ʃ≡\n4(・`ω ・ ´)\n5∪ ・ ω ・ ∪\n Omorrow\n").to_stdout
  end
end

I personally feel that the verification and main methods can be improved, but I didn't know how to do it.

References

[Sando Katsura -Wikipedia](https://ja.wikipedia.org/wiki/%E6%A1%82%E4%B8%89%E5%BA%A6#%E4%B8%96%E7%95% 8C% E3% 81% AE% E3% 83% 8A% E3% 83% 99% E3% 82% A2% E3% 83% 84)

[\ Ruby ] Unit test starting with Rspec \ (Unit test ) \ | qs Developers

Test standard output with Rspec -Qiita

Recommended Posts

Write a test by implementing the story of Mr. Nabeats in the world with ruby
Implementing poker little by little in Ruby Part 1
Implementing poker little by little in Ruby Part 4
Implementing poker little by little in Ruby Part 3
Class in Ruby
Heavy in Ruby! ??
Write a test by implementing the story of Mr. Nabeats in the world with ruby
About eval in Ruby
Output triangle in Ruby
Variable type in ruby
Fast popcount in Ruby
Call a method of the parent class by explicitly specifying the name in Ruby
Count the number of occurrences of a string in Ruby
The story of making a reverse proxy with ProxyServlet
Determine that the value is a multiple of 〇 in Ruby
A story packed with the basics of Spring Boot (solved)
Find the number of days in a month with Kotlin
The story of the first Rails app refactored with a self-made helper
A story about hitting the League Of Legends API with JAVA
A story that struggled with the introduction of Web Apple Pay
[Illustration] Finding the sum of coins with a recursive function [Ruby]
The story of making it possible to build a project that was built by Maven with Ant
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
The story of forgetting to close a file in Java and failing
The story of making a game launcher with automatic loading function [Java]
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Implementing poker little by little in Ruby Part 2
Implementing poker little by little in Ruby Part 1
Implementing poker little by little in Ruby Part 4
The story of AppClip support in Anyca
Extract a part of a string with Ruby
The story of writing Java in Emacs
Write the movement of Rakefile in the runbook
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to get the ID of a user authenticated with Firebase in Swift
Validate the identity token of a user authenticated with AWS Cognito in Java
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
The story of making ordinary Othello in Java
A story about the JDK in the Java 11 era
Measure the size of a folder in Java
The story of introducing Ajax communication to ruby
Story of implementing update function in form object
Manage the version of Ruby itself with rbenv
The story of tuning android apps with libGDX
I checked the number of taxis with Ruby
Create a native extension of Ruby in Rust
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.
[Java] Integer information of characters in a text file acquired by the read () method
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to request by passing an array to the query with HTTP Client of Ruby
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Count the frequency of occurrence of words in a sentence by stream processing (Apache Apex)
A review of the code used by rails beginners
Format of the log output by Tomcat itself in Tomcat 8
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby] The role of subscripts in learning elements in arrays
Let's write a Qiita article in org-mode of Emacs !!
Get the name of the test case in the JUnit test class
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Calculate the difference between numbers in a Ruby array
[Ruby / Rails] Set a unique (unique) value in the class
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Get the URL of the HTTP redirect destination in Ruby
The story of making dto, dao-like with java, sqlite
Use Coveralls with GitHub Actions in a Ruby repository
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
The story of introducing a very Rails-like serverless framework "Ruby on Jets" into the production environment
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.