def test_get_single_group_named_groups(self): """ get_single_group should work the same with named groups """ m = re.search("% (?P<tag>\w+) (?P<val>\w+) %", "dumdummy % test val % mudmud") self.assertEqual("test", helpers.get_single_group(m, "tag")) self.assertEqual("test", helpers.get_single_group(m)) self.assertEqual("val", helpers.get_single_group(m, "val")) self.assertEqual("val", helpers.get_single_group(m, 2))
def sub(match, **context): """ Default tag processor. Returns the appropriate value from **context for a matched tag. """ tag = helpers.get_single_group(match) if re.search(r"\[.+\]|\.", tag): # Attribute/Indice lookup val = utils.unicode(eval(tag, {"__builtins__": None}, context)) else: # Straight value val = utils.unicode(context.get(tag, "")) # TODO: Error check if not val and tag not in context.keys(): warnings.warn( "No context variable matched the tag %s" % tag, ContextWarning ) return val
def test_get_single_group_several_groups_no_key(self): """ get_single_group should default to the first group if several are defined """ m = re.search("% (\w+) (\w+) %", "dumdummy % test foo % mudmud") self.assertEqual("test", helpers.get_single_group(m))
def test_get_single_group_several(self): """ get_single_group with several groups defined """ m = re.search("% (\w+) (\w+) %", "dumdummy % test foo % mudmud") self.assertEqual("test", helpers.get_single_group(m, 1)) self.assertEqual("foo", helpers.get_single_group(m, 2))
def test_get_single_group_no_groups(self): """ get_single_group should return the whole match if no group is defined """ m = re.search("% test %", "dumdummy % test % mudmud") self.assertEqual("% test %", helpers.get_single_group(m)) # invalid key should be ignored self.assertEqual("% test %", helpers.get_single_group(m, 32))
def test_get_sinle_group(self): """ Basic usage of get_single_group """ m = re.search("% (\w+) %", "dumdummy % test % mudmud") self.assertEqual("test", helpers.get_single_group(m))