Debian Codegraph @

dist/Locale-Maketext/t/94_denylist.t

raw
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use warnings;
5 use Test::More tests => 17;
6
7 BEGIN {
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
45 my $lh = MyTestLocale->get_handle('en');
46 my $res;
47
48 # get_handle blocked by default
49 $res = eval { $lh->maketext('[get_handle,en]') };
50 is( $res, undef, 'no return value from blocked expansion' );
51 like( $@, 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]') };
55 is( $res, undef, 'no return value from blocked expansion' );
56 like( $@, 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]') };
60 is( $res, "_internal_method_response", '_internal_method allowed in bracket notation by default denylist' );
61 is( $@, '', '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]') };
65 is( $res, "hello", 'sprintf allowed in bracket notation by default denylist' );
66 is( $@, '', 'no exception thrown by use of sprintf under default denylist' );
67
68 # denylisting sprintf and numerate
69 $lh->denylist( 'sprintf', 'numerate' );
70
71 # sprintf blocked by custom denylist
72 $res = eval { $lh->maketext('[sprintf,%s,hello]') };
73 is( $res, undef, 'no return value from blocked expansion' );
74 like( $@, 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->denylist('numf');
78 $lh->denylist('_internal_method');
79
80 # sprintf blocked by custom denylist
81 $res = eval { $lh->maketext('[sprintf,%s,hello]') };
82 is( $res, undef, 'no return value from blocked expansion' );
83 like( $@, 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]') };
87 is( $res, undef, 'no return value from blocked expansion' );
88 like( $@, 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]') };
92 is( $res, "custom_handler_response", 'custom_handler allowed in bracket notation by default and custom denylist' );
93 is( $@, '', 'no exception thrown by use of custom_handler under default and custom denylist' );