Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IntegerList:
- def __init__(self, *args):
- self.__data = []
- for x in args:
- if type(x) == int:
- self.__data.append(x)
- def get_data(self):
- return self.__data
- def add(self, element):
- if not type(element) == int:
- raise ValueError("Element is not Integer")
- self.get_data().append(element)
- return self.get_data()
- def remove_index(self, index):
- if index >= len(self.get_data()):
- raise IndexError("Index is out of range")
- a = self.get_data()[index]
- del self.get_data()[index]
- return a
- def get(self, index):
- if index >= len(self.get_data()):
- raise IndexError("Index is out of range")
- return self.get_data()[index]
- import unittest
- class Tests(unittest.TestCase):
- def test_should_return_3(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- self.assertEqual(len(listen.get_data()), 3)
- def test_should_return_2(self):
- listen = IntegerList('a', 53464564, 8978970890)
- self.assertEqual(len(listen.get_data()), 2)
- def test_add_should_work(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- listen.add(7)
- self.assertEqual(len(listen.get_data()), 4)
- def test_add_should_throw_error(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- with self.assertRaises(ValueError):
- listen.add("kek")
- def test_remove_should_work(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- listen.remove_index(0)
- self.assertTrue(len(listen.get_data()), 2)
- def test_remove_should_throw_error(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- with self.assertRaises(IndexError):
- listen.remove_index(5)
- def test_get_should_work(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- a = listen.get(0)
- self.assertTrue(a, 12312)
- def test_get_should_throw_error(self):
- listen = IntegerList(12312, 53464564, 8978970890)
- with self.assertRaises(IndexError):
- listen.get(5)
- if __name__ == '__main__':
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement