1# Copyright (C) 2023 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", "analysistest", "asserts")
16load(":cc_xsd_config_library.bzl", "cc_xsd_config_library")
17
18def _xsd_config_generates_cpp_outputs_test_impl(ctx):
19    env = analysistest.begin(ctx)
20
21    actions = analysistest.target_actions(env)
22
23    asserts.equals(
24        env,
25        1,
26        len(actions),
27        "Incorrect number of actions",
28    )
29
30    action_outputs = actions[0].outputs.to_list()
31
32    asserts.equals(
33        env,
34        len(ctx.attr.expected_output_filenames),
35        len(action_outputs),
36        "Incorrect number of action outputs",
37    )
38
39    for i in range(0, len(ctx.attr.expected_output_filenames)):
40        asserts.equals(
41            env,
42            ctx.attr.expected_output_filenames[i],
43            action_outputs[i].basename,
44        )
45
46    return analysistest.end(env)
47
48_xsd_config_generates_cpp_outputs_test = analysistest.make(
49    _xsd_config_generates_cpp_outputs_test_impl,
50    attrs = {
51        "expected_output_filenames": attr.string_list(),
52    },
53)
54
55def _xsd_config_generates_parser_and_enums_by_default():
56    subject_name = "xsd_config_parser_and_enum"
57
58    # Test the internal _cc_yacc_parser_gen created by cc_xsd_config_library
59    test_subject_name = subject_name + "_gen"
60    test_name = subject_name + "_test"
61
62    cc_xsd_config_library(
63        name = subject_name,
64        src = "foo.xsd",
65        package_name = "foo",
66        tags = ["manual"],
67    )
68    _xsd_config_generates_cpp_outputs_test(
69        name = test_name,
70        target_under_test = test_subject_name,
71        expected_output_filenames = ["foo.cpp", "foo_enums.cpp", "foo.h", "foo_enums.h"],
72    )
73    return test_name
74
75def _xsd_config_generates_parser_only():
76    subject_name = "xsd_config_parser_only"
77
78    # Test the internal _cc_yacc_parser_gen created by cc_xsd_config_library
79    test_subject_name = subject_name + "_gen"
80    test_name = subject_name + "_test"
81
82    cc_xsd_config_library(
83        name = subject_name,
84        src = "foo.xsd",
85        package_name = "foo",
86        parser_only = True,
87        tags = ["manual"],
88    )
89    _xsd_config_generates_cpp_outputs_test(
90        name = test_name,
91        target_under_test = test_subject_name,
92        expected_output_filenames = ["foo.cpp", "foo.h", "foo_enums.h"],
93    )
94    return test_name
95
96def _xsd_config_generates_enums_only():
97    subject_name = "xsd_config_enums_only"
98
99    # Test the internal _cc_yacc_parser_gen created by cc_xsd_config_library
100    test_subject_name = subject_name + "_gen"
101    test_name = subject_name + "_test"
102
103    cc_xsd_config_library(
104        name = subject_name,
105        src = "foo.xsd",
106        package_name = "foo",
107        enums_only = True,
108        tags = ["manual"],
109    )
110    _xsd_config_generates_cpp_outputs_test(
111        name = test_name,
112        target_under_test = test_subject_name,
113        expected_output_filenames = ["foo_enums.cpp", "foo_enums.h"],
114    )
115    return test_name
116
117def cc_xsd_config_library_test_suite(name):
118    native.test_suite(
119        name = name,
120        tests = [
121            _xsd_config_generates_parser_and_enums_by_default(),
122            _xsd_config_generates_parser_only(),
123            _xsd_config_generates_enums_only(),
124        ],
125    )
126