basic.t 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!perl
  2. #
  3. # Tests of basic, essential functionality
  4. #
  5. use strict;
  6. use warnings;
  7. use Test::More tests => 34;
  8. use File::Temp;
  9. my $tmpfile = File::Temp->new;
  10. use_ok 'Text::Template' or exit 1;
  11. $X::v = $Y::v = 0; # Suppress `var used only once'
  12. my $template_1 = <<EOM;
  13. We will put value of \$v (which is "abc") here -> {\$v}
  14. We will evaluate 1+1 here -> {1 + 1}
  15. EOM
  16. # (1) Construct temporary template file for testing
  17. # file operations
  18. my $TEMPFILE = $tmpfile->filename;
  19. eval {
  20. open my $tmp, '>', $TEMPFILE
  21. or die "Couldn't write tempfile $TEMPFILE: $!";
  22. print $tmp $template_1;
  23. close $tmp;
  24. pass;
  25. };
  26. if ($@) {
  27. fail $@;
  28. }
  29. # (2) Build template from file
  30. my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
  31. ok(defined $template) or diag $Text::Template::ERROR;
  32. # (3) Fill in template from file
  33. $X::v = "abc";
  34. my $resultX = <<EOM;
  35. We will put value of \$v (which is "abc") here -> abc
  36. We will evaluate 1+1 here -> 2
  37. EOM
  38. $Y::v = "ABC";
  39. my $resultY = <<EOM;
  40. We will put value of \$v (which is "abc") here -> ABC
  41. We will evaluate 1+1 here -> 2
  42. EOM
  43. my $text = $template->fill_in('package' => 'X');
  44. is $text, $resultX;
  45. # (4) Fill in same template again
  46. $text = $template->fill_in('package' => 'Y');
  47. is $text, $resultY;
  48. # (5) Simple test of `fill_this_in'
  49. $text = Text::Template->fill_this_in($template_1, 'package' => 'X');
  50. is $text, $resultX;
  51. # (6) test creation of template from filehandle
  52. open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
  53. $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
  54. ok defined $template or diag $Text::Template::ERROR;
  55. # (7) test filling in of template from filehandle
  56. $text = $template->fill_in('package' => 'X');
  57. is $text, $resultX;
  58. # (8) test second fill_in on same template object
  59. $text = $template->fill_in('package' => 'Y');
  60. is $text, $resultY;
  61. close $tmpl;
  62. # (9) test creation of template from array
  63. $template = Text::Template->new(
  64. type => 'ARRAY',
  65. source => [
  66. 'We will put value of $v (which is "abc") here -> {$v}', "\n",
  67. 'We will evaluate 1+1 here -> {1+1}', "\n"
  68. ]
  69. );
  70. ok defined $template; # or diag $Text::Template::ERROR;
  71. # (10) test filling in of template from array
  72. $text = $template->fill_in('package' => 'X');
  73. is $text, $resultX;
  74. # (11) test second fill_in on same array template object
  75. $text = $template->fill_in('package' => 'Y');
  76. is $text, $resultY;
  77. # (12) Make sure \ is working properly
  78. # Test added for version 1.11
  79. $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D');
  80. # This should fail if the \ are not interpreted properly.
  81. $text = $tmpl->fill_in();
  82. is $text, 'B}C{D';
  83. # (13) Make sure \ is working properly
  84. # Test added for version 1.11
  85. $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B});
  86. # Symptom of old problem: ALL \ were special in templates, so
  87. # The lexer would return (A, PROGTEXT("t"), B), and the
  88. # result text would be AtB instead of A(tab)B.
  89. $text = $tmpl->fill_in();
  90. is $text, "A\tB";
  91. # (14-27) Make sure \ is working properly
  92. # Test added for version 1.11
  93. # This is a sort of general test.
  94. my @tests = (
  95. '{""}' => '', # (14)
  96. '{"}"}' => undef, # (15)
  97. '{"\\}"}' => '}', # One backslash
  98. '{"\\\\}"}' => undef, # Two backslashes
  99. '{"\\\\\\}"}' => '}', # Three backslashes
  100. '{"\\\\\\\\}"}' => undef, # Four backslashes
  101. '{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
  102. '{"x20"}' => 'x20',
  103. '{"\\x20"}' => ' ', # One backslash
  104. '{"\\\\x20"}' => '\\x20', # Two backslashes
  105. '{"\\\\\\x20"}' => '\\ ', # Three backslashes
  106. '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
  107. '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
  108. '{"\\x20\\}"}' => ' }', # (27)
  109. );
  110. while (my ($test, $result) = splice @tests, 0, 2) {
  111. my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
  112. my $text = $tmpl->fill_in;
  113. ok(!defined $text && !defined $result || $text eq $result)
  114. or diag "expected .$result. got .$text.";
  115. }
  116. # (28-30) I discovered that you can't pass a glob ref as your filehandle.
  117. # MJD 20010827
  118. # (28) test creation of template from filehandle
  119. $tmpl = undef;
  120. ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
  121. $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
  122. ok(defined $template) or diag $Text::Template::ERROR;
  123. # (29) test filling in of template from filehandle
  124. $text = $template->fill_in('package' => 'X');
  125. is $text, $resultX;
  126. # (30) test second fill_in on same template object
  127. $text = $template->fill_in('package' => 'Y');
  128. is $text, $resultY;
  129. close $tmpl;
  130. # (31) Test _scrubpkg for leakiness
  131. $Text::Template::GEN0::test = 1;
  132. Text::Template::_scrubpkg('Text::Template::GEN0');
  133. ok !($Text::Template::GEN0::test
  134. || exists $Text::Template::GEN0::{test}
  135. || exists $Text::Template::{'GEN0::'});
  136. # that filename parameter works. we use BROKEN to verify this
  137. $text = Text::Template->new(
  138. TYPE => 'string',
  139. SOURCE => 'Hello {1/0}'
  140. )->fill_in(FILENAME => 'foo.txt');
  141. like $text, qr/division by zero at foo\.txt line 1/;