#!/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 '
italic
' ) { print( "wiki_line_to_html fails on \"''italic''\" (got \"$result\")\n" ); } $result = Wala::wiki_line_to_html( "''italic1'' and ''italic2''" ); if( $result ne 'italic1 and italic2
' ) { 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 'italic and bold, italic and bold
' ) { print( "wiki_line_to_html fails on italic and bold test (got \"$result\").\n" ); } $result = Wala::wiki_line_to_html( "-------" ); if( $result ne "I like this WalaWalaPage.
\n\n\nHow cool!
\n" ) < 0 || index( $result, "" ) < 0 ) { print( "wiki_page_to_html failed on single-space-to-
.\n" ); } $result = Wala::wiki_page_to_html( ":indent line 1\n:indent line 2\n" ); if( index( $result, "" ) < 0 || index( $result, "" ) < 0 ) { print( "wiki_page_to_html failed on colon-to-.\n" ); } $result = Wala::wiki_page_to_html( "" ); if( index( $result, '<tags>' ) < 0 ) { print( "wiki_page_to_html failed on < and > to < and >.\n" ); } $result = Wala::wiki_page_to_html( "&" ); if( index( $result, '&' ) < 0 ) { print( "wiki_page_to_html failed on & to &.\n" ); } $result = Wala::wiki_page_to_html( "* number 1\n* number 2" ); if( index( $result, ' ' ) < 0 || index( $result, '
' ) < 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, '- ' ) < 0 || index( $result, '
' ) < 0 || index( $result, '
' ) < 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, '- ' ) < 0 || index( $result, '
' ); if( index( $result, '
', $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, "http://www.yahoo.com/~gurney/" ) < 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, "(http://www.yahoo.com/)" ) < 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, "http://c2.com/cgi/wiki?BrentNewhall" ) < 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, "ftp://ftp.yahoo.com/" ) < 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, "news://news.yahoo.com/" ) < 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, "me\@other-space.com" ) < 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, "" ) < 0 || index( $result, "" ) < 0 || index( $result, "
" ) < 0 || index( $result, " " ) < 0 || index( $result, "" ) < 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, "Otherspace" ) < 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 HomePage 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 TestWord5 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, "Test" ) < 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, "alternate" ) < 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 another." ) < 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, "" ) < 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 .=; } 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" ) < 0 || index( $result, "href=\"test.pl?RecentChanges\"" ) < 0 || index( $result, ">Recent Changes" ) < 0 || index( $result, "href=\"test.pl?action=edit&id=HomePage\"" ) < 0 || index( $result, ">Edit this page" ) < 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 = ; } 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, " - " ) != 4 ) { print( "recent_changes had the wrong number of entries (got \"$result\").\n" ); } $result = Wala::wiki_page_to_html( "
" ); if( count( $result, " - " ) != 6 ) { print( "wiki_page_to_html failed on
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 .= "$filename
\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;