Debian Codegraph @

t/sl_g1len.t

raw
1 #!/usr/bin/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 # Test new g1len action
18 # and now-deprecated g1length action
19
20 use 5.010001;
21 use strict;
22 use warnings;
23 use Scalar::Util;
24 use Data::Dumper;
25 use Test::More tests => 11;
26
27 use lib 'inc';
28 use Marpa::R2::Test;
29
30 ## no critic (ErrorHandling::RequireCarping);
31
32 use Marpa::R2;
33
34 say 'Libmarpa tag: ' . Marpa::R2::Thin::tag();
35
36 my $bnf = <<'END_OF_BNF';
37 :default ::= action => [name,g1start,g1len,g1length,values]
38 lexeme default = latm => 1
39 top ::= beginning middle end
40 beginning ::= '(beginning)'
41 middle ::= '(middle)'
42 end ::= '(end)'
43
44 :discard ~ whitespace
45 whitespace ~ [\s]+
46 END_OF_BNF
47
48 my $grammar = Marpa::R2::Scanless::G->new(
49 {
50 source => \$bnf,
51 }
52 );
53
54 my $string = '(beginning) (middle) (end)';
55
56 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
57 $recce->read( \$string );
58
59 my $value_ref1;
60 my %parses = (
61 q{\['top',0,3,4,['beginning',0,1,2,'(beginning)'],['middle',1,1,2,'(middle)'],['end',2,1,2,'(end)']]}
62 => '' );
63 PARSE: while (1) {
64 my $value_ref = $recce->value();
65 $value_ref1 = $value_ref if not defined $value_ref1;
66 last PARSE if not defined $value_ref;
67 my $dumped_value = myDump( $value_ref );
68 $parses{$dumped_value} = 'found';
69 }
70 my %expected_parses = (
71 q{\['top',0,3,4,['beginning',0,1,2,'(beginning)'],['middle',1,1,2,'(middle)'],['end',2,1,2,'(end)']]}
72 => 'found' );
73 cmpHash(\%parses, \%expected_parses, 'Parses');
74
75 my %expected_substrings = (
76 q{<top> using g1len} => '(beginning) (middle) (end)',
77 q{<top> using g1length} => '(beginning) (middle) (end)',
78 q{<beginning> using g1len} => '(beginning)',
79 q{<beginning> using g1length} => '(beginning)',
80 q{<middle> using g1len} => '(middle)',
81 q{<middle> using g1length} => '(middle)',
82 q{<end> using g1len} => '(end)',
83 q{<end> using g1length} => '(end)',
84 );
85 my %substrings = ();
86 showNode($recce, \%substrings, $$value_ref1);
87 cmpHash(\%substrings, \%expected_substrings, "Substrings");
88
89 sub showNode {
90 my ( $recce, $substrings, $node ) = @_;
91 return if ref $node ne 'ARRAY';
92 my ( $name, $g1start, $g1len, $g1length, @values ) = @$node;
93 $substrings->{ "<$name> using g1len"} = $recce->substring( $g1start, $g1len );
94 $substrings->{ "<$name> using g1length"} = $recce->substring( $g1start, $g1length-1 );
95 for my $childNode (@values) {
96 showNode( $recce, $substrings, $childNode );
97 }
98 }
99
100 sub myDump {
101 my $v = shift;
102 return Data::Dumper->new( [$v] )->Indent(0)->Terse(1)->Dump;
103 }
104
105 sub cmpHash {
106 my ( $got, $wanted, $tag ) = @_;
107 my $i = 0;
108 GOT: for my $key ( sort keys %$got ) {
109 my $v = $got->{$key};
110 my $wanted_v = $wanted->{$key};
111 if ( defined $wanted_v ) {
112 delete $wanted->{$key};
113 if ( $wanted_v eq $v ) {
114 Test::More::pass("Value of $key in $tag matches: $v");
115 next GOT;
116 }
117 Test::More::fail("Mismatch for $key in $tag: $wanted_v vs. $v");
118 }
119 else {
120 Test::More::fail("Unexpected item in $tag: $key");
121 Test::More::diag("Value of unexpected item in $tag was $v");
122 }
123 $i++;
124 }
125 my @not_found = sort keys %$wanted;
126 my $not_found_count = scalar @not_found;
127 if ($not_found_count) {
128 Test::More::fail("$not_found_count expected item(s) not found in $tag");
129 for my $value (@not_found) {
130 Test::More::diag("$value");
131 }
132 }
133 else {
134 Test::More::pass("All expected items found in $tag");
135 }
136 }
137
138 # vim: expandtab shiftwidth=4: