delimiters.t 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!perl
  2. #
  3. # Tests for user-specified delimiter functions
  4. # These tests first appeared in version 1.20.
  5. use strict;
  6. use warnings;
  7. use Test::More tests => 19;
  8. use_ok 'Text::Template' or exit 1;
  9. # (1) Try a simple delimiter: <<..>>
  10. # First with the delimiters specified at object creation time
  11. our $V = $V = 119;
  12. my $template = q{The value of $V is <<$V>>.};
  13. my $result = q{The value of $V is 119.};
  14. my $template1 = Text::Template->new(
  15. TYPE => 'STRING',
  16. SOURCE => $template,
  17. DELIMITERS => [ '<<', '>>' ])
  18. or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
  19. my $text = $template1->fill_in();
  20. is $text, $result;
  21. # (2) Now with delimiter choice deferred until fill-in time.
  22. $template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
  23. $text = $template1->fill_in(DELIMITERS => [ '<<', '>>' ]);
  24. is $text, $result;
  25. # (3) Now we'll try using regex metacharacters
  26. # First with the delimiters specified at object creation time
  27. $template = q{The value of $V is [$V].};
  28. $template1 = Text::Template->new(
  29. TYPE => 'STRING',
  30. SOURCE => $template,
  31. DELIMITERS => [ '[', ']' ])
  32. or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
  33. $text = $template1->fill_in();
  34. is $text, $result;
  35. # (4) Now with delimiter choice deferred until fill-in time.
  36. $template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
  37. $text = $template1->fill_in(DELIMITERS => [ '[', ']' ]);
  38. is $text, $result;
  39. # (5-18) Make sure \ is working properly
  40. # (That is to say, it is ignored.)
  41. # These tests are similar to those in 01-basic.t.
  42. my @tests = (
  43. '{""}' => '', # (5)
  44. # Backslashes don't matter
  45. '{"}"}' => undef,
  46. '{"\\}"}' => undef, # One backslash
  47. '{"\\\\}"}' => undef, # Two backslashes
  48. '{"\\\\\\}"}' => undef, # Three backslashes
  49. '{"\\\\\\\\}"}' => undef, # Four backslashes (10)
  50. '{"\\\\\\\\\\}"}' => undef, # Five backslashes
  51. # Backslashes are always passed directly to Perl
  52. '{"x20"}' => 'x20',
  53. '{"\\x20"}' => ' ', # One backslash
  54. '{"\\\\x20"}' => '\\x20', # Two backslashes
  55. '{"\\\\\\x20"}' => '\\ ', # Three backslashes (15)
  56. '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes
  57. '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
  58. '{"\\x20\\}"}' => undef, # (18)
  59. );
  60. while (my ($test, $result) = splice @tests, 0, 2) {
  61. my $tmpl = Text::Template->new(
  62. TYPE => 'STRING',
  63. SOURCE => $test,
  64. DELIMITERS => [ '{', '}' ]);
  65. my $text = $tmpl->fill_in;
  66. my $ok = (!defined $text && !defined $result || $text eq $result);
  67. ok($ok) or diag "expected .$result., got .$text.";
  68. }