mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
New filters to calculate the union, intersection, difference and symmetric difference of lists by preserving the items order (#7985)
New filters lists_union, lists_intersect, lists_difference and lists_symmetric_difference added. Signed-off-by: Christoph Fiehe <c.fiehe@eurodata.de> Co-authored-by: Christoph Fiehe <c.fiehe@eurodata.de>
This commit is contained in:
parent
9510988abc
commit
102a0857db
11 changed files with 573 additions and 0 deletions
5
tests/integration/targets/filter_lists/aliases
Normal file
5
tests/integration/targets/filter_lists/aliases
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
azp/posix/2
|
64
tests/integration/targets/filter_lists/tasks/main.yml
Normal file
64
tests/integration/targets/filter_lists/tasks/main.yml
Normal file
|
@ -0,0 +1,64 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Test predictive list union
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- 'list1 | community.general.lists_union(list2, list3) == [1, 2, 5, 3, 4, 10, 11, 99, 101]'
|
||||
- '[list1, list2, list3] | community.general.lists_union(flatten=True) == [1, 2, 5, 3, 4, 10, 11, 99, 101]'
|
||||
- '[1, 2, 3] | community.general.lists_union([4, 5, 6]) == [1, 2, 3, 4, 5, 6]'
|
||||
- '[1, 2, 3] | community.general.lists_union([3, 4, 5, 6]) == [1, 2, 3, 4, 5, 6]'
|
||||
- '[1, 2, 3] | community.general.lists_union([3, 2, 1]) == [1, 2, 3]'
|
||||
- '["a", "A", "b"] | community.general.lists_union(["B", "c", "C"]) == ["a", "A", "b", "B", "c", "C"]'
|
||||
- '["a", "A", "b"] | community.general.lists_union(["b", "B", "c", "C"]) == ["a", "A", "b", "B", "c", "C"]'
|
||||
- '["a", "A", "b"] | community.general.lists_union(["b", "A", "a"]) == ["a", "A", "b"]'
|
||||
- '[["a"]] | community.general.lists_union([["b"], ["a"]]) == [["a"], ["b"]]'
|
||||
- '[["a"]] | community.general.lists_union([["b"]], ["a"]) == [["a"], ["b"], "a"]'
|
||||
- '[["a"]] | community.general.lists_union(["b"], ["a"]) == [["a"], "b", "a"]'
|
||||
|
||||
- name: Test predictive list intersection
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- 'list1 | community.general.lists_intersect(list2, list3) == [1, 2, 5, 4]'
|
||||
- '[list1, list2, list3] | community.general.lists_intersect(flatten=True) == [1, 2, 5, 4]'
|
||||
- '[1, 2, 3] | community.general.lists_intersect([4, 5, 6]) == []'
|
||||
- '[1, 2, 3] | community.general.lists_intersect([3, 4, 5, 6]) == [3]'
|
||||
- '[1, 2, 3] | community.general.lists_intersect([3, 2, 1]) == [1, 2, 3]'
|
||||
- '["a", "A", "b"] | community.general.lists_intersect(["B", "c", "C"]) == []'
|
||||
- '["a", "A", "b"] | community.general.lists_intersect(["b", "B", "c", "C"]) == ["b"]'
|
||||
- '["a", "A", "b"] | community.general.lists_intersect(["b", "A", "a"]) == ["a", "A", "b"]'
|
||||
- '[["a"]] | community.general.lists_intersect([["b"], ["a"]]) == [["a"]]'
|
||||
- '[["a"]] | community.general.lists_intersect([["b"]], ["a"]) == []'
|
||||
- '[["a"]] | community.general.lists_intersect(["b"], ["a"]) == []'
|
||||
|
||||
- name: Test predictive list difference
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- 'list1 | community.general.lists_difference(list2, list3) == []'
|
||||
- '[list1, list2, list3] | community.general.lists_difference(flatten=True) == []'
|
||||
- '[1, 2, 3] | community.general.lists_difference([4, 5, 6]) == [1, 2, 3]'
|
||||
- '[1, 2, 3] | community.general.lists_difference([3, 4, 5, 6]) == [1, 2]'
|
||||
- '[1, 2, 3] | community.general.lists_difference([3, 2, 1]) == []'
|
||||
- '["a", "A", "b"] | community.general.lists_difference(["B", "c", "C"]) == ["a", "A", "b"]'
|
||||
- '["a", "A", "b"] | community.general.lists_difference(["b", "B", "c", "C"]) == ["a", "A"]'
|
||||
- '["a", "A", "b"] | community.general.lists_difference(["b", "A", "a"]) == []'
|
||||
- '[["a"]] | community.general.lists_difference([["b"], ["a"]]) == []'
|
||||
- '[["a"]] | community.general.lists_difference([["b"]], ["a"]) == [["a"]]'
|
||||
- '[["a"]] | community.general.lists_difference(["b"], ["a"]) == [["a"]]'
|
||||
|
||||
- name: Test predictive list symmetric difference
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- 'list1 | community.general.lists_symmetric_difference(list2, list3) == [11, 1, 2, 4, 5, 101]'
|
||||
- '[list1, list2, list3] | community.general.lists_symmetric_difference(flatten=True) == [11, 1, 2, 4, 5, 101]'
|
||||
- '[1, 2, 3] | community.general.lists_symmetric_difference([4, 5, 6]) == [1, 2, 3, 4, 5, 6]'
|
||||
- '[1, 2, 3] | community.general.lists_symmetric_difference([3, 4, 5, 6]) == [1, 2, 4, 5, 6]'
|
||||
- '[1, 2, 3] | community.general.lists_symmetric_difference([3, 2, 1]) == []'
|
||||
- '["a", "A", "b"] | community.general.lists_symmetric_difference(["B", "c", "C"]) == ["a", "A", "b", "B", "c", "C"]'
|
||||
- '["a", "A", "b"] | community.general.lists_symmetric_difference(["b", "B", "c", "C"]) == ["a", "A", "B", "c", "C"]'
|
||||
- '["a", "A", "b"] | community.general.lists_symmetric_difference(["b", "A", "a"]) == []'
|
||||
- '[["a"]] | community.general.lists_symmetric_difference([["b"], ["a"]]) == [["b"]]'
|
||||
- '[["a"]] | community.general.lists_symmetric_difference([["b"]], ["a"]) == [["a"], ["b"], "a"]'
|
||||
- '[["a"]] | community.general.lists_symmetric_difference(["b"], ["a"]) == [["a"], "b", "a"]'
|
8
tests/integration/targets/filter_lists/vars/main.yml
Normal file
8
tests/integration/targets/filter_lists/vars/main.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
list1: [1, 2, 5, 3, 4, 10]
|
||||
list2: [1, 2, 3, 4, 5, 11, 99]
|
||||
list3: [1, 2, 4, 5, 10, 99, 101]
|
Loading…
Add table
Add a link
Reference in a new issue