2006-05-04 20:51:26 +02:00
#!/usr/bin/perl
use warnings ;
use strict ;
2007-03-07 10:48:59 +01:00
use Test::More tests = > 16 ;
2006-05-04 20:51:26 +02:00
2006-09-10 00:50:27 +02:00
BEGIN { use_ok ( "IkiWiki" ) ; }
2006-05-26 18:11:53 +02:00
sub linkify ($$$$) {
my $ lpage = shift ;
2006-05-04 20:51:26 +02:00
my $ page = shift ;
2006-05-26 18:11:53 +02:00
2006-05-26 10:24:36 +02:00
my $ content = shift ;
2006-05-04 20:51:26 +02:00
my @ existing_pages = @ { shift ( ) } ;
# This is what linkify and htmllink need set right now to work.
# This could change, if so, update it..
2006-08-13 04:03:43 +02:00
% IkiWiki:: pagecase = ( ) ;
2006-09-10 00:50:27 +02:00
% links = ( ) ;
2006-05-04 20:51:26 +02:00
foreach my $ page ( @ existing_pages ) {
2006-08-13 04:03:43 +02:00
$ IkiWiki:: pagecase { lc $ page } = $ page ;
2006-09-10 00:50:27 +02:00
$ links { $ page } = [] ;
2006-10-08 23:56:50 +02:00
$ renderedfiles { "$page.mdwn" } = [ $ page ] ;
2007-04-14 22:41:43 +02:00
$ destsources { $ page } = "$page.mdwn" ;
2006-05-04 20:51:26 +02:00
}
2006-09-10 00:50:27 +02:00
% config = IkiWiki:: defaultconfig ( ) ;
2006-12-28 21:44:33 +01:00
$ config { cgiurl } = "http://somehost/ikiwiki.cgi" ;
2007-04-30 00:58:27 +02:00
# currently coded for non usedirs mode (TODO: check both)
$ config { usedirs } = 0 ;
2006-05-04 20:51:26 +02:00
2006-05-26 18:11:53 +02:00
return IkiWiki:: linkify ( $ lpage , $ page , $ content ) ;
2006-05-04 20:51:26 +02:00
}
sub links_to ($$) {
my $ link = shift ;
my $ content = shift ;
if ( $ content =~ m !<a href="[^"]*\Q$link\E[^"]*">! ) {
return 1 ;
}
else {
print STDERR "# expected link to $link in $content\n" ;
return ;
}
}
sub not_links_to ($$) {
my $ link = shift ;
my $ content = shift ;
if ( $ content !~ m !<a href="[^"]*\Q$link\E[^"]*">! ) {
return 1 ;
}
else {
print STDERR "# expected no link to $link in $content\n" ;
return ;
}
}
sub links_text ($$) {
my $ text = shift ;
my $ content = shift ;
if ( $ content =~ m !>\Q$text\E</a>! ) {
return 1 ;
}
else {
print STDERR "# expected link text $text in $content\n" ;
return ;
}
}
2006-05-26 18:11:53 +02:00
ok ( links_to ( "bar" , linkify ( "foo" , "foo" , "link to [[bar]] ok" , [ "foo" , "bar" ] ) ) , "ok link" ) ;
2007-03-07 10:48:59 +01:00
ok ( links_to ( "bar_baz" , linkify ( "foo" , "foo" , "link to [[bar_baz]] ok" , [ "foo" , "bar_baz" ] ) ) , "ok link" ) ;
2006-05-26 18:11:53 +02:00
ok ( not_links_to ( "bar" , linkify ( "foo" , "foo" , "link to \\[[bar]] ok" , [ "foo" , "bar" ] ) ) , "escaped link" ) ;
ok ( links_to ( "page=bar" , linkify ( "foo" , "foo" , "link to [[bar]] ok" , [ "foo" ] ) ) , "broken link" ) ;
ok ( links_to ( "bar" , linkify ( "foo" , "foo" , "link to [[baz]] and [[bar]] ok" , [ "foo" , "baz" , "bar" ] ) ) , "dual links" ) ;
ok ( links_to ( "baz" , linkify ( "foo" , "foo" , "link to [[baz]] and [[bar]] ok" , [ "foo" , "baz" , "bar" ] ) ) , "dual links" ) ;
ok ( links_to ( "bar" , linkify ( "foo" , "foo" , "link to [[some_page|bar]] ok" , [ "foo" , "bar" ] ) ) , "named link" ) ;
ok ( links_text ( "some page" , linkify ( "foo" , "foo" , "link to [[some_page|bar]] ok" , [ "foo" , "bar" ] ) ) , "named link text" ) ;
ok ( links_to ( "bar" , linkify ( "foo" , "foo" , "link to [[some page|bar]] ok" , [ "foo" , "bar" ] ) ) , "named link, with whitespace" ) ;
ok ( links_text ( "some page" , linkify ( "foo" , "foo" , "link to [[some page|bar]] ok" , [ "foo" , "bar" ] ) ) , "named link text, with whitespace" ) ;
2007-02-20 02:51:23 +01:00
ok ( links_text ( "0" , linkify ( "foo" , "foo" , "link to [[0|bar]] ok" , [ "foo" , "bar" ] ) ) , "named link to 0" ) ;
2006-05-26 18:11:53 +02:00
ok ( links_text ( "Some long, & complex page name." , linkify ( "foo" , "foo" , "link to [[Some long, & complex page name.|bar]] ok, and this is not a link]] here" , [ "foo" , "bar" ] ) ) , "complex named link text" ) ;
ok ( links_to ( "foo/bar" , linkify ( "foo/item" , "foo" , "link to [[bar]] ok" , [ "foo" , "foo/item" , "foo/bar" ] ) ) , "inline page link" ) ;
ok ( links_to ( "bar" , linkify ( "foo" , "foo" , "link to [[bar]] ok" , [ "foo" , "foo/item" , "foo/bar" ] ) ) , "same except not inline" ) ;
2007-02-20 04:05:47 +01:00
ok ( links_to ( "bar#baz" , linkify ( "foo" , "foo" , "link to [[bar#baz]] ok" , [ "foo" , "bar" ] ) ) , "anchor link" ) ;