02-hash.t 3.1 KB

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