aboutsummaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 6dff0a02954a7c61b5d05fc79a994d1486d2e005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 32;

BEGIN { use_ok("IkiWiki"); }

# Initialize link plugin
%config=IkiWiki::defaultconfig();
IkiWiki::loadplugins();

my $prefix_directives;

sub linkify ($$$$) {
	my $lpage=shift;
	my $page=shift;

	my $content=shift;
	my @existing_pages=@{shift()};
	
	# This is what linkify and htmllink need set right now to work.
	# This could change, if so, update it..
	%IkiWiki::pagecase=();
	%links=();
	foreach my $p (@existing_pages) {
		$IkiWiki::pagecase{lc $p}=$p;
		$links{$p}=[];
		$renderedfiles{"$p.mdwn"}=[$p];
		$destsources{$p}="$p.mdwn";
	}

	%config=IkiWiki::defaultconfig();
	$config{cgiurl}="http://somehost/ikiwiki.cgi";
	$config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
	# currently coded for non usedirs mode (TODO: check both)
	$config{usedirs}=0;
	$config{prefix_directives}=$prefix_directives;

	IkiWiki::checkconfig();

	return IkiWiki::linkify($lpage, $page, $content);
}

sub links_to ($$) {
	my $link=shift;
	my $content=shift;
	
	if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*"\s*[^>]*>!) {
		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;
	}
}

# Tests that are the same for both styles of prefix directives.
foreach $prefix_directives (0,1) {
	ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
	ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
	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_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
	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");
	ok(links_to("bar#baz", linkify("foo",      "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");
}

$prefix_directives=0;
ok(not_links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
	"link with whitespace, without prefix_directives");
ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
	"named link, with whitespace, without prefix_directives");

$prefix_directives=1;
ok(links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
	"link with whitespace");
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");