broken.t 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!perl
  2. # test apparatus for Text::Template module
  3. use strict;
  4. use warnings;
  5. use Test::More tests => 7;
  6. use_ok 'Text::Template' or exit 1;
  7. # (1) basic error delivery
  8. {
  9. my $r = Text::Template->new(
  10. TYPE => 'string',
  11. SOURCE => '{1/0}',)->fill_in();
  12. is $r, q{Program fragment delivered error ``Illegal division by zero at template line 1.''};
  13. }
  14. # (2) BROKEN sub called in ->new?
  15. {
  16. my $r = Text::Template->new(
  17. TYPE => 'string',
  18. SOURCE => '{1/0}',
  19. BROKEN => sub { '---' },)->fill_in();
  20. is $r, q{---};
  21. }
  22. # (3) BROKEN sub called in ->fill_in?
  23. {
  24. my $r = Text::Template->new(
  25. TYPE => 'string',
  26. SOURCE => '{1/0}',)->fill_in(BROKEN => sub { '---' });
  27. is $r, q{---};
  28. }
  29. # (4) BROKEN sub passed correct args when called in ->new?
  30. {
  31. my $r = Text::Template->new(
  32. TYPE => 'string',
  33. SOURCE => '{1/0}',
  34. BROKEN => sub {
  35. my %a = @_;
  36. qq{$a{lineno},$a{error},$a{text}};
  37. },)->fill_in();
  38. is $r, qq{1,Illegal division by zero at template line 1.\n,1/0};
  39. }
  40. # (5) BROKEN sub passed correct args when called in ->fill_in?
  41. {
  42. my $r = Text::Template->new(
  43. TYPE => 'string',
  44. SOURCE => '{1/0}',
  45. )->fill_in(
  46. BROKEN => sub {
  47. my %a = @_;
  48. qq{$a{lineno},$a{error},$a{text}};
  49. });
  50. is $r, qq{1,Illegal division by zero at template line 1.\n,1/0};
  51. }
  52. # BROKEN sub handles undef
  53. {
  54. my $r = Text::Template->new(TYPE => 'string', SOURCE => 'abc{1/0}defg')
  55. ->fill_in(BROKEN => sub { undef });
  56. is $r, 'abc';
  57. }