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