Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0032b78

Browse files
authored
Support ignoring order with Collections.Lists Should Be Equal
Fixes #2703.
1 parent e428e47 commit 0032b78

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

atest/robot/standard_libraries/collections/list.robot

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ Lists Should Be Equal With Named Indices As Dictionary
199199
Lists Should Be Equal With Named Indices As Dictionary With Too Few Values
200200
Check Test Case ${TEST NAME}
201201

202+
Lists Should Be Equal Ignore Order
203+
Check Test Case ${TEST NAME}
204+
202205
List Should Contain Sub List
203206
Check Test Case ${TEST NAME}
204207

atest/testdata/standard_libraries/collections/list.robot

+5
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ Lists Should Be Equal With Named Indices As Dictionary With Too Few Values
331331
${names} = Create Dictionary 0=a 2=c
332332
Lists Should Be Equal ${L3} ${L3B} names=${names}
333333

334+
Lists Should Be Equal Ignore Order
335+
${list1} = Create List A B C D
336+
${list2} = Create List D B C A
337+
Lists Should Be Equal ${list1} ${list2} ignore_order=True
338+
334339
List Should Contain Sub List
335340
List Should Contain Sub List ${LONG} ${L4}
336341

src/robot/libraries/Collections.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def list_should_not_contain_duplicates(self, list_, msg=None):
346346
'%s found multiple times.' % seq2str(dupes))
347347

348348
def lists_should_be_equal(self, list1, list2, msg=None, values=True,
349-
names=None):
349+
names=None, ignore_order=False):
350350
"""Fails if given lists are unequal.
351351
352352
The keyword first verifies that the lists have equal lengths, and then
@@ -370,6 +370,10 @@ def lists_should_be_equal(self, list1, list2, msg=None, values=True,
370370
need to be named. It is not necessary to name all of the indices. When
371371
using a dictionary, keys can be either integers or strings that can be
372372
converted to integers.
373+
374+
Optional ``ignore_order`` argument can be used to ignore the order of the
375+
elements (element index) in the list while comparing two lists. If set to
376+
true, element order will be ignored. This option is new in RF 3.2 now.
373377
374378
Examples:
375379
| ${names} = | Create List | First Name | Family Name | Email |
@@ -380,13 +384,23 @@ def lists_should_be_equal(self, list1, list2, msg=None, values=True,
380384
If the items in index 2 would differ in the above examples, the error
381385
message would contain a row like ``Index 2 (email): [email protected] !=
382386
387+
388+
| ${list1} = | Create List | apple | cherry | banana |
389+
| ${list2} = | Create List | cherry | banana | apple |
390+
| Lists Should Be Equal | ${list1} | ${list2} | ignore_order=True |
391+
392+
list index would be ignored while comparing two lists. Above two list
393+
will be matched.
383394
"""
384395
self._validate_lists(list1, list2)
385396
len1 = len(list1)
386397
len2 = len(list2)
387398
default = 'Lengths are different: %d != %d' % (len1, len2)
388399
_verify_condition(len1 == len2, default, msg, values)
389400
names = self._get_list_index_name_mapping(names, len1)
401+
if ignore_order:
402+
list1 = sorted(list1)
403+
list2 = sorted(list2)
390404
diffs = list(self._yield_list_diffs(list1, list2, names))
391405
default = 'Lists are different:\n' + '\n'.join(diffs)
392406
_verify_condition(diffs == [], default, msg, values)

0 commit comments

Comments
 (0)