# $Id: 1.t,v 1.5 2002/07/14 15:00:43 simon Exp $
use Test;
BEGIN { plan tests => 34 }
## If in @INC, should succeed
use HTML::TableContentParser;
ok(1);
## make sure we can turn on debugging..
$HTML::TableContentParser::DEBUG = 1;
ok(1);
## ..and back off.
$HTML::TableContentParser::DEBUG = 0;
ok(1);
## Test object creation
$obj = HTML::TableContentParser->new();
ok(defined $obj, 1, $@);
## Test basic functionality. Create a table, and make sure parsing it returns
## the correct values to the callback.
$table_caption = 'This is a caption';
$table_content1 = 'This is table cell content 1';
$table_content2 = 'This is table cell content 2';
$table_content3 = 'This is table cell content 3, a link';
$table_content4 = 'Some more text wrapping This is table cell content 4 a link.';
$header_text = 'Header text';
$html = qq{
Some text that should /not/ get picked up by the parser.
$table_caption
| $header_text |
| $table_content1 |
| $table_content2 |
| $table_content3 |
| $table_content4 |
};
$HTML::TableContentParser::DEBUG = 0;
$tables = $obj->parse($html);
ok($tables->[0]->{caption}->{data}, $table_caption, $@);
ok($tables->[0]->{rows}->[0]->{cells}->[0]->{data}, $table_content1, $@);
ok($tables->[0]->{rows}->[1]->{cells}->[0]->{data}, $table_content2, $@);
ok($tables->[0]->{rows}->[2]->{cells}->[0]->{data}, $table_content3, $@);
ok($tables->[0]->{rows}->[3]->{cells}->[0]->{data}, $table_content4, $@);
## Some more complicated tables..
my @rows = (
['r1td1', 'r1td2', 'r1td3'],
['r2td1', 'r2td2', 'r2td3'],
['r3td1', 'r3td2', 'r3td3'],
);
my @hdrs = qw(h1 h2 h3);
$html = qq{
Some text that should /not/ get picked up by the parser.
};
$html .= '| ' . join(' | ', @hdrs) . " | \n";
for (@rows) {
$html .= '| ' . join(' | ', @$_) . " |
\n";
}
$html .= qq{
Some more intermediary text which should be ignored.
};
$html .= '| ' . join(' | ', @hdrs) . " | \n";
for (@rows) {
$html .= '| ' . join(' | ', @$_) . " |
\n";
}
$html .= qq{
};
## Set to 1 to debug this parse.
$HTML::TableContentParser::DEBUG = 0;
$tables = $obj->parse($html);
## We should have two tables..
ok(@$tables, 2, @_);
## and three headers for each table
for $t (0..$#{@$tables}) {
for (0..$#hdrs) {
ok($tables->[$t]->{headers}->[$_]->{data}, $hdrs[$_], $@);
}
}
## and three rows of three cells each, for each table.. (18 total).
for $t (0..$#{@$tables}) {
for $r (0..$#rows) {
for (0..2) {
ok($tables->[$t]->{rows}->[$r]->{cells}->[$_]->{data}, $rows[$r]->[$_], $@);
}
}
}
### Tests for broken table removed in v0.12.
## A nested table, tests added in v0.13
#####
#####my @rows = (
##### ['r1td1', 'r1td2', 'r1td3'],
##### ['r2td1', 'r2td2', 'r2td3'],
##### ['r3td1', 'r3td2', 'r3td3'],
#####);
#####
#####my @hdrs = qw(h1 h2 h3);
#####
#####$html = qq{
#####
#####
#####
#####
#####Some text that should /not/ get picked up by the parser.
#####};
#####
#####for $i (1..2) {
##### $html .= "\n";
##### $html .= '| ' . join(' | ', @hdrs) . " | \n";
#####
##### for (@rows) {
##### $html .= "| t$i" . join(" | t$i", @$_) . " |
\n";
##### }
#####
##### $html .= "
\n";
#####}
#####
#####$html .= qq{
#####Some more intermediary text which should be ignored.
#####
#####
#####};
#####
#####
####### Set to 1 to debug this parse.
#####$HTML::TableContentParser::DEBUG = 0;
#####$tables = $obj->parse($html);
#####
####### We should have two tables..
#####ok(@$tables, 2, @_);
#####
####### and three headers for each table
#####for $t (0..$#{@$tables}) {
##### for (0..$#hdrs) {
##### ok($tables->[$t]->{headers}->[$_]->{data}, $hdrs[$_], $@);
##### }
#####}
#####
#####
####### and three rows of three cells each, for each table.. (18 total).
#####for $t (0..$#{@$tables}) {
##### for $r (0..$#rows) {
##### for (0..2) {
##### my $table = $t + 1;
##### ok($tables->[$t]->{rows}->[$r]->{cells}->[$_]->{data},
##### "t$table" . $rows[$r]->[$_], $@);
##### }
##### }
#####}
#####