def test_width_value(self): s0 = Square(1) self.assertEqual(s0.width, 1) Base._Base__nb_objects = 0
def test_width_zero(self): ''' Testing with negative int ''' with self.assertRaises(ValueError): sq = Square(0, 5)
def test_width_float(self): ''' Testing for other than int ''' with self.assertRaises(TypeError): sq = Square(1.07, 5)
def test_y_string(self): ''' Testing for other than int ''' with self.assertRaises(TypeError): sq = Square(1, 7, "46")
def test_y_list(self): ''' Testing for other than int ''' with self.assertRaises(TypeError): sq = Square(1, 7, [10, 6])
def test_square(self): """Test if Square is instnace of superclass""" s = Square(3) self.assertTrue(isinstance(s, Base))
def test_23_ret_dict_18(self): """test update instance 4""" Base._Base__nb_objects = 0 s3 = Square(10, 2, 1) s3_dictionary = s3.to_dictionary() self.assertEqual(type(s3_dictionary), dict)
def testarea(self): """Tests rectangle area function""" global idct a = Square(4) idct += 1 self.assertEqual(a.area(), 16)
def testarea2(self): """tests rectangle area function""" a = Square(4, 100, 20, 10) self.assertEqual(a.area(), 16)
def testzerosize(self): """Tries a zero width""" with self.assertRaises(ValueError) as e: a = Square(0) self.assertEqual(e.exception.args[0], "width must be > 0")
def testnegy(self): """Tries a negative y""" with self.assertRaises(ValueError) as e: a = Square(10, 1, -1) self.assertEqual(e.exception.args[0], "y must be >= 0")
def testnegsize(self): """Tries a negative size""" with self.assertRaises(ValueError) as e: a = Square(-10) self.assertEqual(e.exception.args[0], "width must be > 0")
def test_x_value(self): s0 = Square(1, 1, 1) self.assertEqual(s0.x, 1) Base._Base__nb_objects = 0
def test_x(self): s0 = Square(1) self.assertTrue(hasattr(s0, 'x')) Base._Base__nb_objects = 0
def test_parameters(self): """Extra parameters""" with self.assertRaises(TypeError): r1 = Square("string") with self.assertRaises(TypeError): r1 = Square(1, "2") with self.assertRaises(TypeError): r1 = Square(1.2) with self.assertRaises(TypeError): r1 = Square(1, 2, 3, 4, 1, 3) with self.assertRaises(NameError): r1 = Square(a) with self.assertRaises(TypeError): r1 = Square(None) with self.assertRaises(TypeError): r1 = Square() with self.assertRaises(ValueError): r1 = Square(0) with self.assertRaises(ValueError): r1 = Square(-1) with self.assertRaises(ValueError): r1 = Square(1, -1, 2, 3) with self.assertRaises(ValueError): r1 = Square(1, 2, -3)
def teststr(self): """Tests stringing a basic square""" global idct a = Square(4) idct += 1 self.assertEqual(str(a), "[Square] ({}) 0/0 - 4".format(idct))
def test_area(self): """Print area""" r1 = Square(10) self.assertTrue(type(r1), Square)
def teststr2(self): """Tests stringing a rectangle with offset""" a = Square(4, 6, 7, 3) self.assertEqual(str(a), "[Square] (3) 6/7 - 4")
def test_21_ret_dict_16(self): """test update instance 2""" Base._Base__nb_objects = 0 s3 = Square(10, 2, 1) self.assertEqual(s3.__str__(), "[Square] (1) 2/1 - 10")
def testupdatetoomany(self): """Tests square update function with extra positional args""" a = Square(4, 6, 7, 3) a.update(11, 5, 15, 12, [], "hello", ()) self.assertEqual(str(a), "[Square] (11) 15/12 - 5")
def test_1_display_10(self): """test display 0""" Base._Base__nb_objects = 0 s1 = Square(5) s2 = Square(2, 2) self.assertEqual(s1.__str__(), "[Square] (1) 0/0 - 5") self.assertEqual(s1.area(), 25) captureOutput = io.StringIO() sys.stdout = captureOutput s1.display() sys.stdout = sys.__stdout__ self.assertEqual(captureOutput.getvalue(), "#####\n" * 5) """test for s2""" self.assertEqual(s2.__str__(), "[Square] (2) 2/0 - 2") self.assertEqual(s2.area(), 4) captureOutput = io.StringIO() sys.stdout = captureOutput s2.display() sys.stdout = sys.__stdout__ self.assertEqual(captureOutput.getvalue(), " ##\n" * 2) """test for __str__""" self.assertEqual(s1.__str__(), "[Square] (1) 0/0 - 5") """test size""" self.assertEqual(s1.size, 5) """test when set size manually""" s1.size = 10 self.assertEqual(s1.__str__(), "[Square] (1) 0/0 - 10") """test when wrong input data""" try: s1.size = "9" except Exception as e: self.assertEqual("[{}] {}".format(e.__class__.__name__, e), "[TypeError] width must be an integer") """test when updating 10 for id""" s1.update(10) self.assertEqual(s1.__str__(), "[Square] (10) 0/0 - 10") """test when update id and size""" s1.update(1, 2) self.assertEqual(s1.__str__(), "[Square] (1) 0/0 - 2") """test when update id, size, x""" s1.update(1, 2, 3) self.assertEqual(s1.__str__(), "[Square] (1) 3/0 - 2") """test update id, size, x, y""" s1.update(1, 2, 3, 4) self.assertEqual(s1.__str__(), "[Square] (1) 3/4 - 2") """test update x by **kwargs""" s1.update(x=12) self.assertEqual(s1.__str__(), "[Square] (1) 12/4 - 2") """test update size and y by **kwargs""" s1.update(size=7, y=1) self.assertEqual(s1.__str__(), "[Square] (1) 12/1 - 7") """test update size and y by **kwargs""" s1.update(size=7, id=89, y=1) self.assertEqual(s1.__str__(), "[Square] (89) 12/1 - 7")
def testtodict(self): """Tests rectangle to dictionary function""" a = Square(4, 6, 7, 3) dictcomp = {"id": 3, "size": 4, "x": 6, "y": 7} self.assertEqual(a.to_dictionary(), dictcomp)
def test_y_bool(self): ''' Testing for other than int ''' with self.assertRaises(TypeError): sq = Square(1, 7, True)
def test_to_dictionary(self): """to dictionary""" s1 = Square(10, 2, 1) s1_dictionary = s1.to_dictionary() self.assertEqual(s1_dictionary, {'id': 1, 'x': 2, 'size': 10, 'y': 1}) self.assertTrue(type(s1_dictionary), dict)
def test_y_negative(self): ''' Testing with negative int ''' with self.assertRaises(ValueError): sq = Square(4, 2, -3)
def test_str_method(self): """test str method for square""" s1 = Square(5) self.assertEqual(str(s1), "[Square] (1) 0/0 - 5")
def setUp(self): ''' Initializing instance with width and height parameters ''' self.s = Square(5)
def test_id(self): """test instance count""" new = Square(9) self.assertEqual(new.id, 1)
def test_y_float(self): ''' Testing for other than int ''' with self.assertRaises(TypeError): sq = Square(5, 8, 1.07)
def test_width(self): s0 = Square(1, 1) self.assertTrue(hasattr(s0, 'width')) Base._Base__nb_objects = 0