| 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-Policy-Plicease-ProhibitArrayAssignAref. |
| 6 | # |
| 7 | # Perl-Critic-Policy-Plicease-ProhibitArrayAssignAref 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-Policy-Plicease-ProhibitArrayAssignAref 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-Policy-Plicease-ProhibitArrayAssignAref. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | |
| 21 | use 5.006; |
| 22 | use strict; |
| 23 | use warnings; |
| 24 | use Test::More tests => 37; |
| 25 | |
| 26 | require Perl::Critic::Policy::Plicease::ProhibitArrayAssignAref; |
| 27 | |
| 28 | |
| 29 | #----------------------------------------------------------------------------- |
| 30 | require Perl::Critic; |
| 31 | my $critic = Perl::Critic->new |
| 32 | ('-profile' => '', |
| 33 | '-single-policy' => '^Perl::Critic::Policy::Plicease::ProhibitArrayAssignAref$'); |
| 34 | { my @p = $critic->policies; |
| 35 | is (scalar @p, 1, |
| 36 | 'single policy ProhibitArrayAssignAref'); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | foreach my $data (## no critic (RequireInterpolationOfMetachars) |
| 41 | |
| 42 | [ 1, '@a = [1,2]', 'Array' ], |
| 43 | [ 1, '@a = []', 'Array' ], |
| 44 | [ 0, '@a = (1,2)' ], |
| 45 | |
| 46 | [ 1, '@$r = [1,2]', 'Array' ], |
| 47 | [ 0, '@$r = ([1,2])' ], |
| 48 | [ 1, '@{$r} = [1,2]', 'Array' ], |
| 49 | [ 0, '@{$r} = ([1,2])' ], |
| 50 | |
| 51 | [ 1, '@a[1,2] = [1,2]', 'Array slice' ], |
| 52 | [ 0, '@a[1,2] = ()' ], |
| 53 | |
| 54 | [ 1, '@a{"x","y"} = [1,2]', 'Hash slice' ], |
| 55 | [ 1, '@a{"x","y"} = []', 'Hash slice' ], |
| 56 | [ 0, '@a{"x","y"} = ()' ], |
| 57 | |
| 58 | [ 1, '@{foo()}[1,2] = [1,2]', 'Array slice' ], |
| 59 | [ 1, '@{$r=foo()}[1,2] = [1,2]', 'Array slice' ], |
| 60 | [ 0, '@{foo()}[1,2] = (1,2)' ], |
| 61 | |
| 62 | [ 1, '@{foo()}{"a","b"} = [1,2]', 'Hash slice' ], |
| 63 | [ 1, '@{$r=foo()}{"a","b"} = [1,2]', 'Hash slice' ], |
| 64 | [ 0, '@{foo()}{"a","b"} = (1,2)' ], |
| 65 | |
| 66 | ## use critic |
| 67 | ) { |
| 68 | my ($want_count, $str, $thing) = @$data; |
| 69 | |
| 70 | my @violations = $critic->critique (\$str); |
| 71 | |
| 72 | my $got_count = scalar @violations; |
| 73 | is ($got_count, $want_count, "str: $str"); |
| 74 | |
| 75 | my $violation = $violations[0]; |
| 76 | my $description = $violation && $violation->description; |
| 77 | if (! defined $thing) { $thing = 'Array'; } |
| 78 | ok (! defined $description || $description =~ /^$thing assigned/, |
| 79 | "str: $str\ndescription: " |
| 80 | .(defined $description ? $description : 'undef')); |
| 81 | |
| 82 | |
| 83 | if ($got_count != $want_count) { |
| 84 | foreach (@violations) { |
| 85 | diag ($_->description); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | exit 0; |