def test_repr(self): exp = '(foo (bar)).*(foo bar)' text = 'foo bar is grouped differently than foo bar' searched = re.search(exp, text) group = Group(text, searched, groups(searched)) expected = "[[foo [bar#2]#1] is grouped differently than [foo bar#3]#0]" self.assertEqual(group.repr, expected)
def test_or(self): exp = r'(other)|(foo \d)|(foo 9)' text = 'foo 9' searched = re.search(exp, text) group = Group(text, searched, groups(searched)) # Interestingly, re stops matching once something has been # matched. This makes sense, as it will improve performance self.assertEqual({0, 2}, group.indexes) self.assertEqual(2, group.index)
def test_embedded(self): exp = '(foo (bar)).*(foo bar)' text = 'foo bar is grouped differently than foo bar' searched = re.search(exp, text) grps = groups(searched) self.assertEqual(grps, (text, 'foo bar', 'bar', 'foo bar')) group = Group(text, searched, grps) matches = group self.assertEqual(matches[0].text, 'foo bar') self.assertEqual(matches[0].indexes, {1}) self.assertEqual(matches[0][0], 'foo ') self.assertEqual(matches[0][1].text, 'bar') self.assertEqual(matches[0][1].indexes, {2}) self.assertIsInstance(matches[1], str) self.assertEqual(matches[1], ' is grouped differently than ') self.assertEqual(matches[2].text, 'foo bar') self.assertEqual(matches[2].indexes, {3})
def test_basic(self): exp = '(foo).*(bar)' text = 'foo is the opposite of bar' searched = re.search(exp, text) grps = groups(searched) self.assertEqual(grps, (text, 'foo', 'bar')) group = Group(text, searched, grps) matches = group self.assertEqual(group.text, text) self.assertEqual(group.indexes, {0}) self.assertEqual(matches[0].text, 'foo') self.assertEqual(matches[0].indexes, {1}) self.assertEqual(matches[1], ' is the opposite of ') self.assertEqual(matches[2].text, 'bar') self.assertEqual(matches[2].indexes, {2}) self.assertEqual(matches[0].index, 1) self.assertEqual(matches[2].index, 2)
def test_str(self): exp = '(foo (bar)).*(foo bar)' text = 'foo bar is grouped differently than foo bar' searched = re.search(exp, text) group = Group(text, searched, groups(searched)) self.assertEqual(group.str, text)