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# get_handle blocked by default
49$res = eval { $lh->maketext('[get_handle,en]') };
50is( $res, undef, 'no return value from blocked expansion' );
51like( $@, qr/Can't use .* as a method name/, 'get_handle blocked in bracket notation by default denylist' );
52
53# _ambient_langprefs blocked by default
54$res = eval { $lh->maketext('[_ambient_langprefs]') };
55is( $res, undef, 'no return value from blocked expansion' );
56like( $@, qr/Can't use .* as a method name/, '_ambient_langprefs blocked in bracket notation by default denylist' );
57
58# _internal_method not blocked by default
59$res = eval { $lh->maketext('[_internal_method]') };
60is( $res, "_internal_method_response", '_internal_method allowed in bracket notation by default denylist' );
61is( $@, '', 'no exception thrown by use of _internal_method under default denylist' );
62
63# sprintf not blocked by default
64$res = eval { $lh->maketext('[sprintf,%s,hello]') };
65is( $res, "hello", 'sprintf allowed in bracket notation by default denylist' );
66is( $@,   '',      'no exception thrown by use of sprintf under default denylist' );
67
68# denylisting sprintf and numerate
69$lh->blacklist( 'sprintf', 'numerate' );
70
71# sprintf blocked by custom denylist
72$res = eval { $lh->maketext('[sprintf,%s,hello]') };
73is( $res, undef, 'no return value from blocked expansion' );
74like( $@, qr/Can't use .* as a method name/, 'sprintf blocked in bracket notation by custom denylist' );
75
76# denylisting numf and _internal_method
77$lh->blacklist('numf');
78$lh->blacklist('_internal_method');
79
80# sprintf blocked by custom denylist
81$res = eval { $lh->maketext('[sprintf,%s,hello]') };
82is( $res, undef, 'no return value from blocked expansion' );
83like( $@, qr/Can't use .* as a method name/, 'sprintf blocked in bracket notation by custom denylist after extension of denylist' );
84
85# _internal_method blocked by custom denylist
86$res = eval { $lh->maketext('[_internal_method]') };
87is( $res, undef, 'no return value from blocked expansion' );
88like( $@, qr/Can't use .* as a method name/, 'sprintf blocked in bracket notation by custom denylist after extension of denylist' );
89
90# custom_handler not in default or custom denylist
91$res = eval { $lh->maketext('[custom_handler]') };
92is( $res, "custom_handler_response", 'custom_handler allowed in bracket notation by default and custom denylist' );
93is( $@, '', 'no exception thrown by use of custom_handler under default and custom denylist' );
94