#!/usr/bin/perl use strict; use Test::More tests => 49; use File::Temp qw/ tempdir /; use Games::SGF::Tournament; my @sgf = (< 1, CLEANUP => 1 ); my $i = 0; foreach (@sgf) { open FH, ">$tempdir/game$i.sgf"; print FH $_; close FH; $i++; } my $base_url = '/~joe/'; my $t = Games::SGF::Tournament->new( sgf_dir => $tempdir, base_url => $base_url ); isa_ok($t, 'Games::SGF::Tournament', 'tournament object'); can_ok($t, qw/ games scores /); SKIP: { eval 'use HTML::TreeBuilder'; skip 'HTML::TreeBuilder required for testing output', 47 if $@; my($table, $anchor, $field, $record, $caption); $table = HTML::TreeBuilder->new_from_content($t->games())->guts(); is($table->tag(), 'table', 'games table @0'); is($table->attr('border'), '1', 'default border attribute @0'); $caption = (grep { $_->tag() eq 'caption' } $table->content_list())[0]; isa_ok($caption, 'HTML::Element', 'caption @0'); is(($caption->content_list())[0], 'Played games', 'default caption filled @0'); $table = HTML::TreeBuilder->new_from_content($t->games( cgi => { border => '3', width => '90%' }, caption => 'My games' ))->guts(); is($table->tag(), 'table', 'games table @0'); is($table->attr('border'), '3', 'border attribute @0'); is($table->attr('width'), '90%', 'width attribute @0'); $caption = (grep { $_->tag() eq 'caption' } $table->content_list())[0]; isa_ok($caption, 'HTML::Element', 'caption @0'); is(($caption->content_list())[0], 'My games', 'caption filled @0'); $caption->delete(); $record = ($table->content_list())[1]; is($record->tag(), 'tr', 'record @0.0'); $field = ($record->content_list())[0]; is($field->tag(), 'td', 'field @0.0.0'); $anchor = ($field->content_list())[0]; is($anchor->tag(), 'a', 'anchor @0.0.0'); is($anchor->attr('href'), "${base_url}game0.sgf", 'href @0.0.0'); is(($anchor->content_list())[0], '1', 'game number @0.0.0'); text_field($record, '0.0.1', 'Bravo', 'black'); text_field($record, '0.0.2', 'Alpha', 'white'); text_field($record, '0.0.3', 'Japanese/9/0/5.5/30:00', 'setup'); text_field($record, '0.0.4', '2005-08-10', 'date'); text_field($record, '0.0.5', 'B+75.5', 'result'); $table = HTML::TreeBuilder->new_from_content($t->scores())->guts(); is($table->tag(), 'table', 'scores table @1'); is($table->attr('border'), '1', 'default border attribute @1'); $caption = (grep { $_->tag() eq 'caption' } $table->content_list())[0]; isa_ok($caption, 'HTML::Element', 'caption @1'); is(($caption->content_list())[0], 'Scoreboard', 'default caption filled @1'); $table = HTML::TreeBuilder->new_from_content($t->scores( cgi => { width => '90%', border => '3' }, caption => 'scores' ))->guts(); is($table->tag(), 'table', 'scores table @1'); is($table->attr('border'), '3', 'border attribute @1'); is($table->attr('width'), '90%', 'width attribute @1'); $caption = (grep { $_->tag() eq 'caption' } $table->content_list())[0]; isa_ok($caption, 'HTML::Element', 'caption @1'); is(($caption->content_list())[0], 'scores', 'caption filled @1'); $caption->delete(); $record = ($table->content_list())[1]; is($record->tag(), 'tr', 'record @1.0'); text_field($record, '1.0.0', '1', 'position'); text_field($record, '1.0.1', 'Bravo', 'name'); text_field($record, '1.0.2', '1', 'score'); $record = ($table->content_list())[2]; is($record->tag(), 'tr', 'record @1.1'); text_field($record, '1.1.0', '2', 'position'); text_field($record, '1.1.1', 'Alpha', 'name'); text_field($record, '1.1.2', '0', 'score'); } sub text_field { my $record = shift; my $matrix = shift; my $expect = shift; my $descr = shift; $matrix =~ /.*\.(.*)/; my $field = ($record->content_list())[$1]; is($field->tag(), 'td', "field \@$matrix"); is(($field->content_list())[0], $expect, "$descr \@$matrix"); }