| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde |
| 4 | |
| 5 | # This file is part of Perl-Critic-Pulp. |
| 6 | # |
| 7 | # Perl-Critic-Pulp is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by the |
| 9 | # Free Software Foundation; either version 3, or (at your option) any later |
| 10 | # version. |
| 11 | # |
| 12 | # Perl-Critic-Pulp is distributed in the hope that it will be useful, but |
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | # for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | |
| 21 | use 5.006scip-perl cpan Perl-Critic-Pulp 100 `5.006`/; |
| 22 | use strictscip-perl cpan Perl-Critic-Pulp 100 strict/; |
| 23 | use warningsscip-perl cpan Perl-Critic-Pulp 100 warnings/; |
| 24 | use Test::Morescip-perl cpan Perl-Critic-Pulp 100 Test/More/ tests => 38; |
| 25 | |
| 26 | use libscip-perl cpan Perl-Critic-Pulp 100 lib/ 't'; |
| 27 | use MyTestHelpersscip-perl cpan Perl-Critic-Pulp 100 MyTestHelpers/; |
| 28 | BEGIN { MyTestHelpers::nowarningsscip-perl cpan Perl-Critic-Pulp 100 MyTestHelpers/nowarnings().() } |
| 29 | |
| 30 | requirescip-perl cpan Perl-Critic-Pulp 100 main/require(). Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral; |
| 31 | |
| 32 | |
| 33 | #----------------------------------------------------------------------------- |
| 34 | my $want_version = 100; |
| 35 | isscip-perl cpan Perl-Critic-Pulp 100 main/is(). ($Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral::VERSIONscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/Policy/ValuesAndExpressions/UnexpandedSpecialLiteral/$VERSION., $want_version, 'VERSION variable'); |
| 36 | isscip-perl cpan Perl-Critic-Pulp 100 main/is(). (Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral->VERSIONscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/Policy/ValuesAndExpressions/UnexpandedSpecialLiteral/VERSION()., $want_version, 'VERSION class method'); |
| 37 | { |
| 38 | okscip-perl cpan Perl-Critic-Pulp 100 main/ok(). (eval { Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral->VERSIONscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/Policy/ValuesAndExpressions/UnexpandedSpecialLiteral/VERSION().($want_version); 1 }, "VERSION class check $want_version"); |
| 39 | my $check_version = $want_version + 1000; |
| 40 | okscip-perl cpan Perl-Critic-Pulp 100 main/ok(). (! eval { Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral->VERSIONscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/Policy/ValuesAndExpressions/UnexpandedSpecialLiteral/VERSION().($check_version); 1 }, "VERSION class check $check_version"); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | #------------------------------------------------------------------------------ |
| 45 | # confirming what's claimed in the POD and subject to critiquing ... |
| 46 | # |
| 47 | { my @x = ('MyExtra::'.__PACKAGE__ => 123); |
| 48 | is_deeplyscip-perl cpan Perl-Critic-Pulp 100 main/is_deeply(). (\@x, [ 'MyExtra::__PACKAGE__', 123 ], |
| 49 | 'list constructor literal on right of a . expression'); |
| 50 | } |
| 51 | { my $hash = { 'Foo'.__FILE__ => 123 }; |
| 52 | my @h = (%$hash); |
| 53 | is_deeplyscip-perl cpan Perl-Critic-Pulp 100 main/is_deeply(). (\@h, [ 'Foo__FILE__', 123 ], |
| 54 | 'hash constructor literal on right of a . expression'); |
| 55 | } |
| 56 | |
| 57 | # Perl 5.20 is probably going to quote all builtins across newline, except |
| 58 | # for __DATA__ and __END__. That would mean __PACKAGE__ doesn't expand, and |
| 59 | # ought to be reported as a violation. |
| 60 | # |
| 61 | # { my @array = (__PACKAGE__ |
| 62 | # => 123); |
| 63 | # is_deeply (\@array, [ 'main', 123 ], |
| 64 | # "fat comma doesn't quote across newline"); |
| 65 | # } |
| 66 | # { my @array = (__PACKAGE__ # and with a comment |
| 67 | # => 123); |
| 68 | # is_deeply (\@array, [ 'main', 123 ], |
| 69 | # "fat comma doesn't quote across comment and newline"); |
| 70 | # } |
| 71 | |
| 72 | |
| 73 | #------------------------------------------------------------------------------ |
| 74 | |
| 75 | requirescip-perl cpan Perl-Critic-Pulp 100 main/require(). Perl::Critic; |
| 76 | my $critic = Perl::Critic->newscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/new(). |
| 77 | ('-profile' => '', |
| 78 | '-single-policy' => '^Perl::Critic::Policy::ValuesAndExpressions::UnexpandedSpecialLiteral$'); |
| 79 | { my @p = $critic->policiesscip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/policies().; |
| 80 | isscip-perl cpan Perl-Critic-Pulp 100 main/is(). (scalarscip-perl cpan Perl-Critic-Pulp 100 main/scalar(). @p, 1, |
| 81 | 'single policy UnexpandedSpecialLiteral'); |
| 82 | |
| 83 | my $policy = $p[0]; |
| 84 | okscip-perl cpan Perl-Critic-Pulp 100 main/ok(). (eval { $policy->VERSIONscip-perl cpan Perl-Critic-Pulp 100 main/VERSION().($want_version); 1 }, |
| 85 | "VERSION object check $want_version"); |
| 86 | my $check_version = $want_version + 1000; |
| 87 | okscip-perl cpan Perl-Critic-Pulp 100 main/ok(). (! eval { $policy->VERSIONscip-perl cpan Perl-Critic-Pulp 100 main/VERSION().($check_version); 1 }, |
| 88 | "VERSION object check $check_version"); |
| 89 | } |
| 90 | |
| 91 | foreach my $data (## no critic (RequireInterpolationOfMetachars) |
| 92 | |
| 93 | # from the POD |
| 94 | [ 1, 'my $seen = { __FILE__ => 1 };' ], |
| 95 | [ 1, '$obj->{__PACKAGE__}{myextra} = 123;' ], |
| 96 | [ 1, 'my $hash = { \'Foo\'.__FILE__ => 123 };' ], |
| 97 | [ 1, 'return (\'MyExtra::\'.__PACKAGE__ => 123);' ], |
| 98 | |
| 99 | [ 1, '$hash{__PACKAGE__}' ], |
| 100 | [ 1, '$hash{__FILE__}' ], |
| 101 | [ 1, '$hash{__LINE__}' ], |
| 102 | [ 1, '$hash{ __PACKAGE__ }' ], |
| 103 | |
| 104 | [ 0, '$hash{"__PACKAGE__"}' ], |
| 105 | [ 0, '$hash{\'__PACKAGE__\'}' ], |
| 106 | [ 0, '$hash{q{__PACKAGE__}}' ], |
| 107 | [ 0, '$hash{SOMETHING}' ], |
| 108 | [ 0, '$hash{(__PACKAGE__)}}' ], |
| 109 | [ 0, '$hash{__PACKAGE__.""}' ], |
| 110 | |
| 111 | [ 1, '$href = { __PACKAGE__ => 123 }' ], |
| 112 | [ 0, "\$href = { __PACKAGE__ \n => 123 }" ], |
| 113 | [ 1, '$href = { __FILE__ => 123 }' ], |
| 114 | [ 1, '$href = { __LINE__ => 123 }' ], |
| 115 | [ 0, '$href = { SOMETHING => 123 }' ], |
| 116 | |
| 117 | [ 1, '$href = { __PACKAGE__ => 123, FOO => 123 }' ], |
| 118 | [ 1, '$href = { FOO => 123, __PACKAGE__ => 123 }' ], |
| 119 | [ 1, '$href = { FOO => 123 => __PACKAGE__ => 123 }' ], |
| 120 | |
| 121 | [ 0, '$href = { __PACKAGE__."x" => 123 }' ], |
| 122 | |
| 123 | # it's the token immediately left of the => which is |
| 124 | # literal, so though __PACKAGE__ is part of a "." |
| 125 | # expression it's used literally, not expanded |
| 126 | [ 1, '$href = { "x".__PACKAGE__ => 123 }' ], |
| 127 | |
| 128 | [ 0, '__PACKAGE__' ], |
| 129 | [ 0, 'return __PACKAGE__;' ], |
| 130 | [ 0, '{__PACKAGE__}' ], |
| 131 | [ 0, '{__PACKAGE__; 123}' ], |
| 132 | |
| 133 | # in a code block return value like this __PACKAGE__ is |
| 134 | # still literal, not expanded |
| 135 | [ 1, '{; __PACKAGE__ => 123}' ], |
| 136 | |
| 137 | ## use critic |
| 138 | ) { |
| 139 | my ($want_count, $str) = @$data; |
| 140 | my @violations = $critic->critiquescip-perl cpan Perl-Critic-Pulp 100 Perl/Critic/critique(). (\$str); |
| 141 | |
| 142 | my $got_count = scalarscip-perl cpan Perl-Critic-Pulp 100 main/scalar(). @violations; |
| 143 | isscip-perl cpan Perl-Critic-Pulp 100 main/is(). ($got_count, $want_count, "str: $str"); |
| 144 | |
| 145 | if ($got_count != $want_count) { |
| 146 | foreach (@violations) { |
| 147 | diagscip-perl cpan Perl-Critic-Pulp 100 main/diag(). ($_scip-perl cpan Perl-Critic-Pulp 100 main/$_.->descriptionscip-perl cpan Perl-Critic-Pulp 100 main/description().); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | exitscip-perl cpan Perl-Critic-Pulp 100 main/exit(). 0; |