--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
――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”
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.
[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