MCPcopy Index your code
hub / github.com/python/cpython / test_index

Method test_index

Lib/test/string_tests.py:292–328  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

290 self.checkequal(0, '<......\u043c...', "rfind", "<")
291
292 def test_index(self):
293 self.checkequal(0, 'abcdefghiabc', 'index', '')
294 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
295 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
296 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
297
298 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
299 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
300 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
301 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
302
303 # to check the ability to pass None as defaults
304 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
305 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
306 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
307 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
308 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
309
310 self.checkraises(TypeError, 'hello', 'index')
311
312 if self.contains_bytes:
313 self.checkraises(ValueError, 'hello', 'index', 42)
314 else:
315 self.checkraises(TypeError, 'hello', 'index', 42)
316
317 # For a variety of combinations,
318 # verify that str.index() matches __contains__
319 # and that the found substring is really at that location
320 teststrings = self._get_teststrings(['', 'a', 'b', 'c'], 5)
321 for i in teststrings:
322 for j in teststrings:
323 if j in i:
324 loc = i.index(j)
325 self.assertGreaterEqual(loc, 0)
326 self.assertEqual(i[loc:loc+len(j)], j)
327 else:
328 self.assertRaises(ValueError, i.index, j)
329
330 def test_rindex(self):
331 self.checkequal(12, 'abcdefghiabc', 'rindex', '')

Callers

nothing calls this directly

Calls 7

checkequalMethod · 0.95
checkraisesMethod · 0.95
_get_teststringsMethod · 0.95
assertRaisesMethod · 0.95
assertGreaterEqualMethod · 0.80
indexMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected