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