def test_display(self): r1 = Rectangle(2, 2) with patch('sys.stdout', new=StringIO()) as output: r1.display() self.assertEqual(output.getvalue().strip(), '##\n##') r2 = Rectangle(3, 2) with patch('sys.stdout', new=StringIO()) as output: r2.display() self.assertEqual(output.getvalue().strip(), '###\n###') r3 = Rectangle(2, 3, 2, 2) with patch('sys.stdout', new=StringIO()) as output: r3.display() self.assertEqual(output.getvalue(), '\n\n ##\n ##\n ##\n') r4 = Rectangle(3, 2, 1) with patch('sys.stdout', new=StringIO()) as output: r4.display() self.assertEqual(output.getvalue(), ' ###\n ###\n') r4 = Rectangle(3, 2, 0, 2) with patch('sys.stdout', new=StringIO()) as output: r4.display() self.assertEqual(output.getvalue(), '\n\n###\n###\n')
def test_display_without_x_y(self): """Display method test""" r = Rectangle(3, 2) height = 2 width = 3 expected = "" i = 0 while (i < height): j = 0 while (j < width): expected += "#" j += 1 expected += "\n" i += 1 with patch('sys.stdout', new=StringIO()) as fake_out: r.display() self.assertEqual(fake_out.getvalue(), expected)
def test_print(self): r = Rectangle(1, 1) f = io.StringIO() with redirect_stdout(f): r.display() s = "#\n" self.assertEqual(f.getvalue(), s) r.width = 3 r.height = 5 f = io.StringIO() with redirect_stdout(f): r.display() s = "\ ###\n\ ###\n\ ###\n\ ###\n\ ###\n\ " self.assertEqual(f.getvalue(), s) r = Rectangle(5, 6, 7, 8) f = io.StringIO() with redirect_stdout(f): r.display()
def test_rectangle_display(self): """test the display method""" f = io.StringIO() r1 = Rectangle(4, 5) with contextlib.redirect_stdout(f): r1.display() s = f.getvalue() res = "####\n####\n####\n####\n####\n" self.assertEqual(s, res) #test for wrong arguments with self.assertRaises(TypeError) as x: r1 = Rectangle(9, 6) r1.display(9) self.assertEqual( "display() takes 1 positional argument but 2 were given", str(x.exception)) #test for x and y display f = io.StringIO() r1 = Rectangle(2, 3, 2, 2) with contextlib.redirect_stdout(f): r1.display() s = f.getvalue() res = "\n\n ##\n ##\n ##\n" self.assertEqual(s, res)
def tests_m_display(self): """ ----------------------- METHOD: tests_m_display ----------------------- Description: Tests the display function that originally is supposed to represent a visual representation of the Rectangle object. """ import io from contextlib import redirect_stdout # Creating the initial r41 = Rectangle(6, 2) f = io.StringIO() with redirect_stdout(f): r41.display() s = f.getvalue() expected_out = s self.assertEqual(s, expected_out)
def test_display(self): """test display""" output = io.StringIO() td_std = Rectangle(3, 5) td_all = Rectangle(3, 5, 0, 1, 99) std_expected = "###\n###\n###\n###\n###\n" all_expected = "\n###\n###\n###\n###\n###\n" with self.subTest(): with contextlib.redirect_stdout(output): td_std.display() self.assertEqual(output.getvalue(), std_expected) output.truncate(0) output.seek(0) with self.subTest(): with contextlib.redirect_stdout(output): td_all.display() self.assertEqual(output.getvalue(), all_expected)
def test_rectangle_display(self): """ Test the output from rectangle when the method display is called """ r = Rectangle(2, 2) r1 = Rectangle(2, 3) r_out = "##\n" \ "##\n" r1_out = "##\n" \ "##\n" \ "##\n" try: r.display() self.assertEqual(sys.stdout.getvalue(), r_out) finally: sys.stdout.seek(0) sys.stdout.truncate(0) try: r1.display() self.assertEqual(sys.stdout.getvalue(), r1_out) finally: sys.stdout.seek(0) sys.stdout.truncate(0)
def test_rectangle_display(self, mock_stdout): r1 = Rectangle(4, 6) r1.display() print("---") r2 = Rectangle(2, 2) r2.display() print("---") r1 = Rectangle(2, 3, 2, 2) r1.display() print("---") r2 = Rectangle(3, 2, 1, 0) r2.display() print("---") r2 = Rectangle(1, 1, 0, 0) r2.display() self.assertEqual( mock_stdout.getvalue(), """#### #### #### #### #### #### --- ## ## --- ## ## ## --- ### ### --- # """)
def test_8_display_with_xy(self): """Test display method. Redirecting stdout to StringIO instance""" Base._Base__nb_objects = 0 R1 = Rectangle(2, 3, 2, 2) R1O = "\n" \ "\n" \ " ##\n" \ " ##\n" \ " ##\n" R2 = Rectangle(3, 2, 1, 0) R2O = " ###\n" \ " ###\n" try: R1.display() self.assertEqual(sys.stdout.getvalue(), R1O) finally: sys.stdout.seek(0) sys.stdout.truncate(0) try: R2.display() self.assertEqual(sys.stdout.getvalue(), R2O) finally: sys.stdout.seek(0) sys.stdout.truncate(0)
def test_display_1(self, mocked_print): r1 = Rectangle(1, 1) r1.display() self.assertEqual(sys.stdout.getvalue(), '#\n') r1 = Rectangle(2, 3, 2, 2) sys.stdout = StringIO() r1.display() self.assertEqual(sys.stdout.getvalue(), '\n\n ##\n ##\n ##\n') sys.stdout = StringIO() r2 = Rectangle(3, 2, 1, 0) r2.display() self.assertEqual(sys.stdout.getvalue(), ' ###\n ###\n')
def test_display(self): """display tests""" temp = sys.stdout sys.stdout = StringIO() r5 = Rectangle(2, 10) r5.display() out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = temp display = "##\n##\n##\n##\n##\n##\n##\n##\n##\n##\n" self.assertEqual(display, out) temp = sys.stdout sys.stdout = StringIO() r6 = Rectangle(1, 1) r6.display() out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = temp display = "#\n" self.assertEqual(display, out) temp = sys.stdout sys.stdout = StringIO() r7 = Rectangle(2, 3, 2, 2) r7.display() out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = temp display = "\n\n ##\n ##\n ##\n" self.assertEqual(display, out) temp = sys.stdout sys.stdout = StringIO() r8 = Rectangle(3, 2, 1, 0) r8.display() out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = temp display = " ###\n ###\n" self.assertEqual(display, out)
def test_display(self): """Test display method""" r1 = Rectangle(2, 2) expected_output = "##\n##\n" with patch('sys.stdout', new=StringIO()) as fake_out: r1.display() self.assertEqual(fake_out.getvalue(), expected_output) r1 = Rectangle(1, 1) expected_output = "#\n" with patch('sys.stdout', new=StringIO()) as fake_out: r1.display() self.assertEqual(fake_out.getvalue(), expected_output) r1 = Rectangle(4, 6) expected_output = "####\n####\n####\n####\n####\n####\n" with patch('sys.stdout', new=StringIO()) as fake_out: r1.display() self.assertEqual(fake_out.getvalue(), expected_output) r1 = Rectangle(2, 3, 2, 2) expected_output = "\n\n ##\n ##\n ##\n" with patch('sys.stdout', new=StringIO()) as fake_out: r1.display() self.assertEqual(fake_out.getvalue(), expected_output)
class TestDisplayMethod(unittest.TestCase): """Test display method for Rectangle class""" def setUp(self): self.r1 = Rectangle(4, 6) self.p1 = "####\n####\n####\n####\n####\n####\n" self.r2 = Rectangle(2, 2) self.p2 = "##\n##\n" self.r3 = Rectangle(2, 3, 2, 2) self.p3 = "\n\n ##\n ##\n ##\n" self.r4 = Rectangle(3, 2, 1, 0) self.p4 = " ###\n ###\n" def test_display_1(self): f = io.StringIO() with contextlib.redirect_stdout(f): self.r1.display() ret = f.getvalue() self.assertEqual(self.p1, ret) def test_display_2(self): f = io.StringIO() with contextlib.redirect_stdout(f): self.r2.display() ret = f.getvalue() self.assertEqual(self.p2, ret) def test_display_offset_3(self): f = io.StringIO() with contextlib.redirect_stdout(f): self.r3.display() ret = f.getvalue() self.assertEqual(self.p3, ret) def test_display_offset_4(self): f = io.StringIO() with contextlib.redirect_stdout(f): self.r4.display() ret = f.getvalue() self.assertEqual(self.p4, ret)
def test_display(self): """test display method""" r1 = Rectangle(1, 2) r2 = Rectangle(4, 3) r3 = Rectangle(2, 3, 2, 2) r4 = Rectangle(3, 2, 1, 0) r1_expect_display = "#\n#\n" r2_expect_display = "####\n####\n####\n" r3_expect_display = "\n\n ##\n ##\n ##\n" r4_expect_display = " ###\n ###\n" with patch("sys.stdout", new=StringIO()) as fake_out: r1.display() self.assertEqual(fake_out.getvalue(), r1_expect_display) with patch("sys.stdout", new=StringIO()) as fake_out: r2.display() self.assertEqual(fake_out.getvalue(), r2_expect_display) with patch("sys.stdout", new=StringIO()) as fake_out: r3.display() self.assertEqual(fake_out.getvalue(), r3_expect_display) with patch("sys.stdout", new=StringIO()) as fake_out: r4.display() self.assertEqual(fake_out.getvalue(), r4_expect_display)
def test_display_x_y(self): """Tests rectangle with x and y""" output = StringIO() sys.stdout = output r = Rectangle(3, 2, 1, 0) r.display() r = Rectangle(1, 1) self.out = StringIO() sys.stdout = self.out r.display() self.assertEqual(self.out.getvalue(), '#\n') self.out = StringIO() sys.stdout = self.out r = Rectangle(2, 2) r.display() self.assertEqual(self.out.getvalue(), '##\n##\n') self.out = StringIO() sys.stdout = self.out r = Rectangle(1, 1, 1, 1) r.display() self.assertEqual(self.out.getvalue(), '\n #\n') sys.stdout = sys.__stdout__ self.assertEqual(output.getvalue(), " ###\n ###\n")
def test_display_with_coordinates(self): """Test display with x and y""" r = Rectangle(10, 2, 5) with io.StringIO() as buf, redirect_stdout(buf): r.display() output = buf.getvalue() self.assertEqual(output, (" " * 5 + "#" * 10 + "\n") * 2) r = Rectangle(4, 15, 1, 4, 99) with io.StringIO() as buf, redirect_stdout(buf): r.display() output = buf.getvalue() self.assertEqual(output, (("\n" * 4) + (" " * 1 + "#" * 4 + "\n") * 15)) r = Rectangle(5, 3, 0, 0, 1) with io.StringIO() as buf, redirect_stdout(buf): r.display() output = buf.getvalue() self.assertEqual(output, (" " * 0 + "#" * 5 + "\n") * 3)
def testdisplay(self): with patch('sys.stdout', new=StringIO()) as salida: r = Rectangle(2, 3) r.display() st = "##\n##\n##\n" self.assertEqual(salida.getvalue(), st) with patch('sys.stdout', new=StringIO()) as salida: r = Rectangle(2, 3, 2) r.display() st = " ##\n ##\n ##\n" self.assertEqual(salida.getvalue(), st) with patch('sys.stdout', new=StringIO()) as salida: r = Rectangle(2, 3, 2, 2) r.display() st = "\n\n ##\n ##\n ##\n" self.assertEqual(salida.getvalue(), st)
def test_display(self): """Test the display method""" r = Rectangle(1, 1) self.out = StringIO() sys.stdout = self.out r.display() self.assertEqual(self.out.getvalue(), '#\n') self.out = StringIO() sys.stdout = self.out r = Rectangle(2, 2) r.display() self.assertEqual(self.out.getvalue(), '##\n##\n') self.out = StringIO() sys.stdout = self.out r = Rectangle(1, 1, 1, 1) r.display() self.assertEqual(self.out.getvalue(), '\n #\n') sys.stdout = sys.__stdout__
def test_display_4(self): """ Test string printed """ r1 = Rectangle(3, 2) res = "###\n###\n" with patch('sys.stdout', new=StringIO()) as str_out: r1.display() self.assertEqual(str_out.getvalue(), res) r1.x = 4 res = " ###\n ###\n" with patch('sys.stdout', new=StringIO()) as str_out: r1.display() self.assertEqual(str_out.getvalue(), res) r1.y = 2 res = "\n\n ###\n ###\n" with patch('sys.stdout', new=StringIO()) as str_out: r1.display() self.assertEqual(str_out.getvalue(), res)
def test_display(self): a = Rectangle(2, 4, 3, 4) s = '\n\n\n\n ##\n ##\n ##\n ##\n' f = io.StringIO() with redirect_stdout(f): a.display() self.assertEqual(f.getvalue(), s) b = Rectangle(2, 4) r = '##\n##\n##\n##\n' d = io.StringIO() with redirect_stdout(d): b.display() self.assertEqual(d.getvalue(), r) b = Rectangle(2, 4, 1) r = ' ##\n ##\n ##\n ##\n' d = io.StringIO() with redirect_stdout(d): b.display() self.assertEqual(d.getvalue(), r)
def test_coord_display(self): r = Rectangle(2, 3, 2, 2) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() r_result = "\n\n ##\n ##\n ##\n" self.assertEqual(f.getvalue(), r_result) r = Rectangle(3, 2, 1, 0) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() r_result = " ###\n ###\n" self.assertEqual(f.getvalue(), r_result) r = Rectangle(3, 2, 0, 0) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() r_result = "###\n###\n" self.assertEqual(f.getvalue(), r_result)
def test_display_simple(self): """Tests display() method output.""" r = Rectangle(1, 1) f = io.StringIO() with redirect_stdout(f): r.display() s = "#\n" self.assertEqual(f.getvalue(), s) r.width = 2 r.height = 2 f = io.StringIO() with redirect_stdout(f): r.display() s = "##\n##\n" self.assertEqual(f.getvalue(), s) r = Rectangle(2, 2, 2, 2) f = io.StringIO() with redirect_stdout(f): r.display() s = "\n\n ##\n ##\n" self.assertEqual(f.getvalue(), s)
def test_display(self): r = Rectangle(4, 6) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() four_by_six = "####\n####\n####\n####\n####\n####\n" self.assertEqual(s, four_by_six) r = Rectangle(2, 4) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() two_by_four = "##\n##\n##\n##\n" self.assertEqual(s, two_by_four) r = Rectangle(1, 1) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() one_by_one = "#\n" self.assertEqual(s, one_by_one)
def test_display(self): """ Checks display function """ r = Rectangle(2, 2) stout = StringIO() with redirect_stdout(stout): r.display() out = stout.getvalue() self.assertEqual(out, ("#" * 2 + "\n") * 2) r2 = Rectangle(5, 10) stout = StringIO() with redirect_stdout(stout): r2.display() out = stout.getvalue() self.assertEqual(out, ("#" * 5 + "\n") * 10) r3 = Rectangle(5, 2, 1, 2) stout = StringIO() with redirect_stdout(stout): r3.display() out = stout.getvalue() self.assertEqual(out, "\n" * 2 + (" " * 1 + "#" * 5 + "\n") * 2)
def test_rectangle26(self): r = Rectangle(4, 6) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() f6 = "####\n####\n####\n####\n####\n####\n" self.assertEqual(s, f6) r = Rectangle(2, 4) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() t = "##\n##\n##\n##\n" self.assertEqual(s, t) r = Rectangle(1, 1) f = io.StringIO() with contextlib.redirect_stdout(f): r.display() s = f.getvalue() o = "#\n" self.assertEqual(s, o)
def test_display(self): """Test display with valid arguments""" f = io.StringIO() s = ('#' * 4 + '\n') * 3 r1 = Rectangle(4, 3) with redirect_stdout(f): r1.display() self.assertEqual(f.getvalue(), s) f = io.StringIO() s = ('\n' * 2) + (" " * 2 + '#' * 2 + '\n') * 3 r1 = Rectangle(2, 3, 2, 2) with redirect_stdout(f): r1.display() self.assertEqual(f.getvalue(), s) f = io.StringIO() s = ('\n' * 0) + (" " * 2 + '#' * 2 + '\n') * 3 r1 = Rectangle(2, 3, 2) with redirect_stdout(f): r1.display() self.assertEqual(f.getvalue(), s)
def test_display_1(self): """Task 7. Display #1""" # Normal use: no args to display, x given r13 = Rectangle(2, 2, 2) self.assertEqual(r13.id, 1) with contextlib.redirect_stdout(None): r13.display() self.assertEqual(r13._Rectangle__display, ' ##\n ##') # Normal use: no args to display, only y given (via update) r14 = Rectangle(2, 2) self.assertEqual(r14.id, 2) self.assertEqual(r14.y, 0) r14.update(y=2) self.assertEqual(r14.y, 2) with contextlib.redirect_stdout(None): r14.display() self.assertEqual(r14._Rectangle__display, '\n\n##\n##') # Normal use: no args to display, x/y given r15 = Rectangle(2, 2, 2, 2) self.assertEqual(r15.id, 3) with contextlib.redirect_stdout(None): r15.display() self.assertEqual(r15._Rectangle__display, '\n\n ##\n ##')
#!/usr/bin/python3 """ 6-main """ from models.rectangle import Rectangle if __name__ == "__main__": r1 = Rectangle(2, 3, 2, 2) r1.display() print("---") r2 = Rectangle(3, 2, 1, 0) r2.display() print("---") r2 = Rectangle(5, 5, 0, 5) r2.display()
class RectangleTest(unittest.TestCase): def setUp(self): self.r1 = Rectangle(2, 4) self.r2 = Rectangle(5, 10, 2) self.r3 = Rectangle(3, 6, 2, 2) self.r4 = Rectangle(10, 7, 1, 1, 7) def test_id(self): self.assertEqual(self.r4.id, 7) def test_width(self): self.assertEqual(self.r1.width, 2) self.assertEqual(self.r2.width, 5) self.assertEqual(self.r3.width, 3) self.assertEqual(self.r4.width, 10) def test_height(self): self.assertEqual(self.r1.height, 4) self.assertEqual(self.r2.height, 10) self.assertEqual(self.r3.height, 6) self.assertEqual(self.r4.height, 7) def test_x(self): self.assertEqual(self.r1.x, 0) self.assertEqual(self.r2.x, 2) self.assertEqual(self.r3.x, 2) self.assertEqual(self.r4.x, 1) def test_y(self): self.assertEqual(self.r1.y, 0) self.assertEqual(self.r2.y, 0) self.assertEqual(self.r3.y, 2) self.assertEqual(self.r4.y, 1) def test_width_missing(self): with self.assertRaises(TypeError): r10 = Rectangle() def test_width_TypeError(self): with self.assertRaisesRegex(TypeError, "width must be an integer"): r10 = Rectangle('five', 5) def test_width_ValueError(self): with self.assertRaisesRegex(ValueError, "width must be > 0"): r10 = Rectangle(-5, 5) with self.assertRaisesRegex(ValueError, "width must be > 0"): r10 = Rectangle(0, 2) def test_height_missing(self): with self.assertRaises(TypeError): r10 = Rectangle(2, ) def test_height_TypeError(self): with self.assertRaisesRegex(TypeError, "height must be an integer"): r10 = Rectangle(5, ['five']) def test_height_ValueError(self): with self.assertRaisesRegex(ValueError, "height must be > 0"): r10 = Rectangle(5, -5) with self.assertRaisesRegex(ValueError, "height must be > 0"): r10 = Rectangle(2, 0) def test_x_TypeError(self): with self.assertRaisesRegex(TypeError, "x must be an integer"): r10 = Rectangle(2, 3, "x") with self.assertRaisesRegex(TypeError, "x must be an integer"): r10 = Rectangle(1, 1, [[12]]) def test_x_ValueError(self): with self.assertRaisesRegex(ValueError, "x must be >= 0"): r10 = Rectangle(2, 3, -5) def test_y_TypeError(self): with self.assertRaisesRegex(TypeError, "y must be an integer"): r10 = Rectangle(2, 3, 1, (12, 1)) with self.assertRaisesRegex(TypeError, "y must be an integer"): r = Rectangle(2, 3, 1, [{'keykey': 'haha'}]) def test_y_ValueError(self): with self.assertRaisesRegex(ValueError, "y must be >= 0"): r10 = Rectangle(2, 3, 5, -10) def test_area(self): self.assertEqual(self.r1.area(), 8) self.assertEqual(self.r2.area(), 50) self.assertEqual(self.r3.area(), 18) self.assertEqual(self.r4.area(), 70) def test_area_unnecessary_arg(self): """too many arg in area""" with self.assertRaises(TypeError): self.r1.area(12) def display(self): """Display normal""" r = Rectangle(1, 1, 1, 1, 1) var = StringIO() sys.stdout = var r.display() self.assertEquals(var.getvalue(), '#\n') sys.stdout = sys.__stdout__ def test_display_too_many_args(self): """Display with too many args""" with self.assertRaises(TypeError): self.r1.display(1) def update_arg(self): """normal condition for updating""" self.assertEqual(str(r4), '[Rectangle] (7) 1/1 - 10/7') r4.update(12) self.assertEqual(str(r4), '[Rectangle] (12) 1/1 - 10/7') r4.update(12, 5) self.assertEqual(str(r4), '[Rectangle] (12) 1/1 - 5/7') r4.update(12, 5, 15) self.assertEqual(str(r4), '[Rectangle] (12) 1/1 - 5/15') r4.update(12, 5, 15, 3) self.assertEqual(str(r4), '[Rectangle] (12) 3/1 - 5/7') r4.update(12, 5, 15, 3, 9) self.assertEqual(str(r4), '[Rectangle] (12) 3/9 - 5/7') def test_update_args_empty(self): """update empty parameter with arg update""" r = Rectangle(4, 2, 1, 0, 6) r.update() self.assertEqual(str(r), '[Rectangle] (6) 1/0 - 4/2') def test_update_args_TypeError(self): """typeerror with arg update""" r = Rectangle(4, 2, 1, 0, 6) with self.assertRaisesRegex(TypeError, "width must be an integer"): r.update(1, "bobobum") with self.assertRaisesRegex(TypeError, "height must be an integer"): r.update(2, 3, "mama") with self.assertRaisesRegex(TypeError, "x must be an integer"): r.update(2, 3, 1, [['34']]) with self.assertRaisesRegex(TypeError, "y must be an integer"): r.update(1, 1, 1, 1, {"potato": "pohtato"}) def test_update_args_ValueError(self): """value error w/arg update""" r = Rectangle(4, 2, 1, 0, 6) with self.assertRaisesRegex(ValueError, "width must be > 0"): r.update(1, 0) with self.assertRaisesRegex(ValueError, "width must be > 0"): r.update(2, -7) with self.assertRaisesRegex(ValueError, "height must be > 0"): r.update(3, 4, 0) with self.assertRaisesRegex(ValueError, "height must be > 0"): r.update(5, 6, -7) with self.assertRaisesRegex(ValueError, "x must be >= 0"): r.update(7, 8, 9, -7) with self.assertRaisesRegex(ValueError, "y must be >= 0"): r.update(10, 1, 2, 3, -7) def test_kwargs(self): """test kwarg works in normal conditions""" r = Rectangle(4, 2, 1, 0, 8) r.update(width=30) self.assertEqual(str(r), '[Rectangle] (8) 1/0 - 30/2') r.update(height=20) self.assertEqual(str(r), '[Rectangle] (8) 1/0 - 30/20') r.update(y=15, x=5) self.assertEqual(str(r), '[Rectangle] (8) 5/15 - 30/20') r.update(id=9) self.assertEqual(str(r), '[Rectangle] (9) 5/15 - 30/20') def test_kwargs_new_key(self): """non-existant key w/kwarg in instance""" r10 = Rectangle(4, 2, 1, 0, 8) r10.update(jigglypuff=30) self.assertEqual(str(r10), '[Rectangle] (8) 1/0 - 4/2') self.assertEqual(r10.jigglypuff, 30) def test_update_kwargs_TypeError(self): """type error for kwargs update""" r = Rectangle(4, 2, 1, 0, 6) with self.assertRaisesRegex(TypeError, "width must be an integer"): r.update(1, "bobobum") with self.assertRaisesRegex(TypeError, "height must be an integer"): r.update(2, 3, "nahnahnah") with self.assertRaisesRegex(TypeError, "x must be an integer"): r.update(2, 3, 1, [['34']]) with self.assertRaisesRegex(TypeError, "y must be an integer"): r.update(1, 1, 1, 1, {"potato": "pohtato"}) def test_update_kwargs_ValueError(self): """value error for kwargs update""" r = Rectangle(4, 2, 1, 0, 6) with self.assertRaisesRegex(ValueError, "width must be > 0"): r.update(1, 0) with self.assertRaisesRegex(ValueError, "width must be > 0"): r.update(2, -7) with self.assertRaisesRegex(ValueError, "height must be > 0"): r.update(3, 4, 0) with self.assertRaisesRegex(ValueError, "height must be > 0"): r.update(5, 6, -7) with self.assertRaisesRegex(ValueError, "x must be >= 0"): r.update(7, 8, 9, -7) with self.assertRaisesRegex(ValueError, "y must be >= 0"): r.update(10, 1, 2, 3, -7) def to_dict(self): """check that to_dictionary work""" d1 = self.r1.to_dictionary() self.assertEqual({ "id": 1, "width": 2, "height": 4, "x": 0, "y": 0 }, d1) d2 = self.r2.to_dictionary() self.assertEqual({ "id": 2, "width": 5, "height": 10, "x": 2, "y": 0 }, d2) d3 = self.r3.to_dictionary() self.assertEqual({ "id": 3, "width": 3, "height": 6, "x": 2, "y": 2 }, d3) d4 = self.r4.to_dictionary() self.assertEqual({ "id": 7, "width": 10, "height": 7, "x": 1, "y": 1 }, d4) self.assertTrue(isinstance(d1, dict)) def create_rect(self): """test normal use of create""" rr = {"id": 2, "width": 1, "height": 1, "x": 1, "y": 1} new_r = Rectangle.create(**rr) self.assertEqual(str(new_r), "[Rectangle] (2) 1/1 - 1/1") def to_dictionary_update(self): """check update for dict works""" r5 = (100, 120, 40, 10, 42) d5 = self.r5.to_dictionary() self.assertEqual( { "id": 42, "width": 100, "height": 120, "x": 40, "y": 10 }, d5) r = Rectangle(11, 22, 33, 44, 55) r.update(**d5) self.assertEqual(r.__dict__, d5) def test_save_to_file(self): """save_to_file under normal conditions""" r11 = Rectangle(1, 2, 3, 4, 5) r12 = Rectangle(11, 22, 33, 44, 55) pobj = [r11, r12] Rectangle.save_to_file(pobj) with open("Rectangle.json", "r", encoding='utf-8') as f: ls = [r11.to_dictionary(), r12.to_dictionary()] self.assertEqual(json.dumps(ls), f.read()) def test_save_to_file_empty(self): """save_to_file with empty list""" tehe = [] Rectangle.save_to_file(tehe) with open("Rectangle.json", "r", encoding='utf-8') as f: self.assertEqual("[]", f.read()) def test_load_from_file(self): """load_from_file under normal conditions""" r01 = Rectangle(1, 2, 3, 4, 5) r02 = Rectangle(11, 22, 33, 44, 55) pobj = [r01, r02] Rectangle.save_to_file(pobj) hiya = Rectangle.load_from_file() self.assertTrue(isinstance(hiya, list))
class TestRectangle(unittest.TestCase): """Rectangle class tests""" def setUp(self): """Base classes to the tests""" b._Base__nb_objects = 0 self.o0 = Rectangle(10, 2) self.o1 = Rectangle(2, 10) self.o2 = Rectangle(10, 2, 0, 0, 12) self.o3 = Rectangle(4, 6) self.o4 = Rectangle(2, 2) b._Base__nb_objects = 0 self.o5 = Rectangle(4, 6, 2, 1, 12) self.o6 = Rectangle(5, 5, 1) self.o7 = Rectangle(2, 3, 2, 2) self.o8 = Rectangle(3, 2, 1, 0) b._Base__nb_objects = 0 self.o9 = Rectangle(10, 10, 10, 10) b._Base__nb_objects = 0 self.o10 = Rectangle(10, 10, 10, 10) b._Base__nb_objects = 0 self.o11 = Rectangle(10, 2, 1, 9) self.o12 = Rectangle(1, 1) b._Base__nb_objects = 0 self.o13 = Rectangle(10, 7, 2, 8) self.o14 = Rectangle(2, 4) def test_module_rectangle_doc(self): """Test to verify the module doc""" self.assertTrue(len(r.__doc__) > 10) def test_class_rectangle_doc(self): """Test to verify the class doc""" self.assertTrue(len(Rectangle.__doc__) > 10) def test_init_rectangle_doc(self): """Test to verify the constructor doc""" self.assertTrue(len(Rectangle.__init__.__doc__) > 10) def test_rectangle_id1(self): """Test to verify the id""" self.assertEqual(self.o0.id, 1) def test_rectangle_id2(self): """Test to verify the id""" self.assertEqual(self.o1.id, 2) def test_rectangle_id3(self): """Test to verify the id""" self.assertEqual(self.o2.id, 12) def test_type_error_width(self): """Test to verify a not number exception""" with self.assertRaises(TypeError) as er: o = Rectangle("hello", 2) self.assertEqual("width must be an integer", str(er.exception)) def test_type_error_heigth(self): """Test to verify a not number exception""" with self.assertRaises(TypeError) as er: o = Rectangle(10, "world") self.assertEqual("height must be an integer", str(er.exception)) def test_type_error_width_zero(self): """Test to verify a 0 exception""" with self.assertRaises(ValueError) as er: o = Rectangle(0, 2) self.assertEqual("width must be > 0", str(er.exception)) def test_type_error_heigth_zero(self): """Test to verify a 0 exception""" with self.assertRaises(ValueError) as er: o = Rectangle(2, 0) self.assertEqual("height must be > 0", str(er.exception)) def test_type_error_x_zero(self): """Test to verify a 0 exception""" with self.assertRaises(ValueError) as er: o = Rectangle(10, 10, -1, 2) self.assertEqual("x must be >= 0", str(er.exception)) def test_type_error_y_zero(self): """Test to verify a 0 exception""" with self.assertRaises(ValueError) as er: o = Rectangle(10, 10, 2, -2) self.assertEqual("y must be >= 0", str(er.exception)) def test_area_doc(self): """Test to verify the area function doc""" self.assertTrue(len(r.Rectangle.area.__doc__) > 10) def test_area1(self): """Test to the area function""" o = r.Rectangle(3, 2) self.assertEqual(o.area(), 6) def test_area2(self): """Test to the area function""" o = r.Rectangle(2, 10) self.assertEqual(o.area(), 20) def test_area3(self): """Test to the area function""" o = r.Rectangle(8, 7, 0, 0, 12) self.assertEqual(o.area(), 56) def test_display1(self): """Test to the display function""" cO = io.StringIO() sys.stdout = cO self.o3.display() sys.stdout = sys.__stdout__ self.assertEqual(cO.getvalue(), '####\n####\n####\n####\n####\n####\n') def test_display2(self): """Test to the display function""" cO = io.StringIO() sys.stdout = cO self.o4.display() sys.stdout = sys.__stdout__ self.assertEqual(cO.getvalue(), '##\n##\n') def test_string1(self): """Test to the string format of the class""" self.assertEqual(str(self.o5), "[Rectangle] (12) 2/1 - 4/6") def test_string2(self): """Test to the string format of the class""" self.assertEqual(str(self.o6), "[Rectangle] (1) 1/0 - 5/5") def test_display3(self): """Test to the display function""" cO = io.StringIO() sys.stdout = cO self.o7.display() sys.stdout = sys.__stdout__ self.assertEqual(cO.getvalue(), '\n\n ##\n ##\n ##\n') def test_display4(self): """Test to the display function""" cO = io.StringIO() sys.stdout = cO self.o8.display() sys.stdout = sys.__stdout__ self.assertEqual(cO.getvalue(), ' ###\n ###\n') def test_string_update0(self): """Test to the update function""" self.assertEqual(str(self.o9), "[Rectangle] (1) 10/10 - 10/10") def test_string_update1(self): """Test to the update function""" self.o9.update(89) self.assertEqual(str(self.o9), "[Rectangle] (89) 10/10 - 10/10") def test_string_update2(self): """Test to the update function""" self.o9.update(89, 2) self.assertEqual(str(self.o9), "[Rectangle] (89) 10/10 - 2/10") def test_string_update3(self): """Test to the update function""" self.o9.update(89, 2, 3) self.assertEqual(str(self.o9), "[Rectangle] (89) 10/10 - 2/3") def test_string_update4(self): """Test to the update function""" self.o9.update(89, 2, 3, 4) self.assertEqual(str(self.o9), "[Rectangle] (89) 4/10 - 2/3") def test_string_update5(self): """Test to the update function""" self.o9.update(89, 2, 3, 4, 5) self.assertEqual(str(self.o9), "[Rectangle] (89) 4/5 - 2/3") def test_string_update6(self): """Test to the update function""" self.assertEqual(str(self.o10), "[Rectangle] (1) 10/10 - 10/10") def test_string_update7(self): """Test to the update function""" self.o10.update(height=1) self.assertEqual(str(self.o10), "[Rectangle] (1) 10/10 - 10/1") def test_dictionary11(self): """Test to the string format of the class""" self.assertEqual(str(self.o11), "[Rectangle] (1) 1/9 - 10/2") def test_dictionary_object11(self): """Test to the string format of the class""" self.assertEqual(str(self.o11), "[Rectangle] (1) 1/9 - 10/2") def test_dictionary_ob11(self): """Test to the to_dictionary function""" self.assertEqual( str(self.o11.to_dictionary()), "{'x': 1, 'y': 9, 'id': 1, \ 'height': 2, 'width': 10}") def test_dictionary_type11(self): """Test to the to_dictionary function""" self.assertTrue(isinstance(self.o11.to_dictionary(), dict)) def test_dictionary_object12(self): """Test to the string format of the class""" self.assertEqual(str(self.o12), "[Rectangle] (2) 0/0 - 1/1") def test_dictionary_object12_update(self): """Test to the to_dictionary function, and update and the string format of the class. """ self.o12.update(**self.o11.to_dictionary()) self.assertEqual(str(self.o12), "[Rectangle] (1) 1/9 - 10/2") def test_dictionary12(self): """Test to verify if both objects are the same""" self.assertFalse(self.o11 == self.o12)