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)
Exemple #2
0
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))
Exemple #3
0
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)
Exemple #4
0
 def test_init_takes_only_ints(self):
     list = IntegerList('baba', 42, 'dqdo')
     self.assertEqual(list.get_data(), [42])
Exemple #5
0
 def test_integer_list_init__when_args_is_not_only_integer__expect_only_integer_to_be_stored(
         self):
     args = [1, 2, 3, "asd"]
     integer_list = IntegerList(*args)
     self.assertEqual(args[:-1], integer_list.get_data())
Exemple #6
0
 def test_integer_list_insert__when_value_is_int_and_index_is_valid__expect_to_add_it_in_data(
         self):
     integer_list = IntegerList(1, 2, 3)
     integer_list.insert(1, 1)
     self.assertEqual([1, 1, 2, 3], integer_list.get_data())