01-basic.t 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!perl
  2. #
  3. # Tests of basic, essential functionality
  4. #
  5. use Text::Template;
  6. $X::v = $Y::v = 0; # Suppress `var used only once'
  7. print "1..31\n";
  8. $n=1;
  9. $template_1 = <<EOM;
  10. We will put value of \$v (which is "abc") here -> {\$v}
  11. We will evaluate 1+1 here -> {1 + 1}
  12. EOM
  13. # (1) Construct temporary template file for testing
  14. # file operations
  15. $TEMPFILE = "tt$$";
  16. open(TMP, "> $TEMPFILE") or print "not ok $n\n" && &abort("Couldn\'t write tempfile $TEMPFILE: $!");
  17. print TMP $template_1;
  18. close TMP;
  19. print "ok $n\n"; $n++;
  20. # (2) Build template from file
  21. $template = new Text::Template ('type' => 'FILE', 'source' => $TEMPFILE);
  22. if (defined($template)) {
  23. print "ok $n\n";
  24. } else {
  25. print "not ok $n $Text::Template::ERROR\n";
  26. }
  27. $n++;
  28. # (3) Fill in template from file
  29. $X::v = "abc";
  30. $resultX = <<EOM;
  31. We will put value of \$v (which is "abc") here -> abc
  32. We will evaluate 1+1 here -> 2
  33. EOM
  34. $Y::v = "ABC";
  35. $resultY = <<EOM;
  36. We will put value of \$v (which is "abc") here -> ABC
  37. We will evaluate 1+1 here -> 2
  38. EOM
  39. $text = $template->fill_in('package' => X);
  40. if ($text eq $resultX) {
  41. print "ok $n\n";
  42. } else {
  43. print "not ok $n\n";
  44. }
  45. $n++;
  46. # (4) Fill in same template again
  47. $text = $template->fill_in('package' => Y);
  48. if ($text eq $resultY) {
  49. print "ok $n\n";
  50. } else {
  51. print "not ok $n\n";
  52. }
  53. $n++;
  54. # (5) Simple test of `fill_this_in'
  55. $text = Text::Template->fill_this_in( $template_1, 'package' => X);
  56. if ($text eq $resultX) {
  57. print "ok $n\n";
  58. } else {
  59. print "not ok $n\n";
  60. }
  61. $n++;
  62. # (6) test creation of template from filehandle
  63. if (open (TMPL, "< $TEMPFILE")) {
  64. $template = new Text::Template ('type' => 'FILEHANDLE',
  65. 'source' => *TMPL);
  66. if (defined($template)) {
  67. print "ok $n\n";
  68. } else {
  69. print "not ok $n $Text::Template::ERROR\n";
  70. }
  71. $n++;
  72. # (7) test filling in of template from filehandle
  73. $text = $template->fill_in('package' => X);
  74. if ($text eq $resultX) {
  75. print "ok $n\n";
  76. } else {
  77. print "not ok $n\n";
  78. }
  79. $n++;
  80. # (8) test second fill_in on same template object
  81. $text = $template->fill_in('package' => Y);
  82. if ($text eq $resultY) {
  83. print "ok $n\n";
  84. } else {
  85. print "not ok $n\n";
  86. }
  87. $n++;
  88. close TMPL;
  89. } else {
  90. print "not ok $n\n"; $n++;
  91. print "not ok $n\n"; $n++;
  92. print "not ok $n\n"; $n++;
  93. }
  94. # (9) test creation of template from array
  95. $template = new Text::Template
  96. ('type' => 'ARRAY',
  97. 'source' => [
  98. 'We will put value of $v (which is "abc") here -> {$v}',
  99. "\n",
  100. 'We will evaluate 1+1 here -> {1+1}',
  101. "\n",
  102. ]);
  103. if (defined($template)) {
  104. print "ok $n\n";
  105. } else {
  106. print "not ok $n $Text::Template::ERROR\n";
  107. }
  108. $n++;
  109. # (10) test filling in of template from array
  110. $text = $template->fill_in('package' => X);
  111. if ($text eq $resultX) {
  112. print "ok $n\n";
  113. } else {
  114. print "not ok $n\n";
  115. }
  116. $n++;
  117. # (11) test second fill_in on same array template object
  118. $text = $template->fill_in('package' => Y);
  119. if ($text eq $resultY) {
  120. print "ok $n\n";
  121. } else {
  122. print "not ok $n\n";
  123. print STDERR "$resultX\n---\n$text";
  124. unless (!defined($text)) { print STDERR "ERROR: $Text::Template::ERROR\n"};
  125. }
  126. $n++;
  127. # (12) Make sure \ is working properly
  128. # Test added for version 1.11
  129. my $tmpl = Text::Template->new(TYPE => 'STRING',
  130. SOURCE => 'B{"\\}"}C{"\\{"}D',
  131. );
  132. # This should fail if the \ are not interpreted properly.
  133. my $text = $tmpl->fill_in();
  134. print +($text eq "B}C{D" ? '' : 'not '), "ok $n\n";
  135. $n++;
  136. # (13) Make sure \ is working properly
  137. # Test added for version 1.11
  138. $tmpl = Text::Template->new(TYPE => 'STRING',
  139. SOURCE => qq{A{"\t"}B},
  140. );
  141. # Symptom of old problem: ALL \ were special in templates, so
  142. # The lexer would return (A, PROGTEXT("t"), B), and the
  143. # result text would be AtB instead of A(tab)B.
  144. $text = $tmpl->fill_in();
  145. print +($text eq "A\tB" ? '' : 'not '), "ok $n\n";
  146. $n++;
  147. # (14-27) Make sure \ is working properly
  148. # Test added for version 1.11
  149. # This is a sort of general test.
  150. my @tests = ('{""}' => '', # (14)
  151. '{"}"}' => undef, # (15)
  152. '{"\\}"}' => '}', # One backslash
  153. '{"\\\\}"}' => undef, # Two backslashes
  154. '{"\\\\\\}"}' => '}', # Three backslashes
  155. '{"\\\\\\\\}"}' => undef, # Four backslashes
  156. '{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
  157. '{"x20"}' => 'x20',
  158. '{"\\x20"}' => ' ', # One backslash
  159. '{"\\\\x20"}' => '\\x20', # Two backslashes
  160. '{"\\\\\\x20"}' => '\\ ', # Three backslashes
  161. '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
  162. '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
  163. '{"\\x20\\}"}' => ' }', # (27)
  164. );
  165. my $i;
  166. for ($i=0; $i<@tests; $i+=2) {
  167. my $tmpl = Text::Template->new(TYPE => 'STRING',
  168. SOURCE => $tests[$i],
  169. );
  170. my $text = $tmpl->fill_in;
  171. my $result = $tests[$i+1];
  172. my $ok = (! defined $text && ! defined $result
  173. || $text eq $result);
  174. unless ($ok) {
  175. print STDERR "($n) expected .$result., got .$text.\n";
  176. }
  177. print +($ok ? '' : 'not '), "ok $n\n";
  178. $n++;
  179. }
  180. # (28-30) I discovered that you can't pass a glob ref as your filehandle.
  181. # MJD 20010827
  182. # (28) test creation of template from filehandle
  183. if (open (TMPL, "< $TEMPFILE")) {
  184. $template = new Text::Template ('type' => 'FILEHANDLE',
  185. 'source' => \*TMPL);
  186. if (defined($template)) {
  187. print "ok $n\n";
  188. } else {
  189. print "not ok $n $Text::Template::ERROR\n";
  190. }
  191. $n++;
  192. # (29) test filling in of template from filehandle
  193. $text = $template->fill_in('package' => X);
  194. if ($text eq $resultX) {
  195. print "ok $n\n";
  196. } else {
  197. print "not ok $n\n";
  198. }
  199. $n++;
  200. # (30) test second fill_in on same template object
  201. $text = $template->fill_in('package' => Y);
  202. if ($text eq $resultY) {
  203. print "ok $n\n";
  204. } else {
  205. print "not ok $n\n";
  206. }
  207. $n++;
  208. close TMPL;
  209. } else {
  210. print "not ok $n\n"; $n++;
  211. print "not ok $n\n"; $n++;
  212. print "not ok $n\n"; $n++;
  213. }
  214. # (31) Test _scrubpkg for leakiness
  215. $Text::Template::GEN0::test = 1;
  216. Text::Template::_scrubpkg('Text::Template::GEN0');
  217. if ($Text::Template::GEN0::test) {
  218. print "not ok $n\n";
  219. } else {
  220. print "ok $n\n";
  221. }
  222. $n++;
  223. END {unlink $TEMPFILE;}
  224. exit;
  225. sub abort {
  226. unlink $TEMPFILE;
  227. die $_[0];
  228. }