Debian Codegraph @

t/use.t

raw
1 #!perl
2 # Copyright 2022 Jeffrey Kegler
3 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
4 # redistribute it and/or modify it under the terms of the GNU Lesser
5 # General Public License as published by the Free Software Foundation,
6 # either version 3 of the License, or (at your option) any later version.
7 #
8 # Marpa::R2 is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser
14 # General Public License along with Marpa::R2. If not, see
15 # http://www.gnu.org/licenses/.
16
17 use 5.010001;
18 use warnings;
19 use strict;
20
21 use English qw( -no_match_vars );
22
23 use Getopt::Long ();
24 use Test::More ( import => [] );
25 use lib 'pperl';
26
27 BEGIN {
28 my $PPI_problem;
29 CHECK_PPI: {
30 if ( not eval { require PPI } ) {
31 $PPI_problem = "PPI not installed: $EVAL_ERROR";
32 last CHECK_PPI;
33 }
34 if ( not PPI->VERSION(1.206) ) {
35 $PPI_problem = 'PPI 1.206 not installed';
36 }
37 } ## end CHECK_PPI:
38 if ($PPI_problem) {
39 Test::More::plan skip_all => $PPI_problem;
40 }
41 else {
42 Test::More::plan tests => 1;
43 }
44 } ## end BEGIN
45
46 use Marpa::R2;
47 use Marpa::R2::Perl;
48 use lib 'inc';
49 use Marpa::R2::Test;
50
51 # Run in utility mode?
52 my $utility = 0;
53 die if not Getopt::Long::GetOptions( utility => \$utility );
54
55 sub concat {
56 shift @_;
57 return join q{}, map { $_ // '!UNDEF in concat!' } @_;
58 }
59 my %closure_by_action = (
60 long_use => sub {
61 'LONG: ' . join q{ }, map { $_ // q{()} } @_[ 1, 3 .. $#_ ];
62 },
63 revlong_use => sub {
64 'REVLONG: ' . join q{ }, map { $_ // q{()} } @_[ 1, 3 .. $#_ ];
65 },
66 perl_version_use => sub {
67 'PERL: ' . join q{ }, map { $_ // q{()} } @_[ 1, 3 .. $#_ ];
68 },
69 short_use => sub {
70 'SHORT: ' . join q{ }, map { $_ // q{()} } @_[ 1, 3 .. $#_ ];
71 },
72 argexpr => \&concat,
73 );
74
75 my %closure_by_lhs = (
76 prog => sub { return $_[1] . "\n" },
77 ary => \&concat,
78 lineseq => sub {
79 shift @_;
80 join "\n", grep { defined and length } @_;
81 },
82 );
83
84 sub gen_closure {
85 my ( $lhs, $rhs, $action ) = @_;
86 my $closure = $closure_by_action{$action} // $closure_by_lhs{$lhs};
87 return $closure if defined $closure and ref $closure eq 'CODE';
88 die "lhs=$lhs: $closure is not a closure" if defined $closure;
89 return sub {undef}
90 if scalar @{$rhs} == 0;
91 return sub { $_[1] }
92 if scalar @{$rhs} == 1;
93 return sub {
94 my @args = map { $_ // 'undef' } @_[ 1 .. $#_ ];
95 return
96 ( join "\n", @args )
97 . "\n$lhs ::= "
98 . ( join q{ }, map { $_ // q{-} } @{$rhs} ) . q{; };
99 };
100 } ## end sub gen_closure
101
102 my $parser = Marpa::R2::Perl->new( { closures => \&gen_closure } );
103
104 my $default_input = <<'END_OF_TEST_DATA';
105 use v5;
106 use 5;
107 use 5.1;
108 use xyz;
109 use v5 xyz;
110 use 5 xyz;
111 use 5.1 xyz;
112 use xyz v5;
113 use xyz 5;
114 use xyz 5.1;
115 use v5 xyz 5;
116 use 5 xyz 5;
117 use 5.1 xyz 5;
118 use xyz v5 5;
119 use xyz 5 5;
120 use xyz 5.1 5;
121 use v5 xyz 5, 5;
122 use 5 xyz 5, 5;
123 use 5.1 xyz 5, 5;
124 use xyz v5 5, 5;
125 use xyz 5 5, 5;
126 use xyz 5.1 5, 5;
127 use xyz 5.1 @a;
128 END_OF_TEST_DATA
129
130 my $string;
131 if ($utility) {
132 $string = do { local $RS = undef; <STDIN> };
133 }
134 else {
135 $string = $default_input;
136 }
137
138 my $expected = <<'EOS';
139 PERL: use v5 ;
140 PERL: use 5 ;
141 PERL: use 5.1 ;
142 SHORT: use xyz () ;
143 REVLONG: use v5 xyz () ;
144 REVLONG: use 5 xyz () ;
145 REVLONG: use 5.1 xyz () ;
146 LONG: use xyz v5 () ;
147 LONG: use xyz 5 () ;
148 LONG: use xyz 5.1 () ;
149 REVLONG: use v5 xyz 5 ;
150 REVLONG: use 5 xyz 5 ;
151 REVLONG: use 5.1 xyz 5 ;
152 LONG: use xyz v5 5 ;
153 LONG: use xyz 5 5 ;
154 LONG: use xyz 5.1 5 ;
155 REVLONG: use v5 xyz 5,5 ;
156 REVLONG: use 5 xyz 5,5 ;
157 REVLONG: use 5.1 xyz 5,5 ;
158 LONG: use xyz v5 5,5 ;
159 LONG: use xyz 5 5,5 ;
160 LONG: use xyz 5.1 5,5 ;
161 LONG: use xyz 5.1 @a ;
162 EOS
163
164 $parser->read( \$string );
165 my $result_ref = $parser->eval();
166 my $result = defined $result_ref ? ${$result_ref} : 'no parse';
167 if ($utility) {
168 say $result or die 'say builtin failed';
169 }
170 else {
171 Marpa::R2::Test::is( $result, $expected, 'Test of use statements' );
172 }
173
174 # vim: expandtab shiftwidth=4: