root/t/regex/extract_pghpw.t

Revision 21749, 3.5 kB (checked in by lwall, 5 months ago)

[STD and t/] various bugs, STD now parses 96% of t/

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1use v6;
2
3# ------------------------ E M I T T E R ------------------------ #
4
5class Talk {
6    has $.presenter;
7    has $.title;
8    method emit {
9        '<talk><title>' ~ $.title ~ '</title><presenter>' ~ $.presenter ~ '</presenter></talk>';
10    }
11}
12
13# --------------------------- P L A N --------------------------- #
14
15package main;
16
17use v6;
18use Test;
19
20plan 11 + (2 * 2);
21
22if !eval('("a" ~~ /a/)') {
23  skip_rest "skipped tests - rules support appears to be missing";
24  exit;
25}
26
27# ------------------------ G R A M M A R ------------------------ #
28
29rule embedded_title {
30    <title>
31}
32
33rule presentation  {
34    <title>
35    <presenter>
36}
37
38rule talk  {
39    <title>
40    <presenter>
41    { return ::Talk(:$$<title>, :$$<presenter>) }
42}
43
44token presenter {
45    '<span class="speaker">' '<tt>'? <( <alpha>+ [<ws> <alpha>+]+ )>
46}
47
48token title {
49    '<span class="talk">' <link> '</span>'
50    { return $<link><label> }
51}
52
53token link {
54    '<a href="'
55    $<url> = ([\/]? \w+ [\/ \w+]+ \. \w+) '">'
56    $<label> = (<alpha>+ [<ws> <alpha>+]+) '</a>'
57}
58
59# ------------------------- S O U R C E ------------------------- #
60
61# snippet from http://pghpw.org/schedule.html
62my $content = '
63    <tr>
64      <td class="time">9:45 AM</td>
65     
66      <td class="beginner"
67        rowspan="2"
68       
69      >
70        <span class="talk"><a href="schedule/making_perl_work_for_you.html">Making Perl Work for You</a></span>
71        <span class="speaker"><tt>brian d foy</tt></span>
72      </td>
73
74     
75      <td class="advanced"
76       
77       
78      >
79        <span class="talk"><a href="schedule/make_your_database_work_for_you.html">Make your database work for you</a></span>
80        <span class="speaker">Beth Skwarecki</span>
81      </td>
82
83    </tr>
84';
85
86# -------------------------- T E S T S -------------------------- #
87
88my @expected = (
89    'Making Perl Work for You',
90    'Make your database work for you',
91);
92
93# L<S05/Subrule captures>
94my $presenter = ($content ~~ m/<presenter>/);
95is(~$presenter, 'brian d foy', 'match presenter');
96
97my $title = ($content ~~ m/<title>/);
98is(~$title, 'Making Perl Work for You', 'match title');
99
100# L<S05/Match objects/"This returned object is also automatically assigned to the lexical $/ variable">
101my $embedded = ($content ~~ m/<embedded_title>/);
102ok($embedded);
103# $embedded behaves just like $/, so you have to specify the <embedded_title> layer
104is(~$embedded<embedded_title><title>, 'Making Perl Work for You', 'match embedded.title');
105
106my $presentation = ($content ~~ m/<presentation>/);
107ok($presentation);
108is(~$presentation<presentation><title>, 'Making Perl Work for You', 'match presentation.title');
109is(~$presentation<presentation><presenter>, 'brian d foy', 'match presentation.presenter');
110
111# L<< S05/Match objects/"you can override that by calling C<make> inside a regex" >>
112my $talk = ($content ~~ m/<talk>/);
113ok($talk);
114my $title_value; eval('$title_value = $talk.title');
115is($title_value, 'Making Perl Work for You', 'match talk.title');
116my $presenter_value; eval('$presenter_value = $talk.presenter');
117is($presenter_value, 'brian d foy', 'match talk.presenter');
118my $emit_value; eval('$emit_value = $talk.emit');
119is($emit_value, '<talk><title>Making Perl Work for You</title><presenter>brian d foy</presenter></talk>', 'talk.emit');
120
121# make sure we can find multiple presentations in our $content
122# L<S05/Match objects/"Match object can produce the rest of the results lazily">
123my $c = 0;
124for $content ~~ m:g/<presentation>/ -> $match {
125    # should loop through two matches
126    ok($match);
127    is(~$match<presentation><title>, @expected[$c], "presentation $c title");
128    $c++;
129}
Note: See TracBrowser for help on using the browser.