#FizzBuzz Exemplo: http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz Utilizando TDD (Test-driven development)
Last active
December 20, 2015 07:39
-
-
Save neiesc/6095226 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
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
""" | |
FizzBuzz | |
""" | |
def fizzbuzz(num): | |
retorno = '' | |
if num <= 0 : | |
return '' | |
if num % 3 == 0: | |
retorno = 'fizz' | |
if num % 5 == 0: | |
retorno += 'buzz' | |
if retorno == '': | |
retorno = str(num) | |
return retorno | |
def exibe_lista(lst): | |
return [fizzbuzz(num) for num in lst] |
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
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import unittest | |
class TestFizzBuzz(unittest.TestCase): | |
def _getTargetClass(self): | |
import FizzBuzz | |
return FizzBuzz | |
def _makeOne(self, *args, **kw): | |
return self._getTargetClass() | |
def test_number(self): | |
fb = self._makeOne() | |
self.assertEqual(fb.fizzbuzz(0), '') | |
self.assertEqual(fb.fizzbuzz(1), '1') | |
self.assertEqual(fb.fizzbuzz(4),'4') | |
def test_fizz(self): | |
fb = self._makeOne() | |
self.assertEqual(fb.fizzbuzz(3),'fizz') | |
self.assertEqual(fb.fizzbuzz(6),'fizz') | |
self.assertEqual(fb.fizzbuzz(9), 'fizz') | |
def test_buzz(self): | |
fb = self._makeOne() | |
self.assertEqual(fb.fizzbuzz(5),'buzz') | |
self.assertEqual(fb.fizzbuzz(10), 'buzz') | |
self.assertEqual(fb.fizzbuzz(20), 'buzz') | |
def test_fizzbuzz(self): | |
fb = self._makeOne() | |
self.assertEqual(fb.fizzbuzz(15), 'fizzbuzz') | |
self.assertEqual(fb.fizzbuzz(30), 'fizzbuzz') | |
self.assertEqual(fb.fizzbuzz(45), 'fizzbuzz') | |
def test_list(self): | |
fb = self._makeOne() | |
self.assertEqual(fb.exibe_lista([1, 2, 3]), ['1', '2', 'fizz']) | |
self.assertEqual(fb.exibe_lista([1, 2, 3, 4, 5]), ['1', '2', 'fizz','4', 'buzz']) | |
self.assertEqual(fb.exibe_lista([9, 10, 11, 12, 13, 14, 15]), ['fizz', | |
'buzz', '11', 'fizz', '13', '14', 'fizzbuzz']) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment