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 allowlist defined' );
51is( $@, '', 'no exception thrown by use of _internal_method without allowlist setting' );
52
53# allowlisting sprintf
54$lh->allowlist('sprintf');
55
56# _internal_method blocked by allowlist
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 allowlist' );
60
61# sprintf allowed by allowlist
62$res = eval { $lh->maketext('[sprintf,%s,hello]') };
63is( $res, "hello", 'sprintf allowed in bracket notation by allowlist' );
64is( $@,   '',      'no exception thrown by use of sprintf with allowlist' );
65
66# custom_handler blocked by allowlist
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 allowlist' );
70
71# adding custom_handler to allowlist
72$lh->allowlist('custom_handler');
73
74# sprintf still allowed by allowlist
75$res = eval { $lh->maketext('[sprintf,%s,hello]') };
76is( $res, "hello", 'sprintf allowed in bracket notation by allowlist' );
77is( $@,   '',      'no exception thrown by use of sprintf with allowlist' );
78
79# custom_handler allowed by allowlist
80$res = eval { $lh->maketext('[custom_handler]') };
81is( $res, "custom_handler_response", 'custom_handler allowed in bracket notation by allowlist' );
82is( $@, '', 'no exception thrown by use of custom_handler with allowlist' );
83
84# _internal_method blocked by allowlist
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 allowlist' );
88
89# adding fail_with to allowlist
90$lh->allowlist('fail_with');
91
92# fail_with still blocked by denylist
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 denylist even when allowlisted' );
96
97