Commits

Christoph Schindler committed 4394a6c

Ergebnis der ersten Dojo-Session.

Comments (0)

Files changed (2)

FizzBuzz/fizzbuzz.py

+
+def convert(number):
+	"""Convert a number into the FizzBuzz representation.
+	
+	
+	"""
+	if number % 15 == 0:
+		return 'FizzBuzz'
+	elif number % 3 == 0:
+		return 'Fizz'
+	elif number % 5 == 0:
+		return 'Buzz'
+		
+	return str(number)
+	
+def convert_range(start, stop):
+	if (start, stop) != (1, 101):
+		raise IndexError
+	#return map(convert, range(start, stop))
+	return [convert(i) for i in range(start, stop)]
+	
+def main():
+	return '\n'.join(convert_range(1,101))
+
+print "mein name: %s" % __name__
+
+if __name__ == '__main__':
+	print main()
+	
+import unittest
+import textwrap
+
+import fizzbuzz
+
+class TestFizzBuzz(unittest.TestCase):
+    def test_convert_number(self):
+        self.assertEqual(fizzbuzz.convert(1), '1')
+        self.assertEqual(fizzbuzz.convert(2), '2')
+
+    def test_convert_fizz(self):
+        self.assertEqual(fizzbuzz.convert(3), 'Fizz')
+        self.assertEqual(fizzbuzz.convert(21), 'Fizz')
+
+    def test_convert_buzz(self):
+        self.assertEqual(fizzbuzz.convert(5),'Buzz')
+        self.assertNotEqual(fizzbuzz.convert(76),'Buzz')
+        
+    def test_convert_fizzbuzz(self):
+        self.assertEqual(fizzbuzz.convert(15), 'FizzBuzz')
+
+    def test_convert_range(self):
+        self.assertTrue(set(['2', 'Fizz', '4', 'Buzz']).issubset(
+                            set(fizzbuzz.convert_range(1, 101))))
+        
+    def test_out_of_bounds(self):
+        self.assertRaises(IndexError, fizzbuzz.convert_range, 0, 1000)
+        
+    def test_main(self):
+        self.assertIn(textwrap.dedent("""
+                         1
+                         2
+                         Fizz
+                         4
+                         Buzz
+                         Fizz
+                         7
+                         8
+                         Fizz
+                         Buzz
+                         11
+                         Fizz
+                         13
+                         14
+                         FizzBuzz
+                         16
+                         17
+                         Fizz
+                         19
+                         Buzz""").strip(), fizzbuzz.main())
+        self.assertIn(textwrap.dedent("""
+                         94
+                         Buzz
+                         Fizz
+                         97
+                         98
+                         Fizz
+                         Buzz""".strip('\n')), fizzbuzz.main())
+if __name__ == '__main__':
+    unittest.main()