@@ -475,6 +475,27 @@ class PieDelims(Template):
475475 self .assertEqual (s .substitute (dict (who = 'tim' , what = 'ham' )),
476476 'tim likes to eat a bag of ham worth $100' )
477477
478+ def test_is_valid (self ):
479+ eq = self .assertEqual
480+ s = Template ('$who likes to eat a bag of ${what} worth $$100' )
481+ self .assertTrue (s .is_valid ())
482+
483+ s = Template ('$who likes to eat a bag of ${what} worth $100' )
484+ self .assertFalse (s .is_valid ())
485+
486+ # if the pattern has an unrecognized capture group,
487+ # it should raise ValueError like substitute and safe_substitute do
488+ class BadPattern (Template ):
489+ pattern = r"""
490+ (?P<badname>.*) |
491+ (?P<escaped>@{2}) |
492+ @(?P<named>[_a-z][._a-z0-9]*) |
493+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
494+ (?P<invalid>@) |
495+ """
496+ s = BadPattern ('@bag.foo.who likes to eat a bag of @bag.what' )
497+ self .assertRaises (ValueError , s .is_valid )
498+
478499 def test_get_identifiers (self ):
479500 eq = self .assertEqual
480501 raises = self .assertRaises
@@ -487,15 +508,24 @@ def test_get_identifiers(self):
487508 ids = s .get_identifiers ()
488509 eq (ids , ['who' , 'what' ])
489510
490- # invalid identifiers are raised
491- s = Template ('$who likes to eat a bag of ${what} worth $100' )
492- raises (ValueError , s .get_identifiers )
493-
494- # invalid identifiers are ignored with raise_on_invalid=False
511+ # invalid identifiers are ignored
495512 s = Template ('$who likes to eat a bag of ${what} worth $100' )
496- ids = s .get_identifiers (raise_on_invalid = False )
513+ ids = s .get_identifiers ()
497514 eq (ids , ['who' , 'what' ])
498515
516+ # if the pattern has an unrecognized capture group,
517+ # it should raise ValueError like substitute and safe_substitute do
518+ class BadPattern (Template ):
519+ pattern = r"""
520+ (?P<badname>.*) |
521+ (?P<escaped>@{2}) |
522+ @(?P<named>[_a-z][._a-z0-9]*) |
523+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
524+ (?P<invalid>@) |
525+ """
526+ s = BadPattern ('@bag.foo.who likes to eat a bag of @bag.what' )
527+ self .assertRaises (ValueError , s .get_identifiers )
528+
499529
500530if __name__ == '__main__' :
501531 unittest .main ()
0 commit comments