class TestRectangle(unittest.TestCase): def setUp(self): self.rec1 = Rectangle(1, 2, 5, 4) self.rec2 = Rectangle(0, 2, 2, 4) self.rec3 = Rectangle(-1, -1, 1, 1) def test_str(self): self.assertEqual( str(self.rec1), "[(1, 2), (5, 4)]", ) self.assertEqual( str(self.rec3), "[(-1, -1), (1, 1)]", ) def test_repr(self): self.assertEqual(repr(self.rec1), "Rectangle(1, 2, 5, 4)") self.assertEqual(repr(self.rec3), "Rectangle(-1, -1, 1, 1)") def test_eq(self): self.assertFalse(self.rec1 == self.rec2) self.assertTrue(self.rec3 == self.rec3) self.assertTrue(self.rec2 == self.rec2) def test_ne(self): self.assertTrue(self.rec1 != self.rec2) self.assertFalse(self.rec3 != self.rec3) self.assertFalse(self.rec2 != self.rec2) def test_center(self): self.assertEqual(self.rec1.center(), Point(3, 3)) self.assertEqual(self.rec2.center(), Point(1, 3)) def test_area(self): self.assertEqual(self.rec1.area(), 8) self.assertEqual(self.rec2.area(), 4) def test_move(self): self.assertEqual(self.rec1.move(1, 1), Rectangle(2, 3, 6, 5)) self.assertEqual(self.rec2.move(0, -1), Rectangle(0, 1, 2, 3)) def test_intersection(self): self.assertEqual(self.rec1.intersection(self.rec2), Rectangle(1, 2, 2, 4)) def test_cover(self): self.assertEqual(self.rec1.cover(self.rec2), Rectangle(0, 2, 5, 4)) self.assertEqual(self.rec1.cover(self.rec3), Rectangle(-1, -1, 5, 4)) def test_make4(self): self.assertEqual(self.rec1.make4(), [ Rectangle(1, 2, 3, 3), Rectangle(1, 3, 3, 4), Rectangle(3, 2, 5, 3), Rectangle(3, 3, 5, 4) ]) def tearDown(self): pass
class TestRectangle(unittest.TestCase): def setUp(self): self.rec1 = Rectangle(1, 2, 5, 4) self.rec2 = Rectangle(2, 0, 0, 2) self.rec3 = Rectangle(1, 1, -1, -1) self.rec4 = Rectangle(6, 4, 1, 2) def test__init__(self): self.assertEqual(self.rec1, Rectangle(1, 2, 5, 4)) self.assertEqual(self.rec2, Rectangle(2, 0, 0, 2)) def test__str__(self): self.assertEqual(str(self.rec1), "[(1, 2), (5, 4)]") self.assertEqual(str(self.rec2), "[(2, 0), (0, 2)]") def test__repr__(self): self.assertEqual(repr(self.rec1), "Rectangle[(1, 2), (5, 4)]") self.assertEqual(repr(self.rec2), "Rectangle[(2, 0), (0, 2)]") def test__eq__(self): self.assertFalse(self.rec1 == self.rec2) self.assertTrue(self.rec1 == self.rec1) def test__ne__(self): self.assertTrue(self.rec1 != self.rec2) self.assertFalse(self.rec1 != self.rec1) def test__center(self): self.assertEqual(self.rec1.center(), Point(3, 3)) self.assertEqual(self.rec2.center(), Point(1, 1)) def test__area(self): self.assertEqual(self.rec1.area(), 8) self.assertEqual(self.rec2.area(), 4) def test__move(self): self.assertEqual(self.rec1.move(3, 1), Rectangle(4, 3, 8, 5)) self.assertEqual(self.rec2.move(-1, 2), Rectangle(1, 2, -1, 4)) def tearDown(self): pass
class TestRectangles(unittest.TestCase): def setUp(self): self.one = Rectangle(1, 2, 3, 4) self.two = Rectangle(2, 2, 2, 2) self.three = Rectangle(0, 0, 2, 2) def test_center(self): self.assertEqual(self.one.center(), '(2.0, 3.0)') self.assertEqual(self.two.center(), '(2.0, 2.0)') self.assertEqual(self.three.center(), '(1.0, 1.0)') def test_area(self): self.assertEquals(self.one.area(), 4) self.assertEquals(self.two.area(), 0) self.assertEquals(self.three.area(), 4) def test_move(self): self.assertEquals(self.one.move(1, 1), '[(2, 3),(4, 5)]') self.assertEquals(self.two.move(0, 0), '[(2, 2),(2, 2)]') self.assertEquals(self.three.move(-1, -1), '[(-1, -1),(1, 1)]') def test_intersection(self): self.assertEqual(self.three.intersection(Rectangle(-1, -1, 1, 1)), Rectangle(0, 0, 1, 1)) self.assertEqual(self.three.intersection(Rectangle(1, 1, 3, 3)), Rectangle(1, 1, 2, 2)) self.assertEqual(self.three.intersection(Rectangle(0, 1, 2, 3)), Rectangle(0, 1, 2, 2)) def test_cover(self): self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(0, 2, 1, 3)), Rectangle(0, 0, 1, 3)) self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(2, 0, 3, 1)), Rectangle(0, 0, 3, 1)) self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(0, -2, 1, -1)), Rectangle(0, -2, 1, 1)) self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(-2, 0, -1, 1)), Rectangle(-2, 0, 1, 1)) self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(2, 2, 3, 3)), Rectangle(0, 0, 3, 3)) self.assertEquals(Rectangle(0, 0, 1, 1).cover(Rectangle(-3, -3, -2, -2)), Rectangle(-3, -3, 1, 1)) def text_make4(self): self.assertEquals(self.rec_two.make4(), [Rectangle(2, 2, 1, 1), Rectangle(1, 1, 2, 2), Rectangle(2, 1, 1, 2), Rectangle(1, 2, 2, 1)])
from rectangles import Rectangle rect1 = Rectangle(-2, 1, 4, -3) rect2 = Rectangle(4, -2, 0, 2) rect1.__str__() rect2.__repr__() print rect1.eq(rect2) print rect2.ne(rect1) print str(rect1.center()) print rect2.area() rect1.move(2, -2) rect1.__str__()
def test_area(self): rect = Rectangle(8, 3) self.assertEqual(24, rect.area())