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:
cfiehe 2024-02-23 20:35:09 +01:00 committed by GitHub
commit 102a0857db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 573 additions and 0 deletions

View 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

View 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"]'

View 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]