2
0

hash.t 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!perl
  2. #
  3. # test apparatus for Text::Template module
  4. # still incomplete.
  5. use strict;
  6. use warnings;
  7. use Test::More tests => 13;
  8. use_ok 'Text::Template' or exit 1;
  9. my $template = 'We will put value of $v (which is "good") here -> {$v}';
  10. my $v = 'oops (main)';
  11. $Q::v = 'oops (Q)';
  12. my $vars = { 'v' => \'good' };
  13. # (1) Build template from string
  14. $template = Text::Template->new('type' => 'STRING', 'source' => $template);
  15. isa_ok $template, 'Text::Template';
  16. # (2) Fill in template in anonymous package
  17. my $result2 = 'We will put value of $v (which is "good") here -> good';
  18. my $text = $template->fill_in(HASH => $vars);
  19. is $text, $result2;
  20. # (3) Did we clobber the main variable?
  21. is $v, 'oops (main)';
  22. # (4) Fill in same template again
  23. my $result4 = 'We will put value of $v (which is "good") here -> good';
  24. $text = $template->fill_in(HASH => $vars);
  25. is $text, $result4;
  26. # (5) Now with a package
  27. my $result5 = 'We will put value of $v (which is "good") here -> good';
  28. $text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
  29. is $text, $result5;
  30. # (6) We expect to have clobbered the Q variable.
  31. is $Q::v, 'good';
  32. # (7) Now let's try it without a package
  33. my $result7 = 'We will put value of $v (which is "good") here -> good';
  34. $text = $template->fill_in(HASH => $vars);
  35. is $text, $result7;
  36. # (8-11) Now what does it do when we pass a hash with undefined values?
  37. # Roy says it does something bad. (Added for 1.20.)
  38. my $WARNINGS = 0;
  39. {
  40. local $SIG{__WARN__} = sub { $WARNINGS++ };
  41. local $^W = 1; # Make sure this is on for this test
  42. my $template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
  43. my $result8 = 'We will put value of $v (which is "good") here -> good';
  44. my $template = Text::Template->new('type' => 'STRING', 'source' => $template8);
  45. my $text = $template->fill_in(HASH => { 'v' => undef });
  46. # (8) Did we generate a warning?
  47. cmp_ok $WARNINGS, '==', 0;
  48. # (9) Was the output correct?
  49. is $text, $result8;
  50. # (10-11) Let's try that again, with a twist this time
  51. $WARNINGS = 0;
  52. $text = $template->fill_in(HASH => [ { 'v' => 17 }, { 'v' => undef } ]);
  53. # (10) Did we generate a warning?
  54. cmp_ok $WARNINGS, '==', 0;
  55. # (11) Was the output correct?
  56. SKIP: {
  57. skip 'not supported before 5.005', 1 unless $] >= 5.005;
  58. is $text, $result8;
  59. }
  60. }
  61. # (12) Now we'll test the multiple-hash option (Added for 1.20.)
  62. $text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].},
  63. HASH => [
  64. { 'v' => 17 },
  65. { 'v' => [ 'a', 'b', 'c' ] },
  66. { 'v' => \23 }
  67. ]
  68. );
  69. my $result = q{$v: 23. @v: [a b c].};
  70. is $text, $result;