Web::Chain project:    Web/Chain/IO/t/Web-Chain-IO-Html.t


     # Test file created outside of h2xs framework.
# Run this like so: `perl Web-Chain-IO-Html.t'
#   doom@kzsu.stanford.edu     2004/10/13 04:42:42

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use warnings;
use strict;
$|=1;

use Test::More;
BEGIN { plan tests => 47 };

use FindBin qw($Bin);

use Web::Definitions qw( $DEBUG 
                         $DF_WHATSNEW_NODE_NAME
                         $DF_CONTENTS_NODE_NAME
                       );
use Web::Chain::IO;

# Doesn't need to be done explicitly (happens dynamically):
#   use Web::Chain::IO::Html;

ok(1); # If we made it this far, we're ok.

#########################

#--------
# Common definitions, initializations for the following tests
# 

   my $input_location = "$Bin/dat";  


#--------
# Output tests
# 
{
   my $file_name_rawtext = "DUMMY_CHAIN";
   my $location_html = "$Bin/dat/out_tmp";
   my $ref_location_html = "$Bin/dat/out_tmp_ref";

   chdir($location_html);

   # Wipe test location clean:
   unlink <*.html>;

   my @existing_files = <*.html>;
   my $count_existing = scalar( @existing_files );

   #   /home/doom/lib/perl/Web/Chain/IO/Input/t/dat/DUMMY_CHAIN
   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory, optional if full path is used.
   $dfh->input_format('Rawtext');   # uses a Web::Chain::IO::Input::Rawtext
   my $chain = $dfh->input($file_name_rawtext);

   my @node_names = $chain->get_names;
   my $count_new = scalar( @node_names );

   $dfh->output_location($location_html);
   $dfh->output_format('Html');     # uses Web::Chain::IO::Output::Html
   $dfh->output($chain);
   
   my @final_files = <*.html>;
   my $final_count = scalar( @final_files );

   is( $final_count, $count_new + $count_existing, "Right number of new html files created.");

   foreach my $node_name (@node_names) { 
   
     my $file_name_1="$location_html/$node_name.html";
     my $file_name_2="$ref_location_html/$node_name.html";

     chomp( my $check = `diff $file_name_1 $file_name_2` );
     is ($check, '', "The new $node_name.html matches previous runs.");
   }
}

#--------
# Some definitions and initializations for the following 
# batteries of Input tests

$input_location = "$Bin/dat/baby_df";  
chdir($input_location);  

my @expected_browse = qw( TOP
                          DESPERATE
                          MISOGYNY
                          DATA
                          FIN
                        );

#--------
# Test reading in a single node: the last location
# (FIN.html) by from the "baby_df" location.

{
   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

   my $chain = $dfh->input('FIN', 1); # should read just the node "FIN"
   my @node_names = $chain->get_names;
   my $count_nodes = scalar( @node_names );

   is( $count_nodes, 1, "Test that we've read only one node.");

   my $first_node_name = $chain->get_beg->get_name;
   is( $first_node_name, 'FIN', "Test that first node is named FIN");

   my $final_node_name = $chain->get_end->get_name;
   is( $final_node_name, 'FIN', "Test that last node is named FIN");

   # Check the contents of a node

   my $expected_contents = define_expected_body_for_fin();
   my $node_fin = $chain->get_node_from_name('FIN');
   my $body_fin = ${ $node_fin->get_body };
   is ( $body_fin, $expected_contents, "Test that contents of node FIN are as expected");

}

#--------
# find_first_node

{
  my (@file, $seed, $begin_node_name);

  my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
  $dfh->input_location($input_location);  # the current input directory
  $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

  chdir($input_location);
  @file = <*.html>;
  ($seed = $file[0]) =~ s/\.html$//;
  $begin_node_name = $dfh->find_first_node_from_input($seed);

  is ($begin_node_name, 'TOP', "Test that find_first_node can tell me that TOP is at top");
}

#--------
# Series of tests involving a read of an entire "baby_df" 
# location, 5 nodes long, from "TOP.html" to "FIN.html".

{
   my @existing_files = <*.html>;
   my $count_files = scalar( @existing_files );

   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

   my $chain = $dfh->input(); # undef, should read from TOP to FIN
   my @node_names = $chain->get_names;
   my $count_nodes = scalar( @node_names );

   is( $count_nodes, $count_files, "Test if number of nodes equals number of files in input location.");

   my $first_node_name = $chain->get_beg->get_name;
   is( $first_node_name, 'TOP', "Test that first node is named TOP");

   my $final_node_name = $chain->get_end->get_name;
   is( $final_node_name, 'FIN', "Test that last node is named FIN");

   # look at a few values of prev nodes
   my $next_to_last_node_name = $chain->get_end->get_prev->get_name;
   is( $next_to_last_node_name, 'DATA', "Test that next to last node is named DATA");

   my $one_step_forward_and_one_back = $chain->get_beg->get_next->get_prev->get_name;
   is( $one_step_forward_and_one_back, 'TOP', "Test one step forward and back returns to TOP");

   my $two_steps_forward_and_two_back = $chain->get_beg->get_next->get_next->get_prev->get_prev->get_name;
   is( $two_steps_forward_and_two_back, 'TOP', "Test two steps forward and back returns to TOP");

   # Check the contents of a node

   my $expected_contents = define_expected_body_for_desperate();
   my $node_desperate = $chain->get_node_from_name('DESPERATE');
   my $body_desperate = ${ $node_desperate->get_body };
   is ( $body_desperate, $expected_contents, "Test that contents of node DESPERATE are as expected");

}

#--------
# Series of Tests based on a partial read from the baby_df location:
# a 3 node long segment, from DESPERATE to DATA (excluding TOP and FIN).

{

   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

   my $chain = $dfh->input('DESPERATE','DATA'); 
   my @node_names = $chain->get_names;
   my $count_nodes = scalar( @node_names );
   ($DEBUG) && print STDERR "node_names in the partial chain:" . join(' ', @node_names) . "\n";

   is( $count_nodes, 3, "Test that right number of nodes were read: 3 from DESPERATE to DATA");

   my $node_name;   
   $node_name = $chain->get_beg->get_name;
   is( $node_name, 'DESPERATE', "Test that first node in chain is called DESPERATE");

   $node_name = $chain->get_beg->get_next->get_name;
   is( $node_name, 'MISOGYNY', "Test that second node in chain is called MISOGYNY");

   $node_name = $chain->get_beg->get_next->get_next->get_name;
   is( $node_name, 'DATA', "Test that third node in chain is called DATA");

   $node_name = $chain->get_end->get_name;
   is( $node_name, 'DATA', "Test that last node in chain is called DATA");

   my $start_name = 'DESPERATE';
   my $one_step_forward_and_one_back = $chain->get_beg->get_next->get_prev->get_name;
   is( $one_step_forward_and_one_back, $start_name, "Test one step forward and back returns to $start_name");

   my $two_steps_forward_and_two_back = $chain->get_beg->get_next->get_next->get_prev->get_prev->get_name;
   is( $two_steps_forward_and_two_back, $start_name, "Test two steps forward and back returns to $start_name");

}

#--------
# get_browse_sequence
{
   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

#   my $node_names_ref = $dfh->get_browse_sequence;
   my $node_names_ref = $dfh->get_browse_sequence_from_input;

   is_deeply( $node_names_ref, \@expected_browse, 'Test that get_browse_sequence returns expected browse sequence');
}


#--------
# get_browse_sequence - find sequence without TOP or FIN nodes.
#

{ 
   my @topless_browse = qw(  DUMMY_NODE
                             LONG_NODE
                             TRASHIC
                             CLASSIC
                             COMPLETENESS
                             KNOW_WAN_OHM
                          );
 

   my $input_location_topless = "$Bin/dat/topless_df";  

   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location_topless);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

   my $node_names_ref = $dfh->get_browse_sequence_from_input;

   is_deeply( $node_names_ref, \@topless_browse, 'Testing get_browse_sequence with location without TOP or FIN');
}



#--------
# testing the output_splice feature
# adding a chain segment to an already existing project, 
# "splicing" the ends together.
 
{
   # Source material: currently just copies of stuff one level up 
   my $rawtext_input_file_location = "$Bin/dat/Splice";
   my $rawtext_input_file = "$rawtext_input_file_location/DUMMY_CHAIN";
   my $storage = "$Bin/dat/Splice/baby_df";

   my $working_location = "$Bin/dat/Splice/tmp_baby_df"; 
   my $reference_location = "$Bin/dat/Splice/tmp_baby_df_ref";  

#  (1) wipe the test location clean; repopulate from $storage
   chdir($working_location);
   unlink <*.html>;
   system("cp $storage/*.html $working_location");

#  (3) do a rawtext read of DUMMY_CHAIN into a chain object

   my $dfh = Web::Chain::IO->new; # Create a DF IO handle
   $dfh->input_location($rawtext_input_file_location);  
   $dfh->input_format('Rawtext');   # dynamically loads Web::Chain::IO::Input::Rawtext
   my $chain = $dfh->input($rawtext_input_file);

#  (4) splice this chain into the tmp_baby_df location (say, after DATA)

    $dfh->output_location($working_location);
    my $ret = $dfh->output_format('Html');     # dynamically loads a Web::Chain::IO::Html
    my $target_position = 'DATA';
    $dfh->output_splice( $chain, $target_position );

#  (6) run tests to compare temp_baby_df to temp_baby_df_ref

   # 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 output_splice: right number of html files afterwards.");

   # Check the "browse_sequence"
   # Trying a freshly created IO handle # TODO ((was there a prob with reset of input_location/input_format?))
   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";

   # building up the expected browse sequence after output_splice at target_position = DATA
   my @original_browse = qw( TOP
                             DESPERATE
                             MISOGYNY
                             DATA
                             FIN
                        );

    my @rawtext_browse = qw(  DUMMY_NODE
                             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";

   is_deeply( $node_names_ref, \@expected_splice, 'Test that post-splice get_browse_sequence is as expected');

   # Check contents of each file
   foreach my $node_name (@expected_splice) { 
     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.");
   }
}


#--------
# testing input_meta_info routines
#

{
   # Still defined:    # $input_location = "$Bin/dat/baby_df";  
   my $dfh = Web::Chain::IO->new; # Create a doomfiles io handle
   $dfh->input_location($input_location);  # the current input directory
   $dfh->input_format('Html');   # uses a Web::Chain::IO::Input::Html

   my ($name_h1, $prev, $next) = $dfh->input_meta_info_from_input('DATA');

   is( $name_h1, 'DATA', "Testing that the DATA node knows it's named DATA");
   is( $prev, 'MISOGYNY', "Testing that the DATA node's PREV link is MISOGYNY");
   is( $next, 'FIN', "Testing that the DATA node's NEXT link is FIN");
}


#--------
#  testing log_new_names
#

{
   my $storage = "$Bin/dat/Whatsnew/Storage";
   my $working_location = "$Bin/dat/Whatsnew/Lognew";
   my $file = "$DF_WHATSNEW_NODE_NAME.html";
   my $work_file = "$working_location/$file";
   my $original_file = "$storage/$file";

   chdir("$working_location/RCS");
   unlink <*.html,v>;
   chdir($working_location);
   unlink <*.html>;
   system("cp $storage/*.html $working_location");
   system("cp $storage/RCS/*.html,v $working_location/RCS");

   system('co -l *.html');

   my $dfh = Web::Chain::IO->new; 
   $dfh->output_location($working_location);
   $dfh->output_format('Html');   

   my @new_node_names = qw( HECKLE JECKLE FEINSTEIN BLOOD ROVE);
   my $datestamp = 
     $dfh->log_new_names( \@new_node_names );

   my $newfilemess = "created new file, huh?";
   my $message = "ran log_new_names";
   my $cmd= "ci -u -m\'$message\' -t-\'$newfilemess\' *.html";
   system($cmd);

   my $delta_result = `diff $work_file $original_file`;
   ($DEBUG) && print STDERR "delta_result: \n$delta_result\n";
   my $delta_expected = define_expected_delta($datestamp);   
   is ($delta_result, $delta_expected, "Testing that log_new_names added the right material to WHATSNEW.html");

}

#--------
# Testing generate_contents_node
# 
# 

{
 # /home/doom/End/Cave/DoomfileTools/lib/perl/Web/Chain/IO/t/dat/ContentsPage/Expected
   my $storage = "$Bin/dat/ContentsPage/Storage";
   my $working_location = "$Bin/dat/ContentsPage/Working";
   my $expected_location = "$Bin/dat/ContentsPage/Expected";   
   my $file = "$DF_CONTENTS_NODE_NAME.html";
   my $work_file = "$working_location/$file";
   my $expected_file = "$expected_location/$file";

   chdir("$working_location/RCS");
   unlink <*.html,v>;
   chdir($working_location);
   unlink <*.html>;
   system("cp $storage/*.html $working_location");
   system("cp $storage/RCS/*.html,v $working_location/RCS");

   system('co -l *.html');

   my $dfh = Web::Chain::IO->new; 
   $dfh->output_location($working_location);
   $dfh->output_format('Html');   

   $dfh->generate_contents_node;

   my $newfilemess = "created new contents, yes?";
   my $message = "ran generate_contents_node";
   my $cmd= "ci -u -m\'$message\' -t-\'$newfilemess\' *.html";
   system($cmd);

   my $delta_result = `diff $work_file $expected_file`;
   ($DEBUG) && print STDERR "generate_contents_node -- delta_result: \n$delta_result\n";
   my $delta_expected = '';
   is ($delta_result, $delta_expected, "Testing that generate_contents_node added the right material to CONTENTS.html");

}


#--------
# subs used by the above
# (a ghetto for here documents)

sub define_expected_delta{
# 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";
9d8
< 
15,31d13
< 
< $datestamp
< 
< Added:
< 
<     <A HREF="HECKLE.html">HECKLE</A>
< 
<     <A HREF="JECKLE.html">JECKLE</A>
< 
<     <A HREF="FEINSTEIN.html">FEINSTEIN</A>
< 
<     <A HREF="BLOOD.html">BLOOD</A>
< 
<     <A HREF="ROVE.html">ROVE</A>
< 
< 
< 
DELTA

}




sub define_expected_body_for_desperate { 

# contents for node named DESPERATE
my $body_test = <<BODYTEST;
<NODEBODY>
O'Reilly, I am.  

</NODEBODY>
BODYTEST
}


sub define_expected_body_for_fin {
# contents for node named DESPERATE
my $body_test = <<BODYTEST_2;
<NODEBODY>
Edna St. Vincent Millay:                       
                                               
     Deep in the muck of unregarded doom,      
     Where none can make a conquest, none have room
     To stretch an aching muscle, -- there might be 
     Intersticies where impulse could go free. . . 
     There, where accomplishment cannot achieve, 
     Valour defend, religion quite believe,   
     Or vengence plot behavior, -- there may still 
     be cracks, uneasy instinct well might fill
     And even worm its way along, until       
     All might begin again; and Man receive   
     In prospect, what he never can retrieve. 





</NODEBODY>
BODYTEST_2
}

     

Joseph Brenner, Sat Nov 6 17:04:11 2004