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