1#!/usr/bin/perl -Tw 2 3use strict; 4use warnings; 5use Test::More tests => 17; 6 7BEGIN { 8 use_ok("Locale::Maketext"); 9} 10 11{ 12 13 package MyTestLocale; 14 no warnings 'once'; 15 16 @MyTestLocale::ISA = qw(Locale::Maketext); 17 %MyTestLocale::Lexicon = (); 18} 19 20{ 21 22 package MyTestLocale::en; 23 no warnings 'once'; 24 25 @MyTestLocale::en::ISA = qw(MyTestLocale); 26 27 %MyTestLocale::en::Lexicon = ( '_AUTO' => 1 ); 28 29 sub custom_handler { 30 return "custom_handler_response"; 31 } 32 33 sub _internal_method { 34 return "_internal_method_response"; 35 } 36 37 sub new { 38 my ( $class, @args ) = @_; 39 my $lh = $class->SUPER::new(@args); 40 $lh->{use_external_lex_cache} = 1; 41 return $lh; 42 } 43} 44 45my $lh = MyTestLocale->get_handle('en'); 46my $res; 47 48# _internal_method not blocked by default 49$res = eval { $lh->maketext('[_internal_method]') }; 50is( $res, "_internal_method_response", '_internal_method allowed when no whitelist defined' ); 51is( $@, '', 'no exception thrown by use of _internal_method without whitelist setting' ); 52 53# whitelisting sprintf 54$lh->whitelist('sprintf'); 55 56# _internal_method blocked by whitelist 57$res = eval { $lh->maketext('[_internal_method]') }; 58is( $res, undef, 'no return value from blocked expansion' ); 59like( $@, qr/Can't use .* as a method name/, '_internal_method blocked in bracket notation by whitelist' ); 60 61# sprintf allowed by whitelist 62$res = eval { $lh->maketext('[sprintf,%s,hello]') }; 63is( $res, "hello", 'sprintf allowed in bracket notation by whitelist' ); 64is( $@, '', 'no exception thrown by use of sprintf with whitelist' ); 65 66# custom_handler blocked by whitelist 67$res = eval { $lh->maketext('[custom_handler]') }; 68is( $res, undef, 'no return value from blocked expansion' ); 69like( $@, qr/Can't use .* as a method name/, 'custom_handler blocked in bracket notation by whitelist' ); 70 71# adding custom_handler to whitelist 72$lh->whitelist('custom_handler'); 73 74# sprintf still allowed by whitelist 75$res = eval { $lh->maketext('[sprintf,%s,hello]') }; 76is( $res, "hello", 'sprintf allowed in bracket notation by whitelist' ); 77is( $@, '', 'no exception thrown by use of sprintf with whitelist' ); 78 79# custom_handler allowed by whitelist 80$res = eval { $lh->maketext('[custom_handler]') }; 81is( $res, "custom_handler_response", 'custom_handler allowed in bracket notation by whitelist' ); 82is( $@, '', 'no exception thrown by use of custom_handler with whitelist' ); 83 84# _internal_method blocked by whitelist 85$res = eval { $lh->maketext('[_internal_method]') }; 86is( $res, undef, 'no return value from blocked expansion' ); 87like( $@, qr/Can't use .* as a method name/, '_internal_method blocked in bracket notation by whitelist' ); 88 89# adding fail_with to whitelist 90$lh->whitelist('fail_with'); 91 92# fail_with still blocked by blacklist 93$res = eval { $lh->maketext('[fail_with,xyzzy]') }; 94is( $res, undef, 'no return value from blocked expansion' ); 95like( $@, qr/Can't use .* as a method name/, 'fail_with blocked in bracket notation by blacklist even when whitelisted' ); 96 97