xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/text_util/render_tests.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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""
16
17load("@rules_testing//lib:test_suite.bzl", "test_suite")
18load("//python/private:text_util.bzl", "render")  # buildifier: disable=bzl-visibility
19
20_tests = []
21
22def _test_render_alias(env):
23    tests = [
24        struct(
25            args = dict(
26                name = "foo",
27                actual = repr("bar"),
28            ),
29            want = [
30                "alias(",
31                '    name = "foo",',
32                '    actual = "bar",',
33                ")",
34            ],
35        ),
36        struct(
37            args = dict(
38                name = "foo",
39                actual = repr("bar"),
40                visibility = ["//:__pkg__"],
41            ),
42            want = [
43                "alias(",
44                '    name = "foo",',
45                '    actual = "bar",',
46                '    visibility = ["//:__pkg__"],',
47                ")",
48            ],
49        ),
50    ]
51    for test in tests:
52        got = render.alias(**test.args)
53        env.expect.that_str(got).equals("\n".join(test.want).strip())
54
55_tests.append(_test_render_alias)
56
57def _test_render_tuple_dict(env):
58    got = render.dict(
59        {
60            ("foo", "bar"): "baz",
61            ("foo",): "bar",
62        },
63        key_repr = render.tuple,
64    )
65    env.expect.that_str(got).equals("""\
66{
67    (
68        "foo",
69        "bar",
70    ): "baz",
71    ("foo",): "bar",
72}""")
73
74_tests.append(_test_render_tuple_dict)
75
76def render_test_suite(name):
77    """Create the test suite.
78
79    Args:
80        name: the name of the test suite
81    """
82    test_suite(name = name, basic_tests = _tests)
83