class TestIntegerList(unittest.TestCase): def setUp(self): self.int_list = IntegerList(1, 2, 3, '4') def test_add__should_add_element_and_return_list(self): self.int_list.add(4) expected = [1, 2, 3, 4] actual = self.int_list.get_data() self.assertEqual(actual, expected) def test_add__raise_ValueError_if_element_not_integer(self): with self.assertRaises(ValueError) as cm: self.int_list.add('5') self.assertEqual(str(cm.exception), "Element is not Integer") def test_remove_index__removes_element_on_given_index_and_returns_it(self): self.assertEqual(self.int_list.remove_index(1), 2) self.assertEqual(self.int_list.get_data(), [1, 3]) def test_remove_index__should_raise_IndexError__if_given_index_incorrect(self): with self.assertRaises(IndexError) as cm: self.int_list.remove_index(3) self.assertEqual(str(cm.exception), "Index is out of range") def test_get__should_return_element_in_given_index(self): self.assertEqual(self.int_list.get(2), 3) def test_get__should_raise_IndexError__if_given_index_incorrect(self): with self.assertRaises(IndexError) as cm: self.int_list.get(3) self.assertEqual(str(cm.exception), "Index is out of range") def test_insert__must_add_integer_to_given_index(self): self.int_list.insert(2, 4) expected = [1, 2, 4, 3] actual = self.int_list.get_data() self.assertEqual(actual, expected) def test_insert__must_raise_IndexError_if_given_index_invalid(self): with self.assertRaises(IndexError) as cm: self.int_list.insert(3, 4) self.assertEqual(str(cm.exception), "Index is out of range") def test_insert__must_raise_ValueError_if_given_element_not_integer(self): with self.assertRaises(ValueError) as cm: self.int_list.insert(2, '4') self.assertEqual(str(cm.exception), "Element is not Integer") def test_get_biggest_must_return_biggest_integer(self): self.assertEqual(self.int_list.get_biggest(), 3) def test_get_index__return_index_of_given_element(self): self.assertEqual(self.int_list.get_index(3), 2)
class IntegerListTests(TestCase): def setUp(self): self.int_list = IntegerList(1, 2, 3, 4, 5) def test_add__element_not_integer__expect_value_error(self): with self.assertRaises(ValueError) as ex: self.int_list.add("asd") self.assertEqual('Element is not Integer', str(ex.exception)) def test_add__valid_element__expect_return_list(self): self.assertEqual([1, 2, 3, 4, 5, 6], self.int_list.add(6)) def test_remove_index__index_not_in_range__expect_index_error(self): with self.assertRaises(IndexError) as ex: self.int_list.remove_index(5) self.assertEqual('Index is out of range', str(ex.exception)) def test_remove_index__return_element__expect_removed_element(self): self.assertEqual(self.int_list.remove_index(0), 1) self.assertEqual([2, 3, 4, 5], self.int_list.get_data()) def test__init__other_types_involved__expect_only_integers(self): self.int_list = IntegerList(1, 'two', 3, 'four', 5) self.assertEqual([1, 3, 5], self.int_list.get_data()) def test_get__index_not_in_range_expect_index_error(self): with self.assertRaises(IndexError) as ex: self.int_list.get(5) self.assertEqual('Index is out of range', str(ex.exception)) def test_insert__index_not_in_range__expect_index_error(self): with self.assertRaises(IndexError) as ex: self.int_list.insert(5, 6) self.assertEqual('Index is out of range', str(ex.exception)) def test_insert__element_not_integer__expect_value_error(self): with self.assertRaises(ValueError) as ex: self.int_list.insert(0, 'zero') self.assertEqual('Element is not Integer', str(ex.exception)) def test_insert__element_valid__expect_change_in_list(self): self.int_list.insert(0, 0) self.assertEqual([0, 1, 2, 3, 4, 5], self.int_list.get_data()) def test_get_biggest(self): self.assertEqual(self.int_list.get_biggest(), 5) def test_get_index(self): self.assertEqual(1, self.int_list.get_index(2))
class IntegerListTest(unittest.TestCase): def setUp(self): self.list = IntegerList([]) def test_add(self): self.assertEqual(self.list.add(42), [42]) def test_add_raises_value_error(self): self.assertRaises(ValueError, self.list.add, 'baba') def test_remove_by_index(self): self.list.add(42) el = self.list.remove_index(0) self.assertEqual(el, 42) def test_remove_by_index_raises_index_error(self): self.assertRaises(IndexError, self.list.remove_index, 0) def test_init_takes_only_ints(self): list = IntegerList('baba', 42, 'dqdo') self.assertEqual(list.get_data(), [42]) def test_get(self): self.list.add(42) self.assertEqual(self.list.get(0), 42) def test_get_raises_index_error(self): self.assertRaises(IndexError, self.list.get, 0) def test_insert(self): self.assertRaises(IndexError, self.list.insert, 0, 42) self.list.add(1) self.assertRaises(ValueError, self.list.insert, 0, 'baba') self.list.insert(0, 42) self.assertEqual(self.list.get_data(), [42, 1]) def test_get_biggest(self): self.list.add(42) self.list.add(43) self.assertEqual(self.list.get_biggest(), 43) def test_get_index(self): self.list.add(42) self.assertEqual(self.list.get_index(42), 0)
def test_integer_list_add__when_value_is_not_int__expect_value_error(self): integer_list = IntegerList(1, 2, 3) with self.assertRaises(ValueError): integer_list.add("asd")
def test_integer_list_add__when_value_is_int__expect_to_add_it_to_the_data_and_return_the_data( self): integer_list = IntegerList(1, 2, 3) self.assertEqual([1, 2, 3, 4], integer_list.add(4))
class TestIntegerList(TestCase): def setUp(self): self.list_integers = IntegerList(5, 6, 7) def test_init_creates_all_attributes(self): self.assertEqual([5, 6, 7], self.list_integers._IntegerList__data) def test_init_takes_non_integers(self): list_integers = IntegerList(5.6, "6", 7.2) self.assertEqual([], list_integers._IntegerList__data) def test_add_integer_is_added(self): result = self.list_integers.add(100) self.assertEqual([5, 6, 7, 100], result) def test_add_non_integer_raises(self): with self.assertRaises(ValueError) as ex: self.list_integers.add(5.5) self.assertEqual("Element is not Integer", str(ex.exception)) def test_remove_index_returns_element(self): el = self.list_integers.remove_index(0) self.assertEqual(5, el) self.assertEqual([6, 7], self.list_integers._IntegerList__data) def test_remove_index_not_in_range_raises(self): with self.assertRaises(IndexError) as ex: self.list_integers.remove_index(3) self.assertEqual("Index is out of range", str(ex.exception)) def test_get_with_valid_index_returns_element(self): el = self.list_integers.get(0) self.assertEqual(5, el) self.assertEqual([5, 6, 7], self.list_integers._IntegerList__data) def test_get_with_not_valid_index_raises(self): with self.assertRaises(IndexError) as ex: self.list_integers.get(3) self.assertEqual("Index is out of range", str(ex.exception)) def test_insert_adds_element_at_index(self): self.list_integers.insert(0, 100) self.assertEqual([100, 5, 6, 7], self.list_integers._IntegerList__data) def test_insert_non_integer_raises(self): with self.assertRaises(ValueError) as ex: self.list_integers.insert(0, 5.6) self.assertEqual("Element is not Integer", str(ex.exception)) def test_insert_integer_to_non_valid_index_raises(self): with self.assertRaises(IndexError) as ex: self.list_integers.insert(3, 5) self.assertEqual("Index is out of range", str(ex.exception)) def test_get_biggest(self): res = self.list_integers.get_biggest() self.assertEqual(7, res) def test_get_index(self): index = self.list_integers.get_index(5) self.assertEqual(0, index)