Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def contains(subseq, inseq):
return any(inseq[pos:pos + len(subseq)] == subseq for pos in range(0, len(inseq) - len(subseq) + 1))
class TestContainsForString(unittest.TestCase):
def test_smoketest(self):
self.assertTrue(contains('abc', 'abc cde fgi'))
def testcontains_middle_seq(self):
self.assertTrue(contains('cde', 'abc cde fgi'))
def testcontains_end_seq(self):
self.assertTrue(contains('fgi', 'abc cde fgi'))
def testcontains_oversized_seq(self):
self.assertFalse(contains('abc cde fgi!', 'abc cde fgi'))
def testcontains_iteslf(self):
self.assertTrue(contains('abc cde fgi', 'abc cde fgi'))
class TestContainsForList(unittest.TestCase):
def test_smoketest(self):
self.assertTrue(contains('ab c'.split(' '), 'ab c cde fgi'.split(' ')))
def testcontains_middle_seq(self):
self.assertTrue(contains('cd e'.split(' '), 'abc cd e fgi'.split(' ')))
def testcontains_end_seq(self):
self.assertTrue(contains('f gi'.split(' '), 'abc cde f gi'.split(' ')))
def testcontains_oversized_seq(self):
self.assertFalse(contains('abc cde fgi fgi'.split(' '), 'abc cde fgi'.split(' ')))
def testcontains_iteslf(self):
self.assertTrue(contains(*['abc cde fgi'] * 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment