xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/ordered.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"""# Ordered"""
16
17# This is just a stub so doc generation is nicer.
18def _ordered_in_order():
19    """Checks that the values were in order."""
20
21IN_ORDER = struct(
22    in_order = _ordered_in_order,
23)
24
25def _ordered_incorrectly_new(format_problem, format_actual, meta):
26    """Creates a new `Ordered` object that fails due to incorrectly ordered values.
27
28    This creates an [`Ordered`] object that always fails. If order is correct,
29    use the `_IN_ORDER` constant.
30
31    Args:
32        format_problem: (callable) accepts no args and returns string (the
33            reported problem description).
34        format_actual: (callable) accepts not args and returns string (the
35            reported actual description).
36        meta: ([`ExpectMeta`]) used to report the failure.
37
38    Returns:
39        [`Ordered`] object.
40    """
41    self = struct(
42        meta = meta,
43        format_problem = format_problem,
44        format_actual = format_actual,
45    )
46    public = struct(
47        in_order = lambda *a, **k: _ordered_incorrectly_in_order(self, *a, **k),
48    )
49    return public
50
51def _ordered_incorrectly_in_order(self):
52    """Checks that the values were in order.
53
54    Args:
55        self: implicitly added.
56    """
57    self.meta.add_failure(self.format_problem(), self.format_actual())
58
59# We use this name so it shows up nice in docs.
60# buildifier: disable=name-conventions
61OrderedIncorrectly = struct(
62    new = _ordered_incorrectly_new,
63    in_order = _ordered_incorrectly_in_order,
64)
65