1*** Settings ***
2Documentation  This module contains keywords for list manipulation.
3Library  Collections
4
5*** Keywords ***
6Smart 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
28Intersect 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}
47    \  ${rc}=  Run Keyword and Return Status  List Should Contain Value
48    ...  ${smaller_list}  ${element}
49    \  Run Keyword If  '${rc}' == 'True'  Append to List  ${intersected_list}
50    ...  ${element}
51
52    @{intersected_list}=  Remove Duplicates  ${intersected_list}
53
54    [Return]  @{intersected_list}
55
56
57Subtract 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    END
73
74    [Return]  ${diff_list}
75