1# Copyright (C) 2022 The Android Open Source Project
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
15load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
16load(":constants.bzl", "power_set")
17
18def _power_set_test(ctx):
19    env = unittest.begin(ctx)
20
21    actual = power_set(ctx.attr.items, include_empty = ctx.attr.include_empty)
22    expected = json.decode(ctx.attr.expected_value_json)
23
24    asserts.equals(env, expected, actual, "expected power_set({items}) to be {expected}, got {actual}".format(
25        items = ctx.attr.items,
26        expected = expected,
27        actual = actual,
28    ))
29
30    return unittest.end(env)
31
32power_set_test = unittest.make(
33    _power_set_test,
34    attrs = {
35        "items": attr.string_list(doc = "Input to the power set function"),
36        "include_empty": attr.bool(doc = "The include_empty argument to the power set function", default = True),
37        "expected_value_json": attr.string(doc = "Expected output as a json-encoded string because attributes can't be a list of lists of strings"),
38    },
39)
40
41def _power_set_tests():
42    power_set_test(
43        name = "power_set_test_0",
44        items = ["a", "b", "c"],
45        include_empty = True,
46        expected_value_json = json.encode([[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]),
47    )
48    power_set_test(
49        name = "power_set_test_1",
50        items = ["a", "b", "c"],
51        include_empty = False,
52        expected_value_json = json.encode([["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]),
53    )
54    power_set_test(
55        name = "power_set_test_2",
56        items = [],
57        include_empty = True,
58        expected_value_json = json.encode([[]]),
59    )
60    power_set_test(
61        name = "power_set_test_3",
62        items = [],
63        include_empty = False,
64        expected_value_json = json.encode([]),
65    )
66    power_set_test(
67        name = "power_set_test_4",
68        items = ["a"],
69        include_empty = True,
70        expected_value_json = json.encode([[], ["a"]]),
71    )
72    power_set_test(
73        name = "power_set_test_5",
74        items = ["a"],
75        include_empty = False,
76        expected_value_json = json.encode([["a"]]),
77    )
78
79    return ["power_set_test_" + str(i) for i in range(6)]
80
81def power_set_test_suite(name):
82    native.test_suite(
83        name = name,
84        tests = _power_set_tests(),
85    )
86