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"""# IntSubject""" 16 17load("@bazel_skylib//lib:types.bzl", "types") 18load(":check_util.bzl", "check_not_equals", "common_subject_is_in") 19load(":truth_common.bzl", "repr_with_type") 20 21def _int_subject_new(value, meta): 22 """Create an "IntSubject" struct. 23 24 Method: IntSubject.new 25 26 Args: 27 value: (optional [`int`]) the value to perform asserts against may be None. 28 meta: ([`ExpectMeta`]) the meta data about the call chain. 29 30 Returns: 31 [`IntSubject`]. 32 """ 33 if not types.is_int(value) and value != None: 34 fail("int required, got: {}".format(repr_with_type(value))) 35 36 # buildifier: disable=uninitialized 37 public = struct( 38 # keep sorted start 39 equals = lambda *a, **k: _int_subject_equals(self, *a, **k), 40 is_greater_than = lambda *a, **k: _int_subject_is_greater_than(self, *a, **k), 41 is_in = lambda *a, **k: common_subject_is_in(self, *a, **k), 42 not_equals = lambda *a, **k: _int_subject_not_equals(self, *a, **k), 43 # keep sorted end 44 ) 45 self = struct(actual = value, meta = meta) 46 return public 47 48def _int_subject_equals(self, other): 49 """Assert that the subject is equal to the given value. 50 51 Method: IntSubject.equals 52 53 Args: 54 self: implicitly added. 55 other: ([`int`]) value the subject must be equal to. 56 """ 57 if self.actual == other: 58 return 59 self.meta.add_failure( 60 "expected: {}".format(other), 61 "actual: {}".format(self.actual), 62 ) 63 64def _int_subject_is_greater_than(self, other): 65 """Asserts that the subject is greater than the given value. 66 67 Method: IntSubject.is_greater_than 68 69 Args: 70 self: implicitly added. 71 other: ([`int`]) value the subject must be greater than. 72 """ 73 if self.actual != None and other != None and self.actual > other: 74 return 75 self.meta.add_failure( 76 "expected to be greater than: {}".format(other), 77 "actual: {}".format(self.actual), 78 ) 79 80def _int_subject_not_equals(self, unexpected): 81 """Assert that the int is not equal to `unexpected`. 82 83 Method: IntSubject.not_equals 84 85 Args: 86 self: implicitly added 87 unexpected: ([`int`]) the value actual cannot equal. 88 """ 89 return check_not_equals( 90 actual = self.actual, 91 unexpected = unexpected, 92 meta = self.meta, 93 ) 94 95# We use this name so it shows up nice in docs. 96# buildifier: disable=name-conventions 97IntSubject = struct( 98 new = _int_subject_new, 99 equals = _int_subject_equals, 100 is_greater_than = _int_subject_is_greater_than, 101 not_equals = _int_subject_not_equals, 102) 103