Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tune table tests, add some new and fix bugs detected by them
  • Loading branch information
Tadeusz Sośnierz committed Jul 12, 2011
1 parent 06f1c95 commit c33a153
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 83 deletions.
59 changes: 40 additions & 19 deletions src/Perl6/Pod.pm
Expand Up @@ -62,9 +62,10 @@ class Perl6::Pod {
@rows.push($_.ast);
}
@rows := process_rows(@rows);
# we need to know 2 things about the separators:
# we need to know 3 things about the separators:
# is there more than one
# where is the first one
# are they different from each other
# Given no separators, our table is just an ordinary, one-lined
# table.
# If there is one separator, the table has a header and
Expand All @@ -75,11 +76,18 @@ class Perl6::Pod {
# Tricky, isn't it? Let's try to handle it sanely
my $sepnum := 0;
my $firstsepindex := 0;
my $differentseps := 0;
my $firstsep;
my $i := 0;
while $i < +@rows {
unless pir::isa(@rows[$i], 'ResizablePMCArray') {
$sepnum := $sepnum + 1;
unless $firstsepindex { $firstsepindex := $i }
if $firstsep {
if $firstsep ne @rows[$i] { $differentseps := 1 }
} else {
$firstsep := @rows[$i];
}
}
$i := $i + 1;
}
Expand Down Expand Up @@ -111,12 +119,14 @@ class Perl6::Pod {
} else {
my @hlines := [];
my $i := 0;
while $i < $firstsepindex {
@hlines.push(@rows.shift);
$i := $i + 1;
if $differentseps {
while $i < $firstsepindex {
@hlines.push(@rows.shift);
$i := $i + 1;
}
@rows.shift;
$headers := merge_rows(@hlines);
}
@rows.shift;
$headers := merge_rows(@hlines);
# let's go through the rows and merge the multi-line ones
my @newrows := [];
my @tmp := [];
Expand All @@ -130,6 +140,9 @@ class Perl6::Pod {
}
$i := $i + 1;
}
if +@tmp > 0 {
@newrows.push(merge_rows(@tmp));
}
$content := @newrows;
}

Expand Down Expand Up @@ -170,13 +183,19 @@ class Perl6::Pod {
if $v ~~ /^'='+ || ^'-'+ || ^'_'+ || ^\h*$/ {
@res[$i] := $v;
} elsif $v ~~ /\h'|'\h/ {
my $m := $v ~~ / :ratchet ([<!before \h+'|'\h+> .]*)
** [ [\h+ || ^^] '|' [\h+ || $$] ] /;
@res[$i] := $m[0];
my $m := $v ~~ /
:ratchet ([<!before [\h+ || ^^] '|' [\h+ || $$]> .]*)
** [ [\h+ || ^^] '|' [\h || $$] ]
/;
@res[$i] := [];
for $m[0] { @res[$i].push(formatted_text($_)) }
} elsif $v ~~ /\h'+'\h/ {
my $m := $v ~~ / :ratchet ([<!before \h+'+'\h+> .]*)
** [ [\h+ || ^^] '+' [\h+ || $$] ] /;
@res[$i] := $m[0];
my $m := $v ~~ /
:ratchet ([<!before [\h+ || ^^] '+' [\h+ || $$]> .]*)
** [ [\h+ || ^^] '+' [\h+ || $$] ]
/;
@res[$i] := [];
for $m[0] { @res[$i].push(formatted_text($_)) }
} else {
# now way to easily split rows
return splitrows(@rows);
Expand Down Expand Up @@ -213,15 +232,17 @@ class Perl6::Pod {

my $i := 0;
while $i < +@rows {
my @line := pir::split('', @rows[$i]);
my $j := 0;
while $j < +@line {
unless @suspects[$j] {
if @line[$j] ne ' ' {
@suspects[$j] := 1;
unless @rows[$i] ~~ /^'='+ || ^'-'+ || ^'_'+ || ^\h*$ / {
my @line := pir::split('', @rows[$i]);
my $j := 0;
while $j < +@line {
unless @suspects[$j] {
if @line[$j] ne ' ' {
@suspects[$j] := 1;
}
}
$j := $j + 1;
}
$j := $j + 1;
}
$i := $i + 1;
}
Expand Down
131 changes: 67 additions & 64 deletions t/pod/07-tables.t
@@ -1,5 +1,5 @@
use Test;
plan 61;
plan 37;
my $r;

=begin table
Expand All @@ -11,18 +11,15 @@ my $r;

$r = $=POD[0];
isa_ok $r, Pod::Block::Table;
is $r.content[0][0], 'The Shoveller';
is $r.content[0][1], 'Eddie Stevens';
is $r.content[0][2], "King Arthur's singing shovel";
is $r.content[1][0], 'Blue Raja';
is $r.content[1][1], 'Geoffrey Smith';
is $r.content[1][2], 'Master of cutlery';
is $r.content[2][0], 'Mr Furious';
is $r.content[2][1], 'Roy Orson';
is $r.content[2][2], 'Ticking time bomb of fury';
is $r.content[3][0], 'The Bowler';
is $r.content[3][1], 'Carol Pinnsler';
is $r.content[3][2], 'Haunted bowling ball';
is $r.content.elems, 4;
is $r.content[0].join('|'),
"The Shoveller|Eddie Stevens|King Arthur's singing shovel";
is $r.content[1].join('|'),
"Blue Raja|Geoffrey Smith|Master of cutlery";
is $r.content[2].join('|'),
"Mr Furious|Roy Orson|Ticking time bomb of fury";
is $r.content[3].join('|'),
"The Bowler|Carol Pinnsler|Haunted bowling ball";

=table
Constants 1
Expand All @@ -31,49 +28,36 @@ is $r.content[3][2], 'Haunted bowling ball';
Everything else 57

$r = $=POD[1];
is $r.content[0][0], 'Constants';
is $r.content[1][0], 'Variables';
is $r.content[2][0], 'Subroutines';
is $r.content[3][0], 'Everything else';
is $r.content[0][1], '1';
is $r.content[1][1], '10';
is $r.content[2][1], '33';
is $r.content[3][1], '57';
is $r.content.elems, 4;
is $r.content[0].join('|'), "Constants|1";
is $r.content[1].join('|'), "Variables|10";
is $r.content[2].join('|'), "Subroutines|33";
is $r.content[3].join('|'), "Everything else|57";

=for table
mouse | mice
horse | horses
elephant | elephants
$r = $=POD[2];
is $r.content[0][0], 'mouse';
is $r.content[0][1], 'mice';
is $r.content[1][0], 'horse';
is $r.content[1][1], 'horses';
is $r.content[2][0], 'elephant';
is $r.content[2][1], 'elephants';
is $r.content.elems, 3;
is $r.content[0].join('|'), "mouse|mice";
is $r.content[1].join('|'), "horse|horses";
is $r.content[2].join('|'), "elephant|elephants";

=table
Animal | Legs | Eats
=======================
Zebra | 4 | Cookies
Human | 2 | Pizza
Shark | 0 | Fish
Zebra + 4 + Cookies
Human + 2 + Pizza
Shark + 0 + Fish

$r = $=POD[3];
is $r.headers[0], 'Animal';
is $r.headers[1], 'Legs';
is $r.headers[2], 'Eats';
is $r.content[0][0], 'Zebra';
is $r.content[0][1], '4';
is $r.content[0][1], '4';
is $r.content[0][2], 'Cookies';
is $r.content[1][0], 'Human';
is $r.content[1][1], '2';
is $r.content[1][2], 'Pizza';
is $r.content[2][0], 'Shark';
is $r.content[2][1], '0';
is $r.content[2][2], 'Fish';
is $r.headers.join('|'), "Animal|Legs|Eats";
is $r.content.elems, 3;
is $r.content[0].join('|'), "Zebra|4|Cookies";
is $r.content[1].join('|'), "Human|2|Pizza";
is $r.content[2].join('|'), "Shark|0|Fish";

=table
Superhero | Secret |
Expand All @@ -82,12 +66,10 @@ is $r.content[2][2], 'Fish';
The Shoveller | Eddie Stevens | King Arthur's singing shovel

$r = $=POD[4];
is $r.headers[0], 'Superhero';
is $r.headers[1], 'Secret Identity';
is $r.headers[2], 'Superpower';
is $r.content[0][0], 'The Shoveller';
is $r.content[0][1], 'Eddie Stevens';
is $r.content[0][2], "King Arthur's singing shovel";
is $r.headers.join('|'), "Superhero|Secret Identity|Superpower";
is $r.content.elems, 1;
is $r.content[0].join('|'),
"The Shoveller|Eddie Stevens|King Arthur's singing shovel";

=begin table
Expand All @@ -107,18 +89,39 @@ is $r.content[0][2], "King Arthur's singing shovel";
=end table

$r = $=POD[5];
is $r.headers[0], 'Superhero';
is $r.headers[1], 'Secret Identity';
is $r.headers[2], 'Superpower';
is $r.content[0][0], 'The Shoveller';
is $r.content[0][1], 'Eddie Stevens';
is $r.content[0][2], "King Arthur's singing shovel";
is $r.content[1][0], 'Blue Raja';
is $r.content[1][1], 'Geoffrey Smith';
is $r.content[1][2], 'Master of cutlery';
is $r.content[2][0], 'Mr Furious';
is $r.content[2][1], 'Roy Orson';
is $r.content[2][2], 'Ticking time bomb of fury';
is $r.content[3][0], 'The Bowler';
is $r.content[3][1], 'Carol Pinnsler';
is $r.content[3][2], 'Haunted bowling ball';
is $r.headers.join('|'), "Superhero|Secret Identity|Superpower";
is $r.content.elems, 4;
is $r.content[0].join('|'),
"The Shoveller|Eddie Stevens|King Arthur's singing shovel";
is $r.content[1].join('|'),
"Blue Raja|Geoffrey Smith|Master of cutlery";
is $r.content[2].join('|'),
"Mr Furious|Roy Orson|Ticking time bomb of fury";
is $r.content[3].join('|'),
"The Bowler|Carol Pinnsler|Haunted bowling ball";

=table
X | O |
---+---+---
| X | O
---+---+---
| | X

$r = $=POD[6];
is $r.content.elems, 3;
is $r.content[0].join(','), 'X,O,';
is $r.content[1].join(','), ',X,O';
is $r.content[2].join(','), ',,X';

=table
X O
===========
X O
===========
X

$r = $=POD[7];
is $r.content.elems, 3;
is $r.content[0].join(','), 'X,O,';
is $r.content[1].join(','), ',X,O';
is $r.content[2].join(','), ',,X';

0 comments on commit c33a153

Please sign in to comment.