File: new_init.t

package info (click to toggle)
libregexp-grammars-perl 1.058-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,328 kB
  • sloc: perl: 53,328; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download | duplicates (6)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use 5.010;
use warnings;
use Test::More 'tests' => 2;

# Use this class declaration to check that classes with ctors
# actually call the ctor when objrules use them...
{ package Speaker;

    sub new {
        my $that = shift;
        #warn "running 'new'\n";
        my $self = bless {}, (ref($that) || $that);
        $self->init(@_);
    }

    sub init {
        my $self = shift;
        #warn "running 'init'\n";
        my %args = (scalar @_ == 1 and UNIVERSAL::isa($_[0], 'HASH')) ? %{ $_[0] } : @_;
        foreach my $accessor (keys %args) {
            next unless $self->can($accessor);
            $self->$accessor($args{$accessor});
        }
        #warn "returning initialized object\n";
        return $self;
    }

    sub name {
        my $self = shift;
        @_ ? ($self->{name} = shift) : $self->{name};
    }

    sub alias {
        my $self = shift;
        @_ ? ($self->{alias} = shift) : $self->{alias};
    }

    sub id {
        my $self = shift;
        @_ ? ($self->{id} = shift) : $self->{id};
    }
}

my $parser = do{
    use Regexp::Grammars;
    qr{
        <speaker>

        <objrule: Speaker=speaker>
            \<a href=\"/yn2011/user/<id>\"\>
            <name>
            (?:\(\&lrm;<alias>\&lrm;\))

        <token: name>
            \w+ (?:(?:<.ws>|\-|\') \w+)

        <token: alias>
            \w+

        <token: id>
            \d+
    }xms
};

my $target = {
      "" => "<a href=\"/yn2011/user/1613\">Nathan Gray (&lrm;kolibrie&lrm;)",
      "speaker" => bless({
        "" => "<a href=\"/yn2011/user/1613\">Nathan Gray (&lrm;kolibrie&lrm;)",
        "alias" => "kolibrie",
        "id" => 1613,
        "name" => "Nathan Gray",
      }, "Speaker"),
};

my $input = do{ local $/; <DATA>};
chomp $input;
my $original_input = $input;

ok +($input =~ $parser)    => 'Matched';
is_deeply \%/, $target     => 'Returned correct data structure';
#is $/{""}, $original_input => 'Captured entire text';


__DATA__
<a href="/yn2011/user/1613">Nathan Gray (&lrm;kolibrie&lrm;)</a> - <a href="/yn2011/talk/3356"><b>&lrm;Practical Extraction with Regexp::Grammars&lrm;</b></a> (50&nbsp;min)  <span id="starcount-3356" style="white-space:nowrap"><span class="starcount">9</span><img style="vertical-align:middle" src="/images/picked.gif" /></span>