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