def lists_are_equal(list_one: Union[Tuple[Any], List[Any]], list_two: Union[Tuple[Any], List[Any]], message: str='') -> bool: """lists_are_equal(list_one, list_two, message='') Assert two collections contain tbe exact same values. .. note:: The order of the values in each collection will be asserted. :param list_one: The first value. (Can be any collection type) :type list_one: Union[Tuple[Any], List[Any]] :param list_two: The second value. (Can be any collection type) :type list_two: Union[Tuple[Any], List[Any]] :param message: A custom message to include when the assertion fails. Default is Empty String. :type message: str, optional :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(list_one): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if not CommonCollectionUtils.is_collection(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if len(list_one) != len(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) if isinstance(list_one, set) or isinstance(list_two, set): return list_one == list_two current_idx = 0 while current_idx < len(list_one): item_one = list_one[current_idx] item_two = list_two[current_idx] if item_one != item_two: raise AssertionError('{}: expected\n {}\n to be equal to\n {}\n Difference:\n {}\n should be\n {}\n at index {}'.format(message, list_one, list_two, item_one, item_two, current_idx)) current_idx += 1 return True
def list_contents_are_same(list_one: Union[Tuple[Any], List[Any]], list_two: Union[Tuple[Any], List[Any]], message: str='') -> bool: """list_contents_are_same(list_one, list_two, message='') Assert the values contained within two collections are the same. .. note:: The order of the values in each collection is ignored. :param list_one: The first value. (Can be any collection type) :type list_one: Union[Tuple[Any], List[Any]] :param list_two: The second value. (Can be any collection type) :type list_two: Union[Tuple[Any], List[Any]] :param message: A custom message to include when the assertion fails. Default is Empty String. :type message: str, optional :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(list_one): raise AssertionError('{}: {} is not a collection'.format(message, list_one)) if not CommonCollectionUtils.is_collection(list_two): raise AssertionError('{}: {} is not a collection'.format(message, list_two)) if len(list_one) != len(list_two): raise AssertionError('{}: expected\n {}\n to be equal to\n {}'.format(message, list_one, list_two)) for item_one in list_one: if item_one not in list_two: raise AssertionError('{}: expected\n {}\n contents to be equal to\n {}\n {} not found in\n {}'.format(message, list_one, list_two, item_one, list_two)) for item_one in list_two: if item_one not in list_one: raise AssertionError('{}: expected\n {}\n contents to be equal to\n {}\n {} not found in\n {}'.format(message, list_one, list_two, item_one, list_one)) return True
def are_similar(value_one: Any, value_two: Any, message: str = '') -> bool: """are_similar(value_one, value_two, message='') Assert two values are similar. If the values are both collections, then the values contained within will be asserted to be similar. .. note:: The order of the values in each collection is ignored. :param value_one: The first value. :type value_one: Any :param value_two: The second value. :type value_two: Any :param message: A custom message to include when the assertion fails. :type message: str :return: True, if the assertion succeeds. :rtype: bool :exception AssertionError: when the assertion fails. """ if CommonCollectionUtils.is_collection( value_one) or CommonCollectionUtils.is_collection(value_two): return CommonAssertionUtils.list_contents_are_same(value_one, value_two, message=message) if value_one != value_two: raise AssertionError( '{}: expected\n {}\n to be similar to\n {}'.format( message, value_one, value_two)) return True
def has_length(value: Union[Tuple[Any], List[Any]], expected_length: int, message: str = '') -> bool: """has_length(value, expected_length, message='') Assert a collection has the specified length. :param value: The collection being asserted. (Any collection that works with `len()` can be used) :type value: Union[Tuple[Any], List[Any]] :param expected_length: The length expected of the collection. :type expected_length: int :param message: A custom message to include when the assertion fails. :type message: str :return: True if the length matches. :rtype: bool :exception AssertionError: when the assertion fails. """ if not CommonCollectionUtils.is_collection(value): raise AssertionError( '{}: expected collection {} to have length {}, but was not a collection' .format(message, value, expected_length)) if len(value) != expected_length: raise AssertionError( '{}: expected collection {} to have length {}, but was {}'. format(message, value, expected_length, len(value))) return True
def get_full_stack_trace() -> List[str]: """ Retrieve the full stacktrace from the current stack. :return: A collection of stack trace strings. """ exception_info = CommonStacktraceUtil.full_exception_info() if CommonCollectionUtils.is_collection(exception_info): exceptions = traceback.format_exception(*exception_info) else: exceptions = traceback.format_stack() return exceptions
def are_equal(value_one: Any, value_two: Any, message: str = '') -> bool: """ Assert the two values are equal to each other. If the values are both collections, then the values contained within will be asserted to be equal. Note: The order of the values in each collection is asserted. :param value_one: The first value. :param value_two: The second value. :param message: A custom message to include when the assertion fails. :return: True if the assertion succeeds. :exception AssertionError when the assertion fails. """ if CommonCollectionUtils.is_collection( value_one) or CommonCollectionUtils.is_collection(value_two): return CommonAssertionUtils.lists_are_equal(value_one, value_two, message=message) if value_one != value_two: raise AssertionError( '{}: expected\n {}\n to be equal to\n {}'.format( message, value_one, value_two)) return True
def has_length(value, expected_length: int, message: str = '') -> bool: """ Assert a collection has the specified length. :param expected_length: The length expected of the collection. :param value: The collection being asserted. :param message: A custom message to include when the assertion fails. :return: True if the length matches. :exception AssertionError when the assertion fails. """ if not CommonCollectionUtils.is_collection(value): raise AssertionError( '{}: expected collection {} to have length {}, but was not a collection' .format(message, value, expected_length)) if len(value) != expected_length: raise AssertionError( '{}: expected collection {} to have length {}, but was {}'. format(message, value, expected_length, len(value))) return True