MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / StringContainsTest

Class StringContainsTest

pythonwhat/Test.py:201–236  ·  view source on GitHub ↗

Check if a string is present in a text. Can use literal strings or a regex. Attributes: feedback (str): A string containing the failure message in case the test fails. string (regex/str): String or regular expression which is searched for. search_string (str): The

Source from the content-addressed store, hash-verified

199
200
201class StringContainsTest(Test):
202 """
203 Check if a string is present in a text. Can use literal strings or a regex.
204
205 Attributes:
206 feedback (str): A string containing the failure message in case the test fails.
207 string (regex/str): String or regular expression which is searched for.
208 search_string (str): The text in which is searched.
209 pattern (bool): If set to True, string is matched with a regex. Literal otherwise.
210 result (bool): True if the test succeed, False if it failed. None if it hasn't been tested yet.
211 """
212
213 def __init__(self, string, search_string, pattern, feedback):
214 """
215 Initialize with a string to look for, a string to search and whether or not to look for a pattern.
216
217 Args:
218 string (regex/str): The string to look for will be set to this.
219 search_string (str): The string to search in will be set to this.
220 pattern (bool): The pattern boolean will be set to this.
221 feedback (str): The failure message will be set to this.
222 """
223 super().__init__(feedback)
224 self.string = string
225 self.search_string = search_string
226 self.pattern = pattern
227
228 def test(self):
229 """
230 Perform the actual test. result will be True if string is found (whether or not with a pattern),
231 False otherwise.
232 """
233 if self.pattern:
234 self.result = re.search(self.search_string, self.string) is not None
235 else:
236 self.result = self.string.find(self.search_string) != -1

Callers 2

has_codeFunction · 0.90
has_outputFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected