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
17    FOR  ${arg}  IN  @{lists}
18      ${type_arg}=  Evaluate  str(type($lists[${index}])).split("'")[1]
19      Run Keyword If  '${type_arg}' != 'list'  Run Keywords  Remove From List  ${lists}  ${index}  AND
20      ...  Continue For Loop
21      ${index}=  Evaluate  ${index}+1
22    END
23
24    ${new_list}=  Combine Lists  @{lists}
25
26    [Return]  ${new_list}
27
28
29Intersect Lists
30    [Documentation]  Intersects the two lists passed in. Returns a list of
31    ...  values common to both lists with no duplicates.
32    [Arguments]  ${list1}  ${list2}
33
34    # list1      The first list to intersect.
35    # list2      The second list to intersect.
36
37    ${length1}=  Get Length  ${list1}
38    ${length2}=  Get Length  ${list2}
39
40    @{intersected_list}  Create List
41
42    @{larger_list}=  Set Variable If  ${length1} >= ${length2}  ${list1}
43    ...                               ${length1} < ${length2}  ${list2}
44    @{smaller_list}=  Set Variable If  ${length1} >= ${length2}  ${list2}
45    ...                                ${length1} < ${length2}  ${list1}
46
47    FOR  ${element}  IN  @{larger_list}
48      ${rc}=  Run Keyword and Return Status  List Should Contain Value  ${smaller_list}  ${element}
49      Run Keyword If  '${rc}' == 'True'  Append to List  ${intersected_list}  ${element}
50    END
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