strict.t 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!perl
  2. #
  3. # Tests for STRICT features
  4. # These tests first appeared in version 1.48.
  5. use strict;
  6. use warnings;
  7. use Test::More tests => 4;
  8. use_ok 'Text::Template' or exit 1;
  9. @Emptyclass1::ISA = 'Text::Template';
  10. @Emptyclass2::ISA = 'Text::Template';
  11. my $tin = q{The value of $foo is: {$foo}};
  12. Text::Template->always_prepend(q{$foo = "global"});
  13. my $tmpl1 = Text::Template->new(
  14. TYPE => 'STRING',
  15. SOURCE => $tin);
  16. my $tmpl2 = Text::Template->new(
  17. TYPE => 'STRING',
  18. SOURCE => $tin,
  19. PREPEND => q{$foo = "template"});
  20. $tmpl1->compile;
  21. $tmpl2->compile;
  22. # strict should cause t1 to contain an error message if wrong variable is used in template
  23. my $t1 = $tmpl1->fill_in(PACKAGE => 'T1', STRICT => 1, HASH => { bar => 'baz' });
  24. # non-strict still works
  25. my $t2 = $tmpl2->fill_in(PACKAGE => 'T2', HASH => { bar => 'baz' });
  26. # prepend overrides the hash values
  27. my $t3 = $tmpl2->fill_in(
  28. PREPEND => q{$foo = "fillin"},
  29. PACKAGE => 'T3',
  30. STRICT => 1,
  31. HASH => { foo => 'hashval2' });
  32. like $t1, qr/Global symbol "\$foo" requires explicit package/;
  33. is $t2, 'The value of $foo is: template', "non-strict hash still works";
  34. is $t3, "The value of \$foo is: fillin", "hash values with prepend, prepend wins, even under strict.";