Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 1 | *** Settings *** |
| 2 | Documentation This module contains keywords for list manipulation. |
| 3 | Library Collections |
| 4 | |
| 5 | *** Keywords *** |
Michael Walsh | a4e79ff | 2017-10-30 14:40:14 -0500 | [diff] [blame] | 6 | Smart Combine Lists |
| 7 | [Documentation] Combine all valid list arguments and return the result. |
| 8 | [Arguments] @{lists} |
| 9 | |
| 10 | # Description of argument(s): |
| 11 | # lists A list of lists to be combined. Any item in this list which is |
| 12 | # NOT a list will be removed. |
| 13 | |
| 14 | ${list_size}= Get Length ${lists} |
| 15 | ${index}= Set Variable ${0} |
| 16 | : FOR ${arg} IN @{lists} |
| 17 | \ ${type_arg}= Evaluate str(type($lists[${index}])).split("'")[1] |
| 18 | \ Run Keyword If '${type_arg}' != 'list' Run Keywords |
| 19 | ... Remove From List ${lists} ${index} AND |
| 20 | ... Continue For Loop |
| 21 | \ ${index}= Evaluate ${index}+1 |
| 22 | |
| 23 | ${new_list}= Combine Lists @{lists} |
| 24 | |
| 25 | [Return] ${new_list} |
| 26 | |
| 27 | |
Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 28 | Intersect Lists |
| 29 | [Documentation] Intersects the two lists passed in. Returns a list of |
| 30 | ... values common to both lists with no duplicates. |
| 31 | [Arguments] ${list1} ${list2} |
| 32 | |
| 33 | # list1 The first list to intersect. |
| 34 | # list2 The second list to intersect. |
| 35 | |
| 36 | ${length1}= Get Length ${list1} |
| 37 | ${length2}= Get Length ${list2} |
| 38 | |
| 39 | @{intersected_list} Create List |
| 40 | |
| 41 | @{larger_list}= Set Variable If ${length1} >= ${length2} ${list1} |
| 42 | ... ${length1} < ${length2} ${list2} |
| 43 | @{smaller_list}= Set Variable If ${length1} >= ${length2} ${list2} |
| 44 | ... ${length1} < ${length2} ${list1} |
| 45 | |
| 46 | :FOR ${element} IN @{larger_list} |
Michael Walsh | a4e79ff | 2017-10-30 14:40:14 -0500 | [diff] [blame] | 47 | \ ${rc}= Run Keyword and Return Status List Should Contain Value |
| 48 | ... ${smaller_list} ${element} |
Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 49 | \ Run Keyword If '${rc}' == 'True' Append to List ${intersected_list} |
| 50 | ... ${element} |
| 51 | |
| 52 | @{intersected_list}= Remove Duplicates ${intersected_list} |
| 53 | |
Gunnar Mills | c9ea936 | 2016-12-13 16:21:13 -0600 | [diff] [blame] | 54 | [Return] @{intersected_list} |
Rahul Maheshwari | a93b8c0 | 2017-05-12 05:23:00 -0500 | [diff] [blame] | 55 | |
| 56 | |
| 57 | Subtract Lists |
| 58 | [Documentation] Subtract list 2 from list 1 and return the result. |
| 59 | # Return list contain items from the list 1 which are not present |
| 60 | # in the list 2. |
| 61 | [Arguments] ${list1} ${list2} |
| 62 | # Description of argument(s): |
| 63 | # list1 The base list which is to be subtracted from. |
| 64 | # list2 The list which is to be subtracted from list1. |
| 65 | |
| 66 | ${diff_list}= Create List |
| 67 | :FOR ${item} IN @{list1} |
| 68 | \ ${status}= Run Keyword And Return Status |
| 69 | ... Should Contain ${list2} ${item} |
| 70 | \ Run Keyword If '${status}' == '${False}' |
| 71 | ... Append To List ${diff_list} ${item} |
| 72 | |
| 73 | [Return] ${diff_list} |