xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/support/cc_info_subject.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"""CcInfo testing subject."""
15
16load("@rules_testing//lib:truth.bzl", "subjects")
17
18def cc_info_subject(info, *, meta):
19    """Creates a new `CcInfoSubject` for a CcInfo provider instance.
20
21    Args:
22        info: The CcInfo object.
23        meta: ExpectMeta object.
24
25    Returns:
26        A `CcInfoSubject` struct.
27    """
28
29    # buildifier: disable=uninitialized
30    public = struct(
31        # go/keep-sorted start
32        actual = info,
33        compilation_context = lambda *a, **k: _cc_info_subject_compilation_context(self, *a, **k),
34        linking_context = lambda *a, **k: _cc_info_subject_linking_context(self, *a, **k),
35        # go/keep-sorted end
36    )
37    self = struct(
38        actual = info,
39        meta = meta,
40    )
41    return public
42
43def _cc_info_subject_compilation_context(self):
44    """Returns the CcInfo.compilation_context as a subject.
45
46    Args:
47        self: implicitly added.
48
49    Returns:
50        [`CompilationContext`] instance.
51    """
52    return _compilation_context_subject_new(
53        self.actual.compilation_context,
54        meta = self.meta.derive("compilation_context()"),
55    )
56
57def _cc_info_subject_linking_context(self):
58    """Returns the CcInfo.linking_context as a subject.
59
60    Args:
61        self: implicitly added.
62
63    Returns:
64        [`LinkingContextSubject`] instance.
65    """
66    return _linking_context_subject_new(
67        self.actual.linking_context,
68        meta = self.meta.derive("linking_context()"),
69    )
70
71def _compilation_context_subject_new(info, *, meta):
72    """Creates a CompilationContextSubject.
73
74    Args:
75        info: ([`CompilationContext`]) object instance.
76        meta: rules_testing `ExpectMeta` instance.
77
78    Returns:
79        [`CompilationContextSubject`] object.
80    """
81
82    # buildifier: disable=uninitialized
83    public = struct(
84        # go/keep-sorted start
85        direct_headers = lambda *a, **k: _compilation_context_subject_direct_headers(self, *a, **k),
86        direct_public_headers = lambda *a, **k: _compilation_context_subject_direct_public_headers(self, *a, **k),
87        system_includes = lambda *a, **k: _compilation_context_subject_system_includes(self, *a, **k),
88        # go/keep-sorted end
89    )
90    self = struct(
91        actual = info,
92        meta = meta,
93    )
94    return public
95
96def _compilation_context_subject_direct_headers(self):
97    """Returns the direct headers as a subjecct.
98
99    Args:
100        self: implicitly added
101
102    Returns:
103        [`CollectionSubject`] of `File` objects of the direct headers.
104    """
105    return subjects.collection(
106        self.actual.direct_headers,
107        meta = self.meta.derive("direct_headers()"),
108        container_name = "direct_headers",
109        element_plural_name = "header files",
110    )
111
112def _compilation_context_subject_direct_public_headers(self):
113    """Returns the direct public headers as a subjecct.
114
115    Args:
116        self: implicitly added
117
118    Returns:
119        [`CollectionSubject`] of `File` objects of the direct headers.
120    """
121    return subjects.collection(
122        self.actual.direct_public_headers,
123        meta = self.meta.derive("direct_public_headers()"),
124        container_name = "direct_public_headers",
125        element_plural_name = "public header files",
126    )
127
128def _compilation_context_subject_system_includes(self):
129    """Returns the system include directories as a subject.
130
131    NOTE: The system includes are the `cc_library.includes` attribute.
132
133    Args:
134        self: implicitly added
135
136    Returns:
137        [`CollectionSubject`] of [`str`]
138    """
139    return subjects.collection(
140        self.actual.system_includes.to_list(),
141        meta = self.meta.derive("includes()"),
142        container_name = "includes",
143        element_plural_name = "include paths",
144    )
145
146def _linking_context_subject_new(info, meta):
147    """Creates a LinkingContextSubject.
148
149    Args:
150        info: ([`LinkingContext`]) object instance.
151        meta: rules_testing `ExpectMeta` instance.
152
153    Returns:
154        [`LinkingContextSubject`] object.
155    """
156
157    # buildifier: disable=uninitialized
158    public = struct(
159        # go/keep-sorted start
160        linker_inputs = lambda *a, **k: _linking_context_subject_linker_inputs(self, *a, **k),
161        # go/keep-sorted end
162    )
163    self = struct(
164        actual = info,
165        meta = meta,
166    )
167    return public
168
169def _linking_context_subject_linker_inputs(self):
170    """Returns the linker inputs.
171
172    Args:
173        self: implicitly added
174
175    Returns:
176        [`CollectionSubject`] of the linker inputs.
177    """
178    return subjects.collection(
179        self.actual.linker_inputs.to_list(),
180        meta = self.meta.derive("linker_inputs()"),
181        container_name = "linker_inputs",
182        element_plural_name = "linker input values",
183    )
184