Last active
February 21, 2018 15:52
-
-
Save inokappa/20d38207310ba0af63ce95cd6befe31d to your computer and use it in GitHub Desktop.
オトナな TDD の FizzBuzz Ruby 版 (プロダクションコードとテストコードの合体版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# コードとテストコードの合体版 (プロダクションコードとテストコードは分けること) | |
# プロダクションコード | |
require 'minitest/autorun' | |
# require './fizzbuzz' | |
class FizzBuzz | |
def create(a, b) | |
res = [] | |
(a..b).each do |n| | |
if n % 3 == 0 and n % 5 == 0 | |
res << 'FizzBuzz' | |
elsif n % 3 == 0 | |
res << 'Fizz' | |
elsif n % 5 == 0 | |
res << 'Buzz' | |
else | |
res << n | |
end | |
end | |
res | |
end | |
end | |
# テストコード | |
class FizzBuzzTest < Minitest::Test | |
def test_running | |
assert(true) | |
end | |
def test_Fizz | |
res = FizzBuzz.new.create(1, 10) | |
assert_equal(res.length, 10) | |
val = 1 | |
(0..9).each do |n| | |
if n == 2 | |
assert_equal(res[n], 'Fizz') | |
elsif n == 5 | |
assert_equal(res[n], 'Fizz') | |
elsif n == 8 | |
assert_equal(res[n], 'Fizz') | |
end | |
val += 1 | |
end | |
end | |
def test_Buzz | |
res = FizzBuzz.new.create(1, 10) | |
assert_equal(res.length, 10) | |
val = 1 | |
(0..9).each do |n| | |
if n == 4 | |
assert_equal(res[n], 'Buzz') | |
elsif n == 9 | |
assert_equal(res[n], 'Buzz') | |
end | |
val += 1 | |
end | |
end | |
def test_FizzBuzz | |
res = FizzBuzz.new.create(1, 30) | |
assert_equal(res.length, 30) | |
val = 1 | |
(0..29).each do |n| | |
if n == 14 | |
assert_equal(res[n], 'FizzBuzz') | |
elsif n == 29 | |
assert_equal(res[n], 'FizzBuzz') | |
end | |
val += 1 | |
end | |
end | |
end |
Author
inokappa
commented
Feb 21, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment