1# Lint as: python2, python3 2# Copyright 2021 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Comparators that be used inplace of mox.<comparator>.""" 6 7 8class IsA(): 9 """Helper class to check whether a class is an instance of another class. 10 11 Class to help replace mox.IsA. Defines the __eq__ and equals. 12 Use to compare to str to see if the other string contains this substr. 13 Example: 14 foo = IsA(host) 15 print(host == foo) 16 >>> True 17 """ 18 19 def __init__(self, arg): 20 self.arg = arg 21 22 def __eq__(self, other): 23 return self.arg == other 24 25 def equals(self, other): 26 """Wrapper for __eq__.""" 27 return self.__eq__(other) 28 29 30class Substrings: 31 """Class for to simplify multiple substring checks.""" 32 33 def __init__(self, substrings): 34 self._substrings = substrings 35 36 def __eq__(self, rhs): 37 """Return true iff all of _substrings are in the other string.""" 38 if not isinstance(rhs, str): 39 return False 40 return all(substr in rhs for substr in self._substrings) 41 42 43class Substring: 44 """Helper class to check whether a substring exists in a string parameter. 45 46 Class to help replace mox.StrContains. Defines the __eq__ and equals. 47 Use to compare to str to see if the other string contains this substr. 48 Example: 49 foobar = Substring("foobar") 50 print(foo == "foobarfizzbuzz") 51 >>> True 52 print(foo == "fizzfoobarbuzz") 53 >>> True 54 print(foo == "barfoofizzbuzz") 55 >>> False 56 """ 57 58 def __init__(self, _substr): 59 if not isinstance(_substr, str): 60 raise TypeError("Substring must be of type str") 61 62 self._substr = _substr 63 64 def __eq__(self, rhs): 65 if not isinstance(rhs, str): 66 return False 67 return self._substr in str(rhs) 68 69 def equals(self, rhs): 70 """Wrapper for __eq__.""" 71 return self.__eq__(rhs) 72