19 from robot.utils import (is_dict_like, is_list_like, is_string, is_truthy, Matcher,
20 plural_or_not
as s, seq2str, seq2str2, type_name)
133 while value
in list_:
170 removed = len(list_) - len(ret)
171 logger.info(f
'{removed} duplicate{s(removed)} removed.')
227 return list_[start:end]
264 return int(start) + list_.index(value)
278 return copy.deepcopy(list_)
314 f
"{seq2str2(list_)} does not contain value '{value}'.",
324 f
"{seq2str2(list_)} contains value '{value}'.",
339 if not isinstance(list_, list):
343 if item
not in dupes:
344 count = list_.count(item)
346 logger.info(f
"'{item}' found {count} times.")
349 raise AssertionError(msg
or f
'{seq2str(dupes)} found multiple times.')
395 names=None, ignore_order=False):
400 f
'Lengths are different: {len1} != {len2}',
404 list1 = sorted(list1)
405 list2 = sorted(list2)
408 f
'Lists are different:\n{diffs}',
415 return dict((int(index), names[index])
for index
in names)
416 return dict(zip(range(list_length), names))
419 for index, (item1, item2)
in enumerate(zip(list1, list2)):
420 name = f
' ({names[index]})' if index
in names
else ''
423 except AssertionError
as err:
436 diffs =
', '.join(str(item)
for item
in list2
if item
not in list1)
438 f
'Following values were not found from first list: {diffs}',
450 logger.write(
'\n'.join(self.
_log_list_log_list(list_)), level)
454 yield 'List is empty.'
455 elif len(list_) == 1:
456 yield f
'List has one item:\n{list_[0]}'
458 yield f
'List length is {len(list_)} and it contains following items:'
459 for index, item
in enumerate(list_):
460 yield f
'{index}: {item}'
463 if empty_to_zero
and not index:
468 raise ValueError(f
"Cannot convert index '{index}' to an integer.")
471 raise IndexError(f
'Given index {index} is out of the range 0-{len(list_)-1}.')
475 raise TypeError(f
"Expected argument {position} to be a list or list-like, "
476 f
"got {type_name(list_)} instead.")
479 for index, item
in enumerate(lists, start=1):
515 if len(key_value_pairs) % 2 != 0:
516 raise ValueError(
"Adding data to a dictionary failed. There "
517 "should be even number of key-value-pairs.")
518 for i
in range(0, len(key_value_pairs), 2):
519 dictionary[key_value_pairs[i]] = key_value_pairs[i+1]
520 dictionary.update(items)
536 if key
in dictionary:
537 value = dictionary.pop(key)
538 logger.info(f
"Removed item with key '{key}' and value '{value}'.")
540 logger.info(f
"Key '{key}' not found.")
556 if default
is NOT_SET:
558 return dictionary.pop(key)
559 return dictionary.pop(key, default)
573 remove_keys = [k
for k
in dictionary
if k
not in keys]
590 return copy.deepcopy(dictionary)
591 return dictionary.copy()
615 keys = dictionary.keys()
646 return [dictionary[k]
for k
in keys]
675 return [i
for key
in keys
for i
in (key, dictionary[key])]
695 return dictionary[key]
697 if default
is not NOT_SET:
699 raise RuntimeError(f
"Dictionary does not contain key '{key}'.")
708 f
"Dictionary does not contain key '{key}'.",
718 f
"Dictionary contains key '{key}'.",
731 msg
or f
"Value of dictionary key '{key}' does not match",
741 f
"Dictionary does not contain value '{value}'.",
751 f
"Dictionary contains value '{value}'.",
780 diffs =
', '.join(str(k)
for k
in keys
if k
not in dict1)
782 f
"Following keys missing from first dictionary: {diffs}",
795 logger.write(
'\n'.join(self.
_log_dictionary_log_dictionary(dictionary)), level)
799 yield 'Dictionary is empty.'
800 elif len(dictionary) == 1:
801 yield 'Dictionary has one item:'
803 yield f
'Dictionary size is {len(dictionary)} and it contains following items:'
805 yield f
'{key}: {dictionary[key]}'
810 miss1 =
', '.join(str(k)
for k
in keys2
if k
not in dict1)
811 miss2 =
', '.join(str(k)
for k
in keys1
if k
not in dict2)
814 error += [f
'Following keys missing from first dictionary: {miss1}']
816 error += [f
'Following keys missing from second dictionary: {miss2}']
823 f
'Following keys have different values:\n{diffs}',
830 except AssertionError
as err:
835 raise TypeError(f
"Expected argument {position} to be a dictionary or "
836 f
"dictionary-like, got {type_name(dictionary)} instead.")
918 ROBOT_LIBRARY_SCOPE =
'GLOBAL'
957 case_insensitive=False,
958 whitespace_insensitive=False):
959 _List._validate_list(self, list)
961 whitespace_insensitive)
962 default = f
"{seq2str2(list)} does not contain match for pattern '{pattern}'."
971 case_insensitive=False,
972 whitespace_insensitive=False):
973 _List._validate_list(self, list)
975 whitespace_insensitive)
976 default = f
"{seq2str2(list)} contains match for pattern '{pattern}'."
990 whitespace_insensitive=False):
991 _List._validate_list(self, list)
993 whitespace_insensitive)
1006 whitespace_insensitive=False):
1007 _List._validate_list(self, list)
1008 return len(self.
get_matchesget_matches(list, pattern, case_insensitive,
1009 whitespace_insensitive))
1017 elif is_truthy(values)
and str(values).upper() !=
'NO VALUES':
1018 msg +=
'\n' + default_msg
1023 whitespace_insensitive=False):
1025 raise TypeError(f
"Pattern must be string, got '{type_name(pattern)}'.")
1027 if pattern.startswith(
'regexp='):
1028 pattern = pattern[7:]
1030 elif pattern.startswith(
'glob='):
1031 pattern = pattern[5:]
1032 matcher = Matcher(pattern,
1034 spaceless=
is_truthy(whitespace_insensitive),
1036 return [string
for string
in iterable
1037 if is_string(string)
and matcher.match(string)]
A library providing keywords for handling lists and dictionaries.
def should_not_contain_match(self, list, pattern, msg=None, case_insensitive=False, whitespace_insensitive=False)
Fails if pattern is found in list.
def get_match_count(self, list, pattern, case_insensitive=False, whitespace_insensitive=False)
Returns the count of matches to pattern in list.
def get_matches(self, list, pattern, case_insensitive=False, whitespace_insensitive=False)
Returns a list of matches to pattern in list.
def should_contain_match(self, list, pattern, msg=None, case_insensitive=False, whitespace_insensitive=False)
Fails if pattern is not found in list.
def _log_dictionary(self, dictionary)
def convert_to_dictionary(self, item)
Converts the given item to a Python dict type.
def set_to_dictionary(self, dictionary, *key_value_pairs, **items)
Adds the given key_value_pairs and items to the dictionary.
def _validate_dictionary(self, dictionary, position=1)
def get_from_dictionary(self, dictionary, key, default=NOT_SET)
Returns a value from the given dictionary based on the given key.
def dictionary_should_contain_item(self, dictionary, key, value, msg=None)
An item of key / value must be found in a dictionary.
def copy_dictionary(self, dictionary, deepcopy=False)
Returns a copy of the given dictionary.
def _keys_should_be_equal(self, dict1, dict2, msg, values)
def log_dictionary(self, dictionary, level='INFO')
Logs the size and contents of the dictionary using given level.
def get_dictionary_keys(self, dictionary, sort_keys=True)
Returns keys of the given dictionary as a list.
def dictionary_should_contain_key(self, dictionary, key, msg=None)
Fails if key is not found from dictionary.
def _yield_dict_diffs(self, keys, dict1, dict2)
def dictionary_should_contain_value(self, dictionary, value, msg=None)
Fails if value is not found from dictionary.
def get_dictionary_values(self, dictionary, sort_keys=True)
Returns values of the given dictionary as a list.
def dictionaries_should_be_equal(self, dict1, dict2, msg=None, values=True)
Fails if the given dictionaries are not equal.
def dictionary_should_contain_sub_dictionary(self, dict1, dict2, msg=None, values=True)
Fails unless all items in dict2 are found from dict1.
def _key_values_should_be_equal(self, keys, dict1, dict2, msg, values)
def dictionary_should_not_contain_key(self, dictionary, key, msg=None)
Fails if key is found from dictionary.
def remove_from_dictionary(self, dictionary, *keys)
Removes the given keys from the dictionary.
def keep_in_dictionary(self, dictionary, *keys)
Keeps the given keys in the dictionary and removes all other.
def get_dictionary_items(self, dictionary, sort_keys=True)
Returns items of the given dictionary as a list.
def pop_from_dictionary(self, dictionary, key, default=NOT_SET)
Pops the given key from the dictionary and returns its value.
def dictionary_should_not_contain_value(self, dictionary, value, msg=None)
Fails if value is found from dictionary.
def log_list(self, list_, level='INFO')
Logs the length and contents of the list using given level.
def remove_duplicates(self, list_)
Returns a list without duplicates based on the given list.
def insert_into_list(self, list_, index, value)
Inserts value into list to the position specified with index.
def list_should_contain_sub_list(self, list1, list2, msg=None, values=True)
Fails if not all elements in list2 are found in list1.
def remove_values_from_list(self, list_, *values)
Removes all occurrences of given values from list.
def count_values_in_list(self, list_, value, start=0, end=None)
Returns the number of occurrences of the given value in list.
def _index_error(self, list_, index)
def list_should_not_contain_value(self, list_, value, msg=None)
Fails if the value is found from list.
def sort_list(self, list_)
Sorts the given list in place.
def _validate_list(self, list_, position=1)
def set_list_value(self, list_, index, value)
Sets the value of list specified by index to the given value.
def list_should_contain_value(self, list_, value, msg=None)
Fails if the value is not found from list.
def _yield_list_diffs(self, list1, list2, names)
def _log_list(self, list_)
def _get_list_index_name_mapping(self, names, list_length)
def lists_should_be_equal(self, list1, list2, msg=None, values=True, names=None, ignore_order=False)
Fails if given lists are unequal.
def get_slice_from_list(self, list_, start=0, end=None)
Returns a slice of the given list between start and end indexes.
def list_should_not_contain_duplicates(self, list_, msg=None)
Fails if any element in the list is found from it more than once.
def _index_to_int(self, index, empty_to_zero=False)
def get_from_list(self, list_, index)
Returns the value specified with an index from list.
def remove_from_list(self, list_, index)
Removes and returns the value specified with an index from list.
def convert_to_list(self, item)
Converts the given item to a Python list type.
def get_index_from_list(self, list_, value, start=0, end=None)
Returns the index of the first occurrence of the value on the list.
def append_to_list(self, list_, *values)
Adds values to the end of list.
def _validate_lists(self, *lists)
def copy_list(self, list_, deepcopy=False)
Returns a copy of the given list.
def reverse_list(self, list_)
Reverses the given list in place.
def combine_lists(self, *lists)
Combines the given lists together and returns the result.
def _verify_condition(condition, default_msg, msg, values=False)
def _get_matches_in_iterable(iterable, pattern, case_insensitive=False, whitespace_insensitive=False)
def assert_equal(first, second, msg=None, values=True, formatter=safe_str)
Fail if given objects are unequal as determined by the '==' operator.
def is_truthy(item)
Returns True or False depending on is the item considered true or not.
def get_version(naked=False)