use v6;
# ------------------------ E M I T T E R ------------------------ #
class Talk {
has $.presenter;
has $.title;
method emit {
'' ~ $.title ~ ' ' ~ $.presenter ~ ' ';
}
}
# --------------------------- P L A N --------------------------- #
package main;
use v6;
use Test;
plan 11 + (2 * 2);
if !eval('("a" ~~ /a/)') {
skip_rest "skipped tests - rules support appears to be missing";
exit;
}
# ------------------------ G R A M M A R ------------------------ #
rule embedded_title {
}
rule presentation {
}
rule talk {
{ return ::Talk(:$$, :$$) }
}
token presenter {
'' ''? <( + [ +]+ )>
}
token title {
'' ' '
{ return $ }
}
token link {
''
$ := (+ [ +]+) ' '
}
# ------------------------- S O U R C E ------------------------- #
# snippet from http://pghpw.org/schedule.html
my $content = '
9:45 AM
Making Perl Work for You
brian d foy
Make your database work for you
Beth Skwarecki
';
# -------------------------- T E S T S -------------------------- #
my @expected = (
'Making Perl Work for You',
'Make your database work for you',
);
# L
my $presenter = ($content ~~ m//);
is(~$presenter, 'brian d foy', 'match presenter');
my $title = ($content ~~ m//);
is(~$title, 'Making Perl Work for You', 'match title');
# L
my $embedded = ($content ~~ m//);
ok($embedded);
# $embedded behaves just like $/, so you have to specify the layer
is(~$embedded, 'Making Perl Work for You', 'match embedded.title');
my $presentation = ($content ~~ m//);
ok($presentation);
is(~$presentation, 'Making Perl Work for You', 'match presentation.title');
is(~$presentation, 'brian d foy', 'match presentation.presenter');
# L<< S05/Match objects/"you can override that by calling C inside a regex" >>
my $talk = ($content ~~ m//);
ok($talk);
my $title_value; eval('$title_value = $talk.title');
is($title_value, 'Making Perl Work for You', 'match talk.title');
my $presenter_value; eval('$presenter_value = $talk.presenter');
is($presenter_value, 'brian d foy', 'match talk.presenter');
my $emit_value; eval('$emit_value = $talk.emit');
is($emit_value, 'Making Perl Work for You brian d foy ', 'talk.emit');
# make sure we can find multiple presentations in our $content
# L
my $c = 0;
for $content ~~ m:g// -> $match {
# should loop through two matches
ok($match);
is(~$match, @expected[$c], "presentation $c title");
$c++;
}