File: precompile.t

package info (click to toggle)
libparse-recdescent-perl 1.967015%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 764 kB
  • sloc: perl: 6,797; makefile: 13; ansic: 9
file content (60 lines) | stat: -rwxr-xr-x 1,656 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use strict;
use warnings;
use Parse::RecDescent;
use Test::More tests => 14;
use lib '.';

# Turn off the "build a -standalone parser" precompile warning
our $RD_HINT = 0;

# mask "subroutine element redefined" warnings
local $^W;

my $grammar = <<'EOGRAMMAR';
TOP: <leftop: element /,/ element>(s?) ';' { $item[1] }
element: 'punctuation' {
    $thisparser->Extend('element: /!/');
    $return = $item[1];
}
| /\w+/
EOGRAMMAR


for my $standalone (0..1) {
    my $standalone_str = $standalone ? 'standalone' : 'dependent';
    my $class = "TestParser$standalone_str";
    my $pm_filename = $class . '.pm';

    if (-e $pm_filename) {
        unlink $pm_filename;
    }
    ok(!-e $pm_filename, "no preexisting precompiled parser");

    eval {
        Parse::RecDescent->Precompile({-standalone => $standalone,},
                                      $grammar,
                                      $class);
    };
    ok(!$@, qq{created a $standalone_str precompiled parser: } . $@);
    ok(-f $pm_filename, "found $standalone_str precompiled parser");

    eval "use $class;";
    ok(!$@, qq{use'd a $standalone_str precompiled parser: }.$@);

    unlink $pm_filename;
    ok(!-e $pm_filename, "deleted $standalone_str precompiled parser");

    my $result = eval qq{
    my \$text = "one, two, three, four,
punctuation, !, five, six, seven ;";

    use $class;
    my \$parser = $class->new();
    \$parser->TOP(\$text);
};
    ok(!$@, qq{ran a $standalone_str precompiled parser});
    is_deeply($result,
              [qw(one two three four punctuation
                  ! five six seven)],
              "correct result from precompiled parser");
}