Skip to content

Instantly share code, notes, and snippets.

@inokappa
Last active February 21, 2018 15:52
Show Gist options
  • Save inokappa/20d38207310ba0af63ce95cd6befe31d to your computer and use it in GitHub Desktop.
Save inokappa/20d38207310ba0af63ce95cd6befe31d to your computer and use it in GitHub Desktop.
オトナな TDD の FizzBuzz Ruby 版 (プロダクションコードとテストコードの合体版)
# コードとテストコードの合体版 (プロダクションコードとテストコードは分けること)
# プロダクションコード
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
@inokappa
Copy link
Author

$ ruby --version
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin15]
$ ruby fizzbuzz_test.rb
Run options: --seed 53475

# Running:

....

Finished in 0.001612s, 2482.0810 runs/s, 6825.7226 assertions/s.

4 runs, 11 assertions, 0 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment