xref: /aosp_15_r20/external/bazel-skylib/tests/collections_tests.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2017 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unit tests for collections.bzl."""
16
17load("//lib:collections.bzl", "collections")
18load("//lib:unittest.bzl", "asserts", "unittest")
19
20def _after_each_test(ctx):
21    """Unit tests for collections.after_each."""
22    env = unittest.begin(ctx)
23
24    asserts.equals(env, [], collections.after_each("1", []))
25    asserts.equals(env, ["a", "1"], collections.after_each("1", ["a"]))
26    asserts.equals(
27        env,
28        ["a", "1", "b", "1"],
29        collections.after_each("1", ["a", "b"]),
30    )
31
32    # We don't care what type the separator is, we just put it there; so None
33    # should be just as valid as anything else.
34    asserts.equals(
35        env,
36        ["a", None, "b", None],
37        collections.after_each(None, ["a", "b"]),
38    )
39
40    return unittest.end(env)
41
42after_each_test = unittest.make(_after_each_test)
43
44def _before_each_test(ctx):
45    """Unit tests for collections.before_each."""
46    env = unittest.begin(ctx)
47
48    asserts.equals(env, [], collections.before_each("1", []))
49    asserts.equals(env, ["1", "a"], collections.before_each("1", ["a"]))
50    asserts.equals(
51        env,
52        ["1", "a", "1", "b"],
53        collections.before_each("1", ["a", "b"]),
54    )
55
56    # We don't care what type the separator is, we just put it there; so None
57    # should be just as valid as anything else.
58    asserts.equals(
59        env,
60        [None, "a", None, "b"],
61        collections.before_each(None, ["a", "b"]),
62    )
63
64    return unittest.end(env)
65
66before_each_test = unittest.make(_before_each_test)
67
68def _uniq_test(ctx):
69    env = unittest.begin(ctx)
70    asserts.equals(env, collections.uniq([0, 1, 2, 3]), [0, 1, 2, 3])
71    asserts.equals(env, collections.uniq([]), [])
72    asserts.equals(env, collections.uniq([1, 1, 1, 1, 1]), [1])
73    asserts.equals(
74        env,
75        collections.uniq([
76            True,
77            5,
78            "foo",
79            5,
80            False,
81            struct(a = 1),
82            True,
83            struct(b = 2),
84            "bar",
85            (1,),
86            "foo",
87            struct(a = 1),
88            (1,),
89        ]),
90        [
91            True,
92            5,
93            "foo",
94            False,
95            struct(a = 1),
96            struct(b = 2),
97            "bar",
98            (1,),
99        ],
100    )
101
102    return unittest.end(env)
103
104uniq_test = unittest.make(_uniq_test)
105
106def collections_test_suite():
107    """Creates the test targets and test suite for collections.bzl tests."""
108    unittest.suite(
109        "collections_tests",
110        after_each_test,
111        before_each_test,
112        uniq_test,
113    )
114