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"""# StrSubject""" 16*d6050574SRomain Jobredeaux 17*d6050574SRomain Jobredeauxload( 18*d6050574SRomain Jobredeaux ":check_util.bzl", 19*d6050574SRomain Jobredeaux "check_not_equals", 20*d6050574SRomain Jobredeaux "common_subject_is_in", 21*d6050574SRomain Jobredeaux) 22*d6050574SRomain Jobredeauxload(":collection_subject.bzl", "CollectionSubject") 23*d6050574SRomain Jobredeaux 24*d6050574SRomain Jobredeauxdef _str_subject_new(actual, meta): 25*d6050574SRomain Jobredeaux """Creates a subject for asserting strings. 26*d6050574SRomain Jobredeaux 27*d6050574SRomain Jobredeaux Method: StrSubject.new 28*d6050574SRomain Jobredeaux 29*d6050574SRomain Jobredeaux Args: 30*d6050574SRomain Jobredeaux actual: ([`str`]) the string to check against. 31*d6050574SRomain Jobredeaux meta: ([`ExpectMeta`]) of call chain information. 32*d6050574SRomain Jobredeaux 33*d6050574SRomain Jobredeaux Returns: 34*d6050574SRomain Jobredeaux [`StrSubject`] object. 35*d6050574SRomain Jobredeaux """ 36*d6050574SRomain Jobredeaux self = struct(actual = actual, meta = meta) 37*d6050574SRomain Jobredeaux public = struct( 38*d6050574SRomain Jobredeaux # keep sorted start 39*d6050574SRomain Jobredeaux contains = lambda *a, **k: _str_subject_contains(self, *a, **k), 40*d6050574SRomain Jobredeaux equals = lambda *a, **k: _str_subject_equals(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: _str_subject_not_equals(self, *a, **k), 43*d6050574SRomain Jobredeaux split = lambda *a, **k: _str_subject_split(self, *a, **k), 44*d6050574SRomain Jobredeaux # keep sorted end 45*d6050574SRomain Jobredeaux ) 46*d6050574SRomain Jobredeaux return public 47*d6050574SRomain Jobredeaux 48*d6050574SRomain Jobredeauxdef _str_subject_contains(self, substr): 49*d6050574SRomain Jobredeaux """Assert that the subject contains the substring `substr`. 50*d6050574SRomain Jobredeaux 51*d6050574SRomain Jobredeaux Method: StrSubject.contains 52*d6050574SRomain Jobredeaux 53*d6050574SRomain Jobredeaux Args: 54*d6050574SRomain Jobredeaux self: implicitly added. 55*d6050574SRomain Jobredeaux substr: ([`str`]) the substring to check for. 56*d6050574SRomain Jobredeaux """ 57*d6050574SRomain Jobredeaux if substr in self.actual: 58*d6050574SRomain Jobredeaux return 59*d6050574SRomain Jobredeaux self.meta.add_failure( 60*d6050574SRomain Jobredeaux "expected to contain: {}".format(substr), 61*d6050574SRomain Jobredeaux "actual: {}".format(self.actual), 62*d6050574SRomain Jobredeaux ) 63*d6050574SRomain Jobredeaux 64*d6050574SRomain Jobredeauxdef _str_subject_equals(self, other): 65*d6050574SRomain Jobredeaux """Assert that the subject string equals the other string. 66*d6050574SRomain Jobredeaux 67*d6050574SRomain Jobredeaux Method: StrSubject.equals 68*d6050574SRomain Jobredeaux 69*d6050574SRomain Jobredeaux Args: 70*d6050574SRomain Jobredeaux self: implicitly added. 71*d6050574SRomain Jobredeaux other: ([`str`]) the expected value it should equal. 72*d6050574SRomain Jobredeaux """ 73*d6050574SRomain Jobredeaux if self.actual == other: 74*d6050574SRomain Jobredeaux return 75*d6050574SRomain Jobredeaux self.meta.add_failure( 76*d6050574SRomain Jobredeaux "expected: {}".format(other), 77*d6050574SRomain Jobredeaux "actual: {}".format(self.actual), 78*d6050574SRomain Jobredeaux ) 79*d6050574SRomain Jobredeaux 80*d6050574SRomain Jobredeauxdef _str_subject_not_equals(self, unexpected): 81*d6050574SRomain Jobredeaux """Assert that the string is not equal to `unexpected`. 82*d6050574SRomain Jobredeaux 83*d6050574SRomain Jobredeaux Method: BoolSubject.not_equals 84*d6050574SRomain Jobredeaux 85*d6050574SRomain Jobredeaux Args: 86*d6050574SRomain Jobredeaux self: implicitly added. 87*d6050574SRomain Jobredeaux unexpected: ([`str`]) 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 Jobredeauxdef _str_subject_split(self, sep): 96*d6050574SRomain Jobredeaux """Return a `CollectionSubject` for the actual string split by `sep`. 97*d6050574SRomain Jobredeaux 98*d6050574SRomain Jobredeaux Method: StrSubject.split 99*d6050574SRomain Jobredeaux """ 100*d6050574SRomain Jobredeaux return CollectionSubject.new( 101*d6050574SRomain Jobredeaux self.actual.split(sep), 102*d6050574SRomain Jobredeaux meta = self.meta.derive("split({})".format(repr(sep))), 103*d6050574SRomain Jobredeaux container_name = "split string", 104*d6050574SRomain Jobredeaux sortable = False, 105*d6050574SRomain Jobredeaux element_plural_name = "parts", 106*d6050574SRomain Jobredeaux ) 107*d6050574SRomain Jobredeaux 108*d6050574SRomain Jobredeaux# We use this name so it shows up nice in docs. 109*d6050574SRomain Jobredeaux# buildifier: disable=name-conventions 110*d6050574SRomain JobredeauxStrSubject = struct( 111*d6050574SRomain Jobredeaux new = _str_subject_new, 112*d6050574SRomain Jobredeaux contains = _str_subject_contains, 113*d6050574SRomain Jobredeaux equals = _str_subject_equals, 114*d6050574SRomain Jobredeaux not_equals = _str_subject_not_equals, 115*d6050574SRomain Jobredeaux split = _str_subject_split, 116*d6050574SRomain Jobredeaux) 117