]> Sergey Matveev's repositories - public-inbox.git/blob - t/linkify.t
99acf17d593830884b1533452bf2ceae9ef8cd28
[public-inbox.git] / t / linkify.t
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Linkify;
7
8 {
9         my $l = PublicInbox::Linkify->new;
10         my $u = 'http://example.com/url-with-trailing-period';
11         my $s = $u . '.';
12         $s = $l->linkify_1($s);
13         $s = $l->linkify_2($s);
14         is($s, qq(<a\nhref="$u">$u</a>.), 'trailing period not in URL');
15 }
16
17 {
18         my $l = PublicInbox::Linkify->new;
19         my $u = 'http://example.com/url-with-trailing-semicolon';
20         my $s = $u . ';';
21         $s = $l->linkify_1($s);
22         $s = $l->linkify_2($s);
23         is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
24 }
25
26 {
27         my $l = PublicInbox::Linkify->new;
28         my $u = 'http://example.com/url-with-(parens)';
29         my $s = "hello $u world";
30         $s = $l->linkify_1($s);
31         $s = $l->linkify_2($s);
32         is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
33
34         $u .= "?query=a";
35         $s = "hello $u world";
36         $s = $l->linkify_1($s);
37         $s = $l->linkify_2($s);
38         is($s, qq(hello <a\nhref="$u">$u</a> world), 'query preserved');
39
40         $u .= "#fragment";
41         $s = "hello $u world";
42         $s = $l->linkify_1($s);
43         $s = $l->linkify_2($s);
44         is($s, qq(hello <a\nhref="$u">$u</a> world),
45           'query + fragment preserved');
46
47         $u = "http://example.com/";
48         $s = "hello $u world";
49         $s = $l->linkify_1($s);
50         $s = $l->linkify_2($s);
51         is($s, qq(hello <a\nhref="$u">$u</a> world), "root URL preserved");
52
53         $u = "http://example.com/#fragment";
54         $s = "hello $u world";
55         $s = $l->linkify_1($s);
56         $s = $l->linkify_2($s);
57         is($s, qq(hello <a\nhref="$u">$u</a> world), "root + fragment");
58 }
59
60 # Markdown compatibility
61 {
62         my $l = PublicInbox::Linkify->new;
63         my $u = 'http://example.com/';
64         my $s = "[markdown]($u)";
65         $s = $l->linkify_1($s);
66         $s = $l->linkify_2($s);
67         is($s, qq![markdown](<a\nhref="$u">$u</a>)!, 'Markdown-compatible');
68
69         $s = qq![markdown]($u "title")!;
70         $s = $l->linkify_1($s);
71         $s = $l->linkify_2($s);
72         is($s, qq![markdown](<a\nhref="$u">$u</a> "title")!,
73                 'Markdown title compatible');
74
75         $s = qq![markdown]($u).!;
76         $s = $l->linkify_1($s);
77         $s = $l->linkify_2($s);
78         is($s, qq![markdown](<a\nhref="$u">$u</a>).!,
79                 'Markdown-compatible end of sentence');
80 }
81
82 done_testing();