|
4 | 4 |
|
5 | 5 | use lib 't/lib'; |
6 | 6 | use PPI::Test::pragmas; |
7 | | -use Test::More tests => 1008 + ($ENV{AUTHOR_TESTING} ? 1 : 0); |
| 7 | +use Test::More tests => 104 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); |
8 | 8 |
|
9 | 9 | use PPI (); |
10 | 10 | use Helper 'safe_new'; |
11 | 11 |
|
| 12 | +sub check; |
| 13 | +sub check_w_subs; |
12 | 14 |
|
13 | 15 | PARSING: { |
14 | | - for my $name ( |
15 | | - 'sub foo', |
16 | | - 'sub foo ', |
17 | | - 'sub', |
18 | | - 'sub ', |
19 | | - 'sub AUTOLOAD', |
20 | | - 'sub AUTOLOAD ', |
21 | | - 'sub DESTROY', |
22 | | - 'sub DESTROY ', |
23 | | - ) { |
24 | | - for my $block ( '{1;}', ';' ) { |
25 | | - for my $proto_and_expected ( |
26 | | - [ '', '', '' ], |
27 | | - [ '()', '()', '' ], |
28 | | - [ '( )', '( )', '' ], |
29 | | - [ ' () ',, '()', '' ], |
30 | | - [ '(+@)', '(+@)', '+@' ], |
31 | | - [ ' (+@) ', '(+@)', '+@' ], |
32 | | - [ '(\[$;$_@])', '(\[$;$_@])', '\[$;$_@]' ], |
33 | | - [ '(\ [ $ ])', '(\ [ $ ])', '\[$]' ], |
34 | | - [ '(\\\ [ $ ])', '(\\\ [ $ ])', '\\\[$]' ], # nonsense, but perl accepts it |
35 | | - [ '($ _ %)', '($ _ %)', '$_%' ], |
36 | | - [ '( Z)', '( Z)', 'Z' ], # invalid chars in prototype |
37 | | - [ '(!-=|)', '(!-=|)', '!-=|' ], # invalid chars in prototype |
38 | | - [ '(()', '(()', '(' ], # perl refuses to compile this |
39 | | - ) { |
40 | | - my ( $code_prototype, $expected_content, $expected_prototype ) = @$proto_and_expected; |
41 | | - my $code = "$name$code_prototype$block"; |
42 | | - my $document = safe_new \$code; |
43 | | - |
44 | | - my $all_prototypes = $document->find( 'PPI::Token::Prototype' ); |
45 | | - if ( $code_prototype eq '' ) { |
46 | | - is( $all_prototypes, "", "$code: got no prototypes" ); |
47 | | - } |
48 | | - else { |
49 | | - $all_prototypes = [] if !ref $all_prototypes; |
50 | | - is( scalar(@$all_prototypes), 1, "$code: got exactly one prototype" ); |
51 | | - my $prototype_obj = $all_prototypes->[0]; |
52 | | - is( $prototype_obj, $expected_content, "$code: prototype object content matches" ); |
53 | | - is( $prototype_obj->prototype, $expected_prototype, "$code: prototype characters match" ); |
54 | | - } |
55 | | - } |
56 | | - } |
| 16 | + my @sub_patterns; |
| 17 | + for my $block ( '{1;}', ';' ) { |
| 18 | + push @sub_patterns, # |
| 19 | + map [ $_, $block ], 'sub foo', 'sub', 'sub AUTOLOAD', 'sub DESTROY'; |
57 | 20 | } |
| 21 | + check_w_subs \@sub_patterns, '', '', ''; |
| 22 | + check_w_subs \@sub_patterns, '()', '()', ''; |
| 23 | + check_w_subs \@sub_patterns, '( )', '( )', ''; |
| 24 | + check_w_subs \@sub_patterns, ' () ',, '()', ''; |
| 25 | + check_w_subs \@sub_patterns, '(+@)', '(+@)', '+@'; |
| 26 | + check_w_subs \@sub_patterns, ' (+@) ', '(+@)', '+@'; |
| 27 | + check_w_subs \@sub_patterns, '(\[$;$_@])', '(\[$;$_@])', '\[$;$_@]'; |
| 28 | + check_w_subs \@sub_patterns, '(\ [ $ ])', '(\ [ $ ])', '\[$]'; |
| 29 | + ## nonsense, but perl accepts it |
| 30 | + check_w_subs \@sub_patterns, '(\\\ [ $ ])', '(\\\ [ $ ])', '\\\[$]'; |
| 31 | + check_w_subs \@sub_patterns, '($ _ %)', '($ _ %)', '$_%'; |
| 32 | + ## invalid chars in prototype |
| 33 | + check_w_subs \@sub_patterns, '( Z)', '( Z)', 'Z'; |
| 34 | + ## invalid chars in prototype |
| 35 | + check_w_subs \@sub_patterns, '(!-=|)', '(!-=|)', '!-=|'; |
| 36 | + ## perl refuses to compile this |
| 37 | + check_w_subs \@sub_patterns, '(()', '(()', '('; |
| 38 | +} |
| 39 | + |
| 40 | +sub check_w_subs { |
| 41 | + local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 42 | + check @{$_}, @_ for @{ shift() }; |
| 43 | + return; |
| 44 | +} |
| 45 | + |
| 46 | +sub check { |
| 47 | + local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 48 | + my ( $name, $block, $code_prototype, $expected_content, |
| 49 | + $expected_prototype ) |
| 50 | + = @_; |
| 51 | + my $desc = my $code = "$name$code_prototype$block"; |
| 52 | + $desc =~ s/\n/\\n/g; |
| 53 | + subtest $desc => sub { |
| 54 | + my $document = safe_new \$code; |
| 55 | + |
| 56 | + my $all_prototypes = $document->find('PPI::Token::Prototype'); |
| 57 | + return is $all_prototypes, "", "got no prototypes" |
| 58 | + if $code_prototype eq ''; |
| 59 | + |
| 60 | + $all_prototypes = [] if !ref $all_prototypes; |
| 61 | + is scalar(@$all_prototypes), 1, "got exactly one prototype"; |
| 62 | + my $prototype_obj = $all_prototypes->[0]; |
| 63 | + is $prototype_obj, $expected_content, |
| 64 | + "prototype object content matches"; |
| 65 | + is $prototype_obj->prototype, $expected_prototype, |
| 66 | + "prototype characters match"; |
| 67 | + }; |
| 68 | + return; |
58 | 69 | } |
0 commit comments