def set_value_2(self, input_obj): result = [] if input_obj.isdigit(): self.__value_2 = int(input_obj) # check is input_obj potentially range or list, if it have brackets then it is probably range object # otherwise it is probably list elif not ('(' in input_obj or '[' in input_obj): if ',' in input_obj: list_obj = [elem.strip() for elem in input_obj.split(',')] else: list_obj = input_obj.split() for i in range(len(list_obj)): if not list_obj[i].isdigit(): message = 'Wrong type of obj. Expected: integer or set of integers or range object' self.logger.log('Set value 2 exc: %s' % message) raise TypeError(message) result.append(int(list_obj[i])) self.__value_2 = result else: try: self.__value_2 = Range(input_obj) except ValueError as exception: self.logger.log('Set value 2 exc: %s' % exception) raise ValueError from exception self.logger.log('Set value 2: %s' % input_obj)
def set_value_1(self, input_range): try: self.__value_1 = Range(input_range) self.logger.log('Set value 1: %s' % input_range) except ValueError as exception: self.logger.log('Set value 1 exc: %s' % exception) raise ValueError from exception
def set_value_2(self, input_obj): result = [] if input_obj.isdigit(): self.__value_2 = int(input_obj) # check is input_obj potentially range or list, if it have brackets then it is probably range object # otherwise it is probably list elif not ('(' in input_obj or '[' in input_obj): if ',' in input_obj: list_obj = [elem.strip() for elem in input_obj.split(',')] else: list_obj = input_obj.split() for i in range(len(list_obj)): if not list_obj[i].isdigit(): raise TypeError( 'Wrong type of obj. Expected: integer or set of integers or range object' ) result.append(int(list_obj[i])) self.__value_2 = result else: self.__value_2 = Range(input_obj)
def test_contain_range(self): range_object = Range('(-1,4]') value = Range('[0,2]') self.assertTrue(range_object.contains_range(value))
def test_not_contain_set(self): range_object = Range('(-1,3]') values = [0, 2, 3, 99] self.assertFalse(range_object.contains_set(values))
def test_contain_set(self): range_object = Range('(-1,3]') values = [0, 2, 3] self.assertTrue(range_object.contains_set(values))
def test_can_create(self): range_object = Range('[1,3]') self.assertTrue(isinstance(range_object, Range))
def test_not_contain_border_element_with_parenthesis(self): range_object = Range('(-1,3]') self.assertFalse(range_object.contains_value(-1))
def test_can_create_with_both_squares(self): range_object = Range('[1,3]') self.assertTrue(isinstance(range_object, Range))
def test_equal_wrong_type_error(self): range_object = Range('(-1,3]') value = 'a' with self.assertRaises(TypeError): range_object.equals(value)
def test_not_overlaps(self): range_object = Range('(-1,4]') value = Range('(5,20]') self.assertFalse(range_object.overlaps_range(value))
def test_overlaps(self): range_object = Range('(-1,4]') value = Range('[-1,20]') self.assertTrue(range_object.overlaps_range(value))
def test_overlaps_wrong_type_error(self): range_object = Range('(-1,3]') value = 'a' with self.assertRaises(TypeError): range_object.overlaps_range(value)
def test_not_contain_range(self): range_object = Range('(-1,4]') value = Range('[5,20]') self.assertFalse(range_object.contains_range(value))
def test_cant_create_from_empty_str(self): with self.assertRaises(ValueError): Range('')
def test_can_create_with_both_parentheses(self): range_object = Range('(1,3)') self.assertTrue(isinstance(range_object, Range))
def test_can_create_with_right_square(self): range_object = Range('(1,3]') self.assertTrue(isinstance(range_object, Range))
def test_equal_diff_brackets(self): range_object = Range('(-1,4]') value = Range('[0,5)') self.assertTrue(range_object.equals(value))
def test_can_create_with_negatives(self): range_object = Range('[-1,3]') self.assertTrue(isinstance(range_object, Range))
def test_cant_create_empty_range(self): with self.assertRaises(ValueError): Range('(1,2)')
def test_not_contain_value(self): range_object = Range('(-1,3]') self.assertFalse(range_object.contains_value(99))
def test_not_equal(self): range_object = Range('(-1,4]') value = Range('(-5,4]') self.assertFalse(range_object.equals(value))
def test_contain_border_element_with_quarter(self): range_object = Range('(-1,3]') self.assertTrue(range_object.contains_value(3))
def test_end_points(self): range_object = Range('(-1,4]') end_points = [0, 4] self.assertEqual(range_object.end_points(), end_points)
def test_wrong_type_contain_set(self): range_object = Range('(-1,3]') value = 'type-error' with self.assertRaises(TypeError): range_object.contains_set(value)
def test_to_string(self): range_object = Range('[1,5]') range_str = '[1,5]' self.assertEqual(range_object.to_string(), range_str)
def test_get_all_points(self): range_object = Range('(-1,3]') values = [0, 1, 2, 3] self.assertEqual(range_object.get_all_points(), values)
def test_cant_create_reversed_range(self): with self.assertRaises(ValueError): Range('(6,2)')
def test_contain_range_wrong_type_error(self): range_object = Range('(-1,3]') value = [555] with self.assertRaises(TypeError): range_object.contains_range(value)
def test_can_create_with_right_parenthesis(self): range_object = Range('[1,3)') self.assertTrue(isinstance(range_object, Range))