Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. class IntegerList:
  2. def __init__(self, *args):
  3. self.__data = []
  4. for x in args:
  5. if type(x) == int:
  6. self.__data.append(x)
  7.  
  8. def get_data(self):
  9. return self.__data
  10.  
  11. def add(self, element):
  12. if not type(element) == int:
  13. raise ValueError("Element is not Integer")
  14. self.get_data().append(element)
  15. return self.get_data()
  16.  
  17. def remove_index(self, index):
  18. if index >= len(self.get_data()):
  19. raise IndexError("Index is out of range")
  20. a = self.get_data()[index]
  21. del self.get_data()[index]
  22. return a
  23.  
  24. def get(self, index):
  25. if index >= len(self.get_data()):
  26. raise IndexError("Index is out of range")
  27. return self.get_data()[index]
  28.  
  29.  
  30. import unittest
  31.  
  32.  
  33. class Tests(unittest.TestCase):
  34. def test_should_return_3(self):
  35. listen = IntegerList(12312, 53464564, 8978970890)
  36. self.assertEqual(len(listen.get_data()), 3)
  37.  
  38. def test_should_return_2(self):
  39. listen = IntegerList('a', 53464564, 8978970890)
  40. self.assertEqual(len(listen.get_data()), 2)
  41.  
  42. def test_add_should_work(self):
  43. listen = IntegerList(12312, 53464564, 8978970890)
  44. listen.add(7)
  45. self.assertEqual(len(listen.get_data()), 4)
  46.  
  47. def test_add_should_throw_error(self):
  48. listen = IntegerList(12312, 53464564, 8978970890)
  49. with self.assertRaises(ValueError):
  50. listen.add("kek")
  51.  
  52. def test_remove_should_work(self):
  53. listen = IntegerList(12312, 53464564, 8978970890)
  54. listen.remove_index(0)
  55. self.assertTrue(len(listen.get_data()), 2)
  56.  
  57. def test_remove_should_throw_error(self):
  58. listen = IntegerList(12312, 53464564, 8978970890)
  59. with self.assertRaises(IndexError):
  60. listen.remove_index(5)
  61.  
  62. def test_get_should_work(self):
  63. listen = IntegerList(12312, 53464564, 8978970890)
  64. a = listen.get(0)
  65. self.assertTrue(a, 12312)
  66.  
  67. def test_get_should_throw_error(self):
  68. listen = IntegerList(12312, 53464564, 8978970890)
  69. with self.assertRaises(IndexError):
  70. listen.get(5)
  71.  
  72.  
  73. if __name__ == '__main__':
  74. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement