class FizzBuzz:
@property
def number(self):
return self._number
@number.setter
def number(self, value):
self._number = value
if self.is_num_or_str(3) and self.is_num_or_str(5):
self._number = 'fizz_buzz'
elif self.is_num_or_str(3):
self._number = 'fizz'
elif self.is_num_or_str(5):
self._number = 'buzz'
elif value < 0 or value > 100:
raise ValueError('number must between 0 ~ 100!')
@number.deleter
def number(self):
del self._number
def is_num_or_str(self, dividend):
return self.number % dividend == 0 or str(self.number).find(str(dividend)) >= 0
if __name__ == '__main__':
for i in range(1, 101):
fizz_buzz = FizzBuzz()
fizz_buzz.number = i
print(fizz_buzz.number)
import unittest
from fizzbuzz import FizzBuzz
class TestFizzBuzz(unittest.TestCase):
def test_by_3_or_5(self):
fz1 = FizzBuzz()
fz2 = FizzBuzz()
fz1.number = 1
fz2.number = 2
self.assertEqual(fz1.number, 1)
self.assertEqual(fz2.number, 2)
def test_by_3(self):
fz1 = FizzBuzz()
fz2 = FizzBuzz()
fz3 = FizzBuzz()
fz1.number = 3
fz2.number = 13
fz3.number = 33
self.assertEqual(fz1.number, 'fizz')
self.assertEqual(fz2.number, 'fizz')
self.assertEqual(fz3.number, 'fizz')
def test_by_5(self):
fz1 = FizzBuzz()
fz2 = FizzBuzz()
fz1.number = 5
fz2.number = 25
self.assertEqual(fz1.number, 'buzz')
self.assertEqual(fz2.number, 'buzz')
def test_by_3_and_5(self):
fz1 = FizzBuzz()
fz2 = FizzBuzz()
fz3 = FizzBuzz()
fz4 = FizzBuzz()
fz1.number = 15
fz2.number = 35
fz3.number = 51
fz4.number = 53
self.assertEqual(fz1.number, 'fizz_buzz')
self.assertEqual(fz2.number, 'fizz_buzz')
self.assertEqual(fz3.number, 'fizz_buzz')
self.assertEqual(fz3.number, 'fizz_buzz')
self.assertEqual(fz4.number, 'fizz_buzz')
if __name__ == '__main__':
unittest.main()