xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/bool_subject.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
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"""# BoolSubject"""
16
17load(":check_util.bzl", "check_not_equals", "common_subject_is_in")
18
19def _bool_subject_new(value, meta):
20    """Creates a "BoolSubject" struct.
21
22    Method: BoolSubject.new
23
24    Args:
25        value: ([`bool`]) the value to assert against.
26        meta: ([`ExpectMeta`]) the metadata about the call chain.
27
28    Returns:
29        A [`BoolSubject`].
30    """
31    self = struct(actual = value, meta = meta)
32    public = struct(
33        # keep sorted start
34        equals = lambda *a, **k: _bool_subject_equals(self, *a, **k),
35        is_in = lambda *a, **k: common_subject_is_in(self, *a, **k),
36        not_equals = lambda *a, **k: _bool_subject_not_equals(self, *a, **k),
37        # keep sorted end
38    )
39    return public
40
41def _bool_subject_equals(self, expected):
42    """Assert that the bool is equal to `expected`.
43
44    Method: BoolSubject.equals
45
46    Args:
47        self: implicitly added.
48        expected: ([`bool`]) the expected value.
49    """
50    if self.actual == expected:
51        return
52    self.meta.add_failure(
53        "expected: {}".format(expected),
54        "actual: {}".format(self.actual),
55    )
56
57def _bool_subject_not_equals(self, unexpected):
58    """Assert that the bool is not equal to `unexpected`.
59
60    Method: BoolSubject.not_equals
61
62    Args:
63        self: implicitly added.
64        unexpected: ([`bool`]) the value actual cannot equal.
65    """
66    return check_not_equals(
67        actual = self.actual,
68        unexpected = unexpected,
69        meta = self.meta,
70    )
71
72# We use this name so it shows up nice in docs.
73# buildifier: disable=name-conventions
74BoolSubject = struct(
75    new = _bool_subject_new,
76    equals = _bool_subject_equals,
77    not_equals = _bool_subject_not_equals,
78)
79