Web::Chain project: Web/Pro/t/Web-Pro-Interact.t
# Test file created outside of h2xs framework.
# Run this like so: `perl Web-Pro-Interact.t'
# doom@kzsu.stanford.edu 2004/10/21 21:32:59
#########################
use warnings;
use strict;
$|=1;
use Test::More;
BEGIN { plan tests => 17 };
use FindBin qw($Bin);
use lib ("$Bin/../../..");
use Web::Pro::Interact qw(process_rawtext
check_links
);
use Web::Definitions qw( $DEBUG
$DF_WHATSNEW_NODE_NAME
);
ok(1); # If we made it this far, we're ok.
#--------
# process_rawtext - the first tests: essentially clones
# of the Html::output_splice tests.
#
{
# (a) definitions: what we're going to do and where
# Source material: currently just copies of stuff one level up
my $rawtext_input_file_location = "$Bin/dat/Processrawtext";
my $rawtext_input_file = "JUST_A_SHORT_ONE"; # Note: pathless, but works fine.
my $storage = "$Bin/dat/Processrawtext/Storage";
my $working_location = "$Bin/dat/Processrawtext/Working";
my $reference_location = "$Bin/dat/Processrawtext/Expected";
my $target_position = 'DATA';
# (b) managing expectations
# building up the expected browse sequence after output_splice at target_position = DATA
my @original_browse = qw( TOP
DESPERATE
MISOGYNY
DATA
WHATSNEW
CONTENTS
FIN
);
my @rawtext_browse = qw( JUST_A_SHORT_ONE
LONG_NODE
TRASHIC
CLASSIC
COMPLETENESS
KNOW_WAN_OHM
);
# get the index for the $target_position value
my $target_i;
for my $i (0 .. $#original_browse) {
if ( $original_browse[$i] eq $target_position ) {
$target_i = $i;
($DEBUG) && print STDERR "Found location in original_browse array of $target_position: $target_i\n";
}
}
# piece together with array slices
my @expected_splice;
push @expected_splice, @original_browse[ 0 .. $target_i ];
push @expected_splice, @rawtext_browse;
push @expected_splice, @original_browse[ $target_i+1 .. $#original_browse];
($DEBUG) && print STDERR "Expected browse sequence after splice in $working_location:\n" . join("\n", @expected_splice) . "\n";
# (c) wipe the test location clean; repopulate from $storage
chdir($working_location);
unlink <*.html>;
system("cp $storage/*.html $working_location");
chdir("$working_location/RCS");
unlink <*.html,v>;
system("cp $storage/RCS/*.html,v $working_location/RCS");
chdir($working_location);
# (d) Set up options hash for process_rawtext
my %opts = ( 'd' => 1,
'v' => 1,
'r' => $rawtext_input_file_location ,
'h' => $working_location ,
);
# (e) Do it
my $datestamp =
process_rawtext($rawtext_input_file, $target_position, \%opts);
# (f) run tests to compare Working to Expected
# Check file counts
chdir($reference_location);
my @expected_files = <*.html>;
my $expected_count = scalar( @expected_files );
chdir($working_location);
my @final_files = <*.html>;
my $final_count = scalar( @final_files );
is( $final_count, $expected_count, "Testing process_rawtext: right number of html files afterwards.");
# Check the "browse_sequence"
# Note: using a freshly created IO handle (needed? Test in Html.t sometime)
my $dfh2 = Web::Chain::IO->new; # Create a DF IO handle
$dfh2->input_location($working_location);
$dfh2->input_format('Html'); # dyanamically loads Web::Chain::IO::Html
my $node_names_ref = $dfh2->get_browse_sequence_from_input;
($DEBUG) && print STDERR "Browse sequence in $working_location:\n" . join(" ", @{ $node_names_ref }) . "\n";
is_deeply( $node_names_ref, \@expected_splice, 'Testing process_rawtext browse sequence is as expected');
# Check contents of each file (except WHATSNEW)
foreach my $node_name (@expected_splice) {
next if ($node_name eq $DF_WHATSNEW_NODE_NAME);
my $file_name_1="$working_location/$node_name.html";
my $file_name_2="$reference_location/$node_name.html";
chomp( my $check = `diff $file_name_1 $file_name_2` );
is ($check, '', "Testing output_splice: $node_name.html matches previous runs.");
}
# Compare updated WHATSNEW to the original
my $whatsnew_actual = "$working_location/$DF_WHATSNEW_NODE_NAME.html";
my $whatsnew_original = "$storage/$DF_WHATSNEW_NODE_NAME.html";
my $delta = `diff $whatsnew_actual $whatsnew_original `;
my $delta_expected = define_delta_expected($datestamp);
is($delta, $delta_expected, "Testing that process_rawtext updated $DF_WHATSNEW_NODE_NAME correctly");
}
#--------
# tests for the check_links function
#
{
($DEBUG) && print STDERR "\n$0: \nTesting the check_links function\n";
# my $storage = "$Bin/dat/Checklinks/Storage";
my $expected = "$Bin/dat/Checklinks/Expected";
my $working = "$Bin/dat/Checklinks/Working";
my $expected_report = "$expected/report.txt";
my $working_report = "$working/report.txt";
# Set up options hash for check_links
my %opts = ( 'd' => 1,
'v' => 1,
'r' => undef,
'h' => $working ,
);
my $report_ref = check_links( \%opts );
($DEBUG) && print STDERR "report:\n" . ${ $report_ref } . "\n";
my $OUT;
open $OUT, ">$working_report" or die "Couldn't open $working_report for write: $!";
print $OUT ${ $report_ref };
close $OUT;
my $delta = `diff $expected_report $working_report`;
is($delta, '', "Testing check_links: report of bad links matches expected report");
}
#--------
# subs used by the above
# (a ghetto for here documents)
sub define_delta_expected {
# Note: this test is not very robust.
# if the @new_node_names array were changed
# this here doc would also need to be revised.
my $datestamp = shift;
my $delta_whatsnew = <<"DELTA";
4c4
< <PRE> <A HREF="KNOW_WAN_OHM.html">[PREV - KNOW_WAN_OHM]</A> <A HREF="TOP.html">[TOP]</A></PRE>
---
> <PRE> <A HREF="DATA.html">[PREV - DATA]</A> <A HREF="TOP.html">[TOP]</A></PRE>
9d8
<
15,33d13
<
< $datestamp
<
< Added:
<
< <A HREF="JUST_A_SHORT_ONE.html">JUST_A_SHORT_ONE</A>
<
< <A HREF="LONG_NODE.html">LONG_NODE</A>
<
< <A HREF="TRASHIC.html">TRASHIC</A>
<
< <A HREF="CLASSIC.html">CLASSIC</A>
<
< <A HREF="COMPLETENESS.html">COMPLETENESS</A>
<
< <A HREF="KNOW_WAN_OHM.html">KNOW_WAN_OHM</A>
<
<
<
DELTA
}
Joseph Brenner,
Sat Nov 6 17:04:11 2004