In [ ]:
"""Create Rectangle and Circle classes conforming to the 2D Shape interface"""
from math import pi
import unittest
class Rectangle:
"""Implement the Rectangle class"""
def __init__(self, x, y):
"""Create a Rectangle with an x and y point coming from the origin"""
pass
def area(self):
"""Return the Rectangle's area"""
pass
def perimeter(self):
"""Return the Rectangle's perimeter"""
pass
class Circle:
"""Implement the Circle class"""
def __init__(self, r):
"""Create a circle with a radius from the origin"""
pass
def area(self):
"""Return the Circle's area"""
pass
def perimeter(self):
"""Return the Circle's perimeter"""
pass
# Tests below
# You can look, but don't touch!
class RectangleTests(unittest.TestCase):
""" Test the Rectangle behavior """
def test_rectangle_area(self):
r = Rectangle(3, 2)
self.assertEqual(r.area(), 6)
def test_rectangle_area_non_negative(self):
r = Rectangle(3, -4)
self.assertEqual(r.area(), 12, "You didn't account for 'negative area'!")
def test_rectangle_perimeter(self):
r = Rectangle(3, 7)
self.assertEqual(r.perimeter(), 20)
def test_rectangle_perimeter_non_negative(self):
r = Rectangle(-10, 4)
self.assertEqual(r.perimeter(), 28, "You didn't account for 'negative perimeter'!")
class CircleTest(unittest.TestCase):
def test_circle_area(self):
c = Circle(2)
self.assertEqual(c.area(), pi*4)
def test_circle_perimeter(self):
c = Circle(3)
self.assertEqual(c.perimeter(), 6*pi)
def test_circle_perimeter_non_negative(self):
c = Circle(-4)
self.assertEqual(c.perimeter(), 8*pi, "You didn't account for 'negative perimeter'!")
rec_suite = unittest.TestLoader().loadTestsFromTestCase(RectangleTests)
circle_suite = unittest.TestLoader().loadTestsFromTestCase(CircleTest)
unittest.TextTestRunner().run(rec_suite)
unittest.TextTestRunner().run(circle_suite)
In [ ]: