Created
October 3, 2011 16:00
-
-
Save jaksonmoura/1259454 to your computer and use it in GitHub Desktop.
FizzBuzz
This file contains hidden or 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
| class FizzBuzz | |
| def fizzbuzz(i) | |
| return "FizzBuzz" if i % 3 == 0 and i % 5 == 0 or (i.to_s =~ /5/ and i.to_s =~ /3/ ) | |
| return "Buzz" if i % 5 == 0 or i.to_s =~ /5/ | |
| return "Fizz" if i % 3 == 0 or i.to_s =~ /3/ | |
| i # Retorna "i" se nenhuma das ações anteriores acontecerem | |
| end | |
| end | |
| @fizzbuzz = FizzBuzz.new | |
| 1.upto(100) do |i| | |
| puts @fizzbuzz.fizzbuzz(i) | |
| end |
This file contains hidden or 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 'test/unit' | |
| require './FizzBuzz' | |
| class TestFizzBuzz < Test::Unit::TestCase | |
| def setup | |
| @fizzbuzz = FizzBuzz.new | |
| end | |
| # Testando para numeros divisíveis por 3 | |
| def test_fizz_divisivel | |
| assert_equal("Fizz", @fizzbuzz.fizzbuzz(3), "Fizz: 3") | |
| end | |
| # Testando se contem 3 | |
| def test_fizz | |
| assert_equal("Fizz", @fizzbuzz.fizzbuzz(31), "Fizz: 31") | |
| end | |
| # Testando para numeros divisíveis por 5 | |
| def test_buzz_divisivel | |
| assert_equal("Buzz", @fizzbuzz.fizzbuzz(5), "Buzz: 5") | |
| end | |
| # Testando se contem 5 | |
| def test_buzz | |
| assert_equal("Buzz", @fizzbuzz.fizzbuzz(55), "Buzz: 55") | |
| end | |
| # Testando para numeros divisíveis por 3 e 5 | |
| def test_fizzbuzz_divisivel | |
| assert_equal("FizzBuzz", @fizzbuzz.fizzbuzz(15), "FizzBuzz: 15") | |
| end | |
| # Testando se contem 3 ou 5 | |
| def test_fizzbuzz | |
| assert_equal("FizzBuzz", @fizzbuzz.fizzbuzz(35), "FizzBuzz: 35") | |
| end | |
| # Testando se não for nenhum | |
| def test_else | |
| assert_equal(11, @fizzbuzz.fizzbuzz(11)) | |
| end | |
| def teardown | |
| @fizzbuzz = nil | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment