#!/usr/bin/perl

# This is a public-domain script to test the functionality of Wala.  Just put
# Wala.pm in the same directory as this script, and run this script.
#
# This script outputs NOTHING upon success; error messages upon failure.

use strict;

use Wala;

sub test_wiki_line_to_html
	{
	my $result;
	$result = Wala::wiki_line_to_html( "''italic''" );
	if( $result ne '<p><em>italic</em></p>' )
		{
		print( "wiki_line_to_html fails on \"''italic''\" (got \"$result\")\n" );
		}
	$result = Wala::wiki_line_to_html( "''italic1'' and ''italic2''" );
	if( $result ne '<p><em>italic1</em> and <em>italic2</em></p>' )
		{
		print( "wiki_line_to_html fails on \"''italic1'' and ''italic2''\" (got \"$result\")\n" );
		}
	$result = Wala::wiki_line_to_html( "''italic'' and '''bold''', ''italic'' and '''bold'''" );
	if( $result ne '<p><em>italic</em> and <strong>bold</strong>, <em>italic</em> and <strong>bold</strong></p>' )
		{
		print( "wiki_line_to_html fails on italic and bold test (got \"$result\").\n" );
		}
	$result = Wala::wiki_line_to_html( "-------" );
	if( $result ne "<hr />" )
		{
		print( "wiki_line_to_html fails on ---- to hr test.\n" );
		}
	}

sub test_wiki_page_to_html
	{
	my( $result, $posit );
	$result = Wala::wiki_page_to_html( "I like this WalaWalaPage.\n\n\nHow ''cool!''\n-------\n" );
	if( $result ne "<p>I like this <a href=\"test.pl?action=edit&id=WalaWalaPage\" class=\"nonexistingpagelink\">WalaWalaPage</a>.</p>\n\n\n<p>How <em>cool!</em></p>\n<hr />\n" )
		{
		print( "wiki_page_to_html fails on generic stuff (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( " code line 1\n code line 2\n" );
	if( index( $result, "<pre>" ) < 0  ||  index( $result, "</pre>" ) < 0 )
		{
		print( "wiki_page_to_html failed on single-space-to-<pre>.\n" );
		}
	$result = Wala::wiki_page_to_html( ":indent line 1\n:indent line 2\n" );
	if( index( $result, "<blockquote>" ) < 0  ||  index( $result, "</blockquote>" ) < 0 )
		{
		print( "wiki_page_to_html failed on colon-to-<blockquote>.\n" );
		}
	$result = Wala::wiki_page_to_html( "<tags>" );
	if( index( $result, '&lt;tags&gt;' ) < 0 )
		{
		print( "wiki_page_to_html failed on < and > to &lt; and &gt;.\n" );
		}
	$result = Wala::wiki_page_to_html( "&" );
	if( index( $result, '&amp;' ) < 0 )
		{
		print( "wiki_page_to_html failed on & to &amp;.\n" );
		}
	$result = Wala::wiki_page_to_html( "* number 1\n* number 2" );
	if( index( $result, '<ul>' ) < 0  ||
	    index( $result, '<li>' ) < 0  ||
	    index( $result, '</ul>' ) < 0 )
		{
		print( "wiki_page_to_html failed on unordered list test (*).\n" );
		}
	$result = Wala::wiki_page_to_html( "# number 1\n# number 2" );
	if( index( $result, '<ol>' ) < 0  ||
	    index( $result, '<li>' ) < 0  ||
	    index( $result, '</ol>' ) < 0 )
		{
		print( "wiki_page_to_html failed on ordered list test (#).\n" );
		}
	$result = Wala::wiki_page_to_html( "# level 1\n## level 2" );
	$posit = index( $result, '<ol>' );
	if( index( $result, '<ol>', $posit + 1 ) < 0 )
		{
		print( "wiki_page_to_html failed on multiple ordered list test (##).\n" );
		}
	$result = Wala::wiki_page_to_html( "http://www.yahoo.com/~gurney/" );
	if( index( $result, "<a href=\"http://www.yahoo.com/~gurney/\"" ) < 0  ||
	    index( $result, "><span class=\"printonly\">http://</span>www.yahoo.com/~gurney/</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"http://www.yahoo.com/\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "(http://www.yahoo.com/)" );
	if( index( $result, "(<a href=\"http://www.yahoo.com/\"" ) < 0  ||
	    index( $result, "><span class=\"printonly\">http://</span>www.yahoo.com/</a>)" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"(http://www.yahoo.com/)\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "http://c2.com/cgi/wiki?BrentNewhall" );
	if( index( $result, "<a href=\"http://c2.com/cgi/wiki?BrentNewhall\"" ) < 0 ||
	    index( $result, "><span class=\"printonly\">http://</span>c2.com/cgi/wiki?BrentNewhall</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"http://c2.com/cgi/wiki?BrentNewhall\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "ftp://ftp.yahoo.com/" );
	if( index( $result, "<a href=\"ftp://ftp.yahoo.com/\"" ) < 0  ||
	    index( $result, "><span class=\"printonly\">ftp://</span>ftp.yahoo.com/</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"ftp://ftp.yahoo.com/\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "news://news.yahoo.com/" );
	if( index( $result, "<a href=\"news://news.yahoo.com/\"" ) < 0  ||
	    index( $result, "><span class=\"printonly\">news://</span>news.yahoo.com/</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"news://news.yahoo.com/\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "mailto:me\@other-space.com" );
	if( index( $result, "<a href=\"mailto:me\@other-space.com\">me\@other-space.com</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on \"mailto:me\@other-space.com.\" (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "http://www.other-space.com/brent_face.gif" );
	if( index( $result, "<img src" ) < 0 )
		{
		print( "wiki_page_to_html failed on .gif image (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "|name|address|" );
	if( index( $result, "<table" ) < 0  ||
	    index( $result, "<td>" ) < 0  ||
	    index( $result, "</td>" ) < 0  ||
	    index( $result, "<tr>" ) < 0  ||
	    index( $result, "</tr>" ) < 0  ||
	    index( $result, "</table>" ) < 0 )
		{
		print( "wiki_page_to_html failed on table (|).\n" );
		}
	$result = Wala::wiki_page_to_html( "a [http://www.other-space.com/WikiWord/ Otherspace] test" );
	if( index( $result, "<a href=\"http://www.other-space.com/WikiWord/\"" ) < 0  ||
	    index( $result, ">Otherspace</a>" ) < 0 )
		{
		print( "wiki_page_to_html failed on [http://www.other-space.com/WikiWord/ Otherspace] (got \"$result\").\n" );
		}
	}

sub test_make_links_of_wiki_words
	{
	my $result = Wala::make_links_of_wiki_words( "a HomePage here" );
	if( index( $result, "a <a href=\"test.pl?HomePage\"" ) < 0  ||
	    index( $result, ">HomePage</a> here" ) < 0 )
		{
		print( "make_links_of_wiki_words failed on \"a HomePage here\" (got \"$result\").\n" );
		}
	my $result = Wala::make_links_of_wiki_words( "a TestWord5 here" );
	if( index( $result, "a <a href=\"test.pl?action=edit&id=TestWord5\"" ) < 0  ||
	    index( $result, ">TestWord5</a> here" ) < 0 )
		{
		print( "make_links_of_wiki_words failed on \"a TestWord5 here\" (got \"$result\").\n" );
		}
	$result = Wala::make_links_of_wiki_words( "a [[Test]] here" );
	if( index( $result, "<a href=\"test.pl?action=edit&id=Test\"" ) < 0  ||
	    index( $result, ">Test</a>" ) < 0 )
		{
		print( "make_links_of_wiki_words failed on \"a [[Test]] here\" (got \"$result\").\n" );
		}
	$result = Wala::make_links_of_wiki_words( "a [[Test|alternate]] here" );
	if( index( $result, "<a href=\"test.pl?action=edit&id=Test\"" ) < 0  ||
	    index( $result, ">alternate</a>" ) < 0 )
		{
		print( "make_links_of_wiki_words failed on \"a [[Test|alternate]] here\" (got \"$result\").\n" );
		}
	$result = Wala::make_links_of_wiki_words( "One war led to [[WarWithNoName|another]]." );
	if( index( $result, "One war led to <a href=\"test.pl?action=edit&id=WarWithNoName\"" ) < 0  ||
	    index( $result, ">another</a>." ) < 0 )
		{
		print( "make_links_of_wiki_words failed on \"a [[WarWithNoName|another]] here\" (got \"$result\").\n" );
		}
	}

sub test_get_wiki_edit_form
	{
	my $result = Wala::get_wiki_edit_form( "WalaPage" );
	if( index( $result, "<textarea" ) < 0 )
		{
		print( "get_wiki_edit_form failed to find a <textarea>.\n" );
		}
	$result = Wala::get_wiki_edit_form( "NonExistentPageNameHaHaHa" );
	if( index( $result, ">Enter text here.\n</textarea>" ) < 0 )
		{
		print( "get_wiki_edit_form failed to find default text.\n" );
		}
	}

sub test_write_page
	{
	my $result = Wala::write_page( "TestWalaPage", "Test in test.", "Test" );
	my $file_text;
	open( FILE, "pages/TestWalaPage" );
	while( ! eof( FILE ) )
		{
		$file_text .= <FILE>;
		}
	close( FILE );
	if( $file_text ne 'Test in test.' )
		{
		print( "write_page failed on TestWalaPage containing \"Test in test.\"\n" );
		}
	}

sub test_get_header
	{
	my $result = Wala::get_header( "HomePage" );
	if( index( $result, "href=\"test.pl?HomePage\"" ) < 0  ||
	    index( $result, ">Discussion Home</a>" ) < 0  ||
	    index( $result, "href=\"test.pl?RecentChanges\"" ) < 0  ||
	    index( $result, ">Recent Changes</a>" ) < 0  ||
	    index( $result, "href=\"test.pl?action=edit&id=HomePage\"" ) < 0  ||
	    index( $result, ">Edit this page</a>" ) < 0 )
		{
		print( "get_header failed (got \"$result\").\n" );
		}
	}

sub test_log_page_edit
	{
	Wala::log_page_edit( "WalaPage", "Made change.", "Brent" );
	my $currenttime = time;
	my $result = open( FILE, "log" );
	my $line;
	if( ! $result )
		{
		print( "log_page_edit failed to open log file.\n" );
		return;
		}
	while( ! eof( FILE ) )
		{
		$line = <FILE>;
		}
	close( FILE );
	if( $line ne "$currenttime WalaPage Brent Made change.\n" )
		{
		print( "log_page_edit failed (last line of log is \"$line\").\n" );
		}
	}

sub test_recent_changes
	{
	my $result = Wala::print_recent_changes( 4 );
	if( count( $result, "<li>" ) != 4 )
		{
		print( "recent_changes had the wrong number of entries (got \"$result\").\n" );
		}
	$result = Wala::wiki_page_to_html( "<recentChanges 6>" );
	if( count( $result, "<li>" ) != 6 )
		{
		print( "wiki_page_to_html failed on <recentChanges> tag (got \"$result\").\n" );
		}
	}

sub test_get_latest_diff_date
	{
	mkdir( "diffs/WalaPage" );
	my $timestamp = time;
	my @months = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
	               'Sep', 'Oct', 'Nov', 'Dec' );
	open( FILE, ">diffs/WalaPage/$timestamp" );
	print( FILE " " );
	close( FILE );
	my $result = Wala::get_latest_diff_date( "WalaPage" );
	my @t = localtime( $timestamp );
	my $correct_datestamp = $months[$t[4]] . ' ' . $t[3] . ', ' . substr( $t[5] + 1900, 2 );
	if( $result ne $correct_datestamp )
		{
		print( "get_latest_diff_date failed (result was \"$result\").\n" );
		}
	}

#################
#   Utilities   #
#################

sub test_display_all_pages
	{
	my $result = Wala::get_list_of_all_pages;
	my( $filename, $text );
	opendir( DIR, "pages/" );
	$filename = readdir( DIR );
	$filename = readdir( DIR );
	$filename = readdir( DIR );
	while( $filename ne '' )
		{
		if( $filename ne 'temp' )
			{
			$text .= "<a href=\"test.pl?$filename\" class=\"existingpagelink\">$filename</a><br />\n";
			}
		$filename = readdir( DIR );
		}
	closedir( DIR );
	if( index( $result, $text ) < 0 )
		{
		print( "display_all_pages failed.\n\nGot:\n$text\n" );
		}
	}

sub test_mygmtime
	{
	my ($etime) = (shift);
	my @months = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
	               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
	my @days = ( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($etime);
	my $timestr = sprintf( "%3s, %02d-%3s-%4d %02d:%02d:%02d GMT",
	$days[$wday],$mday,$months[$mon],$year+1900,$hour,$min,$sec);
	my $result = Wala::mygmtime;
	if( $result ne $timestr )
		{
		print( "mygmtime failed (expected \"$timestr\"; got \"$result\")\n" );
		}
	}

sub setup_tests
	{
	Wala::setup;
	if( ! -e "log" )
		{
		open( LOG_FILE, ">log" );
		print( LOG_FILE "1064527294 Sandbox Brent Added basic information\n" .
		                "1064599468 HomePage Anonymous Blah\n" .
		                "1065532920 HomePage Anonymous Interesting.\n" .
		                "1065533001 HomePage Anonymous Interesting.\n" .
		                "1066123642 HomePage Anonymous Interesting.\n" .
		                "1066140584 Sandbox Anonymous Cool\n" );
		close( LOG_FILE );
		}
	}


sub run_tests
	{
	&test_wiki_line_to_html;
	&test_wiki_page_to_html;
	&test_make_links_of_wiki_words;
	&test_get_wiki_edit_form;
	&test_get_header;
	# &test_write_page;
	# &test_log_page_edit;
	&test_recent_changes;
	&test_get_latest_diff_date;
	&test_display_all_pages;
	&test_mygmtime;
	}

##############################
#   Test Utility Functions   #
##############################

sub count
	{
	my( $haystack, $needle ) = ( shift, shift );
	my( $posit, $count );
	$count = 0;
	$posit = index( $haystack, $needle );
	while( $posit >= 0 )
		{
		$count++;
		$posit = index( $haystack, $needle, $posit + 1 );
		}
	return $count;
	}

&setup_tests;
&run_tests;
