xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/label_subject.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1*d6050574SRomain Jobredeaux# Copyright 2023 The Bazel Authors. All rights reserved.
2*d6050574SRomain Jobredeaux#
3*d6050574SRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*d6050574SRomain Jobredeaux# you may not use this file except in compliance with the License.
5*d6050574SRomain Jobredeaux# You may obtain a copy of the License at
6*d6050574SRomain Jobredeaux#
7*d6050574SRomain Jobredeaux#     http://www.apache.org/licenses/LICENSE-2.0
8*d6050574SRomain Jobredeaux#
9*d6050574SRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*d6050574SRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*d6050574SRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d6050574SRomain Jobredeaux# See the License for the specific language governing permissions and
13*d6050574SRomain Jobredeaux# limitations under the License.
14*d6050574SRomain Jobredeaux
15*d6050574SRomain Jobredeaux"""# LabelSubject"""
16*d6050574SRomain Jobredeaux
17*d6050574SRomain Jobredeauxload("@bazel_skylib//lib:types.bzl", "types")
18*d6050574SRomain Jobredeauxload(":check_util.bzl", "common_subject_is_in")
19*d6050574SRomain Jobredeauxload(":truth_common.bzl", "to_list")
20*d6050574SRomain Jobredeaux
21*d6050574SRomain Jobredeauxdef _label_subject_new(label, meta):
22*d6050574SRomain Jobredeaux    """Creates a new `LabelSubject` for asserting `Label` objects.
23*d6050574SRomain Jobredeaux
24*d6050574SRomain Jobredeaux    Method: LabelSubject.new
25*d6050574SRomain Jobredeaux
26*d6050574SRomain Jobredeaux    Args:
27*d6050574SRomain Jobredeaux        label: ([`Label`]) the label to check against.
28*d6050574SRomain Jobredeaux        meta: ([`ExpectMeta`]) the metadata about the call chain.
29*d6050574SRomain Jobredeaux
30*d6050574SRomain Jobredeaux    Returns:
31*d6050574SRomain Jobredeaux        [`LabelSubject`].
32*d6050574SRomain Jobredeaux    """
33*d6050574SRomain Jobredeaux
34*d6050574SRomain Jobredeaux    # buildifier: disable=uninitialized
35*d6050574SRomain Jobredeaux    public = struct(
36*d6050574SRomain Jobredeaux        # keep sorted start
37*d6050574SRomain Jobredeaux        equals = lambda *a, **k: _label_subject_equals(self, *a, **k),
38*d6050574SRomain Jobredeaux        is_in = lambda *a, **k: _label_subject_is_in(self, *a, **k),
39*d6050574SRomain Jobredeaux        # keep sorted end
40*d6050574SRomain Jobredeaux    )
41*d6050574SRomain Jobredeaux    self = struct(actual = label, meta = meta)
42*d6050574SRomain Jobredeaux    return public
43*d6050574SRomain Jobredeaux
44*d6050574SRomain Jobredeauxdef _label_subject_equals(self, other):
45*d6050574SRomain Jobredeaux    """Asserts the label is equal to `other`.
46*d6050574SRomain Jobredeaux
47*d6050574SRomain Jobredeaux    Method: LabelSubject.equals
48*d6050574SRomain Jobredeaux
49*d6050574SRomain Jobredeaux    Args:
50*d6050574SRomain Jobredeaux        self: implicitly added.
51*d6050574SRomain Jobredeaux        other: ([`Label`] | [`str`]) the expected value. If a `str` is passed, it
52*d6050574SRomain Jobredeaux            will be converted to a `Label` using the `Label` function.
53*d6050574SRomain Jobredeaux    """
54*d6050574SRomain Jobredeaux    if types.is_string(other):
55*d6050574SRomain Jobredeaux        other = Label(other)
56*d6050574SRomain Jobredeaux    if self.actual == other:
57*d6050574SRomain Jobredeaux        return
58*d6050574SRomain Jobredeaux    self.meta.add_failure(
59*d6050574SRomain Jobredeaux        "expected: {}".format(other),
60*d6050574SRomain Jobredeaux        "actual: {}".format(self.actual),
61*d6050574SRomain Jobredeaux    )
62*d6050574SRomain Jobredeaux
63*d6050574SRomain Jobredeauxdef _label_subject_is_in(self, any_of):
64*d6050574SRomain Jobredeaux    """Asserts that the label is any of the provided values.
65*d6050574SRomain Jobredeaux
66*d6050574SRomain Jobredeaux    Args:
67*d6050574SRomain Jobredeaux        self: implicitly added.
68*d6050574SRomain Jobredeaux        any_of: ([`collection`] of ([`Label`] | [`str`])) If strings are
69*d6050574SRomain Jobredeaux            provided, they must be parsable by `Label`.
70*d6050574SRomain Jobredeaux    """
71*d6050574SRomain Jobredeaux    any_of = [
72*d6050574SRomain Jobredeaux        Label(v) if types.is_string(v) else v
73*d6050574SRomain Jobredeaux        for v in to_list(any_of)
74*d6050574SRomain Jobredeaux    ]
75*d6050574SRomain Jobredeaux    common_subject_is_in(self, any_of)
76*d6050574SRomain Jobredeaux
77*d6050574SRomain Jobredeaux# We use this name so it shows up nice in docs.
78*d6050574SRomain Jobredeaux# buildifier: disable=name-conventions
79*d6050574SRomain JobredeauxLabelSubject = struct(
80*d6050574SRomain Jobredeaux    new = _label_subject_new,
81*d6050574SRomain Jobredeaux    equals = _label_subject_equals,
82*d6050574SRomain Jobredeaux    is_in = _label_subject_is_in,
83*d6050574SRomain Jobredeaux)
84