@@ -475,6 +475,61 @@ 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+ # TODO: RUSTPYTHON
479+ @unittest .expectedFailure
480+ def test_is_valid (self ):
481+ eq = self .assertEqual
482+ s = Template ('$who likes to eat a bag of ${what} worth $$100' )
483+ self .assertTrue (s .is_valid ())
484+
485+ s = Template ('$who likes to eat a bag of ${what} worth $100' )
486+ self .assertFalse (s .is_valid ())
487+
488+ # if the pattern has an unrecognized capture group,
489+ # it should raise ValueError like substitute and safe_substitute do
490+ class BadPattern (Template ):
491+ pattern = r"""
492+ (?P<badname>.*) |
493+ (?P<escaped>@{2}) |
494+ @(?P<named>[_a-z][._a-z0-9]*) |
495+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
496+ (?P<invalid>@) |
497+ """
498+ s = BadPattern ('@bag.foo.who likes to eat a bag of @bag.what' )
499+ self .assertRaises (ValueError , s .is_valid )
500+
501+ # TODO: RUSTPYTHON
502+ @unittest .expectedFailure
503+ def test_get_identifiers (self ):
504+ eq = self .assertEqual
505+ raises = self .assertRaises
506+ s = Template ('$who likes to eat a bag of ${what} worth $$100' )
507+ ids = s .get_identifiers ()
508+ eq (ids , ['who' , 'what' ])
509+
510+ # repeated identifiers only included once
511+ s = Template ('$who likes to eat a bag of ${what} worth $$100; ${who} likes to eat a bag of $what worth $$100' )
512+ ids = s .get_identifiers ()
513+ eq (ids , ['who' , 'what' ])
514+
515+ # invalid identifiers are ignored
516+ s = Template ('$who likes to eat a bag of ${what} worth $100' )
517+ ids = s .get_identifiers ()
518+ eq (ids , ['who' , 'what' ])
519+
520+ # if the pattern has an unrecognized capture group,
521+ # it should raise ValueError like substitute and safe_substitute do
522+ class BadPattern (Template ):
523+ pattern = r"""
524+ (?P<badname>.*) |
525+ (?P<escaped>@{2}) |
526+ @(?P<named>[_a-z][._a-z0-9]*) |
527+ @{(?P<braced>[_a-z][._a-z0-9]*)} |
528+ (?P<invalid>@) |
529+ """
530+ s = BadPattern ('@bag.foo.who likes to eat a bag of @bag.what' )
531+ self .assertRaises (ValueError , s .get_identifiers )
532+
478533
479534if __name__ == '__main__' :
480535 unittest .main ()
0 commit comments