xref: /aosp_15_r20/external/bazel-skylib/tests/structs_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 structs.bzl."""
16
17load("//lib:structs.bzl", "structs")
18load("//lib:unittest.bzl", "asserts", "unittest")
19
20def _add_test(ctx):
21    """Unit tests for dicts.add."""
22    env = unittest.begin(ctx)
23
24    # Test zero- and one-argument behavior.
25    asserts.equals(env, {}, structs.to_dict(struct()))
26    asserts.equals(env, {"a": 1}, structs.to_dict(struct(a = 1)))
27
28    # Test simple two-argument behavior.
29    asserts.equals(env, {"a": 1, "b": 2}, structs.to_dict(struct(a = 1, b = 2)))
30
31    # Test simple more-than-two-argument behavior.
32    asserts.equals(
33        env,
34        {"a": 1, "b": 2, "c": 3, "d": 4},
35        structs.to_dict(struct(a = 1, b = 2, c = 3, d = 4)),
36    )
37
38    # Test transformation is not applied transitively.
39    asserts.equals(
40        env,
41        {"a": 1, "b": struct(bb = 1)},
42        structs.to_dict(struct(a = 1, b = struct(bb = 1))),
43    )
44
45    return unittest.end(env)
46
47add_test = unittest.make(_add_test)
48
49def structs_test_suite():
50    """Creates the test targets and test suite for structs.bzl tests."""
51    unittest.suite(
52        "structs_tests",
53        add_test,
54    )
55